Tracer is a header-only lightweight performance tracing library for recording and analyzing C++ program execution. It helps developers identify performance bottlenecks, analyze function execution times, and outputs results in Chrome Tracing compatible format for easy visualization.
- Easy Integration: Just include a single header file
- Low Overhead: Designed to minimize impact on the analyzed program
- Visualization Friendly: Generates Chrome Tracing compatible JSON output
- Thread-Safe: Supports multi-threaded application analysis
- Automatic Management: Uses RAII-style C++ objects for automatic scope tracing
- Optional Compilation: Can be completely disabled at compile time via macro definition
-
Copy the
tracer.hfile to your project -
Include the header in source files you want to trace:
#include "tracer/tracer.h"
-
Use the provided macros to trace code execution:
void some_function() { TRACE_SCOPE("some_function"); // Automatically traces function scope // Function code... TRACE_INSTANT("interesting_point"); // Records an instant event // More code... }
-
Save trace results at the end of your program:
TRACE_SAVE("trace_result.json");
-
View results in Chrome browser:
- Open Chrome
- Visit
chrome://tracing - Click "Load" button to load the generated JSON file
TRACE_SCOPE(name): Creates a scope event that automatically records entry and exit timesTRACE_INSTANT(name): Records an instant eventTRACE_DATA(p_str): Gets collected trace data and saves to the specified string pointerTRACE_SAVE(filename): Saves trace data to a file
Define the TRACE_DISABLED macro to disable all tracing code at compile time:
#define TRACE_DISABLED
#include "tracer/tracer.h"Or add to your compile command:
g++ -DTRACE_DISABLED ...Check out the example program in the example directory to see how to use Tracer in a real project:
cd example
make
./tracer_exampleThe trace_result.json file generated by the example program can be directly opened in Chrome's tracing page.
- Tracer uses a ring buffer to store events, with a default capacity of 10,000 events
- When capacity is reached, new events overwrite the oldest ones
- Each event includes name, timestamp, duration, thread ID, process ID, and CPU usage information
- Efficient multi-threading support is implemented via thread-local storage
This project is released under the MIT License. See the LICENSE file for details.
Feedback and improvements are welcome via Issues and Pull Requests.