summaryrefslogtreecommitdiffstats
path: root/dom/media/AsyncLogger.h
blob: 2dedabea7c2b681efe8ef1b1c20d471d3055b53e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* 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>
#include <cinttypes>
#include "mozilla/ArrayUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/BaseProfilerMarkerTypes.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Sprintf.h"
#include "mozilla/TimeStamp.h"
#include "GeckoProfiler.h"
#include "mozilla/dom/MPSCQueue.h"

#if defined(_WIN32)
#  include <process.h>
#  define getpid() _getpid()
#else
#  include <unistd.h>
#endif

namespace mozilla {

// 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;
};

const size_t PAYLOAD_TOTAL_SIZE = 2 << 9;

// This class implements a lock-free asynchronous logger, that
// adds profiler markers.
// 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.
// If profiling is disabled, all the calls are no-op and cheap.
class AsyncLogger {
 public:
  enum class TracingPhase : uint8_t { BEGIN, END, COMPLETE };

  const char TRACING_PHASE_STRINGS[3] = {'B', 'E', 'X'};

  struct TextPayload {
    char mPayload[PAYLOAD_TOTAL_SIZE - MPSC_MSG_RESERVED];
  };

  // The order of the fields is important here to minimize padding.
  struct TracePayload {
#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 */                                                      \
  TracingPhase mPhase;                                                       \
  /* Offset at which the comment part of the string starts, in mName */      \
  uint8_t mCommentStart;

    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) -
        ((MPSC_MSG_RESERVED + alignof(MembersWithChar) - 1) &
         ~(alignof(MembersWithChar) - 1));
#undef MEMBERS_EXCEPT_NAME

   public:
    // An arbitrary string, usually containing a function signature or a
    // recognizable tag of some sort, to be displayed when analyzing the
    // profile.
    char mName[scRemainingSpaceForName];
  };

  // 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.
  static_assert(sizeof(MPSCQueue<TracePayload>::Message) == PAYLOAD_TOTAL_SIZE,
                "MPSCQueue internal allocations has an unexpected size.");

  explicit AsyncLogger() : mThread(nullptr), mRunning(false) {}

  void Start() {
    MOZ_ASSERT(!mRunning, "Double calls to AsyncLogger::Start");
    mRunning = true;
    Run();
  }

  void Stop() {
    if (mRunning) {
      mRunning = false;
    }
  }

  // Log something that has a beginning and an end
  void Log(const char* aName, const char* aCategory, const char* aComment,
           TracingPhase aPhase) {
    if (!Enabled()) {
      return;
    }

    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);
  }

  // Log something that has a beginning and a duration
  void LogDuration(const char* aName, const char* aCategory, uint64_t aDuration,
                   uint64_t aFrames, uint64_t aSampleRate) {
    if (Enabled()) {
      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);
    }
  }

  bool Enabled() { return mRunning; }

 private:
  void Run() {
    mThread.reset(new std::thread([this]() {
      AUTO_PROFILER_REGISTER_THREAD("AsyncLogger");
      while (mRunning) {
        {
          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;
              MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
              schema.SetChartLabel("{marker.data.name}");
              schema.SetTableLabel("{marker.name} - {marker.data.name}");
              schema.AddKeyLabelFormatSearchable("name", "Comment",
                                                 MS::Format::String,
                                                 MS::Searchable::Searchable);
              return schema;
            }
          };

          struct TracingMarker {
            static constexpr Span<const char> MarkerTypeName() {
              return MakeStringSpan("Real-time");
            }
            static void StreamJSONMarkerData(
                baseprofiler::SpliceableJSONWriter& aWriter) {}
            static MarkerSchema MarkerTypeDisplay() {
              using MS = MarkerSchema;
              MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
              // Nothing outside the defaults.
              return schema;
            }
          };

          TracePayload message;
          while (mMessageQueueProfiler.Pop(&message) && mRunning) {
            if (message.mPhase != TracingPhase::COMPLETE) {
              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])));
              }
            } else {
              profiler_add_marker(
                  ProfilerString8View::WrapNullTerminatedString(message.mName),
                  geckoprofiler::category::MEDIA_RT,
                  {MarkerThreadId(message.mTID),
                   MarkerTiming::Interval(
                       message.mTimestamp,
                       message.mTimestamp + TimeDuration::FromMicroseconds(
                                                message.mDurationUs))},
                  TracingMarker{});
            }
          }
        }
        Sleep();
      }
    }));
    // cleanup is done via mRunning
    mThread->detach();
  }

  uint64_t NowInUs() {
    static TimeStamp base = TimeStamp::Now();
    return (TimeStamp::Now() - base).ToMicroseconds();
  }

  void Sleep() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }

  std::unique_ptr<std::thread> mThread;
  MPSCQueue<TracePayload> mMessageQueueProfiler;
  std::atomic<bool> mRunning;
};

}  // end namespace mozilla

#if defined(_WIN32)
#  undef getpid
#endif

#endif  // mozilla_dom_AsyncLogger_h