Bug 1858921 - Part 6: Remove unused default template arguments r=sfink
[gecko.git] / dom / media / Tracing.h
blobbbacd197f550e026fa9afa1c06ec9453b176b40a
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/. */
7 #ifndef TRACING_H
8 #define TRACING_H
10 #include <algorithm>
11 #include <cstdint>
12 #include <cstdio>
14 #include "AsyncLogger.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/UniquePtr.h"
19 #if defined(_MSC_VER)
20 // MSVC
21 # define FUNCTION_SIGNATURE __FUNCSIG__
22 #elif defined(__GNUC__)
23 // gcc, clang
24 # define FUNCTION_SIGNATURE __PRETTY_FUNCTION__
25 #endif
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(id, aFrames, aSampleRate) \
39 AutoTracer budget(gAudioCallbackTraceLogger, id, \
40 AutoTracer::EventType::BUDGET, aFrames, aSampleRate);
41 #else
42 # define TRACE(aName)
43 # define TRACE_COMMENT(aFmt, ...)
44 # define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate)
45 #endif
47 class MOZ_RAII AutoTracer {
48 public:
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)
60 : mLogger(aLogger),
61 mLocation(aLocation),
62 mComment(mBuffer),
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);
68 mBuffer[size] = 0;
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);
77 ~AutoTracer();
79 private:
80 void PrintEvent(const char* aName, const char* aCategory,
81 const char* aComment,
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
94 // lifetime.
95 const char* mComment;
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 */