summaryrefslogtreecommitdiffstats
path: root/tools/profiler/core/ProfilerBindings.cpp
blob: 280580e80baad708f8eb94ae9fb78233180d0013 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* -*- Mode: C++; tab-width: 8; 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/. */

/* FFI functions for Profiler Rust API to call into profiler */

#include "ProfilerBindings.h"

#include "GeckoProfiler.h"

#include <set>
#include <type_traits>

void gecko_profiler_register_thread(const char* aName) {
  PROFILER_REGISTER_THREAD(aName);
}

void gecko_profiler_unregister_thread() { PROFILER_UNREGISTER_THREAD(); }

void gecko_profiler_construct_label(mozilla::AutoProfilerLabel* aAutoLabel,
                                    JS::ProfilingCategoryPair aCategoryPair) {
#ifdef MOZ_GECKO_PROFILER
  new (aAutoLabel) mozilla::AutoProfilerLabel(
      "", nullptr, aCategoryPair,
      uint32_t(
          js::ProfilingStackFrame::Flags::LABEL_DETERMINED_BY_CATEGORY_PAIR));
#endif
}

void gecko_profiler_destruct_label(mozilla::AutoProfilerLabel* aAutoLabel) {
#ifdef MOZ_GECKO_PROFILER
  aAutoLabel->~AutoProfilerLabel();
#endif
}

void gecko_profiler_construct_timestamp_now(mozilla::TimeStamp* aTimeStamp) {
  new (aTimeStamp) mozilla::TimeStamp(mozilla::TimeStamp::Now());
}

void gecko_profiler_clone_timestamp(const mozilla::TimeStamp* aSrcTimeStamp,
                                    mozilla::TimeStamp* aDestTimeStamp) {
  new (aDestTimeStamp) mozilla::TimeStamp(*aSrcTimeStamp);
}

void gecko_profiler_destruct_timestamp(mozilla::TimeStamp* aTimeStamp) {
  aTimeStamp->~TimeStamp();
}

void gecko_profiler_add_timestamp(const mozilla::TimeStamp* aTimeStamp,
                                  mozilla::TimeStamp* aDestTimeStamp,
                                  double aMicroseconds) {
  new (aDestTimeStamp) mozilla::TimeStamp(
      *aTimeStamp + mozilla::TimeDuration::FromMicroseconds(aMicroseconds));
}

void gecko_profiler_subtract_timestamp(const mozilla::TimeStamp* aTimeStamp,
                                       mozilla::TimeStamp* aDestTimeStamp,
                                       double aMicroseconds) {
  new (aDestTimeStamp) mozilla::TimeStamp(
      *aTimeStamp - mozilla::TimeDuration::FromMicroseconds(aMicroseconds));
}

void gecko_profiler_construct_marker_timing_instant_at(
    mozilla::MarkerTiming* aMarkerTiming, const mozilla::TimeStamp* aTime) {
#ifdef MOZ_GECKO_PROFILER
  static_assert(std::is_trivially_copyable_v<mozilla::MarkerTiming>);
  mozilla::MarkerTiming::UnsafeConstruct(aMarkerTiming, *aTime,
                                         mozilla::TimeStamp{},
                                         mozilla::MarkerTiming::Phase::Instant);
#endif
}

void gecko_profiler_construct_marker_timing_instant_now(
    mozilla::MarkerTiming* aMarkerTiming) {
#ifdef MOZ_GECKO_PROFILER
  static_assert(std::is_trivially_copyable_v<mozilla::MarkerTiming>);
  mozilla::MarkerTiming::UnsafeConstruct(
      aMarkerTiming, mozilla::TimeStamp::Now(), mozilla::TimeStamp{},
      mozilla::MarkerTiming::Phase::Instant);
#endif
}

void gecko_profiler_construct_marker_timing_interval(
    mozilla::MarkerTiming* aMarkerTiming, const mozilla::TimeStamp* aStartTime,
    const mozilla::TimeStamp* aEndTime) {
#ifdef MOZ_GECKO_PROFILER
  static_assert(std::is_trivially_copyable_v<mozilla::MarkerTiming>);
  mozilla::MarkerTiming::UnsafeConstruct(
      aMarkerTiming, *aStartTime, *aEndTime,
      mozilla::MarkerTiming::Phase::Interval);
#endif
}

