| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include "mozilla/ScopeExit.h" |
| 6 |
#include "mozilla/UniquePtr.h" |
| 7 |
|
| 8 |
#include <iterator> |
| 9 |
|
| 10 |
#include "jsapi-tests/tests.h" |
| 11 |
|
| 12 |
static unsigned gSliceCallbackCount = 0; |
| 13 |
static bool gSawAllSliceCallbacks; |
| 14 |
static bool gSawAllGCCallbacks; |
| 15 |
|
| 16 |
static void NonIncrementalGCSliceCallback(JSContext* cx, |
| 17 |
JS::GCProgress progress, |
| 18 |
const JS::GCDescription& desc) { |
| 19 |
using namespace JS; |
| 20 |
static GCProgress expect[] = {GC_CYCLE_BEGIN, GC_SLICE_BEGIN, GC_SLICE_END, |
| 21 |
GC_CYCLE_END}; |
| 22 |
|
| 23 |
MOZ_RELEASE_ASSERT(gSliceCallbackCount < std::size(expect)); |
| 24 |
MOZ_RELEASE_ASSERT(progress == expect[gSliceCallbackCount++]); |
| 25 |
MOZ_RELEASE_ASSERT(desc.isZone_ == false); |
| 26 |
MOZ_RELEASE_ASSERT(desc.options_ == JS::GCOptions::Normal); |
| 27 |
MOZ_RELEASE_ASSERT(desc.reason_ == JS::GCReason::API); |
| 28 |
if (progress == GC_CYCLE_END) { |
| 29 |
mozilla::UniquePtr<char16_t> summary(desc.formatSummaryMessage(cx)); |
| 30 |
mozilla::UniquePtr<char16_t> message(desc.formatSliceMessage(cx)); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
BEGIN_TEST(testGCSliceCallback) { |
| 35 |
gSliceCallbackCount = 0; |
| 36 |
JS::SetGCSliceCallback(cx, NonIncrementalGCSliceCallback); |
| 37 |
JS_GC(cx); |
| 38 |
JS::SetGCSliceCallback(cx, nullptr); |
| 39 |
CHECK(gSliceCallbackCount == 4); |
| 40 |
return true; |
| 41 |
} |
| 42 |
END_TEST(testGCSliceCallback) |
| 43 |
|
| 44 |
static void RootsRemovedGCSliceCallback(JSContext* cx, JS::GCProgress progress, |
| 45 |
const JS::GCDescription& desc) { |
| 46 |
using namespace JS; |
| 47 |
|
| 48 |
static constexpr struct { |
| 49 |
GCProgress progress; |
| 50 |
GCReason reason; |
| 51 |
} expect[] = { |
| 52 |
// Explicitly requested a full GC. |
| 53 |
{GC_CYCLE_BEGIN, GCReason::DEBUG_GC}, |
| 54 |
{GC_SLICE_BEGIN, GCReason::DEBUG_GC}, |
| 55 |
{GC_SLICE_END, GCReason::DEBUG_GC}, |
| 56 |
{GC_SLICE_BEGIN, GCReason::DEBUG_GC}, |
| 57 |
{GC_SLICE_END, GCReason::DEBUG_GC}, |
| 58 |
{GC_CYCLE_END, GCReason::DEBUG_GC}, |
| 59 |
// Shutdown GC with ROOTS_REMOVED. |
| 60 |
{GC_CYCLE_BEGIN, GCReason::ROOTS_REMOVED}, |
| 61 |
{GC_SLICE_BEGIN, GCReason::ROOTS_REMOVED}, |
| 62 |
{GC_SLICE_END, GCReason::ROOTS_REMOVED}, |
| 63 |
{GC_CYCLE_END, GCReason::ROOTS_REMOVED} |
| 64 |
// All done. |
| 65 |
}; |
| 66 |
|
| 67 |
MOZ_RELEASE_ASSERT(gSliceCallbackCount < std::size(expect)); |
| 68 |
MOZ_RELEASE_ASSERT(progress == expect[gSliceCallbackCount].progress); |
| 69 |
MOZ_RELEASE_ASSERT(desc.isZone_ == false); |
| 70 |
MOZ_RELEASE_ASSERT(desc.options_ == JS::GCOptions::Shrink); |
| 71 |
MOZ_RELEASE_ASSERT(desc.reason_ == expect[gSliceCallbackCount].reason); |
| 72 |
gSliceCallbackCount++; |
| 73 |
} |
| 74 |
|
| 75 |
BEGIN_TEST(testGCRootsRemoved) { |
| 76 |
AutoLeaveZeal nozeal(cx); |
| 77 |
|
| 78 |
AutoGCParameter param1(cx, JSGC_INCREMENTAL_GC_ENABLED, true); |
| 79 |
|
| 80 |
gSliceCallbackCount = 0; |
| 81 |
JS::SetGCSliceCallback(cx, RootsRemovedGCSliceCallback); |
| 82 |
auto byebye = |
| 83 |
mozilla::MakeScopeExit([=] { JS::SetGCSliceCallback(cx, nullptr); }); |
| 84 |
|
| 85 |
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); |
| 86 |
CHECK(obj); |
| 87 |
|
| 88 |
JS::PrepareForFullGC(cx); |
| 89 |
JS::SliceBudget budget(JS::WorkBudget(1)); |
| 90 |
cx->runtime()->gc.startDebugGC(JS::GCOptions::Shrink, budget); |
| 91 |
CHECK(JS::IsIncrementalGCInProgress(cx)); |
| 92 |
|
| 93 |
// Trigger another GC after the current one in shrinking / shutdown GCs. |
| 94 |
cx->runtime()->gc.notifyRootsRemoved(); |
| 95 |
|
| 96 |
JS::FinishIncrementalGC(cx, JS::GCReason::DEBUG_GC); |
| 97 |
CHECK(!JS::IsIncrementalGCInProgress(cx)); |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
END_TEST(testGCRootsRemoved) |
| 102 |
|
| 103 |
#define ASSERT_MSG(cond, ...) \ |
| 104 |
do { \ |
| 105 |
if (!(cond)) { \ |
| 106 |
fprintf(stderr, __VA_ARGS__); \ |
| 107 |
MOZ_RELEASE_ASSERT(cond); \ |
| 108 |
} \ |
| 109 |
} while (false) |
| 110 |
|
| 111 |
// Trigger some nested GCs to ensure that they get their own reasons and |
| 112 |
// fullGCRequested state. |
| 113 |
// |
| 114 |
// The callbacks will form the following tree: |
| 115 |
// |
| 116 |
// Begin(DEBUG_GC) |
| 117 |
// Begin(API) |
| 118 |
// End(API) |
| 119 |
// End(DEBUG_GC) |
| 120 |
// Begin(MEM_PRESSURE) |
| 121 |
// End(MEM_PRESSURE) |
| 122 |
// Begin(DOM_WINDOW_UTILS) |
| 123 |
// End(DOM_WINDOW_UTILS) |
| 124 |
// |
| 125 |
// JSGC_BEGIN and JSGC_END callbacks will be observed as a preorder traversal |
| 126 |
// of the above tree. |
| 127 |
// |
| 128 |
// Note that the ordering of the *slice* callbacks don't match up simply to the |
| 129 |
// ordering above. If a JSGC_BEGIN triggers another GC, we won't see the outer |
| 130 |
// GC's GC_CYCLE_BEGIN until the inner one is done. The slice callbacks are |
| 131 |
// reporting the actual order that the GCs are happening in. |
| 132 |
// |
| 133 |
// JSGC_END, on the other hand, won't be emitted until the GC is complete and |
| 134 |
// the GC_CYCLE_BEGIN callback has fired. So its ordering is straightforward. |
| 135 |
// |
| 136 |
static void GCTreeCallback(JSContext* cx, JSGCStatus status, |
| 137 |
JS::GCReason reason, void* data) { |
| 138 |
using namespace JS; |
| 139 |
|
| 140 |
static constexpr struct { |
| 141 |
JSGCStatus expectedStatus; |
| 142 |
JS::GCReason expectedReason; |
| 143 |
bool fireGC; |
| 144 |
JS::GCReason reason; |
| 145 |
bool requestFullGC; |
| 146 |
} invocations[] = { |
| 147 |
{JSGC_BEGIN, GCReason::DEBUG_GC, true, GCReason::API, false}, |
| 148 |
{JSGC_BEGIN, GCReason::API, false}, |
| 149 |
{JSGC_END, GCReason::API, false}, |
| 150 |
{JSGC_END, GCReason::DEBUG_GC, true, GCReason::MEM_PRESSURE, true}, |
| 151 |
{JSGC_BEGIN, GCReason::MEM_PRESSURE, false}, |
| 152 |
{JSGC_END, GCReason::MEM_PRESSURE, true, GCReason::DOM_WINDOW_UTILS, |
| 153 |
false}, |
| 154 |
{JSGC_BEGIN, GCReason::DOM_WINDOW_UTILS, false}, |
| 155 |
{JSGC_END, GCReason::DOM_WINDOW_UTILS, false}}; |
| 156 |
|
| 157 |
static size_t i = 0; |
| 158 |
MOZ_RELEASE_ASSERT(i < std::size(invocations)); |
| 159 |
auto& invocation = invocations[i++]; |
| 160 |
if (i == std::size(invocations)) { |
| 161 |
gSawAllGCCallbacks = true; |
| 162 |
} |
| 163 |
ASSERT_MSG(status == invocation.expectedStatus, |
| 164 |
"GC callback #%zu: got status %d expected %d\n", i, status, |
| 165 |
invocation.expectedStatus); |
| 166 |
ASSERT_MSG(reason == invocation.expectedReason, |
| 167 |
"GC callback #%zu: got reason %s expected %s\n", i, |
| 168 |
ExplainGCReason(reason), |
| 169 |
ExplainGCReason(invocation.expectedReason)); |
| 170 |
if (invocation.fireGC) { |
| 171 |
if (invocation.requestFullGC) { |
| 172 |
JS::PrepareForFullGC(cx); |
| 173 |
} |
| 174 |
JS::SliceBudget budget = JS::SliceBudget(JS::WorkBudget(1)); |
| 175 |
cx->runtime()->gc.startGC(GCOptions::Normal, invocation.reason, budget); |
| 176 |
MOZ_RELEASE_ASSERT(JS::IsIncrementalGCInProgress(cx)); |
| 177 |
|
| 178 |
JS::FinishIncrementalGC(cx, invocation.reason); |
| 179 |
MOZ_RELEASE_ASSERT(!JS::IsIncrementalGCInProgress(cx)); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
static void GCTreeSliceCallback(JSContext* cx, JS::GCProgress progress, |
| 184 |
const JS::GCDescription& desc) { |
| 185 |
using namespace JS; |
| 186 |
|
| 187 |
static constexpr struct { |
| 188 |
GCProgress progress; |
| 189 |
GCReason reason; |
| 190 |
bool isZonal; |
| 191 |
} expectations[] = { |
| 192 |
// JSGC_BEGIN triggers a new GC before we get any slice callbacks from the |
| 193 |
// original outer GC. So the very first reason observed is API, not |
| 194 |
// DEBUG_GC. |
| 195 |
{GC_CYCLE_BEGIN, GCReason::API, true}, |
| 196 |
{GC_SLICE_BEGIN, GCReason::API, true}, |
| 197 |
{GC_SLICE_END, GCReason::API, true}, |
| 198 |
{GC_SLICE_BEGIN, GCReason::API, true}, |
| 199 |
{GC_SLICE_END, GCReason::API, true}, |
| 200 |
{GC_CYCLE_END, GCReason::API, true}, |
| 201 |
// Now the "outer" GC runs. It requested a full GC. |
| 202 |
{GC_CYCLE_BEGIN, GCReason::DEBUG_GC, false}, |
| 203 |
{GC_SLICE_BEGIN, GCReason::DEBUG_GC, false}, |
| 204 |
{GC_SLICE_END, GCReason::DEBUG_GC, false}, |
| 205 |
{GC_SLICE_BEGIN, GCReason::DEBUG_GC, false}, |
| 206 |
{GC_SLICE_END, GCReason::DEBUG_GC, false}, |
| 207 |
{GC_CYCLE_END, GCReason::DEBUG_GC, false}, |
| 208 |
// The outer JSGC_DEBUG GC's end callback triggers a full MEM_PRESSURE |
| 209 |
// GC, which runs next. (Its JSGC_BEGIN does not run a GC.) |
| 210 |
{GC_CYCLE_BEGIN, GCReason::MEM_PRESSURE, false}, |
| 211 |
{GC_SLICE_BEGIN, GCReason::MEM_PRESSURE, false}, |
| 212 |
{GC_SLICE_END, GCReason::MEM_PRESSURE, false}, |
| 213 |
{GC_SLICE_BEGIN, GCReason::MEM_PRESSURE, false}, |
| 214 |
{GC_SLICE_END, GCReason::MEM_PRESSURE, false}, |
| 215 |
{GC_CYCLE_END, GCReason::MEM_PRESSURE, false}, |
| 216 |
// The MEM_PRESSURE's GC's end callback now triggers a (zonal) |
| 217 |
// DOM_WINDOW_UTILS GC. |
| 218 |
{GC_CYCLE_BEGIN, GCReason::DOM_WINDOW_UTILS, true}, |
| 219 |
{GC_SLICE_BEGIN, GCReason::DOM_WINDOW_UTILS, true}, |
| 220 |
{GC_SLICE_END, GCReason::DOM_WINDOW_UTILS, true}, |
| 221 |
{GC_SLICE_BEGIN, GCReason::DOM_WINDOW_UTILS, true}, |
| 222 |
{GC_SLICE_END, GCReason::DOM_WINDOW_UTILS, true}, |
| 223 |
{GC_CYCLE_END, GCReason::DOM_WINDOW_UTILS, true}, |
| 224 |
// All done. |
| 225 |
}; |
| 226 |
|
| 227 |
MOZ_RELEASE_ASSERT(gSliceCallbackCount < std::size(expectations)); |
| 228 |
auto& expect = expectations[gSliceCallbackCount]; |
| 229 |
ASSERT_MSG(progress == expect.progress, "iteration %d: wrong progress\n", |
| 230 |
gSliceCallbackCount); |
| 231 |
ASSERT_MSG(desc.reason_ == expect.reason, |
| 232 |
"iteration %d: expected %s got %s\n", gSliceCallbackCount, |
| 233 |
JS::ExplainGCReason(expect.reason), |
| 234 |
JS::ExplainGCReason(desc.reason_)); |
| 235 |
ASSERT_MSG(desc.isZone_ == expect.isZonal, "iteration %d: wrong zonal\n", |
| 236 |
gSliceCallbackCount); |
| 237 |
MOZ_RELEASE_ASSERT(desc.options_ == JS::GCOptions::Normal); |
| 238 |
gSliceCallbackCount++; |
| 239 |
if (gSliceCallbackCount == std::size(expectations)) { |
| 240 |
gSawAllSliceCallbacks = true; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
BEGIN_TEST(testGCTree) { |
| 245 |
AutoLeaveZeal nozeal(cx); |
| 246 |
|
| 247 |
AutoGCParameter param1(cx, JSGC_INCREMENTAL_GC_ENABLED, true); |
| 248 |
|
| 249 |
gSliceCallbackCount = 0; |
| 250 |
gSawAllSliceCallbacks = false; |
| 251 |
gSawAllGCCallbacks = false; |
| 252 |
JS::SetGCSliceCallback(cx, GCTreeSliceCallback); |
| 253 |
JS_SetGCCallback(cx, GCTreeCallback, nullptr); |
| 254 |
|
| 255 |
// Automate the callback clearing. Otherwise if a CHECK fails, it will get |
| 256 |
// cluttered with additional failures from the callback unexpectedly firing |
| 257 |
// during the final shutdown GC. |
| 258 |
auto byebye = mozilla::MakeScopeExit([=] { |
| 259 |
JS::SetGCSliceCallback(cx, nullptr); |
| 260 |
JS_SetGCCallback(cx, nullptr, nullptr); |
| 261 |
}); |
| 262 |
|
| 263 |
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); |
| 264 |
CHECK(obj); |
| 265 |
|
| 266 |
// Outer GC is a full GC. |
| 267 |
JS::PrepareForFullGC(cx); |
| 268 |
JS::SliceBudget budget(JS::WorkBudget(1)); |
| 269 |
cx->runtime()->gc.startDebugGC(JS::GCOptions::Normal, budget); |
| 270 |
CHECK(JS::IsIncrementalGCInProgress(cx)); |
| 271 |
|
| 272 |
JS::FinishIncrementalGC(cx, JS::GCReason::DEBUG_GC); |
| 273 |
CHECK(!JS::IsIncrementalGCInProgress(cx)); |
| 274 |
CHECK(gSawAllSliceCallbacks); |
| 275 |
CHECK(gSawAllGCCallbacks); |
| 276 |
|
| 277 |
return true; |
| 278 |
} |
| 279 |
END_TEST(testGCTree) |