| 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 |
#ifndef js_ProfilingStack_h |
| 8 |
#define js_ProfilingStack_h |
| 9 |
|
| 10 |
#include "mozilla/Atomics.h" |
| 11 |
#include "mozilla/BaseProfilerMarkersPrerequisites.h" |
| 12 |
#include "mozilla/TimeStamp.h" |
| 13 |
|
| 14 |
#include <stdint.h> |
| 15 |
|
| 16 |
#include "jstypes.h" |
| 17 |
|
| 18 |
#include "js/ProfilingCategory.h" |
| 19 |
#include "js/TypeDecls.h" |
| 20 |
|
| 21 |
class JS_PUBLIC_API JSTracer; |
| 22 |
class JS_PUBLIC_API ProfilingStack; |
| 23 |
|
| 24 |
// This file defines the classes ProfilingStack and ProfilingStackFrame. |
| 25 |
// The ProfilingStack manages an array of ProfilingStackFrames. |
| 26 |
// It keeps track of the "label stack" and the JS interpreter stack. |
| 27 |
// The two stack types are interleaved. |
| 28 |
// |
| 29 |
// Usage: |
| 30 |
// |
| 31 |
// ProfilingStack* profilingStack = ...; |
| 32 |
// |
| 33 |
// // For label frames: |
| 34 |
// profilingStack->pushLabelFrame(...); |
| 35 |
// // Execute some code. When finished, pop the frame: |
| 36 |
// profilingStack->pop(); |
| 37 |
// |
| 38 |
// // For JS stack frames: |
| 39 |
// profilingStack->pushJSFrame(...); |
| 40 |
// // Execute some code. When finished, pop the frame: |
| 41 |
// profilingStack->pop(); |
| 42 |
// |
| 43 |
// |
| 44 |
// Concurrency considerations |
| 45 |
// |
| 46 |
// A thread's profiling stack (and the frames inside it) is only modified by |
| 47 |
// that thread. However, the profiling stack can be *read* by a different |
| 48 |
// thread, the sampler thread: Whenever the profiler wants to sample a given |
| 49 |
// thread A, the following happens: |
| 50 |
// (1) Thread A is suspended. |
| 51 |
// (2) The sampler thread (thread S) reads the ProfilingStack of thread A, |
| 52 |
// including all ProfilingStackFrames that are currently in that stack |
| 53 |
// (profilingStack->frames[0..profilingStack->stackSize()]). |
| 54 |
// (3) Thread A is resumed. |
| 55 |
// |
| 56 |
// Thread suspension is achieved using platform-specific APIs; refer to each |
| 57 |
// platform's Sampler::SuspendAndSampleAndResumeThread implementation in |
| 58 |
// platform-*.cpp for details. |
| 59 |
// |
| 60 |
// When the thread is suspended, the values in profilingStack->stackPointer and |
| 61 |
// in the stack frame range |
| 62 |
// profilingStack->frames[0..profilingStack->stackPointer] need to be in a |
| 63 |
// consistent state, so that thread S does not read partially- constructed stack |
| 64 |
// frames. More specifically, we have two requirements: |
| 65 |
// (1) When adding a new frame at the top of the stack, its ProfilingStackFrame |
| 66 |
// data needs to be put in place *before* the stackPointer is incremented, |
| 67 |
// and the compiler + CPU need to know that this order matters. |
| 68 |
// (2) When popping an frame from the stack and then preparing the |
| 69 |
// ProfilingStackFrame data for the next frame that is about to be pushed, |
| 70 |
// the decrement of the stackPointer in pop() needs to happen *before* the |
| 71 |
// ProfilingStackFrame for the new frame is being popuplated, and the |
| 72 |
// compiler + CPU need to know that this order matters. |
| 73 |
// |
| 74 |
// We can express the relevance of these orderings in multiple ways. |
| 75 |
// Option A is to make stackPointer an atomic with SequentiallyConsistent |
| 76 |
// memory ordering. This would ensure that no writes in thread A would be |
| 77 |
// reordered across any writes to stackPointer, which satisfies requirements |
| 78 |
// (1) and (2) at the same time. Option A is the simplest. |
| 79 |
// Option B is to use ReleaseAcquire memory ordering both for writes to |
| 80 |
// stackPointer *and* for writes to ProfilingStackFrame fields. Release-stores |
| 81 |
// ensure that all writes that happened *before this write in program order* are |
| 82 |
// not reordered to happen after this write. ReleaseAcquire ordering places no |
| 83 |
// requirements on the ordering of writes that happen *after* this write in |
| 84 |
// program order. |
| 85 |
// Using release-stores for writes to stackPointer expresses requirement (1), |
| 86 |
// and using release-stores for writes to the ProfilingStackFrame fields |
| 87 |
// expresses requirement (2). |
| 88 |
// |
| 89 |
// Option B is more complicated than option A, but has much better performance |
| 90 |
// on x86/64: In a microbenchmark run on a Macbook Pro from 2017, switching |
| 91 |
// from option A to option B reduced the overhead of pushing+popping a |
| 92 |
// ProfilingStackFrame by 10 nanoseconds. |
| 93 |
// On x86/64, release-stores require no explicit hardware barriers or lock |
| 94 |
// instructions. |
| 95 |
// On ARM/64, option B may be slower than option A, because the compiler will |
| 96 |
// generate hardware barriers for every single release-store instead of just |
| 97 |
// for the writes to stackPointer. However, the actual performance impact of |
| 98 |
// this has not yet been measured on ARM, so we're currently using option B |
| 99 |
// everywhere. This is something that we may want to change in the future once |
| 100 |
// we've done measurements. |
| 101 |
|
| 102 |
namespace js { |
| 103 |
|
| 104 |
// A call stack can be specified to the JS engine such that all JS entry/exits |
| 105 |
// to functions push/pop a stack frame to/from the specified stack. |
| 106 |
// |
| 107 |
// For more detailed information, see vm/GeckoProfiler.h. |
| 108 |
// |
| 109 |
class ProfilingStackFrame { |
| 110 |
// A ProfilingStackFrame represents either a label frame or a JS frame. |
| 111 |
|
| 112 |
// WARNING WARNING WARNING |
| 113 |
// |
| 114 |
// All the fields below are Atomic<...,ReleaseAcquire>. This is needed so |
| 115 |
// that writes to these fields are release-writes, which ensures that |
| 116 |
// earlier writes in this thread don't get reordered after the writes to |
| 117 |
// these fields. In particular, the decrement of the stack pointer in |
| 118 |
// ProfilingStack::pop() is a write that *must* happen before the values in |
| 119 |
// this ProfilingStackFrame are changed. Otherwise, the sampler thread might |
| 120 |
// see an inconsistent state where the stack pointer still points to a |
| 121 |
// ProfilingStackFrame which has already been popped off the stack and whose |
| 122 |
// fields have now been partially repopulated with new values. |
| 123 |
// See the "Concurrency considerations" paragraph at the top of this file |
| 124 |
// for more details. |
| 125 |
|
| 126 |
// Descriptive label for this stack frame. Must be a static string! Can be |
| 127 |
// an empty string, but not a null pointer. |
| 128 |
mozilla::Atomic<const char*, mozilla::ReleaseAcquire> label_; |
| 129 |
|
| 130 |
// An additional descriptive string of this frame which is combined with |
| 131 |
// |label_| in profiler output. Need not be (and usually isn't) static. Can |
| 132 |
// be null. |
| 133 |
mozilla::Atomic<const char*, mozilla::ReleaseAcquire> dynamicString_; |
| 134 |
|
| 135 |
// Stack pointer for non-JS stack frames, the script pointer otherwise. |
| 136 |
mozilla::Atomic<void*, mozilla::ReleaseAcquire> spOrScript; |
| 137 |
|
| 138 |
// ID of the JS Realm for JS stack frames. |
| 139 |
// Must not be used on non-JS frames; it'll contain either the default 0, |
| 140 |
// or a leftover value from a previous JS stack frame that was using this |
| 141 |
// ProfilingStackFrame object. |
| 142 |
mozilla::Atomic<uint64_t, mozilla::ReleaseAcquire> realmID_; |
| 143 |
|
| 144 |
// The bytecode offset for JS stack frames. |
| 145 |
// Must not be used on non-JS frames; it'll contain either the default 0, |
| 146 |
// or a leftover value from a previous JS stack frame that was using this |
| 147 |
// ProfilingStackFrame object. |
| 148 |
mozilla::Atomic<int32_t, mozilla::ReleaseAcquire> pcOffsetIfJS_; |
| 149 |
|
| 150 |
// Bits 0...8 hold the Flags. Bits 9...31 hold the category pair. |
| 151 |
mozilla::Atomic<uint32_t, mozilla::ReleaseAcquire> flagsAndCategoryPair_; |
| 152 |
|
| 153 |
static int32_t pcToOffset(JSScript* aScript, jsbytecode* aPc); |
| 154 |
|
| 155 |
public: |
| 156 |
ProfilingStackFrame() = default; |
| 157 |
ProfilingStackFrame& operator=(const ProfilingStackFrame& other) { |
| 158 |
label_ = other.label(); |
| 159 |
dynamicString_ = other.dynamicString(); |
| 160 |
void* spScript = other.spOrScript; |
| 161 |
spOrScript = spScript; |
| 162 |
int32_t offsetIfJS = other.pcOffsetIfJS_; |
| 163 |
pcOffsetIfJS_ = offsetIfJS; |
| 164 |
uint64_t realmID = other.realmID_; |
| 165 |
realmID_ = realmID; |
| 166 |
uint32_t flagsAndCategory = other.flagsAndCategoryPair_; |
| 167 |
flagsAndCategoryPair_ = flagsAndCategory; |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
// Reserve up to 16 bits for flags, and 16 for category pair. |
| 172 |
enum class Flags : uint32_t { |
| 173 |
// The first three flags describe the kind of the frame and are |
| 174 |
// mutually exclusive. (We still give them individual bits for |
| 175 |
// simplicity.) |
| 176 |
|
| 177 |
// A regular label frame. These usually come from AutoProfilerLabel. |
| 178 |
IS_LABEL_FRAME = 1 << 0, |
| 179 |
|
| 180 |
// A special frame indicating the start of a run of JS profiling stack |
| 181 |
// frames. IS_SP_MARKER_FRAME frames are ignored, except for the sp |
| 182 |
// field. These frames are needed to get correct ordering between JS |
| 183 |
// and LABEL frames because JS frames don't carry sp information. |
| 184 |
// SP is short for "stack pointer". |
| 185 |
IS_SP_MARKER_FRAME = 1 << 1, |
| 186 |
|
| 187 |
// A JS frame. |
| 188 |
IS_JS_FRAME = 1 << 2, |
| 189 |
|
| 190 |
// An interpreter JS frame that has OSR-ed into baseline. IS_JS_FRAME |
| 191 |
// frames can have this flag set and unset during their lifetime. |
| 192 |
// JS_OSR frames are ignored. |
| 193 |
JS_OSR = 1 << 3, |
| 194 |
|
| 195 |
// The next three are mutually exclusive. |
| 196 |
// By default, for profiling stack frames that have both a label and a |
| 197 |
// dynamic string, the two strings are combined into one string of the |
| 198 |
// form "<label> <dynamicString>" during JSON serialization. The |
| 199 |
// following flags can be used to change this preset. |
| 200 |
STRING_TEMPLATE_METHOD = 1 << 4, // "<label>.<dynamicString>" |
| 201 |
STRING_TEMPLATE_GETTER = 1 << 5, // "get <label>.<dynamicString>" |
| 202 |
STRING_TEMPLATE_SETTER = 1 << 6, // "set <label>.<dynamicString>" |
| 203 |
|
| 204 |
// If set, causes this stack frame to be marked as "relevantForJS" in |
| 205 |
// the profile JSON, which will make it show up in the "JS only" call |
| 206 |
// tree view. |
| 207 |
RELEVANT_FOR_JS = 1 << 7, |
| 208 |
|
| 209 |
// If set, causes the label on this ProfilingStackFrame to be ignored |
| 210 |
// and to be replaced by the subcategory's label. |
| 211 |
LABEL_DETERMINED_BY_CATEGORY_PAIR = 1 << 8, |
| 212 |
|
| 213 |
// Frame dynamic string does not contain user data. |
| 214 |
NONSENSITIVE = 1 << 9, |
| 215 |
|
| 216 |
// A JS Baseline Interpreter frame. |
| 217 |
IS_BLINTERP_FRAME = 1 << 10, |
| 218 |
|
| 219 |
FLAGS_BITCOUNT = 16, |
| 220 |
FLAGS_MASK = (1 << FLAGS_BITCOUNT) - 1 |
| 221 |
}; |
| 222 |
|
| 223 |
static_assert( |
| 224 |
uint32_t(JS::ProfilingCategoryPair::LAST) <= |
| 225 |
(UINT32_MAX >> uint32_t(Flags::FLAGS_BITCOUNT)), |
| 226 |
"Too many category pairs to fit into u32 with together with the " |
| 227 |
"reserved bits for the flags"); |
| 228 |
|
| 229 |
bool isLabelFrame() const { |
| 230 |
return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::IS_LABEL_FRAME); |
| 231 |
} |
| 232 |
|
| 233 |
bool isNonsensitive() const { |
| 234 |
return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::NONSENSITIVE); |
| 235 |
} |
| 236 |
|
| 237 |
bool isSpMarkerFrame() const { |
| 238 |
return uint32_t(flagsAndCategoryPair_) & |
| 239 |
uint32_t(Flags::IS_SP_MARKER_FRAME); |
| 240 |
} |
| 241 |
|
| 242 |
bool isJsFrame() const { |
| 243 |
return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::IS_JS_FRAME); |
| 244 |
} |
| 245 |
|
| 246 |
bool isJsBlinterpFrame() const { |
| 247 |
return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::IS_BLINTERP_FRAME); |
| 248 |
} |
| 249 |
|
| 250 |
bool isOSRFrame() const { |
| 251 |
return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::JS_OSR); |
| 252 |
} |
| 253 |
|
| 254 |
void setIsOSRFrame(bool isOSR) { |
| 255 |
if (isOSR) { |
| 256 |
flagsAndCategoryPair_ = |
| 257 |
uint32_t(flagsAndCategoryPair_) | uint32_t(Flags::JS_OSR); |
| 258 |
} else { |
| 259 |
flagsAndCategoryPair_ = |
| 260 |
uint32_t(flagsAndCategoryPair_) & ~uint32_t(Flags::JS_OSR); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void setLabelCategory(JS::ProfilingCategoryPair aCategoryPair) { |
| 265 |
MOZ_ASSERT(isLabelFrame()); |
| 266 |
flagsAndCategoryPair_ = |
| 267 |
(uint32_t(aCategoryPair) << uint32_t(Flags::FLAGS_BITCOUNT)) | flags(); |
| 268 |
} |
| 269 |
|
| 270 |
const char* label() const { |
| 271 |
uint32_t flagsAndCategoryPair = flagsAndCategoryPair_; |
| 272 |
if (flagsAndCategoryPair & |
| 273 |
uint32_t(Flags::LABEL_DETERMINED_BY_CATEGORY_PAIR)) { |
| 274 |
auto categoryPair = JS::ProfilingCategoryPair( |
| 275 |
flagsAndCategoryPair >> uint32_t(Flags::FLAGS_BITCOUNT)); |
| 276 |
return JS::GetProfilingCategoryPairInfo(categoryPair).mLabel; |
| 277 |
} |
| 278 |
return label_; |
| 279 |
} |
| 280 |
|
| 281 |
const char* dynamicString() const { return dynamicString_; } |
| 282 |
|
| 283 |
void initLabelFrame(const char* aLabel, const char* aDynamicString, void* sp, |
| 284 |
JS::ProfilingCategoryPair aCategoryPair, |
| 285 |
uint32_t aFlags) { |
| 286 |
label_ = aLabel; |
| 287 |
dynamicString_ = aDynamicString; |
| 288 |
spOrScript = sp; |
| 289 |
// pcOffsetIfJS_ is not set and must not be used on label frames. |
| 290 |
flagsAndCategoryPair_ = |
| 291 |
uint32_t(Flags::IS_LABEL_FRAME) | |
| 292 |
(uint32_t(aCategoryPair) << uint32_t(Flags::FLAGS_BITCOUNT)) | aFlags; |
| 293 |
MOZ_ASSERT(isLabelFrame()); |
| 294 |
} |
| 295 |
|
| 296 |
void initSpMarkerFrame(void* sp) { |
| 297 |
label_ = ""; |
| 298 |
dynamicString_ = nullptr; |
| 299 |
spOrScript = sp; |
| 300 |
// pcOffsetIfJS_ is not set and must not be used on sp marker frames. |
| 301 |
flagsAndCategoryPair_ = uint32_t(Flags::IS_SP_MARKER_FRAME) | |
| 302 |
(uint32_t(JS::ProfilingCategoryPair::OTHER) |
| 303 |
<< uint32_t(Flags::FLAGS_BITCOUNT)); |
| 304 |
MOZ_ASSERT(isSpMarkerFrame()); |
| 305 |
} |
| 306 |
|
| 307 |
template <JS::ProfilingCategoryPair Category, uint32_t ExtraFlags = 0> |
| 308 |
void initJsFrame(const char* aLabel, const char* aDynamicString, |
| 309 |
JSScript* aScript, jsbytecode* aPc, uint64_t aRealmID) { |
| 310 |
label_ = aLabel; |
| 311 |
dynamicString_ = aDynamicString; |
| 312 |
spOrScript = aScript; |
| 313 |
pcOffsetIfJS_ = pcToOffset(aScript, aPc); |
| 314 |
realmID_ = aRealmID; |
| 315 |
flagsAndCategoryPair_ = |
| 316 |
(uint32_t(Category) << uint32_t(Flags::FLAGS_BITCOUNT)) | |
| 317 |
uint32_t(Flags::IS_JS_FRAME) | ExtraFlags; |
| 318 |
MOZ_ASSERT(isJsFrame()); |
| 319 |
} |
| 320 |
|
| 321 |
uint32_t flags() const { |
| 322 |
return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::FLAGS_MASK); |
| 323 |
} |
| 324 |
|
| 325 |
JS::ProfilingCategoryPair categoryPair() const { |
| 326 |
return JS::ProfilingCategoryPair(flagsAndCategoryPair_ >> |
| 327 |
uint32_t(Flags::FLAGS_BITCOUNT)); |
| 328 |
} |
| 329 |
|
| 330 |
uint64_t realmID() const { return realmID_; } |
| 331 |
|
| 332 |
void* stackAddress() const { |
| 333 |
MOZ_ASSERT(!isJsFrame()); |
| 334 |
return spOrScript; |
| 335 |
} |
| 336 |
|
| 337 |
JS_PUBLIC_API JSScript* script() const; |
| 338 |
|
| 339 |
JS_PUBLIC_API JSFunction* function() const; |
| 340 |
|
| 341 |
// Note that the pointer returned might be invalid. |
| 342 |
JSScript* rawScript() const { |
| 343 |
MOZ_ASSERT(isJsFrame()); |
| 344 |
void* script = spOrScript; |
| 345 |
return static_cast<JSScript*>(script); |
| 346 |
} |
| 347 |
|
| 348 |
// We can't know the layout of JSScript, so look in vm/GeckoProfiler.cpp. |
| 349 |
JS_PUBLIC_API jsbytecode* pc() const; |
| 350 |
void setPC(jsbytecode* pc); |
| 351 |
|
| 352 |
void trace(JSTracer* trc); |
| 353 |
|
| 354 |
// The offset of a pc into a script's code can actually be 0, so to |
| 355 |
// signify a nullptr pc, use a -1 index. This is checked against in |
| 356 |
// pc() and setPC() to set/get the right pc. |
| 357 |
static const int32_t NullPCOffset = -1; |
| 358 |
}; |
| 359 |
|
| 360 |
JS_PUBLIC_API void SetContextProfilingStack(JSContext* cx, |
| 361 |
ProfilingStack* profilingStack); |
| 362 |
|
| 363 |
// GetContextProfilingStack also exists, but it's defined in RootingAPI.h. |
| 364 |
|
| 365 |
JS_PUBLIC_API void EnableContextProfilingStack(JSContext* cx, bool enabled); |
| 366 |
|
| 367 |
JS_PUBLIC_API void RegisterContextProfilingEventMarker( |
| 368 |
JSContext* cx, |
| 369 |
void (*mark)(mozilla::MarkerCategory, const char*, const char*), |
| 370 |
void (*interval)(mozilla::MarkerCategory, const char*, mozilla::TimeStamp, |
| 371 |
const char*)); |
| 372 |
|
| 373 |
} // namespace js |
| 374 |
|
| 375 |
namespace JS { |
| 376 |
|
| 377 |
typedef ProfilingStack* (*RegisterThreadCallback)(const char* threadName, |
| 378 |
void* stackBase); |
| 379 |
|
| 380 |
typedef void (*UnregisterThreadCallback)(); |
| 381 |
|
| 382 |
// regiserThread and unregisterThread callbacks are functions which are called |
| 383 |
// by other threads without any locking mechanism. |
| 384 |
JS_PUBLIC_API void SetProfilingThreadCallbacks( |
| 385 |
RegisterThreadCallback registerThread, |
| 386 |
UnregisterThreadCallback unregisterThread); |
| 387 |
|
| 388 |
} // namespace JS |
| 389 |
|
| 390 |
// Each thread has its own ProfilingStack. That thread modifies the |
| 391 |
// ProfilingStack, pushing and popping elements as necessary. |
| 392 |
// |
| 393 |
// The ProfilingStack is also read periodically by the profiler's sampler |
| 394 |
// thread. This happens only when the thread that owns the ProfilingStack is |
| 395 |
// suspended. So there are no genuine parallel accesses. |
| 396 |
// |
| 397 |
// However, it is possible for pushing/popping to be interrupted by a periodic |
| 398 |
// sample. Because of this, we need pushing/popping to be effectively atomic. |
| 399 |
// |
| 400 |
// - When pushing a new frame, we increment the stack pointer -- making the new |
| 401 |
// frame visible to the sampler thread -- only after the new frame has been |
| 402 |
// fully written. The stack pointer is Atomic<uint32_t,ReleaseAcquire>, so |
| 403 |
// the increment is a release-store, which ensures that this store is not |
| 404 |
// reordered before the writes of the frame. |
| 405 |
// |
| 406 |
// - When popping an old frame, the only operation is the decrementing of the |
| 407 |
// stack pointer, which is obviously atomic. |
| 408 |
// |
| 409 |
class JS_PUBLIC_API ProfilingStack final { |
| 410 |
public: |
| 411 |
ProfilingStack() = default; |
| 412 |
|
| 413 |
~ProfilingStack(); |
| 414 |
|
| 415 |
void pushLabelFrame(const char* label, const char* dynamicString, void* sp, |
| 416 |
JS::ProfilingCategoryPair categoryPair, |
| 417 |
uint32_t flags = 0) { |
| 418 |
// This thread is the only one that ever changes the value of |
| 419 |
// stackPointer. |
| 420 |
// Store the value of the atomic in a non-atomic local variable so that |
| 421 |
// the compiler won't generate two separate loads from the atomic for |
| 422 |
// the size check and the frames[] array indexing operation. |
| 423 |
uint32_t stackPointerVal = stackPointer; |
| 424 |
|
| 425 |
if (MOZ_UNLIKELY(stackPointerVal >= capacity)) { |
| 426 |
ensureCapacitySlow(); |
| 427 |
} |
| 428 |
frames[stackPointerVal].initLabelFrame(label, dynamicString, sp, |
| 429 |
categoryPair, flags); |
| 430 |
|
| 431 |
// This must happen at the end! The compiler will not reorder this |
| 432 |
// update because stackPointer is Atomic<..., ReleaseAcquire>, so any |
| 433 |
// the writes above will not be reordered below the stackPointer store. |
| 434 |
// Do the read and the write as two separate statements, in order to |
| 435 |
// make it clear that we don't need an atomic increment, which would be |
| 436 |
// more expensive on x86 than the separate operations done here. |
| 437 |
// However, don't use stackPointerVal here; instead, allow the compiler |
| 438 |
// to turn this store into a non-atomic increment instruction which |
| 439 |
// takes up less code size. |
| 440 |
stackPointer = stackPointer + 1; |
| 441 |
} |
| 442 |
|
| 443 |
void pushSpMarkerFrame(void* sp) { |
| 444 |
uint32_t oldStackPointer = stackPointer; |
| 445 |
|
| 446 |
if (MOZ_UNLIKELY(oldStackPointer >= capacity)) { |
| 447 |
ensureCapacitySlow(); |
| 448 |
} |
| 449 |
frames[oldStackPointer].initSpMarkerFrame(sp); |
| 450 |
|
| 451 |
// This must happen at the end, see the comment in pushLabelFrame. |
| 452 |
stackPointer = oldStackPointer + 1; |
| 453 |
} |
| 454 |
|
| 455 |
void pushJsFrame(const char* label, const char* dynamicString, |
| 456 |
JSScript* script, jsbytecode* pc, uint64_t aRealmID) { |
| 457 |
// This thread is the only one that ever changes the value of |
| 458 |
// stackPointer. Only load the atomic once. |
| 459 |
uint32_t oldStackPointer = stackPointer; |
| 460 |
|
| 461 |
if (MOZ_UNLIKELY(oldStackPointer >= capacity)) { |
| 462 |
ensureCapacitySlow(); |
| 463 |
} |
| 464 |
frames[oldStackPointer] |
| 465 |
.initJsFrame<JS::ProfilingCategoryPair::JS_Interpreter>( |
| 466 |
label, dynamicString, script, pc, aRealmID); |
| 467 |
|
| 468 |
// This must happen at the end, see the comment in pushLabelFrame. |
| 469 |
stackPointer = stackPointer + 1; |
| 470 |
} |
| 471 |
|
| 472 |
void pop() { |
| 473 |
MOZ_ASSERT(stackPointer > 0); |
| 474 |
// Do the read and the write as two separate statements, in order to |
| 475 |
// make it clear that we don't need an atomic decrement, which would be |
| 476 |
// more expensive on x86 than the separate operations done here. |
| 477 |
// This thread is the only one that ever changes the value of |
| 478 |
// stackPointer. |
| 479 |
uint32_t oldStackPointer = stackPointer; |
| 480 |
stackPointer = oldStackPointer - 1; |
| 481 |
} |
| 482 |
|
| 483 |
uint32_t stackSize() const { return stackPointer; } |
| 484 |
uint32_t stackCapacity() const { return capacity; } |
| 485 |
|
| 486 |
private: |
| 487 |
// Out of line path for expanding the buffer, since otherwise this would get |
| 488 |
// inlined in every DOM WebIDL call. |
| 489 |
MOZ_COLD void ensureCapacitySlow(); |
| 490 |
|
| 491 |
// No copying. |
| 492 |
ProfilingStack(const ProfilingStack&) = delete; |
| 493 |
void operator=(const ProfilingStack&) = delete; |
| 494 |
|
| 495 |
// No moving either. |
| 496 |
ProfilingStack(ProfilingStack&&) = delete; |
| 497 |
void operator=(ProfilingStack&&) = delete; |
| 498 |
|
| 499 |
uint32_t capacity = 0; |
| 500 |
|
| 501 |
public: |
| 502 |
// The pointer to the stack frames, this is read from the profiler thread and |
| 503 |
// written from the current thread. |
| 504 |
// |
| 505 |
// This is effectively a unique pointer. |
| 506 |
mozilla::Atomic<js::ProfilingStackFrame*, mozilla::SequentiallyConsistent> |
| 507 |
frames{nullptr}; |
| 508 |
|
| 509 |
// This may exceed the capacity, so instead use the stackSize() method to |
| 510 |
// determine the number of valid frames in stackFrames. When this is less |
| 511 |
// than stackCapacity(), it refers to the first free stackframe past the top |
| 512 |
// of the in-use stack (i.e. frames[stackPointer - 1] is the top stack |
| 513 |
// frame). |
| 514 |
// |
| 515 |
// WARNING WARNING WARNING |
| 516 |
// |
| 517 |
// This is an atomic variable that uses ReleaseAcquire memory ordering. |
| 518 |
// See the "Concurrency considerations" paragraph at the top of this file |
| 519 |
// for more details. |
| 520 |
mozilla::Atomic<uint32_t, mozilla::ReleaseAcquire> stackPointer{0}; |
| 521 |
}; |
| 522 |
|
| 523 |
namespace js { |
| 524 |
|
| 525 |
class AutoGeckoProfilerEntry; |
| 526 |
class GeckoProfilerEntryMarker; |
| 527 |
class GeckoProfilerBaselineOSRMarker; |
| 528 |
|
| 529 |
class GeckoProfilerThread { |
| 530 |
friend class AutoGeckoProfilerEntry; |
| 531 |
friend class GeckoProfilerEntryMarker; |
| 532 |
friend class GeckoProfilerBaselineOSRMarker; |
| 533 |
|
| 534 |
ProfilingStack* profilingStack_; |
| 535 |
|
| 536 |
// Same as profilingStack_ if the profiler is currently active, otherwise |
| 537 |
// null. |
| 538 |
ProfilingStack* profilingStackIfEnabled_; |
| 539 |
|
| 540 |
public: |
| 541 |
GeckoProfilerThread(); |
| 542 |
|
| 543 |
uint32_t stackPointer() { |
| 544 |
MOZ_ASSERT(infraInstalled()); |
| 545 |
return profilingStack_->stackPointer; |
| 546 |
} |
| 547 |
ProfilingStackFrame* stack() { return profilingStack_->frames; } |
| 548 |
ProfilingStack* getProfilingStack() { return profilingStack_; } |
| 549 |
ProfilingStack* getProfilingStackIfEnabled() { |
| 550 |
return profilingStackIfEnabled_; |
| 551 |
} |
| 552 |
|
| 553 |
/* |
| 554 |
* True if the profiler infrastructure is setup. Should be true in builds |
| 555 |
* that include profiler support except during early startup or late |
| 556 |
* shutdown. Unrelated to the presence of the Gecko Profiler addon. |
| 557 |
*/ |
| 558 |
bool infraInstalled() { return profilingStack_ != nullptr; } |
| 559 |
|
| 560 |
void setProfilingStack(ProfilingStack* profilingStack, bool enabled); |
| 561 |
void enable(bool enable) { |
| 562 |
profilingStackIfEnabled_ = enable ? profilingStack_ : nullptr; |
| 563 |
} |
| 564 |
void trace(JSTracer* trc); |
| 565 |
|
| 566 |
/* |
| 567 |
* Functions which are the actual instrumentation to track run information |
| 568 |
* |
| 569 |
* - enter: a function has started to execute |
| 570 |
* - updatePC: updates the pc information about where a function |
| 571 |
* is currently executing |
| 572 |
* - exit: this function has ceased execution, and no further |
| 573 |
* entries/exits will be made |
| 574 |
*/ |
| 575 |
bool enter(JSContext* cx, JSScript* script); |
| 576 |
void exit(JSContext* cx, JSScript* script); |
| 577 |
inline void updatePC(JSContext* cx, JSScript* script, jsbytecode* pc); |
| 578 |
}; |
| 579 |
|
| 580 |
} // namespace js |
| 581 |
|
| 582 |
#endif /* js_ProfilingStack_h */ |