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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
// IWYU pragma: private, include "GeckoProfiler.h"
#ifndef TOOLS_SPS_SAMPLER_H_
#define TOOLS_SPS_SAMPLER_H_
#include <stdlib.h>
#include <signal.h>
#include <stdarg.h>
#include "mozilla/Assertions.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/Sprintf.h"
#include "mozilla/ThreadLocal.h"
#include "mozilla/UniquePtr.h"
#ifndef SPS_STANDALONE
#include "nscore.h"
#include "nsISupports.h"
#endif
#include "GeckoProfilerFunc.h"
#include "PseudoStack.h"
#include "ProfilerBacktrace.h"
// Make sure that we can use std::min here without the Windows headers messing with us.
#ifdef min
#undef min
#endif
class GeckoSampler;
namespace mozilla {
class TimeStamp;
} // namespace mozilla
extern MOZ_THREAD_LOCAL(PseudoStack *) tlsPseudoStack;
extern MOZ_THREAD_LOCAL(GeckoSampler *) tlsTicker;
extern MOZ_THREAD_LOCAL(void *) tlsStackTop;
extern bool stack_key_initialized;
#ifndef SAMPLE_FUNCTION_NAME
# ifdef __GNUC__
# define SAMPLE_FUNCTION_NAME __FUNCTION__
# elif defined(_MSC_VER)
# define SAMPLE_FUNCTION_NAME __FUNCTION__
# else
# define SAMPLE_FUNCTION_NAME __func__ // defined in C99, supported in various C++ compilers. Just raw function name.
# endif
#endif
static inline
void profiler_init(void* stackTop)
{
mozilla_sampler_init(stackTop);
}
static inline
void profiler_shutdown()
{
mozilla_sampler_shutdown();
}
static inline
void profiler_start(int aProfileEntries, double aInterval,
const char** aFeatures, uint32_t aFeatureCount,
const char** aThreadNameFilters, uint32_t aFilterCount)
{
mozilla_sampler_start(aProfileEntries, aInterval, aFeatures, aFeatureCount, aThreadNameFilters, aFilterCount);
}
static inline
void profiler_stop()
{
mozilla_sampler_stop();
}
static inline
bool profiler_is_paused()
{
return mozilla_sampler_is_paused();
}
static inline
void profiler_pause()
{
mozilla_sampler_pause();
}
static inline
void profiler_resume()
{
mozilla_sampler_resume();
}
static inline
ProfilerBacktrace* profiler_get_backtrace()
{
return mozilla_sampler_get_backtrace();
}
static inline
void profiler_free_backtrace(ProfilerBacktrace* aBacktrace)
{
mozilla_sampler_free_backtrace(aBacktrace);
}
static inline
void profiler_get_backtrace_noalloc(char *output, size_t outputSize)
{
return mozilla_sampler_get_backtrace_noalloc(output, outputSize);
}
static inline
bool profiler_is_active()
{
return mozilla_sampler_is_active();
}
static inline
bool profiler_feature_active(const char* aName)
{
return mozilla_sampler_feature_active(aName);
}
static inline
void profiler_responsiveness(const mozilla::TimeStamp& aTime)
{
mozilla_sampler_responsiveness(aTime);
}
static inline
void profiler_set_frame_number(int frameNumber)
{
return mozilla_sampler_frame_number(frameNumber);
}
static inline
mozilla::UniquePtr<char[]> profiler_get_profile(double aSinceTime = 0)
{
return mozilla_sampler_get_profile(aSinceTime);
}
#ifndef SPS_STANDALONE
static inline
JSObject* profiler_get_profile_jsobject(JSContext* aCx, double aSinceTime = 0)
{
return mozilla_sampler_get_profile_data(aCx, aSinceTime);
}
static inline
void profiler_get_profile_jsobject_async(double aSinceTime = 0,
mozilla::dom::Promise* aPromise = 0)
{
mozilla_sampler_get_profile_data_async(aSinceTime, aPromise);
}
static inline
void profiler_get_start_params(int* aEntrySize,
double* aInterval,
mozilla::Vector<const char*>* aFilters,
mozilla::Vector<const char*>* aFeatures)
{
mozilla_sampler_get_profiler_start_params(aEntrySize, aInterval, aFilters, aFeatures);
}
static inline
void profiler_get_gatherer(nsISupports** aRetVal)
{
mozilla_sampler_get_gatherer(aRetVal);
}
#endif
static inline
void profiler_save_profile_to_file(const char* aFilename)
{
return mozilla_sampler_save_profile_to_file(aFilename);
}
static inline
const char** profiler_get_features()
{
return mozilla_sampler_get_features();
}
static inline
void profiler_get_buffer_info(uint32_t *aCurrentPosition, uint32_t *aTotalSize,
uint32_t *aGeneration)
{
return mozilla_sampler_get_buffer_info(aCurrentPosition, aTotalSize, aGeneration);
}
static inline
void profiler_lock()
{
return mozilla_sampler_lock();
}
static inline
void profiler_unlock()
{
return mozilla_sampler_unlock();
}
static inline
void profiler_register_thread(const char* name, void* guessStackTop)
{
mozilla_sampler_register_thread(name, guessStackTop);
}
static inline
void profiler_unregister_thread()
{
mozilla_sampler_unregister_thread();
}
static inline
void profiler_sleep_start()
{
mozilla_sampler_sleep_start();
}
static inline
void profiler_sleep_end()
{
mozilla_sampler_sleep_end();
}
static inline
bool profiler_is_sleeping()
{
return mozilla_sampler_is_sleeping();
}
#ifndef SPS_STANDALONE
static inline
void profiler_js_operation_callback()
{
PseudoStack *stack = tlsPseudoStack.get();
if (!stack) {
return;
}
stack->jsOperationCallback();
}
#endif
static inline
double profiler_time()
{
return mozilla_sampler_time();
}
static inline
double profiler_time(const mozilla::TimeStamp& aTime)
{
return mozilla_sampler_time(aTime);
}
static inline
bool profiler_in_privacy_mode()
{
PseudoStack *stack = tlsPseudoStack.get();
if (!stack) {
return false;
}
return stack->mPrivacyMode;
}
static inline void profiler_tracing(const char* aCategory, const char* aInfo,
ProfilerBacktrace* aCause,
TracingMetadata aMetaData = TRACING_DEFAULT)
{
// Don't insert a marker if we're not profiling to avoid
// the heap copy (malloc).
if (!stack_key_initialized || !profiler_is_active()) {
delete aCause;
return;
}
mozilla_sampler_tracing(aCategory, aInfo, aCause, aMetaData);
}
static inline void profiler_tracing(const char* aCategory, const char* aInfo,
TracingMetadata aMetaData = TRACING_DEFAULT)
{
if (!stack_key_initialized)
return;
// Don't insert a marker if we're not profiling to avoid
// the heap copy (malloc).
if (!profiler_is_active()) {
return;
}
mozilla_sampler_tracing(aCategory, aInfo, aMetaData);
}
#define SAMPLER_APPEND_LINE_NUMBER_PASTE(id, line) id ## line
#define SAMPLER_APPEND_LINE_NUMBER_EXPAND(id, line) SAMPLER_APPEND_LINE_NUMBER_PASTE(id, line)
#define SAMPLER_APPEND_LINE_NUMBER(id) SAMPLER_APPEND_LINE_NUMBER_EXPAND(id, __LINE__)
// Uncomment this to turn on systrace or build with
// ac_add_options --enable-systace
//#define MOZ_USE_SYSTRACE
#ifdef MOZ_USE_SYSTRACE
#ifndef ATRACE_TAG
# define ATRACE_TAG ATRACE_TAG_ALWAYS
#endif
// We need HAVE_ANDROID_OS to be defined for Trace.h.
// If its not set we will set it temporary and remove it.
# ifndef HAVE_ANDROID_OS
# define HAVE_ANDROID_OS
# define REMOVE_HAVE_ANDROID_OS
# endif
// Android source code will include <cutils/trace.h> before this. There is no
// HAVE_ANDROID_OS defined in Firefox OS build at that time. Enabled it globally
// will cause other build break. So atrace_begin and atrace_end are not defined.
// It will cause a build-break when we include <utils/Trace.h>. Use undef
// _LIBS_CUTILS_TRACE_H will force <cutils/trace.h> to define atrace_begin and
// atrace_end with defined HAVE_ANDROID_OS again. Then there is no build-break.
# undef _LIBS_CUTILS_TRACE_H
# include <utils/Trace.h>
# define MOZ_PLATFORM_TRACING(name) android::ScopedTrace SAMPLER_APPEND_LINE_NUMBER(scopedTrace)(ATRACE_TAG, name);
# ifdef REMOVE_HAVE_ANDROID_OS
# undef HAVE_ANDROID_OS
# undef REMOVE_HAVE_ANDROID_OS
# endif
#else
# define MOZ_PLATFORM_TRACING(name)
#endif
// we want the class and function name but can't easily get that using preprocessor macros
// __func__ doesn't have the class name and __PRETTY_FUNCTION__ has the parameters
#define PROFILER_LABEL(name_space, info, category) MOZ_PLATFORM_TRACING(name_space "::" info) mozilla::SamplerStackFrameRAII SAMPLER_APPEND_LINE_NUMBER(sampler_raii)(name_space "::" info, category, __LINE__)
#define PROFILER_LABEL_FUNC(category) MOZ_PLATFORM_TRACING(SAMPLE_FUNCTION_NAME) mozilla::SamplerStackFrameRAII SAMPLER_APPEND_LINE_NUMBER(sampler_raii)(SAMPLE_FUNCTION_NAME, category, __LINE__)
#define PROFILER_LABEL_PRINTF(name_space, info, category, ...) MOZ_PLATFORM_TRACING(name_space "::" info) mozilla::SamplerStackFramePrintfRAII SAMPLER_APPEND_LINE_NUMBER(sampler_raii)(name_space "::" info, category, __LINE__, __VA_ARGS__)
#define PROFILER_MARKER(info) mozilla_sampler_add_marker(info)
#define PROFILER_MARKER_PAYLOAD(info, payload) mozilla_sampler_add_marker(info, payload)
#define PROFILER_MAIN_THREAD_MARKER(info) MOZ_ASSERT(NS_IsMainThread(), "This can only be called on the main thread"); mozilla_sampler_add_marker(info)
#define PROFILER_MAIN_THREAD_LABEL(name_space, info, category) MOZ_ASSERT(NS_IsMainThread(), "This can only be called on the main thread"); mozilla::SamplerStackFrameRAII SAMPLER_APPEND_LINE_NUMBER(sampler_raii)(name_space "::" info, category, __LINE__)
#define PROFILER_MAIN_THREAD_LABEL_PRINTF(name_space, info, category, ...) MOZ_ASSERT(NS_IsMainThread(), "This can only be called on the main thread"); mozilla::SamplerStackFramePrintfRAII SAMPLER_APPEND_LINE_NUMBER(sampler_raii)(name_space "::" info, category, __LINE__, __VA_ARGS__)
#if !defined(PLATFORM_LIKELY_MEMORY_CONSTRAINED) && !defined(ARCH_ARMV6)
# define PROFILE_DEFAULT_ENTRY 1000000
#else
# define PROFILE_DEFAULT_ENTRY 100000
#endif
// In the case of profiler_get_backtrace we know that we only need enough space
// for a single backtrace.
#define GET_BACKTRACE_DEFAULT_ENTRY 1000
#if defined(PLATFORM_LIKELY_MEMORY_CONSTRAINED)
/* A 1ms sampling interval has been shown to be a large perf hit
* (10fps) on memory-contrained (low-end) platforms, and additionally
* to yield different results from the profiler. Where this is the
* important case, b2g, there are also many gecko processes which
* magnify these effects. */
# define PROFILE_DEFAULT_INTERVAL 10
#elif defined(ANDROID)
// We use a lower frequency on Android, in order to make things work
// more smoothly on phones. This value can be adjusted later with
// some libunwind optimizations.
// In one sample measurement on Galaxy Nexus, out of about 700 backtraces,
// 60 of them took more than 25ms, and the average and standard deviation
// were 6.17ms and 9.71ms respectively.
// For now since we don't support stackwalking let's use 1ms since it's fast
// enough.
#define PROFILE_DEFAULT_INTERVAL 1
#else
#define PROFILE_DEFAULT_INTERVAL 1
#endif
#define PROFILE_DEFAULT_FEATURES NULL
#define PROFILE_DEFAULT_FEATURE_COUNT 0
namespace mozilla {
class MOZ_RAII GeckoProfilerTracingRAII {
public:
GeckoProfilerTracingRAII(const char* aCategory, const char* aInfo,
mozilla::UniquePtr<ProfilerBacktrace> aBacktrace
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: mCategory(aCategory)
, mInfo(aInfo)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
profiler_tracing(mCategory, mInfo, aBacktrace.release(), TRACING_INTERVAL_START);
}
~GeckoProfilerTracingRAII() {
profiler_tracing(mCategory, mInfo, TRACING_INTERVAL_END);
}
protected:
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
const char* mCategory;
const char* mInfo;
};
class MOZ_RAII SamplerStackFrameRAII {
public:
// we only copy the strings at save time, so to take multiple parameters we'd need to copy them then.
SamplerStackFrameRAII(const char *aInfo,
js::ProfileEntry::Category aCategory, uint32_t line
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
mHandle = mozilla_sampler_call_enter(aInfo, aCategory, this, false, line);
}
~SamplerStackFrameRAII() {
mozilla_sampler_call_exit(mHandle);
}
private:
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
void* mHandle;
};
static const int SAMPLER_MAX_STRING = 128;
class MOZ_RAII SamplerStackFramePrintfRAII {
public:
// we only copy the strings at save time, so to take multiple parameters we'd need to copy them then.
SamplerStackFramePrintfRAII(const char *aInfo,
js::ProfileEntry::Category aCategory, uint32_t line, const char *aFormat, ...)
: mHandle(nullptr)
{
if (profiler_is_active() && !profiler_in_privacy_mode()) {
va_list args;
va_start(args, aFormat);
char buff[SAMPLER_MAX_STRING];
// We have to use seperate printf's because we're using
// the vargs.
VsprintfLiteral(buff, aFormat, args);
SprintfLiteral(mDest, "%s %s", aInfo, buff);
mHandle = mozilla_sampler_call_enter(mDest, aCategory, this, true, line);
va_end(args);
} else {
mHandle = mozilla_sampler_call_enter(aInfo, aCategory, this, false, line);
}
}
~SamplerStackFramePrintfRAII() {
mozilla_sampler_call_exit(mHandle);
}
private:
char mDest[SAMPLER_MAX_STRING];
void* mHandle;
};
} // namespace mozilla
inline PseudoStack* mozilla_get_pseudo_stack(void)
{
if (!stack_key_initialized)
return nullptr;
return tlsPseudoStack.get();
}
inline void* mozilla_sampler_call_enter(const char *aInfo,
js::ProfileEntry::Category aCategory, void *aFrameAddress, bool aCopy, uint32_t line)
{
// check if we've been initialized to avoid calling pthread_getspecific
// with a null tlsStack which will return undefined results.
if (!stack_key_initialized)
return nullptr;
PseudoStack *stack = tlsPseudoStack.get();
// we can't infer whether 'stack' has been initialized
// based on the value of stack_key_intiailized because
// 'stack' is only intialized when a thread is being
// profiled.
if (!stack) {
return stack;
}
stack->push(aInfo, aCategory, aFrameAddress, aCopy, line);
// The handle is meant to support future changes
// but for now it is simply use to save a call to
// pthread_getspecific on exit. It also supports the
// case where the sampler is initialized between
// enter and exit.
return stack;
}
inline void mozilla_sampler_call_exit(void *aHandle)
{
if (!aHandle)
return;
PseudoStack *stack = (PseudoStack*)aHandle;
stack->popAndMaybeDelete();
}
void mozilla_sampler_add_marker(const char *aMarker, ProfilerMarkerPayload *aPayload);
static inline
void profiler_log(const char *str)
{
profiler_tracing("log", str, TRACING_EVENT);
}
static inline
void profiler_log(const char *fmt, va_list args)
{
mozilla_sampler_log(fmt, args);
}
#endif /* ndef TOOLS_SPS_SAMPLER_H_ */
|