2018-03-20 20:14:47 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/* Implementation of an asynchronous lock-free logging system. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_AsyncLogger_h
|
|
|
|
#define mozilla_dom_AsyncLogger_h
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
2020-06-15 16:45:08 +03:00
|
|
|
#include <cinttypes>
|
2020-06-12 16:12:59 +03:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2018-03-20 20:14:47 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2021-08-26 12:15:41 +03:00
|
|
|
#include "mozilla/BaseProfilerMarkerTypes.h"
|
2018-03-20 20:14:47 +03:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
|
|
|
#include "mozilla/Sprintf.h"
|
2021-02-18 01:36:28 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2020-06-12 16:12:59 +03:00
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#include "MPSCQueue.h"
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <process.h>
|
|
|
|
# define getpid() _getpid()
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2018-03-20 20:14:47 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2021-08-26 12:15:39 +03:00
|
|
|
// Allows writing 0-terminated C-strings in a buffer, and returns the start
|
|
|
|
// index of the string that's been appended. Automatically truncates the strings
|
|
|
|
// as needed if the storage is too small, returning true when that's the case.
|
|
|
|
class MOZ_STACK_CLASS StringWriter {
|
|
|
|
public:
|
|
|
|
StringWriter(char* aMemory, size_t aLength)
|
|
|
|
: mMemory(aMemory), mWriteIndex(0), mLength(aLength) {}
|
|
|
|
|
|
|
|
bool AppendCString(const char* aString, size_t* aIndexStart) {
|
|
|
|
*aIndexStart = mWriteIndex;
|
|
|
|
if (!aString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t toCopy = strlen(aString);
|
|
|
|
bool truncated = false;
|
|
|
|
|
|
|
|
if (toCopy > Available()) {
|
|
|
|
truncated = true;
|
|
|
|
toCopy = Available() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&(mMemory[mWriteIndex]), aString, toCopy);
|
|
|
|
mWriteIndex += toCopy;
|
|
|
|
mMemory[mWriteIndex] = 0;
|
|
|
|
mWriteIndex++;
|
|
|
|
|
|
|
|
return truncated;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t Available() {
|
|
|
|
MOZ_ASSERT(mLength > mWriteIndex);
|
|
|
|
return mLength - mWriteIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* mMemory;
|
|
|
|
size_t mWriteIndex;
|
|
|
|
size_t mLength;
|
|
|
|
};
|
|
|
|
|
2020-06-12 16:12:59 +03:00
|
|
|
const size_t PAYLOAD_TOTAL_SIZE = 2 << 9;
|
|
|
|
|
2021-08-26 12:15:41 +03:00
|
|
|
// This class implements a lock-free asynchronous logger, that
|
|
|
|
// adds profiler markers.
|
2018-03-20 20:14:47 +03:00
|
|
|
// Any thread can use this logger without external synchronization and without
|
|
|
|
// being blocked. This log is suitable for use in real-time audio threads.
|
|
|
|
// This class uses a thread internally, and must be started and stopped
|
|
|
|
// manually.
|
2021-08-26 12:15:41 +03:00
|
|
|
// If profiling is disabled, all the calls are no-op and cheap.
|
2018-03-20 20:14:47 +03:00
|
|
|
class AsyncLogger {
|
|
|
|
public:
|
2020-06-12 16:11:47 +03:00
|
|
|
enum class TracingPhase : uint8_t { BEGIN, END, COMPLETE };
|
|
|
|
|
|
|
|
const char TRACING_PHASE_STRINGS[3] = {'B', 'E', 'X'};
|
|
|
|
|
2020-06-12 16:12:59 +03:00
|
|
|
struct TextPayload {
|
2021-08-26 12:15:39 +03:00
|
|
|
char mPayload[PAYLOAD_TOTAL_SIZE - MPSC_MSG_RESERVED];
|
2020-06-12 16:12:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// The order of the fields is important here to minimize padding.
|
|
|
|
struct TracePayload {
|
2021-08-11 06:27:50 +03:00
|
|
|
#define MEMBERS_EXCEPT_NAME \
|
|
|
|
/* If this marker is of phase B or E (begin or end), this is the time at \
|
|
|
|
* which it was captured. */ \
|
|
|
|
TimeStamp mTimestamp; \
|
|
|
|
/* The thread on which this tracepoint was gathered. */ \
|
|
|
|
ProfilerThreadId mTID; \
|
|
|
|
/* If this marker is of phase X (COMPLETE), this holds the duration of the \
|
|
|
|
* event in microseconds. Else, the value is not used. */ \
|
|
|
|
uint32_t mDurationUs; \
|
|
|
|
/* A trace payload can be either: \
|
|
|
|
* - Begin - this marks the beginning of a temporal region \
|
|
|
|
* - End - this marks the end of a temporal region \
|
|
|
|
* - Complete - this is a timestamp and a length, forming complete a \
|
|
|
|
* temporal region */ \
|
2021-08-26 12:15:39 +03:00
|
|
|
TracingPhase mPhase; \
|
|
|
|
/* Offset at which the comment part of the string starts, in mName */ \
|
|
|
|
uint8_t mCommentStart;
|
2021-08-11 06:27:50 +03:00
|
|
|
|
|
|
|
MEMBERS_EXCEPT_NAME;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Mock structure, to know where the first character of the name will be.
|
|
|
|
struct MembersWithChar {
|
|
|
|
MEMBERS_EXCEPT_NAME;
|
|
|
|
char c;
|
|
|
|
};
|
|
|
|
static constexpr size_t scRemainingSpaceForName =
|
|
|
|
PAYLOAD_TOTAL_SIZE - offsetof(MembersWithChar, c) -
|
2021-08-26 12:15:39 +03:00
|
|
|
((MPSC_MSG_RESERVED + alignof(MembersWithChar) - 1) &
|
2021-08-11 06:27:50 +03:00
|
|
|
~(alignof(MembersWithChar) - 1));
|
|
|
|
#undef MEMBERS_EXCEPT_NAME
|
|
|
|
|
|
|
|
public:
|
2020-06-12 16:12:59 +03:00
|
|
|
// An arbitrary string, usually containing a function signature or a
|
|
|
|
// recognizable tag of some sort, to be displayed when analyzing the
|
|
|
|
// profile.
|
2021-08-11 06:27:50 +03:00
|
|
|
char mName[scRemainingSpaceForName];
|
2020-06-12 16:12:59 +03:00
|
|
|
};
|
2020-07-03 15:24:37 +03:00
|
|
|
|
|
|
|
// The goal here is to make it easy on the allocator. We pack a pointer in the
|
|
|
|
// message struct, and we still want to do power of two allocations to
|
|
|
|
// minimize allocator slop.
|
2020-09-24 11:20:17 +03:00
|
|
|
static_assert(sizeof(MPSCQueue<TracePayload>::Message) == PAYLOAD_TOTAL_SIZE,
|
|
|
|
"MPSCQueue internal allocations has an unexpected size.");
|
|
|
|
|
2021-08-26 12:15:41 +03:00
|
|
|
explicit AsyncLogger() : mThread(nullptr), mRunning(false) {}
|
2018-03-20 20:14:47 +03:00
|
|
|
|
|
|
|
void Start() {
|
|
|
|
MOZ_ASSERT(!mRunning, "Double calls to AsyncLogger::Start");
|
2020-06-12 16:12:59 +03:00
|
|
|
mRunning = true;
|
|
|
|
Run();
|
2018-03-20 20:14:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() {
|
2020-06-12 16:12:59 +03:00
|
|
|
if (mRunning) {
|
|
|
|
mRunning = false;
|
2018-03-20 20:14:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:12:50 +03:00
|
|
|
// Log something that has a beginning and an end
|
2020-06-12 16:11:47 +03:00
|
|
|
void Log(const char* aName, const char* aCategory, const char* aComment,
|
2020-06-12 16:12:50 +03:00
|
|
|
TracingPhase aPhase) {
|
2021-08-26 12:15:41 +03:00
|
|
|
if (!Enabled()) {
|
|
|
|
return;
|
2020-06-12 16:11:47 +03:00
|
|
|
}
|
2021-08-26 12:15:41 +03:00
|
|
|
|
|
|
|
auto* msg = new MPSCQueue<TracePayload>::Message();
|
|
|
|
|
|
|
|
msg->data.mTID = profiler_current_thread_id();
|
|
|
|
msg->data.mPhase = aPhase;
|
|
|
|
msg->data.mTimestamp = TimeStamp::Now();
|
|
|
|
msg->data.mDurationUs = 0; // unused, duration is end - begin
|
|
|
|
|
|
|
|
StringWriter writer(msg->data.mName, ArrayLength(msg->data.mName));
|
|
|
|
|
|
|
|
size_t commentIndex;
|
|
|
|
DebugOnly<bool> truncated = writer.AppendCString(aName, &commentIndex);
|
|
|
|
MOZ_ASSERT(!truncated, "Tracing payload truncated: name");
|
|
|
|
|
|
|
|
if (aComment) {
|
|
|
|
truncated = writer.AppendCString(aComment, &commentIndex);
|
|
|
|
MOZ_ASSERT(!truncated, "Tracing payload truncated: comment");
|
|
|
|
msg->data.mCommentStart = commentIndex;
|
|
|
|
} else {
|
|
|
|
msg->data.mCommentStart = 0;
|
|
|
|
}
|
|
|
|
mMessageQueueProfiler.Push(msg);
|
2020-06-12 16:11:47 +03:00
|
|
|
}
|
|
|
|
|
2020-06-12 16:12:50 +03:00
|
|
|
// Log something that has a beginning and a duration
|
2020-06-12 16:11:47 +03:00
|
|
|
void LogDuration(const char* aName, const char* aCategory, uint64_t aDuration,
|
2020-06-12 16:12:50 +03:00
|
|
|
uint64_t aFrames, uint64_t aSampleRate) {
|
2020-06-12 16:11:47 +03:00
|
|
|
if (Enabled()) {
|
2021-08-26 12:15:41 +03:00
|
|
|
auto* msg = new MPSCQueue<TracePayload>::Message();
|
|
|
|
msg->data.mTID = profiler_current_thread_id();
|
|
|
|
msg->data.mPhase = TracingPhase::COMPLETE;
|
|
|
|
msg->data.mTimestamp = TimeStamp::Now();
|
|
|
|
msg->data.mDurationUs =
|
|
|
|
(static_cast<double>(aFrames) / aSampleRate) * 1e6;
|
|
|
|
size_t len = std::min(strlen(aName), ArrayLength(msg->data.mName));
|
|
|
|
memcpy(msg->data.mName, aName, len);
|
|
|
|
msg->data.mName[len] = 0;
|
|
|
|
mMessageQueueProfiler.Push(msg);
|
2018-03-20 20:14:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 17:18:20 +03:00
|
|
|
bool Enabled() { return mRunning; }
|
2018-03-20 20:14:47 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Run() {
|
|
|
|
mThread.reset(new std::thread([this]() {
|
2021-08-24 07:47:12 +03:00
|
|
|
AUTO_PROFILER_REGISTER_THREAD("AsyncLogger");
|
2018-03-20 20:14:47 +03:00
|
|
|
while (mRunning) {
|
2020-06-12 16:12:59 +03:00
|
|
|
{
|
2021-08-26 12:15:39 +03:00
|
|
|
struct TracingMarkerWithComment {
|
|
|
|
static constexpr Span<const char> MarkerTypeName() {
|
|
|
|
return MakeStringSpan("Real-Time");
|
|
|
|
}
|
|
|
|
static void StreamJSONMarkerData(
|
|
|
|
baseprofiler::SpliceableJSONWriter& aWriter,
|
|
|
|
const ProfilerString8View& aText) {
|
|
|
|
aWriter.StringProperty("name", aText);
|
|
|
|
}
|
|
|
|
static MarkerSchema MarkerTypeDisplay() {
|
|
|
|
using MS = MarkerSchema;
|
2021-09-03 17:30:09 +03:00
|
|
|
MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
|
2021-08-26 12:15:39 +03:00
|
|
|
schema.SetChartLabel("{marker.data.name}");
|
|
|
|
schema.SetTableLabel("{marker.name} - {marker.data.name}");
|
2021-09-03 17:30:10 +03:00
|
|
|
schema.AddKeyLabelFormat("name", "Comment", MS::Format::String);
|
2021-08-26 12:15:39 +03:00
|
|
|
return schema;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TracingMarker {
|
2020-11-19 00:43:50 +03:00
|
|
|
static constexpr Span<const char> MarkerTypeName() {
|
2021-08-26 12:15:39 +03:00
|
|
|
return MakeStringSpan("Real-time");
|
2020-11-19 00:43:50 +03:00
|
|
|
}
|
|
|
|
static void StreamJSONMarkerData(
|
|
|
|
baseprofiler::SpliceableJSONWriter& aWriter) {}
|
|
|
|
static MarkerSchema MarkerTypeDisplay() {
|
|
|
|
using MS = MarkerSchema;
|
2021-09-03 17:30:09 +03:00
|
|
|
MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
|
2020-11-19 00:43:50 +03:00
|
|
|
// Nothing outside the defaults.
|
|
|
|
return schema;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-12 16:12:59 +03:00
|
|
|
TracePayload message;
|
|
|
|
while (mMessageQueueProfiler.Pop(&message) && mRunning) {
|
|
|
|
if (message.mPhase != TracingPhase::COMPLETE) {
|
2021-08-26 12:15:39 +03:00
|
|
|
if (!message.mCommentStart) {
|
|
|
|
profiler_add_marker(
|
|
|
|
ProfilerString8View::WrapNullTerminatedString(
|
|
|
|
message.mName),
|
|
|
|
geckoprofiler::category::MEDIA_RT,
|
|
|
|
{MarkerThreadId(message.mTID),
|
|
|
|
(message.mPhase == TracingPhase::BEGIN)
|
|
|
|
? MarkerTiming::IntervalStart(message.mTimestamp)
|
|
|
|
: MarkerTiming::IntervalEnd(message.mTimestamp)},
|
|
|
|
TracingMarker{});
|
|
|
|
} else {
|
|
|
|
profiler_add_marker(
|
|
|
|
ProfilerString8View::WrapNullTerminatedString(
|
|
|
|
message.mName),
|
|
|
|
geckoprofiler::category::MEDIA_RT,
|
|
|
|
{MarkerThreadId(message.mTID),
|
|
|
|
(message.mPhase == TracingPhase::BEGIN)
|
|
|
|
? MarkerTiming::IntervalStart(message.mTimestamp)
|
|
|
|
: MarkerTiming::IntervalEnd(message.mTimestamp)},
|
|
|
|
TracingMarkerWithComment{},
|
|
|
|
ProfilerString8View::WrapNullTerminatedString(
|
|
|
|
&(message.mName[message.mCommentStart])));
|
|
|
|
}
|
2020-06-12 16:12:59 +03:00
|
|
|
} else {
|
2020-11-19 00:43:50 +03:00
|
|
|
profiler_add_marker(
|
|
|
|
ProfilerString8View::WrapNullTerminatedString(message.mName),
|
|
|
|
geckoprofiler::category::MEDIA_RT,
|
|
|
|
{MarkerThreadId(message.mTID),
|
|
|
|
MarkerTiming::Interval(
|
|
|
|
message.mTimestamp,
|
|
|
|
message.mTimestamp + TimeDuration::FromMicroseconds(
|
|
|
|
message.mDurationUs))},
|
2021-08-26 12:15:39 +03:00
|
|
|
TracingMarker{});
|
2020-06-12 16:12:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 20:14:47 +03:00
|
|
|
Sleep();
|
|
|
|
}
|
|
|
|
}));
|
2020-06-12 16:12:59 +03:00
|
|
|
// cleanup is done via mRunning
|
|
|
|
mThread->detach();
|
2018-03-20 20:14:47 +03:00
|
|
|
}
|
|
|
|
|
2020-06-12 16:11:47 +03:00
|
|
|
uint64_t NowInUs() {
|
2021-07-14 21:18:17 +03:00
|
|
|
static TimeStamp base = TimeStamp::Now();
|
|
|
|
return (TimeStamp::Now() - base).ToMicroseconds();
|
2020-06-12 16:11:47 +03:00
|
|
|
}
|
|
|
|
|
2018-03-20 20:14:47 +03:00
|
|
|
void Sleep() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }
|
|
|
|
|
|
|
|
std::unique_ptr<std::thread> mThread;
|
2020-06-12 16:12:59 +03:00
|
|
|
MPSCQueue<TracePayload> mMessageQueueProfiler;
|
2018-03-20 20:14:47 +03:00
|
|
|
std::atomic<bool> mRunning;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace mozilla
|
|
|
|
|
2020-06-12 16:12:59 +03:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# undef getpid
|
|
|
|
#endif
|
|
|
|
|
2018-03-20 20:14:47 +03:00
|
|
|
#endif // mozilla_dom_AsyncLogger_h
|