/work/obj-fuzz/dist/include/Tracing.h
Line | Count | Source (jump to first uncovered line) |
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(_WIN32) |
20 | | #include <process.h> |
21 | | #define getpid() _getpid() |
22 | | #else |
23 | | #include <unistd.h> |
24 | | #endif |
25 | | |
26 | | #if defined(_MSC_VER) |
27 | | // MSVC |
28 | | #define FUNCTION_SIGNATURE __FUNCSIG__ |
29 | | #elif defined(__GNUC__) |
30 | | // gcc, clang |
31 | 0 | #define FUNCTION_SIGNATURE __PRETTY_FUNCTION__ |
32 | | #endif |
33 | | |
34 | | #ifdef TRACING |
35 | | /* TRACE is for use in the real-time audio rendering thread. |
36 | | * It would be better to always pass in the thread id. However, the thread an |
37 | | * audio callback runs on can change when the underlying audio device change, |
38 | | * and also it seems to be called from a thread pool in a round-robin fashion |
39 | | * when audio remoting is activated, making the traces unreadable. |
40 | | * The thread on which the AudioCallbackDriver::DataCallback is to always |
41 | | * be thread 0, and the budget is set to always be thread 1. This allows |
42 | | * displaying those elements in two separate lanes. |
43 | | * The other thread have "normal" tid. Hashing allows being able to get a |
44 | | * string representation that is unique and guaranteed to be portable. */ |
45 | | #define TRACE_AUDIO_CALLBACK() \ |
46 | | AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(), 0); |
47 | | #define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate) \ |
48 | | AutoTracer budget(gMSGTraceLogger, "Real-time budget", getpid(), 1, \ |
49 | | AutoTracer::EventType::BUDGET, aFrames, aSampleRate); |
50 | | #define TRACE_AUDIO_CALLBACK_COMMENT(aFmt, ...) \ |
51 | | AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(), 0, \ |
52 | | AutoTracer::EventType::DURATION, \ |
53 | | aFmt, ##__VA_ARGS__); |
54 | | #define TRACE() \ |
55 | | AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(), \ |
56 | | std::hash<std::thread::id>{}(std::this_thread::get_id())); |
57 | | #define TRACE_COMMENT(aFmt, ...) \ |
58 | 0 | AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(), \ |
59 | 0 | std::hash<std::thread::id>{}(std::this_thread::get_id()), \ |
60 | 0 | AutoTracer::EventType::DURATION, \ |
61 | 0 | aFmt, ##__VA_ARGS__); |
62 | | #else |
63 | | #define TRACE_AUDIO_CALLBACK() |
64 | | #define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate) |
65 | | #define TRACE_AUDIO_CALLBACK_COMMENT(aFmt, ...) |
66 | | #define TRACE() |
67 | | #define TRACE_COMMENT(aFmt, ...) |
68 | | #endif |
69 | | |
70 | | class MOZ_RAII AutoTracer |
71 | | { |
72 | | public: |
73 | | static const int32_t BUFFER_SIZE = mozilla::AsyncLogger::MAX_MESSAGE_LENGTH / 2; |
74 | | |
75 | | enum class EventType |
76 | | { |
77 | | DURATION, |
78 | | BUDGET |
79 | | }; |
80 | | |
81 | | AutoTracer(mozilla::AsyncLogger& aLogger, |
82 | | const char* aLocation, |
83 | | uint64_t aPID, |
84 | | uint64_t aTID, |
85 | | EventType aEventType = EventType::DURATION, |
86 | | const char* aComment = nullptr); |
87 | | |
88 | | template<typename... Args> |
89 | | AutoTracer(mozilla::AsyncLogger& aLogger, |
90 | | const char* aLocation, |
91 | | uint64_t aPID, |
92 | | uint64_t aTID, |
93 | | EventType aEventType, |
94 | | const char* aFormat, |
95 | | Args... aArgs) |
96 | | : mLogger(aLogger) |
97 | | , mLocation(aLocation) |
98 | | , mComment(mBuffer) |
99 | | , mEventType(aEventType) |
100 | | , mPID(aPID) |
101 | | , mTID(aTID) |
102 | 0 | { |
103 | 0 | MOZ_ASSERT(aEventType == EventType::DURATION); |
104 | 0 | if (aLogger.Enabled()) { |
105 | 0 | int32_t size = snprintf(mBuffer, BUFFER_SIZE, aFormat, aArgs...); |
106 | 0 | size = std::min(size, BUFFER_SIZE - 1); |
107 | 0 | mBuffer[size] = 0; |
108 | 0 | PrintEvent(aLocation, "perf", mComment, TracingPhase::BEGIN, NowInUs(), aPID, aTID); |
109 | 0 | } |
110 | 0 | } Unexecuted instantiation: AutoTracer::AutoTracer<mozilla::AudioTrackEncoder*>(mozilla::AsyncLogger&, char const*, unsigned long, unsigned long, AutoTracer::EventType, char const*, mozilla::AudioTrackEncoder*) Unexecuted instantiation: AutoTracer::AutoTracer<mozilla::VideoTrackEncoder*>(mozilla::AsyncLogger&, char const*, unsigned long, unsigned long, AutoTracer::EventType, char const*, mozilla::VideoTrackEncoder*) |
111 | | |
112 | | AutoTracer(mozilla::AsyncLogger& aLogger, |
113 | | const char* aLocation, |
114 | | uint64_t aPID, |
115 | | uint64_t aTID, |
116 | | EventType aEventType, |
117 | | uint64_t aFrames, |
118 | | uint64_t aSampleRate); |
119 | | |
120 | | ~AutoTracer(); |
121 | | private: |
122 | | uint64_t NowInUs(); |
123 | | |
124 | | enum class TracingPhase |
125 | | { |
126 | | BEGIN, |
127 | | END, |
128 | | COMPLETE |
129 | | }; |
130 | | |
131 | | const char TRACING_PHASE_STRINGS[3] = { |
132 | | 'B', |
133 | | 'E', |
134 | | 'X' |
135 | | }; |
136 | | |
137 | | void PrintEvent(const char* aName, |
138 | | const char* aCategory, |
139 | | const char* aComment, |
140 | | TracingPhase aPhase, |
141 | | uint64_t aTime, |
142 | | uint64_t aPID, |
143 | | uint64_t aThread); |
144 | | |
145 | | void PrintBudget(const char* aName, |
146 | | const char* aCategory, |
147 | | uint64_t aDuration, |
148 | | uint64_t aPID, |
149 | | uint64_t aThread, |
150 | | uint64_t aFrames, |
151 | | uint64_t aSampleRate); |
152 | | |
153 | | // The logger to use. It musdt have a lifetime longer than the block an |
154 | | // instance of this class traces. |
155 | | mozilla::AsyncLogger& mLogger; |
156 | | // The location for this trace point, arbitrary string literal, often the |
157 | | // name of the calling function, with a static lifetime. |
158 | | const char* mLocation; |
159 | | // A comment for this trace point, abitrary string literal with a static |
160 | | // lifetime. |
161 | | const char* mComment; |
162 | | // A buffer used to hold string-formatted traces. |
163 | | char mBuffer[BUFFER_SIZE]; |
164 | | // The event type, for now either a budget or a duration. |
165 | | const EventType mEventType; |
166 | | // The process ID of the calling process. Traces are grouped by PID in the |
167 | | // vizualizer. |
168 | | const uint64_t mPID; |
169 | | // The thread ID of the calling thread, will be displayed in a separate |
170 | | // section in the trace visualizer. |
171 | | const uint64_t mTID; |
172 | | }; |
173 | | |
174 | | #if defined(_WIN32) |
175 | | #undef getpid |
176 | | #endif |
177 | | |
178 | | #endif /* TRACING_H */ |