Skip to content

Commit

Permalink
Improved profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Dec 20, 2024
1 parent 4cc8566 commit 1f60a3f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
7 changes: 7 additions & 0 deletions modules/juce_core/juce_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@
#define JUCE_PROFILING_CATEGORIES
#endif

/** Config: JUCE_PROFILING_FILE_PREFIX
If provided, it will be used as prefix for profilation files generated. By default it will use "yup-profile".
*/
#ifndef JUCE_PROFILING_FILE_PREFIX
#define JUCE_PROFILING_FILE_PREFIX "yup-profile"
#endif

#ifndef JUCE_STRING_UTF_TYPE
#define JUCE_STRING_UTF_TYPE 8
#endif
Expand Down
21 changes: 17 additions & 4 deletions modules/juce_core/profiling/juce_Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,23 @@ void Profiler::startTracing (uint32 sizeInKilobytes)

String fileName;
fileName
<< "yup-profile"
<< JUCE_PROFILING_FILE_PREFIX
#if JUCE_DEBUG
<< "-DEBUG-"
#else
<< "-RELEASE-"
#endif
<< Time::getCurrentTime().formatted ("%Y-%m-%d_%H%M%S")
<< Time::getCurrentTime().formatted ("%Y%m%d%H%M%S")
<< "-" << String::toHexString (static_cast<uint16> (Random::getSystemRandom().nextInt()))
<< ".pftrace";

const auto destination = File::getSpecialLocation (File::userHomeDirectory) // TODO - make it configurable
.getChildFile (fileName);
File destination;
if (outputFolder != File() && outputFolder.isDirectory())
destination = outputFolder;
else
destination = File::getSpecialLocation (File::userHomeDirectory);

destination = destination.getChildFile (fileName);

if (destination.existsAsFile())
destination.deleteFile();
Expand Down Expand Up @@ -121,6 +127,13 @@ void Profiler::stopTracing()
Profiler::deleteInstance();
}

//==============================================================================

void Profiler::setOutputFolder (const File& newOutputFolder)
{
outputFolder = newOutputFolder;
}

} // namespace juce

#endif
30 changes: 25 additions & 5 deletions modules/juce_core/profiling/juce_Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class JUCE_API Profiler
This method starts the tracing process using a default buffer size. The tracing session is managed internally
and will continue until `stopTracing()` is called.
@see YUP_PROFILE_START
*/
void startTracing();

Expand All @@ -54,16 +56,30 @@ class JUCE_API Profiler
This method allows you to specify the buffer size used for tracing. The buffer size is defined in kilobytes.
@param sizeInKilobytes The size of the tracing buffer in kilobytes.
@see YUP_PROFILE_START
*/
void startTracing (uint32 sizeInKilobytes);

/** Stops the current tracing session.
This method stops the tracing process and finalizes the trace data.
Once tracing is stopped, the data can be retrieved and analyzed.
@see YUP_PROFILE_STOP
*/
void stopTracing();

/** Define the output folder of the traces.
Call this method as erly as possible to
@param newOutputFolder The output folder where to save traces.
@see YUP_PROFILE_SET_OUTPUT_FOLDER
*/
void setOutputFolder (const File& newOutputFolder);

/** A constexpr function that prettifies a function name at compile time.
This template function accepts a function name and formats it in a more readable manner at compile time.
Expand All @@ -84,6 +100,7 @@ class JUCE_API Profiler
Profiler();

std::unique_ptr<perfetto::TracingSession> session;
File outputFolder;
int fileDescriptor;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Profiler);
Expand Down Expand Up @@ -154,6 +171,9 @@ constexpr auto Profiler::compileTimePrettierFunction (F func)
*/
#define YUP_PROFILE_STOP(...) ::juce::Profiler::getInstance()->stopTracing()

/** Define the output folder of the traces. */
#define YUP_PROFILE_SET_OUTPUT_FOLDER(path) ::juce::Profiler::getInstance()->setOutputFolder (path)

#if ! YUP_PROFILE_DISABLE_TRACE
/** Records a profiling trace event.
Expand All @@ -168,7 +188,7 @@ constexpr auto Profiler::compileTimePrettierFunction (F func)
constexpr auto JUCE_JOIN_MACRO (juce_pfn_, __LINE__) = ::juce::Profiler::compileTimePrettierFunction ([] { return PERFETTO_DEBUG_FUNCTION_IDENTIFIER(); }); \
TRACE_EVENT (category, ::perfetto::StaticString (JUCE_JOIN_MACRO (juce_pfn_, __LINE__).data()), ##__VA_ARGS__)

#define YUP_PROFILE_NAMED_TRACE(category, name, ...) \
#define YUP_PROFILE_NAMED_TRACE(category, name, ...) \
TRACE_EVENT (category, ::perfetto::StaticString (#name), ##__VA_ARGS__)

/** Records a profiling internal trace event.
Expand All @@ -177,18 +197,18 @@ constexpr auto Profiler::compileTimePrettierFunction (F func)
@param ... Optional additional arguments for the trace event.
*/
#define YUP_PROFILE_INTERNAL_TRACE(...) \
#define YUP_PROFILE_INTERNAL_TRACE(...) \
constexpr auto JUCE_JOIN_MACRO (juce_pfn_, __LINE__) = ::juce::Profiler::compileTimePrettierFunction ([] { return PERFETTO_DEBUG_FUNCTION_IDENTIFIER(); }); \
TRACE_EVENT ("yup", ::perfetto::StaticString (JUCE_JOIN_MACRO (juce_pfn_, __LINE__).data()), ##__VA_ARGS__)

#define YUP_PROFILE_NAMED_INTERNAL_TRACE(name, ...) \
#define YUP_PROFILE_NAMED_INTERNAL_TRACE(name, ...) \
TRACE_EVENT ("yup", ::perfetto::StaticString (#name), ##__VA_ARGS__)

#else
#define YUP_PROFILE_TRACE(category, ...)
#define YUP_PROFILE_NAMED_TRACE(category, name, ...) \
#define YUP_PROFILE_NAMED_TRACE(category, name, ...)
#define YUP_PROFILE_INTERNAL_TRACE(...)
#define YUP_PROFILE_NAMED_INTERNAL_TRACE(name, ...) \
#define YUP_PROFILE_NAMED_INTERNAL_TRACE(name, ...)

#endif
// clang-format on
Expand Down

0 comments on commit 1f60a3f

Please sign in to comment.