Tracing.h (3859B)
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 #include "mozilla/Attributes.h" 16 17 #if defined(_MSC_VER) 18 // MSVC 19 # define FUNCTION_SIGNATURE __FUNCSIG__ 20 #elif defined(__GNUC__) 21 // gcc, clang 22 # define FUNCTION_SIGNATURE __PRETTY_FUNCTION__ 23 #endif 24 25 extern mozilla::AsyncLogger gAudioCallbackTraceLogger; 26 27 // This is no-op if tracing is not enabled, and is idempotent. 28 void StartAudioCallbackTracing(); 29 void StopAudioCallbackTracing(); 30 31 #ifdef MOZ_REAL_TIME_TRACING 32 # define TRACE(aName) AutoTracer trace(gAudioCallbackTraceLogger, aName); 33 # define TRACE_COMMENT(aName, aFmt, ...) \ 34 AutoTracer trace(gAudioCallbackTraceLogger, aName, \ 35 AutoTracer::DurationType::ELAPSED_TIME, aFmt, \ 36 ##__VA_ARGS__); 37 # define TRACE_AUDIO_CALLBACK_FRAME_COUNT(aLocation, aFrames, aSampleRate) \ 38 do { \ 39 AutoTracer tracer(gAudioCallbackTraceLogger, aLocation, \ 40 AutoTracer::DurationType::FRAME_COUNT, aFrames, \ 41 aSampleRate); \ 42 } while (false) 43 #else 44 # define TRACE(aName) 45 # define TRACE_COMMENT(aFmt, ...) 46 # define TRACE_AUDIO_CALLBACK_FRAME_COUNT(aLocation, aFrames, aSampleRate) 47 #endif 48 49 class MOZ_RAII AutoTracer { 50 public: 51 static const int32_t BUFFER_SIZE = 256; 52 53 enum class DurationType { ELAPSED_TIME, FRAME_COUNT }; 54 55 AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation, 56 DurationType aDurationType = DurationType::ELAPSED_TIME, 57 const char* aComment = nullptr); 58 59 template <typename... Args> 60 AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation, 61 DurationType aDurationType, const char* aFormat, Args... aArgs) 62 : mLogger(aLogger), 63 mLocation(aLocation), 64 mComment(mBuffer), 65 mDurationType(aDurationType) { 66 MOZ_ASSERT(aDurationType == DurationType::ELAPSED_TIME); 67 if (aLogger.Enabled()) { 68 int32_t size = snprintf(mBuffer, BUFFER_SIZE, aFormat, aArgs...); 69 size = std::min(size, BUFFER_SIZE - 1); 70 mBuffer[size] = 0; 71 PrintEvent(aLocation, "perf", mComment, 72 mozilla::AsyncLogger::TracingPhase::BEGIN); 73 } 74 } 75 76 AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation, 77 DurationType aDurationType, uint64_t aFrames, 78 uint64_t aSampleRate); 79 80 ~AutoTracer(); 81 82 private: 83 void PrintEvent(const char* aName, const char* aCategory, 84 const char* aComment, 85 mozilla::AsyncLogger::TracingPhase aPhase); 86 87 void PrintDuration(const char* aName, const char* aCategory, 88 uint64_t aDuration, uint64_t aFrames, 89 uint64_t aSampleRate); 90 91 // The logger to use. It musdt have a lifetime longer than the block an 92 // instance of this class traces. 93 mozilla::AsyncLogger& mLogger; 94 // The location for this trace point, arbitrary string literal, often the 95 // name of the calling function, with a static lifetime. 96 const char* mLocation; 97 // A comment for this trace point, abitrary string literal with a static 98 // lifetime. 99 const char* mComment; 100 // A buffer used to hold string-formatted traces. 101 char mBuffer[BUFFER_SIZE]; 102 // The duration type, for now either elapsed time or frame count. 103 const DurationType mDurationType; 104 }; 105 106 #endif /* TRACING_H */