void gecko_profiler_construct_marker_timing_interval_until_now_from(
    mozilla::MarkerTiming* aMarkerTiming,
    const mozilla::TimeStamp* aStartTime) {
#ifdef MOZ_GECKO_PROFILER
  static_assert(std::is_trivially_copyable_v<mozilla::MarkerTiming>);
  mozilla::MarkerTiming::UnsafeConstruct(
      aMarkerTiming, *aStartTime, mozilla::TimeStamp::Now(),
      mozilla::MarkerTiming::Phase::Interval);
#endif
}

void gecko_profiler_construct_marker_timing_interval_start(
    mozilla::MarkerTiming* aMarkerTiming, const mozilla::TimeStamp* aTime) {
#ifdef MOZ_GECKO_PROFILER
  static_assert(std::is_trivially_copyable_v<mozilla::MarkerTiming>);
  mozilla::MarkerTiming::UnsafeConstruct(
      aMarkerTiming, *aTime, mozilla::TimeStamp{},
      mozilla::MarkerTiming::Phase::IntervalStart);
#endif
}

void gecko_profiler_construct_marker_timing_interval_end(
    mozilla::MarkerTiming* aMarkerTiming, const mozilla::TimeStamp* aTime) {
#ifdef MOZ_GECKO_PROFILER
  static_assert(std::is_trivially_copyable_v<mozilla::MarkerTiming>);
  mozilla::MarkerTiming::UnsafeConstruct(
      aMarkerTiming, mozilla::TimeStamp{}, *aTime,
      mozilla::MarkerTiming::Phase::IntervalEnd);
#endif
}

void gecko_profiler_destruct_marker_timing(
    mozilla::MarkerTiming* aMarkerTiming) {
#ifdef MOZ_GECKO_PROFILER
  aMarkerTiming->~MarkerTiming();
#endif
}

void gecko_profiler_construct_marker_schema(
    mozilla::MarkerSchema* aMarkerSchema,
    const mozilla::MarkerSchema::Location* aLocations, size_t aLength) {
#ifdef MOZ_GECKO_PROFILER
  new (aMarkerSchema) mozilla::MarkerSchema(aLocations, aLength);
#endif
}

void gecko_profiler_construct_marker_schema_with_special_front_end_location(
    mozilla::MarkerSchema* aMarkerSchema) {
#ifdef MOZ_GECKO_PROFILER
  new (aMarkerSchema)
      mozilla::MarkerSchema(mozilla::MarkerSchema::SpecialFrontendLocation{});
#endif
}

void gecko_profiler_destruct_marker_schema(
    mozilla::MarkerSchema* aMarkerSchema) {
#ifdef MOZ_GECKO_PROFILER
  aMarkerSchema->~MarkerSchema();
#endif
}

void gecko_profiler_marker_schema_set_chart_label(
    mozilla::MarkerSchema* aSchema, const char* aLabel, size_t aLabelLength) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->SetChartLabel(std::string(aLabel, aLabelLength));
#endif
}

void gecko_profiler_marker_schema_set_tooltip_label(
    mozilla::MarkerSchema* aSchema, const char* aLabel, size_t aLabelLength) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->SetTooltipLabel(std::string(aLabel, aLabelLength));
#endif
}

void gecko_profiler_marker_schema_set_table_label(
    mozilla::MarkerSchema* aSchema, const char* aLabel, size_t aLabelLength) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->SetTableLabel(std::string(aLabel, aLabelLength));
#endif
}

void gecko_profiler_marker_schema_set_all_labels(mozilla::MarkerSchema* aSchema,
                                                 const char* aLabel,
                                                 size_t aLabelLength) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->SetAllLabels(std::string(aLabel, aLabelLength));
#endif
}

void gecko_profiler_marker_schema_add_key_format(
    mozilla::MarkerSchema* aSchema, const char* aKey, size_t aKeyLength,
    mozilla::MarkerSchema::Format aFormat) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->AddKeyFormat(std::string(aKey, aKeyLength), aFormat);
#endif
}

