| 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 |
/* |
| 8 |
* JS engine garbage collector API. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef gc_GC_h |
| 12 |
#define gc_GC_h |
| 13 |
|
| 14 |
#include "gc/AllocKind.h" |
| 15 |
#include "gc/GCEnum.h" |
| 16 |
#include "js/GCAPI.h" |
| 17 |
#include "js/HeapAPI.h" |
| 18 |
#include "js/RealmIterators.h" |
| 19 |
#include "js/TraceKind.h" |
| 20 |
|
| 21 |
class JSTracer; |
| 22 |
|
| 23 |
namespace JS { |
| 24 |
class RealmOptions; |
| 25 |
} |
| 26 |
|
| 27 |
namespace js { |
| 28 |
|
| 29 |
class Nursery; |
| 30 |
|
| 31 |
namespace gc { |
| 32 |
|
| 33 |
class Arena; |
| 34 |
class ArenaChunk; |
| 35 |
|
| 36 |
} /* namespace gc */ |
| 37 |
|
| 38 |
// Define name, key and writability for the GC parameters. |
| 39 |
#define FOR_EACH_GC_PARAM(_) \ |
| 40 |
_("maxBytes", JSGC_MAX_BYTES, true) \ |
| 41 |
_("minNurseryBytes", JSGC_MIN_NURSERY_BYTES, true) \ |
| 42 |
_("maxNurseryBytes", JSGC_MAX_NURSERY_BYTES, true) \ |
| 43 |
_("gcBytes", JSGC_BYTES, false) \ |
| 44 |
_("nurseryBytes", JSGC_NURSERY_BYTES, false) \ |
| 45 |
_("gcNumber", JSGC_NUMBER, false) \ |
| 46 |
_("majorGCNumber", JSGC_MAJOR_GC_NUMBER, false) \ |
| 47 |
_("minorGCNumber", JSGC_MINOR_GC_NUMBER, false) \ |
| 48 |
_("sliceNumber", JSGC_SLICE_NUMBER, false) \ |
| 49 |
_("incrementalGCEnabled", JSGC_INCREMENTAL_GC_ENABLED, true) \ |
| 50 |
_("perZoneGCEnabled", JSGC_PER_ZONE_GC_ENABLED, true) \ |
| 51 |
_("unusedChunks", JSGC_UNUSED_CHUNKS, false) \ |
| 52 |
_("totalChunks", JSGC_TOTAL_CHUNKS, false) \ |
| 53 |
_("sliceTimeBudgetMS", JSGC_SLICE_TIME_BUDGET_MS, true) \ |
| 54 |
_("highFrequencyTimeLimit", JSGC_HIGH_FREQUENCY_TIME_LIMIT, true) \ |
| 55 |
_("smallHeapSizeMax", JSGC_SMALL_HEAP_SIZE_MAX, true) \ |
| 56 |
_("largeHeapSizeMin", JSGC_LARGE_HEAP_SIZE_MIN, true) \ |
| 57 |
_("highFrequencySmallHeapGrowth", JSGC_HIGH_FREQUENCY_SMALL_HEAP_GROWTH, \ |
| 58 |
true) \ |
| 59 |
_("highFrequencyLargeHeapGrowth", JSGC_HIGH_FREQUENCY_LARGE_HEAP_GROWTH, \ |
| 60 |
true) \ |
| 61 |
_("lowFrequencyHeapGrowth", JSGC_LOW_FREQUENCY_HEAP_GROWTH, true) \ |
| 62 |
_("balancedHeapLimitsEnabled", JSGC_BALANCED_HEAP_LIMITS_ENABLED, true) \ |
| 63 |
_("heapGrowthFactor", JSGC_HEAP_GROWTH_FACTOR, true) \ |
| 64 |
_("allocationThreshold", JSGC_ALLOCATION_THRESHOLD, true) \ |
| 65 |
_("smallHeapIncrementalLimit", JSGC_SMALL_HEAP_INCREMENTAL_LIMIT, true) \ |
| 66 |
_("largeHeapIncrementalLimit", JSGC_LARGE_HEAP_INCREMENTAL_LIMIT, true) \ |
| 67 |
_("minEmptyChunkCount", JSGC_MIN_EMPTY_CHUNK_COUNT, true) \ |
| 68 |
_("compactingEnabled", JSGC_COMPACTING_ENABLED, true) \ |
| 69 |
_("nurseryEnabled", JSGC_NURSERY_ENABLED, true) \ |
| 70 |
_("parallelMarkingEnabled", JSGC_PARALLEL_MARKING_ENABLED, true) \ |
| 71 |
_("parallelMarkingThresholdMB", JSGC_PARALLEL_MARKING_THRESHOLD_MB, true) \ |
| 72 |
_("minLastDitchGCPeriod", JSGC_MIN_LAST_DITCH_GC_PERIOD, true) \ |
| 73 |
_("nurseryEagerCollectionThresholdKB", \ |
| 74 |
JSGC_NURSERY_EAGER_COLLECTION_THRESHOLD_KB, true) \ |
| 75 |
_("nurseryEagerCollectionThresholdPercent", \ |
| 76 |
JSGC_NURSERY_EAGER_COLLECTION_THRESHOLD_PERCENT, true) \ |
| 77 |
_("nurseryEagerCollectionTimeoutMS", \ |
| 78 |
JSGC_NURSERY_EAGER_COLLECTION_TIMEOUT_MS, true) \ |
| 79 |
_("nurseryMaxTimeGoalMS", JSGC_NURSERY_MAX_TIME_GOAL_MS, true) \ |
| 80 |
_("zoneAllocDelayKB", JSGC_ZONE_ALLOC_DELAY_KB, true) \ |
| 81 |
_("mallocThresholdBase", JSGC_MALLOC_THRESHOLD_BASE, true) \ |
| 82 |
_("urgentThreshold", JSGC_URGENT_THRESHOLD_MB, true) \ |
| 83 |
_("chunkBytes", JSGC_CHUNK_BYTES, false) \ |
| 84 |
_("helperThreadRatio", JSGC_HELPER_THREAD_RATIO, true) \ |
| 85 |
_("maxHelperThreads", JSGC_MAX_HELPER_THREADS, true) \ |
| 86 |
_("helperThreadCount", JSGC_HELPER_THREAD_COUNT, false) \ |
| 87 |
_("maxMarkingThreads", JSGC_MAX_MARKING_THREADS, true) \ |
| 88 |
_("markingThreadCount", JSGC_MARKING_THREAD_COUNT, false) \ |
| 89 |
_("systemPageSizeKB", JSGC_SYSTEM_PAGE_SIZE_KB, false) \ |
| 90 |
_("semispaceNurseryEnabled", JSGC_SEMISPACE_NURSERY_ENABLED, true) \ |
| 91 |
_("generateMissingAllocSites", JSGC_GENERATE_MISSING_ALLOC_SITES, true) \ |
| 92 |
_("highFrequencyMode", JSGC_HIGH_FREQUENCY_MODE, false) |
| 93 |
|
| 94 |
// Get the key and writability give a GC parameter name. |
| 95 |
extern bool GetGCParameterInfo(const char* name, JSGCParamKey* keyOut, |
| 96 |
bool* writableOut); |
| 97 |
|
| 98 |
extern void TraceRuntime(JSTracer* trc); |
| 99 |
|
| 100 |
// Trace roots but don't evict the nursery first; used from DumpHeap. |
| 101 |
extern void TraceRuntimeWithoutEviction(JSTracer* trc); |
| 102 |
|
| 103 |
extern void ReleaseAllJITCode(JS::GCContext* gcx); |
| 104 |
|
| 105 |
extern void PrepareForDebugGC(JSRuntime* rt); |
| 106 |
|
| 107 |
/* Functions for managing cross compartment gray pointers. */ |
| 108 |
|
| 109 |
extern void NotifyGCNukeWrapper(JSContext* cx, JSObject* wrapper); |
| 110 |
|
| 111 |
extern unsigned NotifyGCPreSwap(JSObject* a, JSObject* b); |
| 112 |
|
| 113 |
extern void NotifyGCPostSwap(JSObject* a, JSObject* b, unsigned removedFlags); |
| 114 |
|
| 115 |
using IterateChunkCallback = void (*)(JSRuntime*, void*, gc::ArenaChunk*, |
| 116 |
const JS::AutoRequireNoGC&); |
| 117 |
using IterateZoneCallback = void (*)(JSRuntime*, void*, JS::Zone*, |
| 118 |
const JS::AutoRequireNoGC&); |
| 119 |
using IterateArenaCallback = void (*)(JSRuntime*, void*, gc::Arena*, |
| 120 |
JS::TraceKind, size_t, |
| 121 |
const JS::AutoRequireNoGC&); |
| 122 |
using IterateCellCallback = void (*)(JSRuntime*, void*, JS::GCCellPtr, size_t, |
| 123 |
const JS::AutoRequireNoGC&); |
| 124 |
|
| 125 |
/* |
| 126 |
* This function calls |zoneCallback| on every zone, |realmCallback| on |
| 127 |
* every realm, |arenaCallback| on every in-use arena, and |cellCallback| |
| 128 |
* on every in-use cell in the GC heap. |
| 129 |
* |
| 130 |
* Note that no read barrier is triggered on the cells passed to cellCallback, |
| 131 |
* so no these pointers must not escape the callback. |
| 132 |
*/ |
| 133 |
extern void IterateHeapUnbarriered(JSContext* cx, void* data, |
| 134 |
IterateZoneCallback zoneCallback, |
| 135 |
JS::IterateRealmCallback realmCallback, |
| 136 |
IterateArenaCallback arenaCallback, |
| 137 |
IterateCellCallback cellCallback); |
| 138 |
|
| 139 |
/* |
| 140 |
* This function is like IterateHeapUnbarriered, but does it for a single zone. |
| 141 |
*/ |
| 142 |
extern void IterateHeapUnbarrieredForZone( |
| 143 |
JSContext* cx, JS::Zone* zone, void* data, IterateZoneCallback zoneCallback, |
| 144 |
JS::IterateRealmCallback realmCallback, IterateArenaCallback arenaCallback, |
| 145 |
IterateCellCallback cellCallback); |
| 146 |
|
| 147 |
/* |
| 148 |
* Invoke chunkCallback on every in-use chunk. |
| 149 |
*/ |
| 150 |
extern void IterateChunks(JSContext* cx, void* data, |
| 151 |
IterateChunkCallback chunkCallback); |
| 152 |
|
| 153 |
using IterateScriptCallback = void (*)(JSRuntime*, void*, BaseScript*, |
| 154 |
const JS::AutoRequireNoGC&); |
| 155 |
|
| 156 |
/* |
| 157 |
* Invoke scriptCallback on every in-use script for the given realm or for all |
| 158 |
* realms if it is null. The scripts may or may not have bytecode. |
| 159 |
*/ |
| 160 |
extern void IterateScripts(JSContext* cx, JS::Realm* realm, void* data, |
| 161 |
IterateScriptCallback scriptCallback); |
| 162 |
|
| 163 |
JS::Realm* NewRealm(JSContext* cx, JSPrincipals* principals, |
| 164 |
const JS::RealmOptions& options); |
| 165 |
|
| 166 |
namespace gc { |
| 167 |
|
| 168 |
void FinishGC(JSContext* cx, JS::GCReason = JS::GCReason::FINISH_GC); |
| 169 |
|
| 170 |
void WaitForBackgroundTasks(JSContext* cx); |
| 171 |
|
| 172 |
enum VerifierType { PreBarrierVerifier, PostBarrierVerifier }; |
| 173 |
|
| 174 |
#ifdef JS_GC_ZEAL |
| 175 |
|
| 176 |
extern const char ZealModeHelpText[]; |
| 177 |
|
| 178 |
/* Check that write barriers have been used correctly. See gc/Verifier.cpp. */ |
| 179 |
void VerifyBarriers(JSRuntime* rt, VerifierType type); |
| 180 |
|
| 181 |
void MaybeVerifyBarriers(JSContext* cx, bool always = false); |
| 182 |
|
| 183 |
void DumpArenaInfo(); |
| 184 |
|
| 185 |
#else |
| 186 |
|
| 187 |
static inline void VerifyBarriers(JSRuntime* rt, VerifierType type) {} |
| 188 |
|
| 189 |
static inline void MaybeVerifyBarriers(JSContext* cx, bool always = false) {} |
| 190 |
|
| 191 |
#endif |
| 192 |
|
| 193 |
/* |
| 194 |
* Instances of this class prevent GC from happening while they are live. If an |
| 195 |
* allocation causes a heap threshold to be exceeded, no GC will be performed |
| 196 |
* and the allocation will succeed. Allocation may still fail for other reasons. |
| 197 |
* |
| 198 |
* Use of this class is highly discouraged, since without GC system memory can |
| 199 |
* become exhausted and this can cause crashes at places where we can't handle |
| 200 |
* allocation failure. |
| 201 |
* |
| 202 |
* Use of this is permissible in situations where it would be impossible (or at |
| 203 |
* least very difficult) to tolerate GC and where only a fixed number of objects |
| 204 |
* are allocated, such as: |
| 205 |
* |
| 206 |
* - error reporting |
| 207 |
* - JIT bailout handling |
| 208 |
* - brain transplants (JSObject::swap) |
| 209 |
* - debugging utilities not exposed to the browser |
| 210 |
* |
| 211 |
* This works by updating the |JSContext::suppressGC| counter which is checked |
| 212 |
* at the start of GC. |
| 213 |
*/ |
| 214 |
class MOZ_RAII JS_HAZ_GC_SUPPRESSED AutoSuppressGC |
| 215 |
: public JS::AutoRequireNoGC { |
| 216 |
int32_t& suppressGC_; |
| 217 |
|
| 218 |
public: |
| 219 |
explicit AutoSuppressGC(JSContext* cx); |
| 220 |
|
| 221 |
~AutoSuppressGC() { suppressGC_--; } |
| 222 |
}; |
| 223 |
|
| 224 |
const char* StateName(State state); |
| 225 |
|
| 226 |
} /* namespace gc */ |
| 227 |
|
| 228 |
/* Use this to avoid assertions when manipulating the wrapper map. */ |
| 229 |
class MOZ_RAII AutoDisableProxyCheck { |
| 230 |
public: |
| 231 |
#ifdef DEBUG |
| 232 |
AutoDisableProxyCheck(); |
| 233 |
~AutoDisableProxyCheck(); |
| 234 |
#else |
| 235 |
AutoDisableProxyCheck() {} |
| 236 |
#endif |
| 237 |
}; |
| 238 |
|
| 239 |
struct MOZ_RAII AutoDisableCompactingGC { |
| 240 |
explicit AutoDisableCompactingGC(JSContext* cx); |
| 241 |
~AutoDisableCompactingGC(); |
| 242 |
|
| 243 |
private: |
| 244 |
JSContext* cx; |
| 245 |
}; |
| 246 |
|
| 247 |
/* |
| 248 |
* Dynamically select the GC heap to allocate into for a graph of GC things. |
| 249 |
* |
| 250 |
* Initially |heap()| will return Heap::Default to select nursery allocation, |
| 251 |
* but when a specified number of nursery collections have been triggered it |
| 252 |
* switches to returning Heap::Tenured. |
| 253 |
*/ |
| 254 |
class MOZ_RAII AutoSelectGCHeap { |
| 255 |
public: |
| 256 |
explicit AutoSelectGCHeap(JSContext* cx, |
| 257 |
size_t allowedNurseryCollections = 0); |
| 258 |
~AutoSelectGCHeap(); |
| 259 |
|
| 260 |
gc::Heap heap() const { return heap_; } |
| 261 |
operator gc::Heap() const { return heap_; } |
| 262 |
|
| 263 |
void onNurseryCollectionEnd(); |
| 264 |
|
| 265 |
private: |
| 266 |
static void NurseryCollectionCallback(JSContext* cx, |
| 267 |
JS::GCNurseryProgress progress, |
| 268 |
JS::GCReason reason, void* data); |
| 269 |
|
| 270 |
JSContext* cx_; |
| 271 |
size_t allowedNurseryCollections_; |
| 272 |
gc::Heap heap_ = gc::Heap::Default; |
| 273 |
}; |
| 274 |
|
| 275 |
} /* namespace js */ |
| 276 |
|
| 277 |
#endif /* gc_GC_h */ |