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/. */
6 
7 #ifndef TRACING_H
8 #define TRACING_H
9 
10 #include <algorithm>
11 #include <cstdint>
12 #include <cstdio>
13 
14 #include "AsyncLogger.h"
15 
16 #include "mozilla/Attributes.h"
17 #include "mozilla/UniquePtr.h"
18 
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
26 
27 extern mozilla::AsyncLogger gAudioCallbackTraceLogger;
28 
29 // This is no-op if tracing is not enabled, and is idempotent.
30 void StartAudioCallbackTracing();
31 void StopAudioCallbackTracing();
32 
33 #ifdef TRACING
34 /* TRACE is for use in the real-time audio rendering thread.
35  * It would be better to always pass in the thread id. However, the thread an
36  * audio callback runs on can change when the underlying audio device change,
37  * and also it seems to be called from a thread pool in a round-robin fashion
38  * when audio remoting is activated, making the traces unreadable.
39  * The thread on which the AudioCallbackDriver::DataCallback is to always
40  * be thread 0, and the budget is set to always be thread 1. This allows
41  * displaying those elements in two separate lanes.
42  * The other thread have "normal" tid. Hashing allows being able to get a
43  * string representation that is unique and guaranteed to be portable. */
44 #  define TRACE_AUDIO_CALLBACK() \
45     AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE);
46 #  define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate)          \
47     AutoTracer budget(gAudioCallbackTraceLogger, "Real-time budget", \
48                       AutoTracer::EventType::BUDGET, aFrames, aSampleRate);
49 #  define TRACE_AUDIO_CALLBACK_COMMENT(aFmt, ...)                   \
50     AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE, \
51                      AutoTracer::EventType::DURATION, aFmt, ##__VA_ARGS__);
52 #  define TRACE() \
53     AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE);
54 #  define TRACE_COMMENT(aFmt, ...)                                  \
55     AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE, \
56                      AutoTracer::EventType::DURATION, aFmt, ##__VA_ARGS__);
57 #else
58 #  define TRACE()
59 #  define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate)
60 #  define TRACE_COMMENT(aFmt, ...)
61 #endif
62 
63 class MOZ_RAII AutoTracer {
64  public:
65   static const int32_t BUFFER_SIZE = 256;
66 
67   enum class EventType { DURATION, BUDGET };
68 
69   AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation,
70              EventType aEventType = EventType::DURATION,
71              const char* aComment = nullptr);
72 
73   template <typename... Args>
AutoTracer(mozilla::AsyncLogger & aLogger,const char * aLocation,EventType aEventType,const char * aFormat,Args...aArgs)74   AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation,
75              EventType aEventType, const char* aFormat, Args... aArgs)
76       : mLogger(aLogger),
77         mLocation(aLocation),
78         mComment(mBuffer),
79         mEventType(aEventType) {
80     MOZ_ASSERT(aEventType == EventType::DURATION);
81     if (aLogger.Enabled()) {
82       int32_t size = snprintf(mBuffer, BUFFER_SIZE, aFormat, aArgs...);
83       size = std::min(size, BUFFER_SIZE - 1);
84       mBuffer[size] = 0;
85       PrintEvent(aLocation, "perf", mComment,
86                  mozilla::AsyncLogger::TracingPhase::BEGIN);
87     }
88   }
89 
90   AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation,
91              EventType aEventType, uint64_t aFrames, uint64_t aSampleRate);
92 
93   ~AutoTracer();
94 
95  private:
96   void PrintEvent(const char* aName, const char* aCategory,
97                   const char* aComment,
98                   mozilla::AsyncLogger::TracingPhase aPhase);
99 
100   void PrintBudget(const char* aName, const char* aCategory, uint64_t aDuration,
101                    uint64_t aFrames, uint64_t aSampleRate);
102 
103   // The logger to use. It musdt have a lifetime longer than the block an
104   // instance of this class traces.
105   mozilla::AsyncLogger& mLogger;
106   // The location for this trace point, arbitrary string literal, often the
107   // name of the calling function, with a static lifetime.
108   const char* mLocation;
109   // A comment for this trace point, abitrary string literal with a static
110   // lifetime.
111   const char* mComment;
112   // A buffer used to hold string-formatted traces.
113   char mBuffer[BUFFER_SIZE];
114   // The event type, for now either a budget or a duration.
115   const EventType mEventType;
116 };
117 
118 #endif /* TRACING_H */
119