| 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 vm_GeckoProfiler_h |
| 8 |
#define vm_GeckoProfiler_h |
| 9 |
|
| 10 |
#include "mozilla/Attributes.h" |
| 11 |
#include "mozilla/BaseProfilerMarkersPrerequisites.h" |
| 12 |
#include "mozilla/DebugOnly.h" |
| 13 |
#include "mozilla/TimeStamp.h" |
| 14 |
|
| 15 |
#include <stddef.h> |
| 16 |
#include <stdint.h> |
| 17 |
|
| 18 |
#include "jspubtd.h" |
| 19 |
|
| 20 |
#include "js/AllocPolicy.h" |
| 21 |
#include "js/HashTable.h" |
| 22 |
#include "js/ProfilingCategory.h" |
| 23 |
#include "js/TypeDecls.h" |
| 24 |
#include "js/Utility.h" |
| 25 |
#include "threading/ProtectedData.h" |
| 26 |
|
| 27 |
/* |
| 28 |
* Gecko Profiler integration with the JS Engine |
| 29 |
* https://developer.mozilla.org/en/Performance/Profiling_with_the_Built-in_Profiler |
| 30 |
* |
| 31 |
* The Gecko Profiler (found in tools/profiler) is an implementation of a |
| 32 |
* profiler which has the ability to walk the C++ stack as well as use |
| 33 |
* instrumentation to gather information. When dealing with JS, however, the |
| 34 |
* profiler needs integration with the engine because otherwise it is very |
| 35 |
* difficult to figure out what javascript is executing. |
| 36 |
* |
| 37 |
* The current method of integration with the profiler is a form of |
| 38 |
* instrumentation: every time a JS function is entered, a bit of information |
| 39 |
* is pushed onto a stack that the profiler owns and maintains. This |
| 40 |
* information is then popped at the end of the JS function. The profiler |
| 41 |
* informs the JS engine of this stack at runtime, and it can by turned on/off |
| 42 |
* dynamically. Each stack frame has type ProfilingStackFrame. |
| 43 |
* |
| 44 |
* Throughout execution, the size of the stack recorded in memory may exceed the |
| 45 |
* maximum. The JS engine will not write any information past the maximum limit, |
| 46 |
* but it will still maintain the size of the stack. Profiler code is aware of |
| 47 |
* this and iterates the stack accordingly. |
| 48 |
* |
| 49 |
* There is some information pushed on the profiler stack for every JS function |
| 50 |
* that is entered. First is a char* label with a description of what function |
| 51 |
* was entered. Currently this string is of the form "function (file:line)" if |
| 52 |
* there's a function name, or just "file:line" if there's no function name |
| 53 |
* available. The other bit of information is the relevant C++ (native) stack |
| 54 |
* pointer. This stack pointer is what enables the interleaving of the C++ and |
| 55 |
* the JS stack. Finally, throughout execution of the function, some extra |
| 56 |
* information may be updated on the ProfilingStackFrame structure. |
| 57 |
* |
| 58 |
* = Profile Strings |
| 59 |
* |
| 60 |
* The profile strings' allocations and deallocation must be carefully |
| 61 |
* maintained, and ideally at a very low overhead cost. For this reason, the JS |
| 62 |
* engine maintains a mapping of all known profile strings. These strings are |
| 63 |
* keyed in lookup by a JSScript*, but are serialized with a JSFunction*, |
| 64 |
* JSScript* pair. A JSScript will destroy its corresponding profile string when |
| 65 |
* the script is finalized. |
| 66 |
* |
| 67 |
* For this reason, a char* pointer pushed on the profiler stack is valid only |
| 68 |
* while it is on the profiler stack. The profiler uses sampling to read off |
| 69 |
* information from this instrumented stack, and it therefore copies the string |
| 70 |
* byte for byte when a JS function is encountered during sampling. |
| 71 |
* |
| 72 |
* = Native Stack Pointer |
| 73 |
* |
| 74 |
* The actual value pushed as the native pointer is nullptr for most JS |
| 75 |
* functions. The reason for this is that there's actually very little |
| 76 |
* correlation between the JS stack and the C++ stack because many JS functions |
| 77 |
* all run in the same C++ frame, or can even go backwards in C++ when going |
| 78 |
* from the JIT back to the interpreter. |
| 79 |
* |
| 80 |
* To alleviate this problem, all JS functions push nullptr as their "native |
| 81 |
* stack pointer" to indicate that it's a JS function call. The function |
| 82 |
* RunScript(), however, pushes an actual C++ stack pointer onto the profiler |
| 83 |
* stack. This way when interleaving C++ and JS, if the Gecko Profiler sees a |
| 84 |
* nullptr native stack pointer on the profiler stack, it looks backwards for |
| 85 |
* the first non-nullptr pointer and uses that for all subsequent nullptr |
| 86 |
* native stack pointers. |
| 87 |
* |
| 88 |
* = Line Numbers |
| 89 |
* |
| 90 |
* One goal of sampling is to get both a backtrace of the JS stack, but also |
| 91 |
* know where within each function on the stack execution currently is. For |
| 92 |
* this, each ProfilingStackFrame has a 'pc' field to tell where its execution |
| 93 |
* currently is. This field is updated whenever a call is made to another JS |
| 94 |
* function, and for the JIT it is also updated whenever the JIT is left. |
| 95 |
* |
| 96 |
* This field is in a union with a uint32_t 'line' so that C++ can make use of |
| 97 |
* the field as well. It was observed that tracking 'line' via PCToLineNumber in |
| 98 |
* JS was far too expensive, so that is why the pc instead of the translated |
| 99 |
* line number is stored. |
| 100 |
* |
| 101 |
* As an invariant, if the pc is nullptr, then the JIT is currently executing |
| 102 |
* generated code. Otherwise execution is in another JS function or in C++. With |
| 103 |
* this in place, only the top frame of the stack can ever have nullptr as its |
| 104 |
* pc. Additionally with this invariant, it is possible to maintain mappings of |
| 105 |
* JIT code to pc which can be accessed safely because they will only be |
| 106 |
* accessed from a signal handler when the JIT code is executing. |
| 107 |
*/ |
| 108 |
|
| 109 |
class JS_PUBLIC_API ProfilingStack; |
| 110 |
|
| 111 |
namespace js { |
| 112 |
|
| 113 |
class BaseScript; |
| 114 |
class GeckoProfilerThread; |
| 115 |
|
| 116 |
// The `ProfileStringMap` weakly holds its `BaseScript*` keys and owns its |
| 117 |
// string values. Entries are removed when the `BaseScript` is finalized; see |
| 118 |
// `GeckoProfiler::onScriptFinalized`. |
| 119 |
using ProfileStringMap = HashMap<BaseScript*, JS::UniqueChars, |
| 120 |
DefaultHasher<BaseScript*>, SystemAllocPolicy>; |
| 121 |
|
| 122 |
class GeckoProfilerRuntime { |
| 123 |
JSRuntime* rt; |
| 124 |
MainThreadData<ProfileStringMap> strings_; |
| 125 |
bool slowAssertions; |
| 126 |
uint32_t enabled_; |
| 127 |
void (*eventMarker_)(mozilla::MarkerCategory, const char*, const char*); |
| 128 |
void (*intervalMarker_)(mozilla::MarkerCategory, const char*, |
| 129 |
mozilla::TimeStamp, const char*); |
| 130 |
|
| 131 |
public: |
| 132 |
explicit GeckoProfilerRuntime(JSRuntime* rt); |
| 133 |
|
| 134 |
/* management of whether instrumentation is on or off */ |
| 135 |
bool enabled() const { return enabled_; } |
| 136 |
void enable(bool enabled); |
| 137 |
void enableSlowAssertions(bool enabled) { slowAssertions = enabled; } |
| 138 |
bool slowAssertionsEnabled() { return slowAssertions; } |
| 139 |
|
| 140 |
void setEventMarker(void (*fn)(mozilla::MarkerCategory, const char*, |
| 141 |
const char*)); |
| 142 |
void setIntervalMarker(void (*fn)(mozilla::MarkerCategory, const char*, |
| 143 |
mozilla::TimeStamp, const char*)); |
| 144 |
|
| 145 |
static JS::UniqueChars allocProfileString(JSContext* cx, BaseScript* script); |
| 146 |
const char* profileString(JSContext* cx, BaseScript* script); |
| 147 |
|
| 148 |
void onScriptFinalized(BaseScript* script); |
| 149 |
|
| 150 |
void markEvent( |
| 151 |
const char* event, const char* details, |
| 152 |
JS::ProfilingCategoryPair jsPair = JS::ProfilingCategoryPair::JS); |
| 153 |
|
| 154 |
void markInterval( |
| 155 |
const char* event, mozilla::TimeStamp start, const char* details, |
| 156 |
JS::ProfilingCategoryPair jsPair = JS::ProfilingCategoryPair::JS); |
| 157 |
|
| 158 |
ProfileStringMap& strings() { return strings_.ref(); } |
| 159 |
|
| 160 |
/* meant to be used for testing, not recommended to call in normal code */ |
| 161 |
size_t stringsCount(); |
| 162 |
void stringsReset(); |
| 163 |
|
| 164 |
const uint32_t* addressOfEnabled() const { return &enabled_; } |
| 165 |
|
| 166 |
void fixupStringsMapAfterMovingGC(); |
| 167 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 168 |
void checkStringsMapAfterMovingGC(); |
| 169 |
#endif |
| 170 |
}; |
| 171 |
|
| 172 |
inline size_t GeckoProfilerRuntime::stringsCount() { return strings().count(); } |
| 173 |
|
| 174 |
inline void GeckoProfilerRuntime::stringsReset() { strings().clear(); } |
| 175 |
|
| 176 |
/* |
| 177 |
* This class is used in RunScript() to push the marker onto the sampling stack |
| 178 |
* that we're about to enter JS function calls. This is the only time in which a |
| 179 |
* valid stack pointer is pushed to the sampling stack. |
| 180 |
*/ |
| 181 |
class MOZ_RAII GeckoProfilerEntryMarker { |
| 182 |
public: |
| 183 |
explicit MOZ_ALWAYS_INLINE GeckoProfilerEntryMarker(JSContext* cx, |
| 184 |
JSScript* script); |
| 185 |
MOZ_ALWAYS_INLINE ~GeckoProfilerEntryMarker(); |
| 186 |
|
| 187 |
private: |
| 188 |
GeckoProfilerThread* profiler_; |
| 189 |
#ifdef DEBUG |
| 190 |
uint32_t spBefore_; |
| 191 |
#endif |
| 192 |
}; |
| 193 |
|
| 194 |
/* |
| 195 |
* RAII class to automatically add Gecko Profiler profiling stack frames. |
| 196 |
* It retrieves the ProfilingStack from the JSContext and does nothing if the |
| 197 |
* profiler is inactive. |
| 198 |
* |
| 199 |
* NB: The `label` string must be statically allocated. |
| 200 |
*/ |
| 201 |
class MOZ_RAII AutoGeckoProfilerEntry { |
| 202 |
public: |
| 203 |
explicit MOZ_ALWAYS_INLINE AutoGeckoProfilerEntry( |
| 204 |
JSContext* cx, const char* label, const char* dynamicString, |
| 205 |
JS::ProfilingCategoryPair categoryPair = JS::ProfilingCategoryPair::JS, |
| 206 |
uint32_t flags = 0); |
| 207 |
explicit MOZ_ALWAYS_INLINE AutoGeckoProfilerEntry( |
| 208 |
JSContext* cx, const char* label, |
| 209 |
JS::ProfilingCategoryPair categoryPair = JS::ProfilingCategoryPair::JS, |
| 210 |
uint32_t flags = 0); |
| 211 |
MOZ_ALWAYS_INLINE ~AutoGeckoProfilerEntry(); |
| 212 |
|
| 213 |
private: |
| 214 |
ProfilingStack* profilingStack_; |
| 215 |
#ifdef DEBUG |
| 216 |
GeckoProfilerThread* profiler_; |
| 217 |
uint32_t spBefore_; |
| 218 |
#endif |
| 219 |
}; |
| 220 |
|
| 221 |
/* |
| 222 |
* Use this RAII class to add Gecko Profiler label frames for methods of the |
| 223 |
* JavaScript builtin API. |
| 224 |
* These frames will be exposed to JavaScript developers (ie they won't be |
| 225 |
* filtered out when using the "JavaScript" filtering option in the Firefox |
| 226 |
* Profiler UI). |
| 227 |
* Technical note: the label and dynamicString values will be joined with a dot |
| 228 |
* separator if dynamicString is present. |
| 229 |
*/ |
| 230 |
class MOZ_RAII AutoJSMethodProfilerEntry : public AutoGeckoProfilerEntry { |
| 231 |
public: |
| 232 |
explicit MOZ_ALWAYS_INLINE AutoJSMethodProfilerEntry( |
| 233 |
JSContext* cx, const char* label, const char* dynamicString = nullptr); |
| 234 |
}; |
| 235 |
|
| 236 |
/* |
| 237 |
* Use this RAII class to add Gecko Profiler label frames for constructors of |
| 238 |
* the JavaScript builtin API. |
| 239 |
* These frames will be exposed to JavaScript developers (ie they won't be |
| 240 |
* filtered out when using the "JavaScript" filtering option in the Firefox |
| 241 |
* Profiler UI). |
| 242 |
* Technical note: the word "constructor" will be appended to the label (with a |
| 243 |
* space separator). |
| 244 |
*/ |
| 245 |
class MOZ_RAII AutoJSConstructorProfilerEntry : public AutoGeckoProfilerEntry { |
| 246 |
public: |
| 247 |
explicit MOZ_ALWAYS_INLINE AutoJSConstructorProfilerEntry(JSContext* cx, |
| 248 |
const char* label); |
| 249 |
}; |
| 250 |
|
| 251 |
/* |
| 252 |
* This class is used in the interpreter to bound regions where the baseline JIT |
| 253 |
* being entered via OSR. It marks the current top profiling stack frame as |
| 254 |
* OSR-ed |
| 255 |
*/ |
| 256 |
class MOZ_RAII GeckoProfilerBaselineOSRMarker { |
| 257 |
public: |
| 258 |
explicit GeckoProfilerBaselineOSRMarker(JSContext* cx, bool hasProfilerFrame); |
| 259 |
~GeckoProfilerBaselineOSRMarker(); |
| 260 |
|
| 261 |
private: |
| 262 |
GeckoProfilerThread* profiler; |
| 263 |
mozilla::DebugOnly<uint32_t> spBefore_; |
| 264 |
}; |
| 265 |
|
| 266 |
} /* namespace js */ |
| 267 |
|
| 268 |
#endif /* vm_GeckoProfiler_h */ |