| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "vm/GeckoProfiler-inl.h" |
| 8 |
|
| 9 |
#include "mozilla/DebugOnly.h" |
| 10 |
#include "mozilla/Sprintf.h" |
| 11 |
|
| 12 |
#include "gc/GC.h" |
| 13 |
#include "gc/PublicIterators.h" |
| 14 |
#include "jit/BaselineJIT.h" |
| 15 |
#include "jit/JitcodeMap.h" |
| 16 |
#include "jit/JitRuntime.h" |
| 17 |
#include "jit/JSJitFrameIter.h" |
| 18 |
#include "jit/PerfSpewer.h" |
| 19 |
#include "js/ProfilingStack.h" |
| 20 |
#include "vm/FrameIter.h" // js::OnlyJSJitFrameIter |
| 21 |
#include "vm/JitActivation.h" |
| 22 |
#include "vm/JSScript.h" |
| 23 |
|
| 24 |
#include "gc/Marking-inl.h" |
| 25 |
#include "jit/JSJitFrameIter-inl.h" |
| 26 |
|
| 27 |
using namespace js; |
| 28 |
|
| 29 |
GeckoProfilerThread::GeckoProfilerThread() |
| 30 |
: profilingStack_(nullptr), profilingStackIfEnabled_(nullptr) {} |
| 31 |
|
| 32 |
GeckoProfilerRuntime::GeckoProfilerRuntime(JSRuntime* rt) |
| 33 |
: rt(rt), |
| 34 |
slowAssertions(false), |
| 35 |
enabled_(false), |
| 36 |
eventMarker_(nullptr), |
| 37 |
intervalMarker_(nullptr) { |
| 38 |
MOZ_ASSERT(rt != nullptr); |
| 39 |
} |
| 40 |
|
| 41 |
void GeckoProfilerThread::setProfilingStack(ProfilingStack* profilingStack, |
| 42 |
bool enabled) { |
| 43 |
profilingStack_ = profilingStack; |
| 44 |
profilingStackIfEnabled_ = enabled ? profilingStack : nullptr; |
| 45 |
} |
| 46 |
|
| 47 |
void GeckoProfilerRuntime::setEventMarker(void (*fn)(mozilla::MarkerCategory, |
| 48 |
const char*, |
| 49 |
const char*)) { |
| 50 |
eventMarker_ = fn; |
| 51 |
} |
| 52 |
|
| 53 |
void GeckoProfilerRuntime::setIntervalMarker(void (*fn)( |
| 54 |
mozilla::MarkerCategory, const char*, mozilla::TimeStamp, const char*)) { |
| 55 |
intervalMarker_ = fn; |
| 56 |
} |
| 57 |
|
| 58 |
// Get a pointer to the top-most profiling frame, given the exit frame pointer. |
| 59 |
static jit::JitFrameLayout* GetTopProfilingJitFrame(jit::JitActivation* act) { |
| 60 |
// If there is no exit frame set, just return. |
| 61 |
if (!act->hasExitFP()) { |
| 62 |
return nullptr; |
| 63 |
} |
| 64 |
|
| 65 |
// Skip wasm frames that might be in the way. |
| 66 |
OnlyJSJitFrameIter iter(act); |
| 67 |
if (iter.done()) { |
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
|
| 71 |
// Skip if the activation has no JS frames. This can happen if there's only a |
| 72 |
// TrampolineNative frame because these are skipped by the profiling frame |
| 73 |
// iterator. |
| 74 |
jit::JSJitProfilingFrameIterator jitIter( |
| 75 |
(jit::CommonFrameLayout*)iter.frame().fp()); |
| 76 |
if (jitIter.done()) { |
| 77 |
return nullptr; |
| 78 |
} |
| 79 |
|
| 80 |
return jitIter.framePtr(); |
| 81 |
} |
| 82 |
|
| 83 |
void GeckoProfilerRuntime::enable(bool enabled) { |
| 84 |
JSContext* cx = rt->mainContextFromAnyThread(); |
| 85 |
MOZ_ASSERT(cx->geckoProfiler().infraInstalled()); |
| 86 |
|
| 87 |
if (enabled_ == enabled) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Ensure all future generated code will be instrumented, or that all |
| 93 |
* currently instrumented code is discarded |
| 94 |
*/ |
| 95 |
ReleaseAllJITCode(rt->gcContext()); |
| 96 |
|
| 97 |
// This function is called when the Gecko profiler makes a new Sampler |
| 98 |
// (and thus, a new circular buffer). Set all current entries in the |
| 99 |
// JitcodeGlobalTable as expired and reset the buffer range start. |
| 100 |
if (rt->hasJitRuntime() && rt->jitRuntime()->hasJitcodeGlobalTable()) { |
| 101 |
rt->jitRuntime()->getJitcodeGlobalTable()->setAllEntriesAsExpired(); |
| 102 |
} |
| 103 |
rt->setProfilerSampleBufferRangeStart(0); |
| 104 |
|
| 105 |
// Ensure that lastProfilingFrame is null for the main thread. |
| 106 |
if (cx->jitActivation) { |
| 107 |
cx->jitActivation->setLastProfilingFrame(nullptr); |
| 108 |
cx->jitActivation->setLastProfilingCallSite(nullptr); |
| 109 |
} |
| 110 |
|
| 111 |
enabled_ = enabled; |
| 112 |
|
| 113 |
/* Toggle Gecko Profiler-related jumps on baseline jitcode. |
| 114 |
* The call to |ReleaseAllJITCode| above will release most baseline jitcode, |
| 115 |
* but not jitcode for scripts with active frames on the stack. These scripts |
| 116 |
* need to have their profiler state toggled so they behave properly. |
| 117 |
*/ |
| 118 |
jit::ToggleBaselineProfiling(cx, enabled); |
| 119 |
|
| 120 |
// Update lastProfilingFrame to point to the top-most JS jit-frame currently |
| 121 |
// on stack. |
| 122 |
if (cx->jitActivation) { |
| 123 |
// Walk through all activations, and set their lastProfilingFrame |
| 124 |
// appropriately. |
| 125 |
if (enabled) { |
| 126 |
jit::JitActivation* jitActivation = cx->jitActivation; |
| 127 |
while (jitActivation) { |
| 128 |
auto* lastProfilingFrame = GetTopProfilingJitFrame(jitActivation); |
| 129 |
jitActivation->setLastProfilingFrame(lastProfilingFrame); |
| 130 |
jitActivation->setLastProfilingCallSite(nullptr); |
| 131 |
jitActivation = jitActivation->prevJitActivation(); |
| 132 |
} |
| 133 |
} else { |
| 134 |
jit::JitActivation* jitActivation = cx->jitActivation; |
| 135 |
while (jitActivation) { |
| 136 |
jitActivation->setLastProfilingFrame(nullptr); |
| 137 |
jitActivation->setLastProfilingCallSite(nullptr); |
| 138 |
jitActivation = jitActivation->prevJitActivation(); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// WebAssembly code does not need to be released, but profiling string |
| 144 |
// labels have to be generated so that they are available during async |
| 145 |
// profiling stack iteration. |
| 146 |
for (RealmsIter r(rt); !r.done(); r.next()) { |
| 147 |
r->wasm.ensureProfilingLabels(enabled); |
| 148 |
} |
| 149 |
|
| 150 |
#ifdef JS_STRUCTURED_SPEW |
| 151 |
// Enable the structured spewer if the environment variable is set. |
| 152 |
if (enabled) { |
| 153 |
cx->spewer().enableSpewing(); |
| 154 |
} else { |
| 155 |
cx->spewer().disableSpewing(); |
| 156 |
} |
| 157 |
#endif |
| 158 |
} |
| 159 |
|
| 160 |
/* Lookup the string for the function/script, creating one if necessary */ |
| 161 |
const char* GeckoProfilerRuntime::profileString(JSContext* cx, |
| 162 |
BaseScript* script) { |
| 163 |
ProfileStringMap::AddPtr s = strings().lookupForAdd(script); |
| 164 |
|
| 165 |
if (!s) { |
| 166 |
UniqueChars str = allocProfileString(cx, script); |
| 167 |
if (!str) { |
| 168 |
return nullptr; |
| 169 |
} |
| 170 |
MOZ_ASSERT(script->hasBytecode()); |
| 171 |
if (!strings().add(s, script, std::move(str))) { |
| 172 |
ReportOutOfMemory(cx); |
| 173 |
return nullptr; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return s->value().get(); |
| 178 |
} |
| 179 |
|
| 180 |
void GeckoProfilerRuntime::onScriptFinalized(BaseScript* script) { |
| 181 |
/* |
| 182 |
* This function is called whenever a script is destroyed, regardless of |
| 183 |
* whether profiling has been turned on, so don't invoke a function on an |
| 184 |
* invalid hash set. Also, even if profiling was enabled but then turned |
| 185 |
* off, we still want to remove the string, so no check of enabled() is |
| 186 |
* done. |
| 187 |
*/ |
| 188 |
if (ProfileStringMap::Ptr entry = strings().lookup(script)) { |
| 189 |
strings().remove(entry); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void GeckoProfilerRuntime::markEvent(const char* event, const char* details, |
| 194 |
JS::ProfilingCategoryPair jsPair) { |
| 195 |
MOZ_ASSERT(enabled()); |
| 196 |
if (eventMarker_) { |
| 197 |
JS::AutoSuppressGCAnalysis nogc; |
| 198 |
mozilla::MarkerCategory category( |
| 199 |
static_cast<mozilla::baseprofiler::ProfilingCategoryPair>(jsPair)); |
| 200 |
eventMarker_(category, event, details); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void GeckoProfilerRuntime::markInterval(const char* event, |
| 205 |
mozilla::TimeStamp start, |
| 206 |
const char* details, |
| 207 |
JS::ProfilingCategoryPair jsPair) { |
| 208 |
MOZ_ASSERT(enabled()); |
| 209 |
if (intervalMarker_) { |
| 210 |
JS::AutoSuppressGCAnalysis nogc; |
| 211 |
mozilla::MarkerCategory category( |
| 212 |
static_cast<mozilla::baseprofiler::ProfilingCategoryPair>(jsPair)); |
| 213 |
intervalMarker_(category, event, start, details); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
bool GeckoProfilerThread::enter(JSContext* cx, JSScript* script) { |
| 218 |
const char* dynamicString = |
| 219 |
cx->runtime()->geckoProfiler().profileString(cx, script); |
| 220 |
if (dynamicString == nullptr) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
#ifdef DEBUG |
| 225 |
// In debug builds, assert the JS profiling stack frames already on the |
| 226 |
// stack have a non-null pc. Only look at the top frames to avoid quadratic |
| 227 |
// behavior. |
| 228 |
uint32_t sp = profilingStack_->stackPointer; |
| 229 |
if (sp > 0 && sp - 1 < profilingStack_->stackCapacity()) { |
| 230 |
size_t start = (sp > 4) ? sp - 4 : 0; |
| 231 |
for (size_t i = start; i < sp - 1; i++) { |
| 232 |
MOZ_ASSERT_IF(profilingStack_->frames[i].isJsFrame(), |
| 233 |
profilingStack_->frames[i].pc()); |
| 234 |
} |
| 235 |
} |
| 236 |
#endif |
| 237 |
|
| 238 |
profilingStack_->pushJsFrame( |
| 239 |
"", dynamicString, script, script->code(), |
| 240 |
script->realm()->creationOptions().profilerRealmID()); |
| 241 |
return true; |
| 242 |
} |
| 243 |
|
| 244 |
void GeckoProfilerThread::exit(JSContext* cx, JSScript* script) { |
| 245 |
profilingStack_->pop(); |
| 246 |
|
| 247 |
#ifdef DEBUG |
| 248 |
/* Sanity check to make sure push/pop balanced */ |
| 249 |
uint32_t sp = profilingStack_->stackPointer; |
| 250 |
if (sp < profilingStack_->stackCapacity()) { |
| 251 |
JSRuntime* rt = script->runtimeFromMainThread(); |
| 252 |
const char* dynamicString = rt->geckoProfiler().profileString(cx, script); |
| 253 |
/* Can't fail lookup because we should already be in the set */ |
| 254 |
MOZ_ASSERT(dynamicString); |
| 255 |
|
| 256 |
// Bug 822041 |
| 257 |
if (!profilingStack_->frames[sp].isJsFrame()) { |
| 258 |
fprintf(stderr, "--- ABOUT TO FAIL ASSERTION ---\n"); |
| 259 |
fprintf(stderr, " frames=%p size=%u/%u\n", (void*)profilingStack_->frames, |
| 260 |
uint32_t(profilingStack_->stackPointer), |
| 261 |
profilingStack_->stackCapacity()); |
| 262 |
for (int32_t i = sp; i >= 0; i--) { |
| 263 |
ProfilingStackFrame& frame = profilingStack_->frames[i]; |
| 264 |
if (frame.isJsFrame()) { |
| 265 |
fprintf(stderr, " [%d] JS %s\n", i, frame.dynamicString()); |
| 266 |
} else { |
| 267 |
fprintf(stderr, " [%d] Label %s\n", i, frame.dynamicString()); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
ProfilingStackFrame& frame = profilingStack_->frames[sp]; |
| 273 |
MOZ_ASSERT(frame.isJsFrame()); |
| 274 |
MOZ_ASSERT(frame.script() == script); |
| 275 |
MOZ_ASSERT(strcmp((const char*)frame.dynamicString(), dynamicString) == 0); |
| 276 |
} |
| 277 |
#endif |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Serializes the script/function pair into a "descriptive string" which is |
| 282 |
* allowed to fail. This function cannot trigger a GC because it could finalize |
| 283 |
* some scripts, resize the hash table of profile strings, and invalidate the |
| 284 |
* AddPtr held while invoking allocProfileString. |
| 285 |
*/ |
| 286 |
/* static */ |
| 287 |
UniqueChars GeckoProfilerRuntime::allocProfileString(JSContext* cx, |
| 288 |
BaseScript* script) { |
| 289 |
// Note: this profiler string is regexp-matched by |
| 290 |
// profiler code. Most recently at |
| 291 |
// https://github.com/firefox-devtools/profiler/blob/245b1a400c5c368ccc13641d0335398bafa0e870/src/profile-logic/process-profile.js#L520-L525 |
| 292 |
|
| 293 |
// If the script has a function, try calculating its name. |
| 294 |
JSAtom* name = nullptr; |
| 295 |
size_t nameLength = 0; |
| 296 |
JSFunction* func = script->function(); |
| 297 |
if (func && func->fullDisplayAtom()) { |
| 298 |
name = func->fullDisplayAtom(); |
| 299 |
nameLength = JS::GetDeflatedUTF8StringLength(name); |
| 300 |
} |
| 301 |
|
| 302 |
// Calculate filename length. We cap this to a reasonable limit to avoid |
| 303 |
// performance impact of strlen/alloc/memcpy. |
| 304 |
constexpr size_t MaxFilenameLength = 200; |
| 305 |
const char* filenameStr = script->filename() ? script->filename() : "(null)"; |
| 306 |
size_t filenameLength = js_strnlen(filenameStr, MaxFilenameLength); |
| 307 |
|
| 308 |
// Calculate line + column length. |
| 309 |
bool hasLineAndColumn = false; |
| 310 |
size_t lineAndColumnLength = 0; |
| 311 |
char lineAndColumnStr[30]; |
| 312 |
if (name || script->isFunction() || script->isForEval()) { |
| 313 |
lineAndColumnLength = |
| 314 |
SprintfLiteral(lineAndColumnStr, "%u:%u", script->lineno(), |
| 315 |
script->column().oneOriginValue()); |
| 316 |
hasLineAndColumn = true; |
| 317 |
} |
| 318 |
|
| 319 |
// Full profile string for scripts with functions is: |
| 320 |
// FuncName (FileName:Lineno:Column) |
| 321 |
// Full profile string for scripts without functions is: |
| 322 |
// FileName:Lineno:Column |
| 323 |
// Full profile string for scripts without functions and without lines is: |
| 324 |
// FileName |
| 325 |
|
| 326 |
// Calculate full string length. |
| 327 |
size_t fullLength = 0; |
| 328 |
if (name) { |
| 329 |
MOZ_ASSERT(hasLineAndColumn); |
| 330 |
fullLength = nameLength + 2 + filenameLength + 1 + lineAndColumnLength + 1; |
| 331 |
} else if (hasLineAndColumn) { |
| 332 |
fullLength = filenameLength + 1 + lineAndColumnLength; |
| 333 |
} else { |
| 334 |
fullLength = filenameLength; |
| 335 |
} |
| 336 |
|
| 337 |
// Allocate string. |
| 338 |
UniqueChars str(cx->pod_malloc<char>(fullLength + 1)); |
| 339 |
if (!str) { |
| 340 |
return nullptr; |
| 341 |
} |
| 342 |
|
| 343 |
size_t cur = 0; |
| 344 |
|
| 345 |
// Fill string with function name if needed. |
| 346 |
if (name) { |
| 347 |
mozilla::DebugOnly<size_t> written = JS::DeflateStringToUTF8Buffer( |
| 348 |
name, mozilla::Span(str.get() + cur, nameLength)); |
| 349 |
MOZ_ASSERT(written == nameLength); |
| 350 |
cur += nameLength; |
| 351 |
str[cur++] = ' '; |
| 352 |
str[cur++] = '('; |
| 353 |
} |
| 354 |
|
| 355 |
// Fill string with filename chars. |
| 356 |
memcpy(str.get() + cur, filenameStr, filenameLength); |
| 357 |
cur += filenameLength; |
| 358 |
|
| 359 |
// Fill line + column chars. |
| 360 |
if (hasLineAndColumn) { |
| 361 |
str[cur++] = ':'; |
| 362 |
memcpy(str.get() + cur, lineAndColumnStr, lineAndColumnLength); |
| 363 |
cur += lineAndColumnLength; |
| 364 |
} |
| 365 |
|
| 366 |
// Terminal ')' if necessary. |
| 367 |
if (name) { |
| 368 |
str[cur++] = ')'; |
| 369 |
} |
| 370 |
|
| 371 |
MOZ_ASSERT(cur == fullLength); |
| 372 |
str[cur] = 0; |
| 373 |
|
| 374 |
return str; |
| 375 |
} |
| 376 |
|
| 377 |
void GeckoProfilerThread::trace(JSTracer* trc) { |
| 378 |
if (profilingStack_) { |
| 379 |
size_t size = profilingStack_->stackSize(); |
| 380 |
for (size_t i = 0; i < size; i++) { |
| 381 |
profilingStack_->frames[i].trace(trc); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void GeckoProfilerRuntime::fixupStringsMapAfterMovingGC() { |
| 387 |
for (ProfileStringMap::Enum e(strings()); !e.empty(); e.popFront()) { |
| 388 |
BaseScript* script = e.front().key(); |
| 389 |
if (IsForwarded(script)) { |
| 390 |
script = Forwarded(script); |
| 391 |
e.rekeyFront(script); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 397 |
void GeckoProfilerRuntime::checkStringsMapAfterMovingGC() { |
| 398 |
CheckTableAfterMovingGC(strings(), [](const auto& entry) { |
| 399 |
BaseScript* script = entry.key(); |
| 400 |
CheckGCThingAfterMovingGC(script); |
| 401 |
return script; |
| 402 |
}); |
| 403 |
} |
| 404 |
#endif |
| 405 |
|
| 406 |
void ProfilingStackFrame::trace(JSTracer* trc) { |
| 407 |
if (isJsFrame()) { |
| 408 |
JSScript* s = rawScript(); |
| 409 |
TraceNullableRoot(trc, &s, "ProfilingStackFrame script"); |
| 410 |
spOrScript = s; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
GeckoProfilerBaselineOSRMarker::GeckoProfilerBaselineOSRMarker( |
| 415 |
JSContext* cx, bool hasProfilerFrame) |
| 416 |
: profiler(&cx->geckoProfiler()) { |
| 417 |
if (!hasProfilerFrame || !cx->runtime()->geckoProfiler().enabled()) { |
| 418 |
profiler = nullptr; |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
uint32_t sp = profiler->profilingStack_->stackPointer; |
| 423 |
if (sp >= profiler->profilingStack_->stackCapacity()) { |
| 424 |
profiler = nullptr; |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
spBefore_ = sp; |
| 429 |
if (sp == 0) { |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
ProfilingStackFrame& frame = profiler->profilingStack_->frames[sp - 1]; |
| 434 |
MOZ_ASSERT(!frame.isOSRFrame()); |
| 435 |
frame.setIsOSRFrame(true); |
| 436 |
} |
| 437 |
|
| 438 |
GeckoProfilerBaselineOSRMarker::~GeckoProfilerBaselineOSRMarker() { |
| 439 |
if (profiler == nullptr) { |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
uint32_t sp = profiler->stackPointer(); |
| 444 |
MOZ_ASSERT(spBefore_ == sp); |
| 445 |
if (sp == 0) { |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
ProfilingStackFrame& frame = profiler->stack()[sp - 1]; |
| 450 |
MOZ_ASSERT(frame.isOSRFrame()); |
| 451 |
frame.setIsOSRFrame(false); |
| 452 |
} |
| 453 |
|
| 454 |
JS_PUBLIC_API JSScript* ProfilingStackFrame::script() const { |
| 455 |
MOZ_ASSERT(isJsFrame()); |
| 456 |
auto* script = reinterpret_cast<JSScript*>(spOrScript.operator void*()); |
| 457 |
if (!script) { |
| 458 |
return nullptr; |
| 459 |
} |
| 460 |
|
| 461 |
// If profiling is supressed then we can't trust the script pointers to be |
| 462 |
// valid as they could be in the process of being moved by a compacting GC |
| 463 |
// (although it's still OK to get the runtime from them). |
| 464 |
JSContext* cx = script->runtimeFromAnyThread()->mainContextFromAnyThread(); |
| 465 |
if (!cx->isProfilerSamplingEnabled()) { |
| 466 |
return nullptr; |
| 467 |
} |
| 468 |
|
| 469 |
MOZ_ASSERT(!IsForwarded(script)); |
| 470 |
return script; |
| 471 |
} |
| 472 |
|
| 473 |
JS_PUBLIC_API JSFunction* ProfilingStackFrame::function() const { |
| 474 |
JSScript* script = this->script(); |
| 475 |
return script ? script->function() : nullptr; |
| 476 |
} |
| 477 |
|
| 478 |
JS_PUBLIC_API jsbytecode* ProfilingStackFrame::pc() const { |
| 479 |
MOZ_ASSERT(isJsFrame()); |
| 480 |
if (pcOffsetIfJS_ == NullPCOffset) { |
| 481 |
return nullptr; |
| 482 |
} |
| 483 |
|
| 484 |
JSScript* script = this->script(); |
| 485 |
return script ? script->offsetToPC(pcOffsetIfJS_) : nullptr; |
| 486 |
} |
| 487 |
|
| 488 |
/* static */ |
| 489 |
int32_t ProfilingStackFrame::pcToOffset(JSScript* aScript, jsbytecode* aPc) { |
| 490 |
return aPc ? aScript->pcToOffset(aPc) : NullPCOffset; |
| 491 |
} |
| 492 |
|
| 493 |
void ProfilingStackFrame::setPC(jsbytecode* pc) { |
| 494 |
MOZ_ASSERT(isJsFrame()); |
| 495 |
JSScript* script = this->script(); |
| 496 |
MOZ_ASSERT( |
| 497 |
script); // This should not be called while profiling is suppressed. |
| 498 |
pcOffsetIfJS_ = pcToOffset(script, pc); |
| 499 |
} |
| 500 |
|
| 501 |
JS_PUBLIC_API void js::SetContextProfilingStack( |
| 502 |
JSContext* cx, ProfilingStack* profilingStack) { |
| 503 |
cx->geckoProfiler().setProfilingStack( |
| 504 |
profilingStack, cx->runtime()->geckoProfiler().enabled()); |
| 505 |
} |
| 506 |
|
| 507 |
JS_PUBLIC_API void js::EnableContextProfilingStack(JSContext* cx, |
| 508 |
bool enabled) { |
| 509 |
cx->geckoProfiler().enable(enabled); |
| 510 |
cx->runtime()->geckoProfiler().enable(enabled); |
| 511 |
} |
| 512 |
|
| 513 |
JS_PUBLIC_API void js::RegisterContextProfilingEventMarker( |
| 514 |
JSContext* cx, |
| 515 |
void (*mark)(mozilla::MarkerCategory, const char*, const char*), |
| 516 |
void (*interval)(mozilla::MarkerCategory, const char*, mozilla::TimeStamp, |
| 517 |
const char*)) { |
| 518 |
MOZ_ASSERT(cx->runtime()->geckoProfiler().enabled()); |
| 519 |
cx->runtime()->geckoProfiler().setEventMarker(mark); |
| 520 |
cx->runtime()->geckoProfiler().setIntervalMarker(interval); |
| 521 |
} |
| 522 |
|
| 523 |
AutoSuppressProfilerSampling::AutoSuppressProfilerSampling(JSContext* cx) |
| 524 |
: cx_(cx), previouslyEnabled_(cx->isProfilerSamplingEnabled()) { |
| 525 |
if (previouslyEnabled_) { |
| 526 |
cx_->disableProfilerSampling(); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
AutoSuppressProfilerSampling::~AutoSuppressProfilerSampling() { |
| 531 |
if (previouslyEnabled_) { |
| 532 |
cx_->enableProfilerSampling(); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
namespace JS { |
| 537 |
|
| 538 |
// clang-format off |
| 539 |
|
| 540 |
// ProfilingSubcategory_X: |
| 541 |
// One enum for each category X, listing that category's subcategories. This |
| 542 |
// allows the sProfilingCategoryInfo macro construction below to look up a |
| 543 |
// per-category index for a subcategory. |
| 544 |
#define SUBCATEGORY_ENUMS_BEGIN_CATEGORY(name, labelAsString, color) \ |
| 545 |
enum class ProfilingSubcategory_##name : uint32_t { |
| 546 |
#define SUBCATEGORY_ENUMS_SUBCATEGORY(category, name, labelAsString) \ |
| 547 |
name, |
| 548 |
#define SUBCATEGORY_ENUMS_END_CATEGORY \ |
| 549 |
}; |
| 550 |
MOZ_PROFILING_CATEGORY_LIST(SUBCATEGORY_ENUMS_BEGIN_CATEGORY, |
| 551 |
SUBCATEGORY_ENUMS_SUBCATEGORY, |
| 552 |
SUBCATEGORY_ENUMS_END_CATEGORY) |
| 553 |
#undef SUBCATEGORY_ENUMS_BEGIN_CATEGORY |
| 554 |
#undef SUBCATEGORY_ENUMS_SUBCATEGORY |
| 555 |
#undef SUBCATEGORY_ENUMS_END_CATEGORY |
| 556 |
|
| 557 |
// sProfilingCategoryPairInfo: |
| 558 |
// A list of ProfilingCategoryPairInfos with the same order as |
| 559 |
// ProfilingCategoryPair, which can be used to map a ProfilingCategoryPair to |
| 560 |
// its information. |
| 561 |
#define CATEGORY_INFO_BEGIN_CATEGORY(name, labelAsString, color) |
| 562 |
#define CATEGORY_INFO_SUBCATEGORY(category, name, labelAsString) \ |
| 563 |
{ProfilingCategory::category, \ |
| 564 |
uint32_t(ProfilingSubcategory_##category::name), labelAsString}, |
| 565 |
#define CATEGORY_INFO_END_CATEGORY |
| 566 |
const ProfilingCategoryPairInfo sProfilingCategoryPairInfo[] = { |
| 567 |
MOZ_PROFILING_CATEGORY_LIST(CATEGORY_INFO_BEGIN_CATEGORY, |
| 568 |
CATEGORY_INFO_SUBCATEGORY, |
| 569 |
CATEGORY_INFO_END_CATEGORY) |
| 570 |
}; |
| 571 |
#undef CATEGORY_INFO_BEGIN_CATEGORY |
| 572 |
#undef CATEGORY_INFO_SUBCATEGORY |
| 573 |
#undef CATEGORY_INFO_END_CATEGORY |
| 574 |
|
| 575 |
// clang-format on |
| 576 |
|
| 577 |
JS_PUBLIC_API const ProfilingCategoryPairInfo& GetProfilingCategoryPairInfo( |
| 578 |
ProfilingCategoryPair aCategoryPair) { |
| 579 |
static_assert( |
| 580 |
std::size(sProfilingCategoryPairInfo) == |
| 581 |
uint32_t(ProfilingCategoryPair::COUNT), |
| 582 |
"sProfilingCategoryPairInfo and ProfilingCategory need to have the " |
| 583 |
"same order and the same length"); |
| 584 |
|
| 585 |
uint32_t categoryPairIndex = uint32_t(aCategoryPair); |
| 586 |
MOZ_RELEASE_ASSERT(categoryPairIndex <= |
| 587 |
uint32_t(ProfilingCategoryPair::LAST)); |
| 588 |
return sProfilingCategoryPairInfo[categoryPairIndex]; |
| 589 |
} |
| 590 |
|
| 591 |
} // namespace JS |