Skip to content

Commit

Permalink
Allow trace to not be started automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sudara committed Nov 17, 2024
1 parent 07fee72 commit b63bd23
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ if (MelatoninPerfetto_IS_TOP_LEVEL)
message (STATUS "Cloning JUCE...")

FetchContent_Declare(JUCE
GIT_REPOSITORY https://github.com/juce-framework/JUCE.git
GIT_REPOSITORY https://github.com/sudara/JUCE.git
GIT_TAG origin/master
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
FIND_PACKAGE_ARGS 7.0.9)
FIND_PACKAGE_ARGS develop)

FetchContent_MakeAvailable(JUCE)
endif ()
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,32 @@ If you use perfetto regularly, you can also do what I do and check for `PERFETTO

<img width="384" alt="AudioPluginHost - 2023-01-06 44@2x" src="https://user-images.githubusercontent.com/472/211118327-e984f359-4e2f-4aec-8b4d-991093b36e67.png">

## Running Perfetto in your tests

It can be really nice to run a few test cases through perfetto.

To do so with Catch2, for example, you'll need to first link against `Catch2::Catch2` instead of `Catch2::Catch2WithMain`:

```
target_link_libraries(Tests PRIVATE SharedCode Catch2::Catch2WithMain)
```

And then define your own `main` function.

```cpp
#include "melatonin_perfetto/melatonin_perfetto.h"
int main (int argc, char* argv[])
{
#if PERFETTO
MelatoninPerfetto perfetto;
#endif

const int result = Catch::Session().run (argc, argv);

return result;
}
```
## Running Melatonin::Perfetto's tests
`melatonin_perfetto` includes a test suite using CTest. To run the tests, clone the code and run these commands:
Expand Down
22 changes: 13 additions & 9 deletions melatonin_perfetto/melatonin_perfetto.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,26 @@ PERFETTO_DEFINE_CATEGORIES (
class MelatoninPerfetto
{
public:
MelatoninPerfetto()
MelatoninPerfetto (bool startTrace = true) : startTraceAutomatically (startTrace)
{
perfetto::TracingInitArgs args;
// The backends determine where trace events are recorded. For this example we
// are going to use the in-process tracing service, which only includes in-app
// events.
// are going to use the in-process tracing service, which only includes in-app events.
args.backends = perfetto::kInProcessBackend;
perfetto::Tracing::Initialize (args);
perfetto::TrackEvent::Register();
beginSession();

if (startTraceAutomatically)
beginSession();
}

~MelatoninPerfetto()
{
endSession();
if (startTraceAutomatically)
endSession();
}

void beginSession (uint32_t buffer_size_kb = 80000)
void beginSession (const uint32_t buffer_size_kb = 80000)
{
perfetto::TraceConfig cfg;
cfg.add_buffers()->set_size_kb (buffer_size_kb); // 80MB is the default
Expand Down Expand Up @@ -98,6 +100,7 @@ class MelatoninPerfetto
}

private:
bool startTraceAutomatically;
juce::File writeFile()
{
// Read trace data
Expand Down Expand Up @@ -160,7 +163,7 @@ namespace melatonin
{
// if we're C++20 or higher, ensure we're compile-time
#if __cplusplus >= 202002L
// This should never assert, but if so, report it on this issue:
// This should never assert, but if so, report it on this issue:
// https://github.com/sudara/melatonin_perfetto/issues/13#issue-1558171132
if (!std::is_constant_evaluated())
jassertfalse;
Expand Down Expand Up @@ -235,9 +238,8 @@ namespace melatonin
}
}


#else
// allow people to keep perfetto helpers in-place even when disabled
// allow people to keep perfetto helpers in-place even when disabled
#define TRACE_EVENT_BEGIN(category, ...)
#define TRACE_EVENT_END(category)
#define TRACE_EVENT(category, ...)
Expand All @@ -253,6 +255,7 @@ namespace melatonin
#define TRACE_DSP(...) \
constexpr auto pf = melatonin::compileTimePrettierFunction (WRAP_COMPILE_TIME_STRING (PERFETTO_DEBUG_FUNCTION_IDENTIFIER())); \
TRACE_EVENT ("dsp", perfetto::StaticString (pf.data()), ##__VA_ARGS__)

#define TRACE_DSP_BEGIN(name) TRACE_EVENT_BEGIN ("dsp", perfetto::StaticString (name))
#define TRACE_DSP_END() TRACE_EVENT_END ("dsp")
#else
Expand All @@ -265,6 +268,7 @@ namespace melatonin
#define TRACE_COMPONENT(...) \
constexpr auto pf = melatonin::compileTimePrettierFunction (WRAP_COMPILE_TIME_STRING (PERFETTO_DEBUG_FUNCTION_IDENTIFIER())); \
TRACE_EVENT ("component", perfetto::StaticString (pf.data()), ##__VA_ARGS__)

#define TRACE_COMPONENT_BEGIN(name) TRACE_EVENT_BEGIN ("component", perfetto::StaticString (name))
#define TRACE_COMPONENT_END() TRACE_EVENT_END ("component")
#else
Expand Down

0 comments on commit b63bd23

Please sign in to comment.