2018-03-20 20:22:08 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "Tracing.h"
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2020-06-12 16:11:47 +03:00
|
|
|
using TracingPhase = mozilla::AsyncLogger::TracingPhase;
|
|
|
|
|
2019-10-02 15:16:22 +03:00
|
|
|
mozilla::AsyncLogger gAudioCallbackTraceLogger("AudioCallbackTracing");
|
2020-02-12 17:02:08 +03:00
|
|
|
static std::atomic<int> gTracingStarted(0);
|
2019-10-02 15:16:22 +03:00
|
|
|
|
|
|
|
void StartAudioCallbackTracing() {
|
|
|
|
#ifdef TRACING
|
2020-02-12 17:02:08 +03:00
|
|
|
int cnt = gTracingStarted.fetch_add(1, std::memory_order_seq_cst);
|
|
|
|
if (cnt == 0) {
|
|
|
|
// This is a noop if the logger has not been enabled.
|
|
|
|
gAudioCallbackTraceLogger.Start();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopAudioCallbackTracing() {
|
|
|
|
#ifdef TRACING
|
|
|
|
int cnt = gTracingStarted.fetch_sub(1, std::memory_order_seq_cst);
|
|
|
|
if (cnt == 1) {
|
|
|
|
// This is a noop if the logger has not been enabled.
|
|
|
|
gAudioCallbackTraceLogger.Stop();
|
2019-10-02 15:16:22 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
void AutoTracer::PrintEvent(const char* aName, const char* aCategory,
|
2020-06-12 16:12:50 +03:00
|
|
|
const char* aComment, TracingPhase aPhase) {
|
|
|
|
mLogger.Log(aName, aCategory, aComment, aPhase);
|
2018-03-20 20:22:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoTracer::PrintBudget(const char* aName, const char* aCategory,
|
2020-06-12 16:12:50 +03:00
|
|
|
uint64_t aDuration, uint64_t aFrames,
|
2018-06-27 16:34:21 +03:00
|
|
|
uint64_t aSampleRate) {
|
2020-06-12 16:12:50 +03:00
|
|
|
mLogger.LogDuration(aName, aCategory, aDuration, aFrames, aSampleRate);
|
2018-03-20 20:22:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoTracer::AutoTracer(AsyncLogger& aLogger, const char* aLocation,
|
2020-06-12 16:12:50 +03:00
|
|
|
EventType aEventType, uint64_t aFrames,
|
|
|
|
uint64_t aSampleRate)
|
2018-03-20 20:22:08 +03:00
|
|
|
: mLogger(aLogger),
|
|
|
|
mLocation(aLocation),
|
2018-06-15 10:25:02 +03:00
|
|
|
mComment(nullptr),
|
2020-06-12 16:12:50 +03:00
|
|
|
mEventType(aEventType) {
|
2018-03-20 20:22:08 +03:00
|
|
|
MOZ_ASSERT(aEventType == EventType::BUDGET);
|
|
|
|
|
|
|
|
if (aLogger.Enabled()) {
|
|
|
|
float durationUS = (static_cast<float>(aFrames) / aSampleRate) * 1e6;
|
2020-06-12 16:12:50 +03:00
|
|
|
PrintBudget(aLocation, "perf", durationUS, aFrames, aSampleRate);
|
2018-03-20 20:22:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoTracer::AutoTracer(AsyncLogger& aLogger, const char* aLocation,
|
2020-06-12 16:12:50 +03:00
|
|
|
EventType aEventType, const char* aComment)
|
2018-03-20 20:22:08 +03:00
|
|
|
: mLogger(aLogger),
|
|
|
|
mLocation(aLocation),
|
|
|
|
mComment(aComment),
|
2020-06-12 16:12:50 +03:00
|
|
|
mEventType(aEventType) {
|
2018-03-20 20:22:08 +03:00
|
|
|
MOZ_ASSERT(aEventType == EventType::DURATION);
|
|
|
|
if (aLogger.Enabled()) {
|
2020-06-12 16:12:50 +03:00
|
|
|
PrintEvent(aLocation, "perf", mComment, AsyncLogger::TracingPhase::BEGIN);
|
2018-03-20 20:22:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoTracer::~AutoTracer() {
|
|
|
|
if (mEventType == EventType::DURATION) {
|
|
|
|
if (mLogger.Enabled()) {
|
2020-06-12 16:12:50 +03:00
|
|
|
PrintEvent(mLocation, "perf", mComment, AsyncLogger::TracingPhase::END);
|
2018-03-20 20:22:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|