Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / dom / media / Tracing.cpp
blobbbab95039c08706df328ca295319a5bb626b86ac
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 #include "Tracing.h"
9 #include <inttypes.h>
11 using namespace mozilla;
13 using TracingPhase = mozilla::AsyncLogger::TracingPhase;
15 mozilla::AsyncLogger gAudioCallbackTraceLogger;
16 static std::atomic<int> gTracingStarted(0);
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();
25 #endif
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();
35 #endif
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
45 void AutoTracer::PrintBudget(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
53 AutoTracer::AutoTracer(AsyncLogger& aLogger, const char* aLocation,
54 EventType aEventType, uint64_t aFrames,
55 uint64_t aSampleRate)
56 : mLogger(aLogger),
57 mLocation(aLocation),
58 mComment(nullptr),
59 mEventType(aEventType) {
60 MOZ_ASSERT(aEventType == EventType::BUDGET);
62 if (aLogger.Enabled()) {
63 float durationUS = (static_cast<float>(aFrames) / aSampleRate) * 1e6;
64 PrintBudget(aLocation, "perf", durationUS, aFrames, aSampleRate);
68 AutoTracer::AutoTracer(AsyncLogger& aLogger, const char* aLocation,
69 EventType aEventType, const char* aComment)
70 : mLogger(aLogger),
71 mLocation(aLocation),
72 mComment(aComment),
73 mEventType(aEventType) {
74 MOZ_ASSERT(aEventType == EventType::DURATION);
75 if (aLogger.Enabled()) {
76 PrintEvent(aLocation, "perf", mComment, AsyncLogger::TracingPhase::BEGIN);
80 AutoTracer::~AutoTracer() {
81 if (mEventType == EventType::DURATION) {
82 if (mLogger.Enabled()) {
83 PrintEvent(mLocation, "perf", mComment, AsyncLogger::TracingPhase::END);