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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* 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/. */
#include "vm/SPSProfiler.h"
#include "mozilla/DebugOnly.h"
#include "jsnum.h"
#include "jsprf.h"
#include "jsscript.h"
#include "jit/BaselineFrame.h"
#include "jit/BaselineJIT.h"
#include "jit/JitcodeMap.h"
#include "jit/JitFrameIterator.h"
#include "jit/JitFrames.h"
#include "vm/StringBuffer.h"
using namespace js;
using mozilla::DebugOnly;
SPSProfiler::SPSProfiler(JSRuntime* rt)
: rt(rt),
strings(mutexid::SPSProfilerStrings),
stack_(nullptr),
size_(nullptr),
max_(0),
slowAssertions(false),
enabled_(false),
eventMarker_(nullptr)
{
MOZ_ASSERT(rt != nullptr);
}
bool
SPSProfiler::init()
{
auto locked = strings.lock();
if (!locked->init())
return false;
return true;
}
void
SPSProfiler::setProfilingStack(ProfileEntry* stack, uint32_t* size, uint32_t max)
{
MOZ_ASSERT_IF(size_ && *size_ != 0, !enabled());
MOZ_ASSERT(strings.lock()->initialized());
stack_ = stack;
size_ = size;
max_ = max;
}
void
SPSProfiler::setEventMarker(void (*fn)(const char*))
{
eventMarker_ = fn;
}
void
SPSProfiler::enable(bool enabled)
{
MOZ_ASSERT(installed());
if (enabled_ == enabled)
return;
/*
* Ensure all future generated code will be instrumented, or that all
* currently instrumented code is discarded
*/
ReleaseAllJITCode(rt->defaultFreeOp());
// This function is called when the Gecko profiler makes a new TableTicker
// (and thus, a new circular buffer). Set all current entries in the
// JitcodeGlobalTable as expired and reset the buffer generation and lap
// count.
if (rt->hasJitRuntime() && rt->jitRuntime()->hasJitcodeGlobalTable())
rt->jitRuntime()->getJitcodeGlobalTable()->setAllEntriesAsExpired(rt);
rt->resetProfilerSampleBufferGen();
rt->resetProfilerSampleBufferLapCount();
// Ensure that lastProfilingFrame is null before 'enabled' becomes true.
if (rt->jitActivation) {
rt->jitActivation->setLastProfilingFrame(nullptr);
rt->jitActivation->setLastProfilingCallSite(nullptr);
}
enabled_ = enabled;
/* Toggle SPS-related jumps on baseline jitcode.
* The call to |ReleaseAllJITCode| above will release most baseline jitcode, but not
* jitcode for scripts with active frames on the stack. These scripts need to have
* their profiler state toggled so they behave properly.
*/
jit::ToggleBaselineProfiling(rt, enabled);
/* Update lastProfilingFrame to point to the top-most JS jit-frame currently on
* stack.
*/
if (rt->jitActivation) {
// Walk through all activations, and set their lastProfilingFrame appropriately.
if (enabled) {
void* lastProfilingFrame = GetTopProfilingJitFrame(rt->jitTop);
jit::JitActivation* jitActivation = rt->jitActivation;
while (jitActivation) {
jitActivation->setLastProfilingFrame(lastProfilingFrame);
jitActivation->setLastProfilingCallSite(nullptr);
lastProfilingFrame = GetTopProfilingJitFrame(jitActivation->prevJitTop());
jitActivation = jitActivation->prevJitActivation();
}
} else {
jit::JitActivation* jitActivation = rt->jitActivation;
while (jitActivation) {
jitActivation->setLastProfilingFrame(nullptr);
jitActivation->setLastProfilingCallSite(nullptr);
jitActivation = jitActivation->prevJitActivation();
}
}
}
}
/* Lookup the string for the function/script, creating one if necessary */
const char*
SPSProfiler::profileString(JSScript* script, JSFunction* maybeFun)
{
auto locked = strings.lock();
MOZ_ASSERT(locked->initialized());
ProfileStringMap::AddPtr s = locked->lookupForAdd(script);
if (!s) {
auto str = allocProfileString(script, maybeFun);
if (!str || !locked->add(s, script, mozilla::Move(str)))
return nullptr;
}
return s->value().get();
}
void
SPSProfiler::onScriptFinalized(JSScript* script)
{
/*
* This function is called whenever a script is destroyed, regardless of
* whether profiling has been turned on, so don't invoke a function on an
* invalid hash set. Also, even if profiling was enabled but then turned
* off, we still want to remove the string, so no check of enabled() is
* done.
*/
auto locked = strings.lock();
if (!locked->initialized())
return;
if (ProfileStringMap::Ptr entry = locked->lookup(script))
locked->remove(entry);
}
void
SPSProfiler::markEvent(const char* event)
{
MOZ_ASSERT(enabled());
if (eventMarker_) {
JS::AutoSuppressGCAnalysis nogc;
eventMarker_(event);
}
}
bool
SPSProfiler::enter(JSContext* cx, JSScript* script, JSFunction* maybeFun)
{
const char* str = profileString(script, maybeFun);
if (str == nullptr) {
ReportOutOfMemory(cx);
return false;
}
#ifdef DEBUG
// In debug builds, assert the JS pseudo frames already on the stack
// have a non-null pc. Only look at the top frames to avoid quadratic
// behavior.
if (*size_ > 0 && *size_ - 1 < max_) {
size_t start = (*size_ > 4) ? *size_ - 4 : 0;
for (size_t i = start; i < *size_ - 1; i++)
MOZ_ASSERT_IF(stack_[i].isJs(), stack_[i].pc() != nullptr);
}
#endif
push(str, nullptr, script, script->code(), /* copy = */ true);
return true;
}
void
SPSProfiler::exit(JSScript* script, JSFunction* maybeFun)
{
pop();
#ifdef DEBUG
/* Sanity check to make sure push/pop balanced */
if (*size_ < max_) {
const char* str = profileString(script, maybeFun);
/* Can't fail lookup because we should already be in the set */
MOZ_ASSERT(str != nullptr);
// Bug 822041
if (!stack_[*size_].isJs()) {
fprintf(stderr, "--- ABOUT TO FAIL ASSERTION ---\n");
fprintf(stderr, " stack=%p size=%d/%d\n", (void*) stack_, *size_, max_);
for (int32_t i = *size_; i >= 0; i--) {
if (stack_[i].isJs())
fprintf(stderr, " [%d] JS %s\n", i, stack_[i].label());
else
fprintf(stderr, " [%d] C line %d %s\n", i, stack_[i].line(), stack_[i].label());
}
}
MOZ_ASSERT(stack_[*size_].isJs());
MOZ_ASSERT(stack_[*size_].script() == script);
MOZ_ASSERT(strcmp((const char*) stack_[*size_].label(), str) == 0);
stack_[*size_].setLabel(nullptr);
stack_[*size_].setPC(nullptr);
}
#endif
}
void
SPSProfiler::beginPseudoJS(const char* string, void* sp)
{
/* these operations cannot be re-ordered, so volatile-ize operations */
volatile ProfileEntry* stack = stack_;
volatile uint32_t* size = size_;
uint32_t current = *size;
MOZ_ASSERT(installed());
if (current < max_) {
stack[current].setLabel(string);
stack[current].initCppFrame(sp, 0);
stack[current].setFlag(ProfileEntry::BEGIN_PSEUDO_JS);
}
*size = current + 1;
}
void
SPSProfiler::push(const char* string, void* sp, JSScript* script, jsbytecode* pc, bool copy,
ProfileEntry::Category category)
{
MOZ_ASSERT_IF(sp != nullptr, script == nullptr && pc == nullptr);
MOZ_ASSERT_IF(sp == nullptr, script != nullptr && pc != nullptr);
/* these operations cannot be re-ordered, so volatile-ize operations */
volatile ProfileEntry* stack = stack_;
volatile uint32_t* size = size_;
uint32_t current = *size;
MOZ_ASSERT(installed());
if (current < max_) {
volatile ProfileEntry& entry = stack[current];
if (sp != nullptr) {
entry.initCppFrame(sp, 0);
MOZ_ASSERT(entry.flags() == js::ProfileEntry::IS_CPP_ENTRY);
}
else {
entry.initJsFrame(script, pc);
MOZ_ASSERT(entry.flags() == 0);
}
entry.setLabel(string);
entry.setCategory(category);
// Track if mLabel needs a copy.
if (copy)
entry.setFlag(js::ProfileEntry::FRAME_LABEL_COPY);
else
entry.unsetFlag(js::ProfileEntry::FRAME_LABEL_COPY);
}
*size = current + 1;
}
void
SPSProfiler::pop()
{
MOZ_ASSERT(installed());
MOZ_ASSERT(*size_ > 0);
(*size_)--;
}
/*
* Serializes the script/function pair into a "descriptive string" which is
* allowed to fail. This function cannot trigger a GC because it could finalize
* some scripts, resize the hash table of profile strings, and invalidate the
* AddPtr held while invoking allocProfileString.
*/
UniqueChars
SPSProfiler::allocProfileString(JSScript* script, JSFunction* maybeFun)
{
// Note: this profiler string is regexp-matched by
// devtools/client/profiler/cleopatra/js/parserWorker.js.
// Get the function name, if any.
JSAtom* atom = maybeFun ? maybeFun->displayAtom() : nullptr;
// Get the script filename, if any, and its length.
const char* filename = script->filename();
if (filename == nullptr)
filename = "<unknown>";
size_t lenFilename = strlen(filename);
// Get the line number and its length as a string.
uint64_t lineno = script->lineno();
size_t lenLineno = 1;
for (uint64_t i = lineno; i /= 10; lenLineno++);
// Determine the required buffer size.
size_t len = lenFilename + lenLineno + 1; // +1 for the ":" separating them.
if (atom) {
len += JS::GetDeflatedUTF8StringLength(atom) + 3; // +3 for the " (" and ")" it adds.
}
// Allocate the buffer.
UniqueChars cstr(js_pod_malloc<char>(len + 1));
if (!cstr)
return nullptr;
// Construct the descriptive string.
DebugOnly<size_t> ret;
if (atom) {
UniqueChars atomStr = StringToNewUTF8CharsZ(nullptr, *atom);
if (!atomStr)
return nullptr;
ret = snprintf(cstr.get(), len + 1, "%s (%s:%" PRIu64 ")", atomStr.get(), filename, lineno);
} else {
ret = snprintf(cstr.get(), len + 1, "%s:%" PRIu64, filename, lineno);
}
MOZ_ASSERT(ret == len, "Computed length should match actual length!");
return cstr;
}
void
SPSProfiler::trace(JSTracer* trc)
{
if (stack_) {
size_t limit = Min(*size_, max_);
for (size_t i = 0; i < limit; i++)
stack_[i].trace(trc);
}
}
void
SPSProfiler::fixupStringsMapAfterMovingGC()
{
auto locked = strings.lock();
if (!locked->initialized())
return;
for (ProfileStringMap::Enum e(locked.get()); !e.empty(); e.popFront()) {
JSScript* script = e.front().key();
if (IsForwarded(script)) {
script = Forwarded(script);
e.rekeyFront(script);
}
}
}
#ifdef JSGC_HASH_TABLE_CHECKS
void
SPSProfiler::checkStringsMapAfterMovingGC()
{
auto locked = strings.lock();
if (!locked->initialized())
return;
for (auto r = locked->all(); !r.empty(); r.popFront()) {
JSScript* script = r.front().key();
CheckGCThingAfterMovingGC(script);
auto ptr = locked->lookup(script);
MOZ_RELEASE_ASSERT(ptr.found() && &*ptr == &r.front());
}
}
#endif
void
ProfileEntry::trace(JSTracer* trc)
{
if (isJs()) {
JSScript* s = rawScript();
TraceNullableRoot(trc, &s, "ProfileEntry script");
spOrScript = s;
}
}
SPSEntryMarker::SPSEntryMarker(JSRuntime* rt,
JSScript* script
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: profiler(&rt->spsProfiler)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (!profiler->installed()) {
profiler = nullptr;
return;
}
size_before = *profiler->size_;
// We want to push a CPP frame so the profiler can correctly order JS and native stacks.
profiler->beginPseudoJS("js::RunScript", this);
profiler->push("js::RunScript", nullptr, script, script->code(), /* copy = */ false);
}
SPSEntryMarker::~SPSEntryMarker()
{
if (profiler == nullptr)
return;
profiler->pop();
profiler->endPseudoJS();
MOZ_ASSERT(size_before == *profiler->size_);
}
AutoSPSEntry::AutoSPSEntry(JSRuntime* rt, const char* label, ProfileEntry::Category category
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: profiler_(&rt->spsProfiler)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (!profiler_->installed()) {
profiler_ = nullptr;
return;
}
sizeBefore_ = *profiler_->size_;
profiler_->beginPseudoJS(label, this);
profiler_->push(label, this, nullptr, nullptr, /* copy = */ false, category);
}
AutoSPSEntry::~AutoSPSEntry()
{
if (!profiler_)
return;
profiler_->pop();
profiler_->endPseudoJS();
MOZ_ASSERT(sizeBefore_ == *profiler_->size_);
}
SPSBaselineOSRMarker::SPSBaselineOSRMarker(JSRuntime* rt, bool hasSPSFrame
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: profiler(&rt->spsProfiler)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (!hasSPSFrame || !profiler->enabled() ||
profiler->size() >= profiler->maxSize())
{
profiler = nullptr;
return;
}
size_before = profiler->size();
if (profiler->size() == 0)
return;
ProfileEntry& entry = profiler->stack()[profiler->size() - 1];
MOZ_ASSERT(entry.isJs());
entry.setOSR();
}
SPSBaselineOSRMarker::~SPSBaselineOSRMarker()
{
if (profiler == nullptr)
return;
MOZ_ASSERT(size_before == *profiler->size_);
if (profiler->size() == 0)
return;
ProfileEntry& entry = profiler->stack()[profiler->size() - 1];
MOZ_ASSERT(entry.isJs());
entry.unsetOSR();
}
JS_PUBLIC_API(JSScript*)
ProfileEntry::script() const volatile
{
MOZ_ASSERT(isJs());
auto script = reinterpret_cast<JSScript*>(spOrScript);
if (!script)
return nullptr;
// If profiling is supressed then we can't trust the script pointers to be
// valid as they could be in the process of being moved by a compacting GC
// (although it's still OK to get the runtime from them).
JSRuntime* rt = script->zoneFromAnyThread()->runtimeFromAnyThread();
if (!rt->isProfilerSamplingEnabled())
return nullptr;
MOZ_ASSERT(!IsForwarded(script));
return script;
}
JS_FRIEND_API(jsbytecode*)
ProfileEntry::pc() const volatile
{
MOZ_ASSERT(isJs());
if (lineOrPcOffset == NullPCOffset)
return nullptr;
JSScript* script = this->script();
return script ? script->offsetToPC(lineOrPcOffset) : nullptr;
}
JS_FRIEND_API(void)
ProfileEntry::setPC(jsbytecode* pc) volatile
{
MOZ_ASSERT(isJs());
JSScript* script = this->script();
MOZ_ASSERT(script); // This should not be called while profiling is suppressed.
lineOrPcOffset = pc == nullptr ? NullPCOffset : script->pcToOffset(pc);
}
JS_FRIEND_API(void)
js::SetContextProfilingStack(JSContext* cx, ProfileEntry* stack, uint32_t* size, uint32_t max)
{
cx->spsProfiler.setProfilingStack(stack, size, max);
}
JS_FRIEND_API(void)
js::EnableContextProfilingStack(JSContext* cx, bool enabled)
{
cx->spsProfiler.enable(enabled);
}
JS_FRIEND_API(void)
js::RegisterContextProfilingEventMarker(JSContext* cx, void (*fn)(const char*))
{
MOZ_ASSERT(cx->spsProfiler.enabled());
cx->spsProfiler.setEventMarker(fn);
}
JS_FRIEND_API(jsbytecode*)
js::ProfilingGetPC(JSContext* cx, JSScript* script, void* ip)
{
return cx->spsProfiler.ipToPC(script, size_t(ip));
}
AutoSuppressProfilerSampling::AutoSuppressProfilerSampling(JSContext* cx
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: rt_(cx->runtime()),
previouslyEnabled_(rt_->isProfilerSamplingEnabled())
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (previouslyEnabled_)
rt_->disableProfilerSampling();
}
AutoSuppressProfilerSampling::AutoSuppressProfilerSampling(JSRuntime* rt
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: rt_(rt),
previouslyEnabled_(rt_->isProfilerSamplingEnabled())
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (previouslyEnabled_)
rt_->disableProfilerSampling();
}
AutoSuppressProfilerSampling::~AutoSuppressProfilerSampling()
{
if (previouslyEnabled_)
rt_->enableProfilerSampling();
}
void*
js::GetTopProfilingJitFrame(uint8_t* exitFramePtr)
{
// For null exitFrame, there is no previous exit frame, just return.
if (!exitFramePtr)
return nullptr;
jit::JitProfilingFrameIterator iter(exitFramePtr);
MOZ_ASSERT(!iter.done());
return iter.fp();
}
|