1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sw=2 et tw=80:
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gc/Pretenuring.h"
#include "mozilla/Sprintf.h"
#include "gc/GCInternals.h"
#include "gc/PublicIterators.h"
#include "jit/Invalidation.h"
#include "gc/PrivateIterators-inl.h"
#include "vm/JSScript-inl.h"
using namespace js;
using namespace js::gc;
// The number of nursery allocations at which to pay attention to an allocation
// site. This must be large enough to ensure we have enough information to infer
// the lifetime and also large enough to avoid pretenuring low volume allocation
// sites.
static constexpr size_t AllocSiteAttentionThreshold = 500;
// The maximum number of alloc sites to create between each minor
// collection. Stop tracking allocation after this limit is reached. This
// prevents unbounded time traversing the list during minor GC.
static constexpr size_t MaxAllocSitesPerMinorGC = 500;
// The maximum number of times to invalidate JIT code for a site. After this we
// leave the site's state as Unknown and don't pretenure allocations.
// Note we use 4 bits to store the invalidation count.
static constexpr size_t MaxInvalidationCount = 5;
// The minimum number of allocated cells needed to determine the survival rate
// of cells in newly created arenas.
static constexpr size_t MinCellsRequiredForSurvivalRate = 100;
// The young survival rate below which a major collection is determined to have
// a low young survival rate.
static constexpr double LowYoungSurvivalThreshold = 0.05;
// The number of consecutive major collections with a low young survival rate
// that must occur before recovery is attempted.
static constexpr size_t LowYoungSurvivalCountBeforeRecovery = 2;
// The proportion of the nursery that must be tenured above which a minor
// collection may be determined to have a high nursery survival rate.
static constexpr double HighNurserySurvivalPromotionThreshold = 0.6;
// The number of nursery allocations made by optimized JIT code that must be
// tenured above which a minor collection may be determined to have a high
// nursery survival rate.
static constexpr size_t HighNurserySurvivalOptimizedAllocThreshold = 10000;
// The number of consecutive minor collections with a high nursery survival rate
// that must occur before recovery is attempted.
static constexpr size_t HighNurserySurvivalCountBeforeRecovery = 2;
AllocSite* const AllocSite::EndSentinel = reinterpret_cast<AllocSite*>(1);
JSScript* const AllocSite::WasmScript =
reinterpret_cast<JSScript*>(AllocSite::STATE_MASK + 1);
static bool SiteBasedPretenuringEnabled = true;
JS_PUBLIC_API void JS::SetSiteBasedPretenuringEnabled(bool enable) {
SiteBasedPretenuringEnabled = enable;
}
bool PretenuringNursery::canCreateAllocSite() {
MOZ_ASSERT(allocSitesCreated <= MaxAllocSitesPerMinorGC);
return SiteBasedPretenuringEnabled &&
allocSitesCreated < MaxAllocSitesPerMinorGC;
}
size_t PretenuringNursery::doPretenuring(GCRuntime* gc, JS::GCReason reason,
bool validPromotionRate,
double promotionRate, bool reportInfo,
size_t reportThreshold) {
size_t sitesActive = 0;
size_t sitesPretenured = 0;
size_t sitesInvalidated = 0;
size_t zonesWithHighNurserySurvival = 0;
// Zero allocation counts.
totalAllocCount_ = 0;
for (ZonesIter zone(gc, SkipAtoms); !zone.done(); zone.next()) {
for (auto& count : zone->pretenuring.nurseryAllocCounts) {
count = 0;
}
}
// Check whether previously optimized code has changed its behaviour and
// needs to be recompiled so that it can pretenure its allocations.
if (validPromotionRate) {
for (ZonesIter zone(gc, SkipAtoms); !zone.done(); zone.next()) {
bool highNurserySurvivalRate =
promotionRate > HighNurserySurvivalPromotionThreshold &&
zone->optimizedAllocSite()->nurseryTenuredCount >=
HighNurserySurvivalOptimizedAllocThreshold;
zone->pretenuring.noteHighNurserySurvivalRate(highNurserySurvivalRate);
if (highNurserySurvivalRate) {
zonesWithHighNurserySurvival++;
}
}
}
if (reportInfo) {
AllocSite::printInfoHeader(reason, promotionRate);
}
AllocSite* site = allocatedSites;
allocatedSites = AllocSite::EndSentinel;
while (site != AllocSite::EndSentinel) {
AllocSite* next = site->nextNurseryAllocated;
site->nextNurseryAllocated = nullptr;
MOZ_ASSERT_IF(site->isNormal(),
site->nurseryAllocCount >= site->nurseryTenuredCount);
if (site->isNormal()) {
processSite(gc, site, sitesActive, sitesPretenured, sitesInvalidated,
reportInfo, reportThreshold);
}
site = next;
}
// Catch-all sites don't end up on the list if they are only used from
// optimized JIT code, so process them here.
for (ZonesIter zone(gc, SkipAtoms); !zone.done(); zone.next()) {
for (auto& site : zone->pretenuring.unknownAllocSites) {
processCatchAllSite(&site, reportInfo, reportThreshold);
}
processCatchAllSite(zone->optimizedAllocSite(), reportInfo,
reportThreshold);
}
if (reportInfo) {
AllocSite::printInfoFooter(allocSitesCreated, sitesActive, sitesPretenured,
sitesInvalidated);
if (zonesWithHighNurserySurvival) {
fprintf(stderr, " %zu zones with high nursery survival rate\n",
zonesWithHighNurserySurvival);
}
}
allocSitesCreated = 0;
return sitesPretenured;
}
void PretenuringNursery::processSite(GCRuntime* gc, AllocSite* site,
size_t& sitesActive,
size_t& sitesPretenured,
size_t& sitesInvalidated, bool reportInfo,
size_t reportThreshold) {
sitesActive++;
updateAllocCounts(site);
bool hasPromotionRate = false;
double promotionRate = 0.0;
bool wasInvalidated = false;
if (site->nurseryAllocCount > AllocSiteAttentionThreshold) {
promotionRate =
double(site->nurseryTenuredCount) / double(site->nurseryAllocCount);
hasPromotionRate = true;
AllocSite::State prevState = site->state();
site->updateStateOnMinorGC(promotionRate);
AllocSite::State newState = site->state();
if (prevState == AllocSite::State::Unknown &&
newState == AllocSite::State::LongLived) {
sitesPretenured++;
// We can optimize JIT code before we realise that a site should be
// pretenured. Make sure we invalidate any existing optimized code.
if (site->hasScript()) {
wasInvalidated = site->invalidateScript(gc);
if (wasInvalidated) {
sitesInvalidated++;
}
}
}
}
if (reportInfo && site->allocCount() >= reportThreshold) {
site->printInfo(hasPromotionRate, promotionRate, wasInvalidated);
}
site->resetNurseryAllocations();
}
void PretenuringNursery::processCatchAllSite(AllocSite* site, bool reportInfo,
size_t reportThreshold) {
if (!site->hasNurseryAllocations()) {
return;
}
updateAllocCounts(site);
if (reportInfo && site->allocCount() >= reportThreshold) {
site->printInfo(false, 0.0, false);
}
site->resetNurseryAllocations();
}
void PretenuringNursery::updateAllocCounts(AllocSite* site) {
JS::TraceKind kind = site->traceKind();
totalAllocCount_ += site->nurseryAllocCount;
PretenuringZone& zone = site->zone()->pretenuring;
zone.nurseryAllocCount(kind) += site->nurseryAllocCount;
}
bool AllocSite::invalidateScript(GCRuntime* gc) {
CancelOffThreadIonCompile(script());
if (!script()->hasIonScript()) {
return false;
}
if (invalidationLimitReached()) {
MOZ_ASSERT(state() == State::Unknown);
return false;
}
invalidationCount++;
if (invalidationLimitReached()) {
setState(State::Unknown);
}
JSContext* cx = gc->rt->mainContextFromOwnThread();
jit::Invalidate(cx, script(),
/* resetUses = */ false,
/* cancelOffThread = */ true);
return true;
}
bool AllocSite::invalidationLimitReached() const {
MOZ_ASSERT(invalidationCount <= MaxInvalidationCount);
return invalidationCount == MaxInvalidationCount;
}
void PretenuringNursery::maybeStopPretenuring(GCRuntime* gc) {
for (GCZonesIter zone(gc); !zone.done(); zone.next()) {
double rate;
if (zone->pretenuring.calculateYoungTenuredSurvivalRate(&rate)) {
bool lowYoungSurvivalRate = rate < LowYoungSurvivalThreshold;
zone->pretenuring.noteLowYoungTenuredSurvivalRate(lowYoungSurvivalRate);
}
}
}
AllocSite::Kind AllocSite::kind() const {
if (isNormal()) {
return Kind::Normal;
}
if (this == zone()->optimizedAllocSite()) {
return Kind::Optimized;
}
MOZ_ASSERT(this == zone()->unknownAllocSite(traceKind()));
return Kind::Unknown;
}
void AllocSite::updateStateOnMinorGC(double promotionRate) {
// The state changes based on whether the promotion rate is deemed high
// (greater that 90%):
//
// high high
// ------------------> ------------------>
// ShortLived Unknown LongLived
// <------------------ <------------------
// !high !high
//
// The nursery is used to allocate if the site's state is Unknown or
// ShortLived. There are no direct transition between ShortLived and LongLived
// to avoid pretenuring sites that we've recently observed being short-lived.
if (invalidationLimitReached()) {
MOZ_ASSERT(state() == State::Unknown);
return;
}
bool highPromotionRate = promotionRate >= 0.9;
switch (state()) {
case State::Unknown:
if (highPromotionRate) {
setState(State::LongLived);
} else {
setState(State::ShortLived);
}
break;
case State::ShortLived: {
if (highPromotionRate) {
setState(State::Unknown);
}
break;
}
case State::LongLived: {
if (!highPromotionRate) {
setState(State::Unknown);
}
break;
}
}
}
bool AllocSite::maybeResetState() {
if (invalidationLimitReached()) {
MOZ_ASSERT(state() == State::Unknown);
return false;
}
invalidationCount++;
setState(State::Unknown);
return true;
}
void AllocSite::trace(JSTracer* trc) {
if (JSScript* s = script()) {
TraceManuallyBarrieredEdge(trc, &s, "AllocSite script");
if (s != script()) {
setScript(s);
}
}
}
bool PretenuringZone::calculateYoungTenuredSurvivalRate(double* rateOut) {
MOZ_ASSERT(allocCountInNewlyCreatedArenas >=
survivorCountInNewlyCreatedArenas);
if (allocCountInNewlyCreatedArenas < MinCellsRequiredForSurvivalRate) {
return false;
}
*rateOut = double(survivorCountInNewlyCreatedArenas) /
double(allocCountInNewlyCreatedArenas);
return true;
}
void PretenuringZone::noteLowYoungTenuredSurvivalRate(
bool lowYoungSurvivalRate) {
if (lowYoungSurvivalRate) {
lowYoungTenuredSurvivalCount++;
} else {
lowYoungTenuredSurvivalCount = 0;
}
}
void PretenuringZone::noteHighNurserySurvivalRate(
bool highNurserySurvivalRate) {
if (highNurserySurvivalRate) {
highNurserySurvivalCount++;
} else {
highNurserySurvivalCount = 0;
}
}
bool PretenuringZone::shouldResetNurseryAllocSites() {
bool shouldReset =
highNurserySurvivalCount >= HighNurserySurvivalCountBeforeRecovery;
if (shouldReset) {
highNurserySurvivalCount = 0;
}
return shouldReset;
}
bool PretenuringZone::shouldResetPretenuredAllocSites() {
bool shouldReset =
lowYoungTenuredSurvivalCount >= LowYoungSurvivalCountBeforeRecovery;
if (shouldReset) {
lowYoungTenuredSurvivalCount = 0;
}
return shouldReset;
}
/* static */
void AllocSite::printInfoHeader(JS::GCReason reason, double promotionRate) {
fprintf(stderr,
"Pretenuring info after %s minor GC with %4.1f%% promotion rate:\n",
ExplainGCReason(reason), promotionRate * 100.0);
}
/* static */
void AllocSite::printInfoFooter(size_t sitesCreated, size_t sitesActive,
size_t sitesPretenured,
size_t sitesInvalidated) {
fprintf(stderr,
" %zu alloc sites created, %zu active, %zu pretenured, %zu "
"invalidated\n",
sitesCreated, sitesActive, sitesPretenured, sitesInvalidated);
}
void AllocSite::printInfo(bool hasPromotionRate, double promotionRate,
bool wasInvalidated) const {
// Zone.
fprintf(stderr, " %p %p", this, zone());
// Script, or which kind of catch-all site this is.
if (!hasScript()) {
fprintf(stderr, " %16s",
kind() == Kind::Optimized
? "optimized"
: (kind() == Kind::Normal ? "normal" : "unknown"));
} else {
fprintf(stderr, " %16p", script());
}
// Nursery allocation count, missing for optimized sites.
char buffer[16] = {'\0'};
if (kind() != Kind::Optimized) {
SprintfLiteral(buffer, "%8" PRIu32, nurseryAllocCount);
}
fprintf(stderr, " %8s", buffer);
// Nursery tenure count.
fprintf(stderr, " %8" PRIu32, nurseryTenuredCount);
// Promotion rate, if there were enough allocations.
buffer[0] = '\0';
if (hasPromotionRate) {
SprintfLiteral(buffer, "%5.1f%%", std::min(1.0, promotionRate) * 100);
}
fprintf(stderr, " %6s", buffer);
// Current state for sites associated with a script.
const char* state = isNormal() ? stateName() : "";
fprintf(stderr, " %10s", state);
// Whether the associated script was invalidated.
if (wasInvalidated) {
fprintf(stderr, " invalidated");
}
fprintf(stderr, "\n");
}
const char* AllocSite::stateName() const {
switch (state()) {
case State::ShortLived:
return "ShortLived";
case State::Unknown:
return "Unknown";
case State::LongLived:
return "LongLived";
}
MOZ_CRASH("Unknown state");
}
|