void gecko_profiler_marker_schema_add_key_label_format(
    mozilla::MarkerSchema* aSchema, const char* aKey, size_t aKeyLength,
    const char* aLabel, size_t aLabelLength,
    mozilla::MarkerSchema::Format aFormat) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->AddKeyLabelFormat(std::string(aKey, aKeyLength),
                             std::string(aLabel, aLabelLength), aFormat);
#endif
}

void gecko_profiler_marker_schema_add_key_format_searchable(
    mozilla::MarkerSchema* aSchema, const char* aKey, size_t aKeyLength,
    mozilla::MarkerSchema::Format aFormat,
    mozilla::MarkerSchema::Searchable aSearchable) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->AddKeyFormatSearchable(std::string(aKey, aKeyLength), aFormat,
                                  aSearchable);
#endif
}

void gecko_profiler_marker_schema_add_key_label_format_searchable(
    mozilla::MarkerSchema* aSchema, const char* aKey, size_t aKeyLength,
    const char* aLabel, size_t aLabelLength,
    mozilla::MarkerSchema::Format aFormat,
    mozilla::MarkerSchema::Searchable aSearchable) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->AddKeyLabelFormatSearchable(std::string(aKey, aKeyLength),
                                       std::string(aLabel, aLabelLength),
                                       aFormat, aSearchable);
#endif
}

void gecko_profiler_marker_schema_add_static_label_value(
    mozilla::MarkerSchema* aSchema, const char* aLabel, size_t aLabelLength,
    const char* aValue, size_t aValueLength) {
#ifdef MOZ_GECKO_PROFILER
  aSchema->AddStaticLabelValue(std::string(aLabel, aLabelLength),
                               std::string(aValue, aValueLength));
#endif
}

void gecko_profiler_marker_schema_stream(
    mozilla::baseprofiler::SpliceableJSONWriter* aWriter, const char* aName,
    size_t aNameLength, mozilla::MarkerSchema* aMarkerSchema,
    void* aStreamedNamesSet) {
#ifdef MOZ_GECKO_PROFILER
  auto* streamedNames = static_cast<std::set<std::string>*>(aStreamedNamesSet);
  // std::set.insert(T&&) returns a pair, its `second` is true if the element
  // was actually inserted (i.e., it was not there yet.).
  const bool didInsert =
      streamedNames->insert(std::string(aName, aNameLength)).second;
  if (didInsert) {
    std::move(*aMarkerSchema)
        .Stream(*aWriter, mozilla::Span(aName, aNameLength));
  }
#endif
}

void gecko_profiler_json_writer_int_property(
    mozilla::baseprofiler::SpliceableJSONWriter* aWriter, const char* aName,
    size_t aNameLength, int64_t aValue) {
#ifdef MOZ_GECKO_PROFILER
  aWriter->IntProperty(mozilla::Span(aName, aNameLength), aValue);
#endif
}

void gecko_profiler_json_writer_float_property(
    mozilla::baseprofiler::SpliceableJSONWriter* aWriter, const char* aName,
    size_t aNameLength, double aValue) {
#ifdef MOZ_GECKO_PROFILER
  aWriter->DoubleProperty(mozilla::Span(aName, aNameLength), aValue);
#endif
}

void gecko_profiler_json_writer_bool_property(
    mozilla::baseprofiler::SpliceableJSONWriter* aWriter, const char* aName,
    size_t aNameLength, bool aValue) {
#ifdef MOZ_GECKO_PROFILER
  aWriter->BoolProperty(mozilla::Span(aName, aNameLength), aValue);
#endif
}
void gecko_profiler_json_writer_string_property(
    mozilla::baseprofiler::SpliceableJSONWriter* aWriter, const char* aName,
    size_t aNameLength, const char* aValue, size_t aValueLength) {
#ifdef MOZ_GECKO_PROFILER
  aWriter->StringProperty(mozilla::Span(aName, aNameLength),
                          mozilla::Span(aValue, aValueLength));
#endif
}

void gecko_profiler_json_writer_null_property(
    mozilla::baseprofiler::SpliceableJSONWriter* aWriter, const char* aName,
    size_t aNameLength) {
#ifdef MOZ_GECKO_PROFILER
  aWriter->NullProperty(mozilla::Span(aName, aNameLength));
#endif
}

