Implementation:CARLA simulator Carla Profiler
| Knowledge Sources | |
|---|---|
| Domains | Profiling, Debugging |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
The Profiler module provides compile-time-configurable profiling macros for measuring function execution time, with automatic periodic logging of statistics.
Description
The profiling system consists of two components:
ProfilerData class -- accumulates timing statistics:
- Tracks total call count and execution time (in nanoseconds via
std::chrono::high_resolution_clock) - Computes average time per call
- Auto-logs statistics every 10 seconds (configurable via
DISPLAY_RATE) since program start - Formats output as:
"name: {avg}ms (approximately {total_calls} per {elapsed_sec}s)" - Stores a name string for identification
CARLA_PROFILE_SCOPE / CARLA_PROFILE_FPS macros:
- CARLA_PROFILE_SCOPE(context, name) -- creates a scoped timer that measures the lifetime of the current scope. On destruction, it adds the elapsed time to the associated
ProfilerData - CARLA_PROFILE_FPS(context, name) -- similar to PROFILE_SCOPE but measures frame rate (calls per second)
- Both macros create a
static thread_local ProfilerDatato accumulate data without lock contention - When
LIBCARLA_ENABLE_PROFILERis disabled, the macros expand to empty statements (zero overhead)
The ScopedProfiler class implements RAII timing -- the constructor records the start time, and the destructor computes the elapsed time and reports it to ProfilerData::Annotate().
Usage
Use the CARLA_PROFILE_SCOPE macro in performance-critical functions during development to identify bottlenecks. The profiler automatically logs statistics periodically.
Code Reference
Source Location
- Repository: CARLA
- Files:
LibCarla/source/carla/profiler/Profiler.h,LibCarla/source/carla/profiler/Profiler.cpp
Signature
class ProfilerData : private NonCopyable {
public:
explicit ProfilerData(std::string name, bool print_fps = false);
void Annotate(const profiler::detail::ProfilerData &data);
void Annotate(size_t nanoseconds);
private:
const std::string _name;
bool _print_fps;
size_t _count = 0u;
size_t _total_microseconds = 0u;
// ...
};
class ScopedProfiler {
public:
explicit ScopedProfiler(ProfilerData &parent);
~ScopedProfiler(); // Records elapsed time
};
// Usage macros
#define CARLA_PROFILE_SCOPE(context, name) ...
#define CARLA_PROFILE_FPS(context, name) ...
Import
#include "carla/profiler/Profiler.h"
I/O Contract
| Input | Type | Description |
|---|---|---|
std::string | Identifier for the profiled scope
| ||
| Scope lifetime | Time measured from construction to destruction |
| Output | Type | Description |
|---|---|---|
log_info | Periodic statistics: avg time, call count, elapsed time
|
Usage Examples
void MyFunction() {
CARLA_PROFILE_SCOPE(this, "MyFunction");
// ... function body ...
}
// Logs every 10s: "MyFunction: 1.25ms (approximately 800 per 10.00s)"