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/. */
|
|
|
|
|
|
|
|
#ifndef TRACING_H
|
|
|
|
#define TRACING_H
|
|
|
|
|
2018-06-27 16:35:14 +03:00
|
|
|
#include <algorithm>
|
2018-03-20 20:22:08 +03:00
|
|
|
#include <cstdint>
|
2018-06-27 16:35:14 +03:00
|
|
|
#include <cstdio>
|
2018-03-20 20:22:08 +03:00
|
|
|
|
|
|
|
#include "AsyncLogger.h"
|
|
|
|
|
2018-06-27 16:35:14 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
2018-03-20 20:22:08 +03:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
// MSVC
|
|
|
|
# define FUNCTION_SIGNATURE __FUNCSIG__
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
// gcc, clang
|
|
|
|
# define FUNCTION_SIGNATURE __PRETTY_FUNCTION__
|
|
|
|
#endif
|
|
|
|
|
2019-10-02 15:16:22 +03:00
|
|
|
extern mozilla::AsyncLogger gAudioCallbackTraceLogger;
|
|
|
|
|
|
|
|
// This is no-op if tracing is not enabled, and is idempotent.
|
|
|
|
void StartAudioCallbackTracing();
|
2020-02-12 17:02:08 +03:00
|
|
|
void StopAudioCallbackTracing();
|
2019-10-02 15:16:22 +03:00
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
#ifdef TRACING
|
|
|
|
/* TRACE is for use in the real-time audio rendering thread.
|
|
|
|
* It would be better to always pass in the thread id. However, the thread an
|
|
|
|
* audio callback runs on can change when the underlying audio device change,
|
|
|
|
* and also it seems to be called from a thread pool in a round-robin fashion
|
|
|
|
* when audio remoting is activated, making the traces unreadable.
|
|
|
|
* The thread on which the AudioCallbackDriver::DataCallback is to always
|
|
|
|
* be thread 0, and the budget is set to always be thread 1. This allows
|
|
|
|
* displaying those elements in two separate lanes.
|
|
|
|
* The other thread have "normal" tid. Hashing allows being able to get a
|
|
|
|
* string representation that is unique and guaranteed to be portable. */
|
2020-06-12 16:12:50 +03:00
|
|
|
# define TRACE_AUDIO_CALLBACK() \
|
|
|
|
AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE);
|
|
|
|
# define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate) \
|
|
|
|
AutoTracer budget(gAudioCallbackTraceLogger, "Real-time budget", \
|
|
|
|
AutoTracer::EventType::BUDGET, aFrames, aSampleRate);
|
|
|
|
# define TRACE_AUDIO_CALLBACK_COMMENT(aFmt, ...) \
|
|
|
|
AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE, \
|
|
|
|
AutoTracer::EventType::DURATION, aFmt, ##__VA_ARGS__);
|
|
|
|
# define TRACE() \
|
|
|
|
AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE);
|
|
|
|
# define TRACE_COMMENT(aFmt, ...) \
|
|
|
|
AutoTracer trace(gAudioCallbackTraceLogger, FUNCTION_SIGNATURE, \
|
2018-03-20 20:22:08 +03:00
|
|
|
AutoTracer::EventType::DURATION, aFmt, ##__VA_ARGS__);
|
|
|
|
#else
|
|
|
|
# define TRACE()
|
2020-04-07 13:45:52 +03:00
|
|
|
# define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate)
|
2018-06-27 16:35:14 +03:00
|
|
|
# define TRACE_COMMENT(aFmt, ...)
|
2018-03-20 20:22:08 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
class MOZ_RAII AutoTracer {
|
|
|
|
public:
|
2020-06-12 16:14:21 +03:00
|
|
|
static const int32_t BUFFER_SIZE = 256;
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
enum class EventType { DURATION, BUDGET };
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation,
|
|
|
|
EventType aEventType = EventType::DURATION,
|
|
|
|
const char* aComment = nullptr);
|
2018-06-27 16:35:14 +03:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation,
|
2020-06-12 16:12:50 +03:00
|
|
|
EventType aEventType, const char* aFormat, Args... aArgs)
|
2018-06-27 16:35:14 +03:00
|
|
|
: mLogger(aLogger),
|
|
|
|
mLocation(aLocation),
|
|
|
|
mComment(mBuffer),
|
2020-06-12 16:12:50 +03:00
|
|
|
mEventType(aEventType) {
|
2018-06-27 16:35:14 +03:00
|
|
|
MOZ_ASSERT(aEventType == EventType::DURATION);
|
|
|
|
if (aLogger.Enabled()) {
|
|
|
|
int32_t size = snprintf(mBuffer, BUFFER_SIZE, aFormat, aArgs...);
|
|
|
|
size = std::min(size, BUFFER_SIZE - 1);
|
|
|
|
mBuffer[size] = 0;
|
2020-06-12 16:11:47 +03:00
|
|
|
PrintEvent(aLocation, "perf", mComment,
|
2020-06-12 16:12:50 +03:00
|
|
|
mozilla::AsyncLogger::TracingPhase::BEGIN);
|
2018-06-27 16:35:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
AutoTracer(mozilla::AsyncLogger& aLogger, const char* aLocation,
|
2020-06-12 16:12:50 +03:00
|
|
|
EventType aEventType, uint64_t aFrames, uint64_t aSampleRate);
|
2018-06-27 16:35:14 +03:00
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
~AutoTracer();
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
private:
|
|
|
|
void PrintEvent(const char* aName, const char* aCategory,
|
2020-06-12 16:11:47 +03:00
|
|
|
const char* aComment,
|
2020-06-12 16:12:50 +03:00
|
|
|
mozilla::AsyncLogger::TracingPhase aPhase);
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2018-03-20 20:22:08 +03:00
|
|
|
void PrintBudget(const char* aName, const char* aCategory, uint64_t aDuration,
|
2020-06-12 16:12:50 +03:00
|
|
|
uint64_t aFrames, uint64_t aSampleRate);
|
2018-03-20 20:22:08 +03:00
|
|
|
|
|
|
|
// The logger to use. It musdt have a lifetime longer than the block an
|
|
|
|
// instance of this class traces.
|
|
|
|
mozilla::AsyncLogger& mLogger;
|
|
|
|
// The location for this trace point, arbitrary string literal, often the
|
|
|
|
// name of the calling function, with a static lifetime.
|
|
|
|
const char* mLocation;
|
|
|
|
// A comment for this trace point, abitrary string literal with a static
|
|
|
|
// lifetime.
|
|
|
|
const char* mComment;
|
2018-06-27 16:35:14 +03:00
|
|
|
// A buffer used to hold string-formatted traces.
|
|
|
|
char mBuffer[BUFFER_SIZE];
|
2018-03-20 20:22:08 +03:00
|
|
|
// The event type, for now either a budget or a duration.
|
|
|
|
const EventType mEventType;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* TRACING_H */
|