void gecko_profiler_add_marker_untyped(
    const char* aName, size_t aNameLength,
    mozilla::baseprofiler::ProfilingCategoryPair aCategoryPair,
    mozilla::MarkerTiming* aMarkerTiming,
    mozilla::StackCaptureOptions aStackCaptureOptions) {
#ifdef MOZ_GECKO_PROFILER
  profiler_add_marker(
      mozilla::ProfilerString8View(aName, aNameLength),
      mozilla::MarkerCategory{aCategoryPair},
      mozilla::MarkerOptions(
          std::move(*aMarkerTiming),
          mozilla::MarkerStack::WithCaptureOptions(aStackCaptureOptions)));
#endif
}

void gecko_profiler_add_marker_text(
    const char* aName, size_t aNameLength,
    mozilla::baseprofiler::ProfilingCategoryPair aCategoryPair,
    mozilla::MarkerTiming* aMarkerTiming,
    mozilla::StackCaptureOptions aStackCaptureOptions, const char* aText,
    size_t aTextLength) {
#ifdef MOZ_GECKO_PROFILER
  profiler_add_marker(
      mozilla::ProfilerString8View(aName, aNameLength),
      mozilla::MarkerCategory{aCategoryPair},
      mozilla::MarkerOptions(
          std::move(*aMarkerTiming),
          mozilla::MarkerStack::WithCaptureOptions(aStackCaptureOptions)),
      geckoprofiler::markers::TextMarker{},
      mozilla::ProfilerString8View(aText, aTextLength));
#endif
}

void gecko_profiler_add_marker(
    const char* aName, size_t aNameLength,
    mozilla::baseprofiler::ProfilingCategoryPair aCategoryPair,
    mozilla::MarkerTiming* aMarkerTiming,
    mozilla::StackCaptureOptions aStackCaptureOptions, uint8_t aMarkerTag,
    const uint8_t* aPayload, size_t aPayloadSize) {
#ifdef MOZ_GECKO_PROFILER
  // Copy the marker timing and create the marker option.
  mozilla::MarkerOptions markerOptions(
      std::move(*aMarkerTiming),
      mozilla::MarkerStack::WithCaptureOptions(aStackCaptureOptions));

  // Currently it's not possible to add a threadId option, but we will
  // have it soon.
  if (markerOptions.ThreadId().IsUnspecified()) {
    // If yet unspecified, set thread to this thread where the marker is added.
    markerOptions.Set(mozilla::MarkerThreadId::CurrentThread());
  }

  auto& buffer = profiler_get_core_buffer();
  mozilla::Span payload(aPayload, aPayloadSize);

  mozilla::StackCaptureOptions captureOptions =
      markerOptions.Stack().CaptureOptions();
  if (captureOptions != mozilla::StackCaptureOptions::NoStack) {
    // A capture was requested, let's attempt to do it here&now. This avoids a
    // lot of allocations that would be necessary if capturing a backtrace
    // separately.
    // TODO use a local on-stack byte buffer to remove last allocation.
    // TODO reduce internal profiler stack levels, see bug 1659872.
    mozilla::ProfileBufferChunkManagerSingle chunkManager(
        mozilla::ProfileBufferChunkManager::scExpectedMaximumStackSize);
    mozilla::ProfileChunkedBuffer chunkedBuffer(
        mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex,
        chunkManager);
    markerOptions.StackRef().UseRequestedBacktrace(
        profiler_capture_backtrace_into(chunkedBuffer, captureOptions)
            ? &chunkedBuffer
            : nullptr);

    // This call must be made from here, while chunkedBuffer is in scope.
    buffer.PutObjects(
        mozilla::ProfileBufferEntryKind::Marker, markerOptions,
        mozilla::ProfilerString8View(aName, aNameLength),
        mozilla::MarkerCategory{aCategoryPair},
        mozilla::base_profiler_markers_detail::Streaming::DeserializerTag(
            aMarkerTag),
        mozilla::MarkerPayloadType::Rust, payload);
    return;
  }

  buffer.PutObjects(
      mozilla::ProfileBufferEntryKind::Marker, markerOptions,
      mozilla::ProfilerString8View(aName, aNameLength),
      mozilla::MarkerCategory{aCategoryPair},
      mozilla::base_profiler_markers_detail::Streaming::DeserializerTag(
          aMarkerTag),
      mozilla::MarkerPayloadType::Rust, payload);
#endif
}