1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
14 #include "AsyncLogger.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/UniquePtr.h"
21 # define FUNCTION_SIGNATURE __FUNCSIG__
22 #elif defined(__GNUC__)
24 # define FUNCTION_SIGNATURE __PRETTY_FUNCTION__
27 extern mozilla::AsyncLogger gAudioCallbackTraceLogger
;
29 // This is no-op if tracing is not enabled, and is idempotent.
30 void StartAudioCallbackTracing();
31 void StopAudioCallbackTracing();
33 #ifdef MOZ_REAL_TIME_TRACING
34 # define TRACE(aName) AutoTracer trace(gAudioCallbackTraceLogger, aName);
35 # define TRACE_COMMENT(aName, aFmt, ...) \
36 AutoTracer trace(gAudioCallbackTraceLogger, aName, \
37 AutoTracer::EventType::DURATION, aFmt, ##__VA_ARGS__);
38 # define TRACE_AUDIO_CALLBACK_BUDGET(aLocation, aFrames, aSampleRate) \
39 AutoTracer budget(gAudioCallbackTraceLogger, aLocation, \
40 AutoTracer::EventType::BUDGET, aFrames, aSampleRate);
43 # define TRACE_COMMENT(aFmt, ...)
44 # define TRACE_AUDIO_CALLBACK_BUDGET(aLocation, aFrames, aSampleRate)
47 class MOZ_RAII AutoTracer
{
49 static const int32_t BUFFER_SIZE
= 256;
51 enum class EventType
{ DURATION
, BUDGET
};
53 AutoTracer(mozilla::AsyncLogger
& aLogger
, const char* aLocation
,
54 EventType aEventType
= EventType::DURATION
,
55 const char* aComment
= nullptr);
57 template <typename
... Args
>
58 AutoTracer(mozilla::AsyncLogger
& aLogger
, const char* aLocation
,
59 EventType aEventType
, const char* aFormat
, Args
... aArgs
)
63 mEventType(aEventType
) {
64 MOZ_ASSERT(aEventType
== EventType::DURATION
);
65 if (aLogger
.Enabled()) {
66 int32_t size
= snprintf(mBuffer
, BUFFER_SIZE
, aFormat
, aArgs
...);
67 size
= std::min(size
, BUFFER_SIZE
- 1);
69 PrintEvent(aLocation
, "perf", mComment
,
70 mozilla::AsyncLogger::TracingPhase::BEGIN
);
74 AutoTracer(mozilla::AsyncLogger
& aLogger
, const char* aLocation
,
75 EventType aEventType
, uint64_t aFrames
, uint64_t aSampleRate
);
80 void PrintEvent(const char* aName
, const char* aCategory
,
82 mozilla::AsyncLogger::TracingPhase aPhase
);
84 void PrintBudget(const char* aName
, const char* aCategory
, uint64_t aDuration
,
85 uint64_t aFrames
, uint64_t aSampleRate
);
87 // The logger to use. It musdt have a lifetime longer than the block an
88 // instance of this class traces.
89 mozilla::AsyncLogger
& mLogger
;
90 // The location for this trace point, arbitrary string literal, often the
91 // name of the calling function, with a static lifetime.
92 const char* mLocation
;
93 // A comment for this trace point, abitrary string literal with a static
96 // A buffer used to hold string-formatted traces.
97 char mBuffer
[BUFFER_SIZE
];
98 // The event type, for now either a budget or a duration.
99 const EventType mEventType
;
102 #endif /* TRACING_H */