Tracing.cpp (2790B)
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 #include "Tracing.h" 8 9 #include <inttypes.h> 10 11 using namespace mozilla; 12 13 using TracingPhase = mozilla::AsyncLogger::TracingPhase; 14 15 MOZ_RUNINIT mozilla::AsyncLogger gAudioCallbackTraceLogger; 16 static std::atomic<int> gTracingStarted(0); 17 18 void StartAudioCallbackTracing() { 19 #ifdef MOZ_REAL_TIME_TRACING 20 int cnt = gTracingStarted.fetch_add(1, std::memory_order_seq_cst); 21 if (cnt == 0) { 22 // This is a noop if the logger has not been enabled. 23 gAudioCallbackTraceLogger.Start(); 24 } 25 #endif 26 } 27 28 void StopAudioCallbackTracing() { 29 #ifdef MOZ_REAL_TIME_TRACING 30 int cnt = gTracingStarted.fetch_sub(1, std::memory_order_seq_cst); 31 if (cnt == 1) { 32 // This is a noop if the logger has not been enabled. 33 gAudioCallbackTraceLogger.Stop(); 34 } 35 #endif 36 } 37 38 void AutoTracer::PrintEvent(const char* aName, const char* aCategory, 39 const char* aComment, TracingPhase aPhase) { 40 #ifdef MOZ_REAL_TIME_TRACING 41 mLogger.Log(aName, aCategory, aComment, aPhase); 42 #endif 43 } 44 45 void AutoTracer::PrintDuration(const char* aName, const char* aCategory, 46 uint64_t aDuration, uint64_t aFrames, 47 uint64_t aSampleRate) { 48 #ifdef MOZ_REAL_TIME_TRACING 49 mLogger.LogDuration(aName, aCategory, aDuration, aFrames, aSampleRate); 50 #endif 51 } 52 53 AutoTracer::AutoTracer(AsyncLogger& aLogger, const char* aLocation, 54 DurationType aDurationType, uint64_t aFrames, 55 uint64_t aSampleRate) 56 : mLogger(aLogger), 57 mLocation(aLocation), 58 mComment(nullptr), 59 mDurationType(aDurationType) { 60 MOZ_ASSERT(aDurationType == DurationType::FRAME_COUNT); 61 62 if (aLogger.Enabled()) { 63 float durationUS = (static_cast<float>(aFrames) / aSampleRate) * 1e6; 64 PrintDuration(aLocation, "perf", durationUS, aFrames, aSampleRate); 65 } 66 } 67 68 AutoTracer::AutoTracer(AsyncLogger& aLogger, const char* aLocation, 69 DurationType aDurationType, const char* aComment) 70 : mLogger(aLogger), 71 mLocation(aLocation), 72 mComment(aComment), 73 mDurationType(aDurationType) { 74 MOZ_ASSERT(aDurationType == DurationType::ELAPSED_TIME); 75 if (aLogger.Enabled()) { 76 PrintEvent(aLocation, "perf", mComment, AsyncLogger::TracingPhase::BEGIN); 77 } 78 } 79 80 AutoTracer::~AutoTracer() { 81 if (mDurationType == DurationType::ELAPSED_TIME) { 82 if (mLogger.Enabled()) { 83 PrintEvent(mLocation, "perf", mComment, AsyncLogger::TracingPhase::END); 84 } 85 } 86 }