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