| 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 |
#include "js/MemoryMetrics.h" |
| 8 |
|
| 9 |
#include "mozilla/MathAlgorithms.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
|
| 13 |
#include "gc/BufferAllocator.h" |
| 14 |
#include "gc/GC.h" |
| 15 |
#include "gc/Memory.h" |
| 16 |
#include "gc/Nursery.h" |
| 17 |
#include "gc/PublicIterators.h" |
| 18 |
#include "jit/BaselineJIT.h" |
| 19 |
#include "jit/Ion.h" |
| 20 |
#include "js/HeapAPI.h" |
| 21 |
#include "util/Text.h" |
| 22 |
#include "vm/BigIntType.h" |
| 23 |
#include "vm/HelperThreadState.h" |
| 24 |
#include "vm/JSObject.h" |
| 25 |
#include "vm/JSScript.h" |
| 26 |
#include "vm/PropMap.h" |
| 27 |
#include "vm/Realm.h" |
| 28 |
#include "vm/Runtime.h" |
| 29 |
#include "vm/Shape.h" |
| 30 |
#include "vm/StringType.h" |
| 31 |
#include "wasm/WasmInstance.h" |
| 32 |
#include "wasm/WasmJS.h" |
| 33 |
#include "wasm/WasmModule.h" |
| 34 |
|
| 35 |
#include "wasm/WasmInstance-inl.h" |
| 36 |
|
| 37 |
using mozilla::MallocSizeOf; |
| 38 |
|
| 39 |
using namespace js; |
| 40 |
|
| 41 |
using JS::ObjectPrivateVisitor; |
| 42 |
using JS::RealmStats; |
| 43 |
using JS::RuntimeStats; |
| 44 |
using JS::ZoneStats; |
| 45 |
|
| 46 |
namespace js { |
| 47 |
|
| 48 |
JS_PUBLIC_API size_t MemoryReportingSundriesThreshold() { return 8 * 1024; } |
| 49 |
|
| 50 |
/* static */ |
| 51 |
HashNumber InefficientNonFlatteningStringHashPolicy::hash(const Lookup& l) { |
| 52 |
if (l->isLinear()) { |
| 53 |
return HashStringChars(&l->asLinear()); |
| 54 |
} |
| 55 |
|
| 56 |
// Use rope's non-copying hash function. |
| 57 |
uint32_t hash = 0; |
| 58 |
if (!l->asRope().hash(&hash)) { |
| 59 |
MOZ_CRASH("oom"); |
| 60 |
} |
| 61 |
return hash; |
| 62 |
} |
| 63 |
|
| 64 |
template <typename Char1, typename Char2> |
| 65 |
static bool EqualStringsPure(JSString* s1, JSString* s2) { |
| 66 |
if (s1->length() != s2->length()) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
const Char1* c1; |
| 71 |
UniquePtr<Char1[], JS::FreePolicy> ownedChars1; |
| 72 |
JS::AutoCheckCannotGC nogc; |
| 73 |
if (s1->isLinear()) { |
| 74 |
c1 = s1->asLinear().chars<Char1>(nogc); |
| 75 |
} else { |
| 76 |
ownedChars1 = |
| 77 |
s1->asRope().copyChars<Char1>(/* tcx */ nullptr, js::MallocArena); |
| 78 |
if (!ownedChars1) { |
| 79 |
MOZ_CRASH("oom"); |
| 80 |
} |
| 81 |
c1 = ownedChars1.get(); |
| 82 |
} |
| 83 |
|
| 84 |
const Char2* c2; |
| 85 |
UniquePtr<Char2[], JS::FreePolicy> ownedChars2; |
| 86 |
if (s2->isLinear()) { |
| 87 |
c2 = s2->asLinear().chars<Char2>(nogc); |
| 88 |
} else { |
| 89 |
ownedChars2 = |
| 90 |
s2->asRope().copyChars<Char2>(/* tcx */ nullptr, js::MallocArena); |
| 91 |
if (!ownedChars2) { |
| 92 |
MOZ_CRASH("oom"); |
| 93 |
} |
| 94 |
c2 = ownedChars2.get(); |
| 95 |
} |
| 96 |
|
| 97 |
return EqualChars(c1, c2, s1->length()); |
| 98 |
} |
| 99 |
|
| 100 |
/* static */ |
| 101 |
bool InefficientNonFlatteningStringHashPolicy::match(const JSString* const& k, |
| 102 |
const Lookup& l) { |
| 103 |
// We can't use js::EqualStrings, because that flattens our strings. |
| 104 |
JSString* s1 = const_cast<JSString*>(k); |
| 105 |
if (k->hasLatin1Chars()) { |
| 106 |
return l->hasLatin1Chars() ? EqualStringsPure<Latin1Char, Latin1Char>(s1, l) |
| 107 |
: EqualStringsPure<Latin1Char, char16_t>(s1, l); |
| 108 |
} |
| 109 |
|
| 110 |
return l->hasLatin1Chars() ? EqualStringsPure<char16_t, Latin1Char>(s1, l) |
| 111 |
: EqualStringsPure<char16_t, char16_t>(s1, l); |
| 112 |
} |
| 113 |
|
| 114 |
} // namespace js |
| 115 |
|
| 116 |
namespace JS { |
| 117 |
|
| 118 |
template <typename CharT> |
| 119 |
static void StoreStringChars(char* buffer, size_t bufferSize, JSString* str) { |
| 120 |
const CharT* chars; |
| 121 |
UniquePtr<CharT[], JS::FreePolicy> ownedChars; |
| 122 |
JS::AutoCheckCannotGC nogc; |
| 123 |
if (str->isLinear()) { |
| 124 |
chars = str->asLinear().chars<CharT>(nogc); |
| 125 |
} else { |
| 126 |
ownedChars = |
| 127 |
str->asRope().copyChars<CharT>(/* tcx */ nullptr, js::MallocArena); |
| 128 |
if (!ownedChars) { |
| 129 |
MOZ_CRASH("oom"); |
| 130 |
} |
| 131 |
chars = ownedChars.get(); |
| 132 |
} |
| 133 |
|
| 134 |
// We might truncate |str| even if it's much shorter than 1024 chars, if |
| 135 |
// |str| contains unicode chars. Since this is just for a memory reporter, |
| 136 |
// we don't care. |
| 137 |
PutEscapedString(buffer, bufferSize, chars, str->length(), /* quote */ 0); |
| 138 |
} |
| 139 |
|
| 140 |
NotableStringInfo::NotableStringInfo(JSString* str, const StringInfo& info) |
| 141 |
: StringInfo(info), length(str->length()) { |
| 142 |
size_t bufferSize = std::min(str->length() + 1, size_t(MAX_SAVED_CHARS)); |
| 143 |
buffer.reset(js_pod_malloc<char>(bufferSize)); |
| 144 |
if (!buffer) { |
| 145 |
MOZ_CRASH("oom"); |
| 146 |
} |
| 147 |
|
| 148 |
if (str->hasLatin1Chars()) { |
| 149 |
StoreStringChars<Latin1Char>(buffer.get(), bufferSize, str); |
| 150 |
} else { |
| 151 |
StoreStringChars<char16_t>(buffer.get(), bufferSize, str); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
NotableClassInfo::NotableClassInfo(const char* className, const ClassInfo& info) |
| 156 |
: ClassInfo(info) { |
| 157 |
className_ = DuplicateString(className); |
| 158 |
if (!className_) { |
| 159 |
MOZ_CRASH("oom"); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
NotableScriptSourceInfo::NotableScriptSourceInfo(const char* filename, |
| 164 |
const ScriptSourceInfo& info) |
| 165 |
: ScriptSourceInfo(info) { |
| 166 |
filename_ = DuplicateString(filename); |
| 167 |
if (!filename_) { |
| 168 |
MOZ_CRASH("oom"); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
} // namespace JS |
| 173 |
|
| 174 |
using SourceSet = |
| 175 |
HashSet<ScriptSource*, DefaultHasher<ScriptSource*>, SystemAllocPolicy>; |
| 176 |
|
| 177 |
struct StatsClosure { |
| 178 |
RuntimeStats* rtStats; |
| 179 |
ObjectPrivateVisitor* opv; |
| 180 |
SourceSet seenSources; |
| 181 |
wasm::CodeMetadata::SeenSet wasmSeenCodeMetadata; |
| 182 |
js::CodeMetadataForAsmJS::SeenSet wasmSeenCodeMetadataForAsmJS; |
| 183 |
wasm::Code::SeenSet wasmSeenCode; |
| 184 |
wasm::Table::SeenSet wasmSeenTables; |
| 185 |
bool anonymize; |
| 186 |
|
| 187 |
StatsClosure(RuntimeStats* rt, ObjectPrivateVisitor* v, bool anon) |
| 188 |
: rtStats(rt), opv(v), anonymize(anon) {} |
| 189 |
}; |
| 190 |
|
| 191 |
static void DecommittedPagesChunkCallback(JSRuntime* rt, void* data, |
| 192 |
gc::ArenaChunk* chunk, |
| 193 |
const JS::AutoRequireNoGC& nogc) { |
| 194 |
auto* gcHeapDecommittedPages = static_cast<size_t*>(data); |
| 195 |
*gcHeapDecommittedPages += chunk->decommittedPages.Count() * gc::PageSize; |
| 196 |
} |
| 197 |
|
| 198 |
static void StatsZoneCallback(JSRuntime* rt, void* data, Zone* zone, |
| 199 |
const JS::AutoRequireNoGC& nogc) { |
| 200 |
// Append a new RealmStats to the vector. |
| 201 |
RuntimeStats* rtStats = static_cast<StatsClosure*>(data)->rtStats; |
| 202 |
|
| 203 |
// CollectRuntimeStats reserves enough space. |
| 204 |
MOZ_ALWAYS_TRUE(rtStats->zoneStatsVector.growBy(1)); |
| 205 |
ZoneStats& zStats = rtStats->zoneStatsVector.back(); |
| 206 |
zStats.initStrings(); |
| 207 |
rtStats->initExtraZoneStats(zone, &zStats, nogc); |
| 208 |
rtStats->currZoneStats = &zStats; |
| 209 |
|
| 210 |
zone->addSizeOfIncludingThis( |
| 211 |
rtStats->mallocSizeOf_, &zStats.zoneObject, &zStats.code, |
| 212 |
&zStats.regexpZone, &zStats.jitZone, &zStats.cacheIRStubs, |
| 213 |
&zStats.uniqueIdMap, &zStats.initialPropMapTable, &zStats.shapeTables, |
| 214 |
&rtStats->runtime.atomsMarkBitmaps, &zStats.compartmentObjects, |
| 215 |
&zStats.crossCompartmentWrappersTables, &zStats.compartmentsPrivateData, |
| 216 |
&zStats.scriptCountsMap); |
| 217 |
zone->bufferAllocator.addSizeOfExcludingThis(&zStats.gcBuffers.usedBytes, |
| 218 |
&zStats.gcBuffers.freeBytes, |
| 219 |
&zStats.gcBuffers.adminBytes); |
| 220 |
} |
| 221 |
|
| 222 |
static void StatsRealmCallback(JSContext* cx, void* data, Realm* realm, |
| 223 |
const JS::AutoRequireNoGC& nogc) { |
| 224 |
// Append a new RealmStats to the vector. |
| 225 |
RuntimeStats* rtStats = static_cast<StatsClosure*>(data)->rtStats; |
| 226 |
|
| 227 |
// CollectRuntimeStats reserves enough space. |
| 228 |
MOZ_ALWAYS_TRUE(rtStats->realmStatsVector.growBy(1)); |
| 229 |
RealmStats& realmStats = rtStats->realmStatsVector.back(); |
| 230 |
realmStats.initClasses(); |
| 231 |
rtStats->initExtraRealmStats(realm, &realmStats, nogc); |
| 232 |
|
| 233 |
realm->setRealmStats(&realmStats); |
| 234 |
|
| 235 |
// Measure the realm object itself, and things hanging off it. |
| 236 |
realm->addSizeOfIncludingThis( |
| 237 |
rtStats->mallocSizeOf_, &realmStats.realmObject, &realmStats.realmTables, |
| 238 |
&realmStats.innerViewsTable, &realmStats.objectMetadataTable, |
| 239 |
&realmStats.savedStacksSet, &realmStats.nonSyntacticLexicalScopesTable); |
| 240 |
} |
| 241 |
|
| 242 |
static void StatsArenaCallback(JSRuntime* rt, void* data, gc::Arena* arena, |
| 243 |
JS::TraceKind traceKind, size_t thingSize, |
| 244 |
const JS::AutoRequireNoGC& nogc) { |
| 245 |
RuntimeStats* rtStats = static_cast<StatsClosure*>(data)->rtStats; |
| 246 |
|
| 247 |
// The admin space includes (a) the header fields and (b) the padding |
| 248 |
// between the end of the header fields and the first GC thing. |
| 249 |
size_t allocationSpace = gc::Arena::thingsSpan(arena->getAllocKind()); |
| 250 |
rtStats->currZoneStats->gcHeapArenaAdmin += gc::ArenaSize - allocationSpace; |
| 251 |
|
| 252 |
// We don't call the callback on unused things. So we compute the |
| 253 |
// unused space like this: arenaUnused = maxArenaUnused - arenaUsed. |
| 254 |
// We do this by setting arenaUnused to maxArenaUnused here, and then |
| 255 |
// subtracting thingSize for every used cell, in StatsCellCallback(). |
| 256 |
rtStats->currZoneStats->unusedGCThings.addToKind(traceKind, allocationSpace); |
| 257 |
} |
| 258 |
|
| 259 |
// FineGrained is used for normal memory reporting. CoarseGrained is used by |
| 260 |
// AddSizeOfTab(), which aggregates all the measurements into a handful of |
| 261 |
// high-level numbers, which means that fine-grained reporting would be a waste |
| 262 |
// of effort. |
| 263 |
enum Granularity { FineGrained, CoarseGrained }; |
| 264 |
|
| 265 |
static void AddClassInfo(Granularity granularity, RealmStats& realmStats, |
| 266 |
const char* className, JS::ClassInfo& info) { |
| 267 |
if (granularity == FineGrained) { |
| 268 |
if (!className) { |
| 269 |
className = "<no class name>"; |
| 270 |
} |
| 271 |
RealmStats::ClassesHashMap::AddPtr p = |
| 272 |
realmStats.allClasses->lookupForAdd(className); |
| 273 |
if (!p) { |
| 274 |
bool ok = realmStats.allClasses->add(p, className, info); |
| 275 |
// Ignore failure -- we just won't record the |
| 276 |
// object/shape/base-shape as notable. |
| 277 |
(void)ok; |
| 278 |
} else { |
| 279 |
p->value().add(info); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
template <Granularity granularity> |
| 285 |
static void CollectScriptSourceStats(StatsClosure* closure, ScriptSource* ss) { |
| 286 |
RuntimeStats* rtStats = closure->rtStats; |
| 287 |
|
| 288 |
SourceSet::AddPtr entry = closure->seenSources.lookupForAdd(ss); |
| 289 |
if (entry) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
bool ok = closure->seenSources.add(entry, ss); |
| 294 |
(void)ok; // Not much to be done on failure. |
| 295 |
|
| 296 |
JS::ScriptSourceInfo info; // This zeroes all the sizes. |
| 297 |
ss->addSizeOfIncludingThis(rtStats->mallocSizeOf_, &info); |
| 298 |
|
| 299 |
rtStats->runtime.scriptSourceInfo.add(info); |
| 300 |
|
| 301 |
if (granularity == FineGrained) { |
| 302 |
const char* filename = ss->filename(); |
| 303 |
if (!filename) { |
| 304 |
filename = "<no filename>"; |
| 305 |
} |
| 306 |
|
| 307 |
JS::RuntimeSizes::ScriptSourcesHashMap::AddPtr p = |
| 308 |
rtStats->runtime.allScriptSources->lookupForAdd(filename); |
| 309 |
if (!p) { |
| 310 |
bool ok = rtStats->runtime.allScriptSources->add(p, filename, info); |
| 311 |
// Ignore failure -- we just won't record the script source as notable. |
| 312 |
(void)ok; |
| 313 |
} else { |
| 314 |
p->value().add(info); |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
// The various kinds of hashing are expensive, and the results are unused when |
| 320 |
// doing coarse-grained measurements. Skipping them more than doubles the |
| 321 |
// profile speed for complex pages such as gmail.com. |
| 322 |
template <Granularity granularity> |
| 323 |
static void StatsCellCallback(JSRuntime* rt, void* data, JS::GCCellPtr cellptr, |
| 324 |
size_t thingSize, |
| 325 |
const JS::AutoRequireNoGC& nogc) { |
| 326 |
StatsClosure* closure = static_cast<StatsClosure*>(data); |
| 327 |
RuntimeStats* rtStats = closure->rtStats; |
| 328 |
ZoneStats* zStats = rtStats->currZoneStats; |
| 329 |
JS::TraceKind kind = cellptr.kind(); |
| 330 |
switch (kind) { |
| 331 |
case JS::TraceKind::Object: { |
| 332 |
JSObject* obj = &cellptr.as<JSObject>(); |
| 333 |
RealmStats& realmStats = obj->maybeCCWRealm()->realmStats(); |
| 334 |
JS::ClassInfo info; // This zeroes all the sizes. |
| 335 |
info.objectsGCHeap += thingSize; |
| 336 |
|
| 337 |
if (!obj->isTenured()) { |
| 338 |
info.objectsGCHeap += Nursery::nurseryCellHeaderSize(); |
| 339 |
} |
| 340 |
|
| 341 |
obj->addSizeOfExcludingThis(rtStats->mallocSizeOf_, &info, |
| 342 |
&rtStats->runtime); |
| 343 |
|
| 344 |
// These classes require special handling due to shared resources which |
| 345 |
// we must be careful not to report twice. |
| 346 |
if (obj->is<WasmModuleObject>()) { |
| 347 |
const wasm::Module& module = obj->as<WasmModuleObject>().module(); |
| 348 |
ScriptSource* ss = module.codeMetaForAsmJS() |
| 349 |
? module.codeMetaForAsmJS()->maybeScriptSource() |
| 350 |
: nullptr; |
| 351 |
if (ss) { |
| 352 |
CollectScriptSourceStats<granularity>(closure, ss); |
| 353 |
} |
| 354 |
module.addSizeOfMisc( |
| 355 |
rtStats->mallocSizeOf_, &closure->wasmSeenCodeMetadata, |
| 356 |
&closure->wasmSeenCodeMetadataForAsmJS, &closure->wasmSeenCode, |
| 357 |
&info.objectsNonHeapCodeWasm, &info.objectsMallocHeapMisc); |
| 358 |
} else if (obj->is<WasmInstanceObject>()) { |
| 359 |
wasm::Instance& instance = obj->as<WasmInstanceObject>().instance(); |
| 360 |
ScriptSource* ss = |
| 361 |
instance.codeMetaForAsmJS() |
| 362 |
? instance.codeMetaForAsmJS()->maybeScriptSource() |
| 363 |
: nullptr; |
| 364 |
if (ss) { |
| 365 |
CollectScriptSourceStats<granularity>(closure, ss); |
| 366 |
} |
| 367 |
instance.addSizeOfMisc( |
| 368 |
rtStats->mallocSizeOf_, &closure->wasmSeenCodeMetadata, |
| 369 |
&closure->wasmSeenCodeMetadataForAsmJS, &closure->wasmSeenCode, |
| 370 |
&closure->wasmSeenTables, &info.objectsNonHeapCodeWasm, |
| 371 |
&info.objectsMallocHeapMisc); |
| 372 |
} |
| 373 |
|
| 374 |
realmStats.classInfo.add(info); |
| 375 |
|
| 376 |
const JSClass* clasp = obj->getClass(); |
| 377 |
const char* className = clasp->name; |
| 378 |
AddClassInfo(granularity, realmStats, className, info); |
| 379 |
|
| 380 |
if (ObjectPrivateVisitor* opv = closure->opv) { |
| 381 |
nsISupports* iface; |
| 382 |
if (opv->getISupports_(obj, &iface) && iface) { |
| 383 |
realmStats.objectsPrivate += opv->sizeOfIncludingThis(iface); |
| 384 |
} |
| 385 |
} |
| 386 |
break; |
| 387 |
} |
| 388 |
|
| 389 |
case JS::TraceKind::Script: { |
| 390 |
BaseScript* base = &cellptr.as<BaseScript>(); |
| 391 |
RealmStats& realmStats = base->realm()->realmStats(); |
| 392 |
realmStats.scriptsGCHeap += thingSize; |
| 393 |
realmStats.scriptsMallocHeapData += |
| 394 |
base->sizeOfExcludingThis(rtStats->mallocSizeOf_); |
| 395 |
if (base->hasJitScript()) { |
| 396 |
JSScript* script = static_cast<JSScript*>(base); |
| 397 |
script->addSizeOfJitScript(rtStats->mallocSizeOf_, |
| 398 |
&realmStats.jitScripts, |
| 399 |
&realmStats.allocSites); |
| 400 |
jit::AddSizeOfBaselineData(script, rtStats->mallocSizeOf_, |
| 401 |
&realmStats.baselineData); |
| 402 |
realmStats.ionData += |
| 403 |
jit::SizeOfIonData(script, rtStats->mallocSizeOf_); |
| 404 |
} |
| 405 |
CollectScriptSourceStats<granularity>(closure, base->scriptSource()); |
| 406 |
break; |
| 407 |
} |
| 408 |
|
| 409 |
case JS::TraceKind::String: { |
| 410 |
JSString* str = &cellptr.as<JSString>(); |
| 411 |
size_t size = thingSize; |
| 412 |
if (!str->isTenured()) { |
| 413 |
size += Nursery::nurseryCellHeaderSize(); |
| 414 |
} |
| 415 |
|
| 416 |
JS::StringInfo info; |
| 417 |
if (str->hasLatin1Chars()) { |
| 418 |
info.gcHeapLatin1 = size; |
| 419 |
info.mallocHeapLatin1 = |
| 420 |
str->sizeOfExcludingThis(rtStats->mallocSizeOf_); |
| 421 |
} else { |
| 422 |
info.gcHeapTwoByte = size; |
| 423 |
info.mallocHeapTwoByte = |
| 424 |
str->sizeOfExcludingThis(rtStats->mallocSizeOf_); |
| 425 |
} |
| 426 |
info.numCopies = 1; |
| 427 |
|
| 428 |
zStats->stringInfo.add(info); |
| 429 |
|
| 430 |
// The primary use case for anonymization is automated crash submission |
| 431 |
// (to help detect OOM crashes). In that case, we don't want to pay the |
| 432 |
// memory cost required to do notable string detection. |
| 433 |
if (granularity == FineGrained && !closure->anonymize) { |
| 434 |
ZoneStats::StringsHashMap::AddPtr p = |
| 435 |
zStats->allStrings->lookupForAdd(str); |
| 436 |
if (!p) { |
| 437 |
bool ok = zStats->allStrings->add(p, str, info); |
| 438 |
// Ignore failure -- we just won't record the string as notable. |
| 439 |
(void)ok; |
| 440 |
} else { |
| 441 |
p->value().add(info); |
| 442 |
} |
| 443 |
} |
| 444 |
break; |
| 445 |
} |
| 446 |
|
| 447 |
case JS::TraceKind::Symbol: |
| 448 |
zStats->symbolsGCHeap += thingSize; |
| 449 |
break; |
| 450 |
|
| 451 |
case JS::TraceKind::BigInt: { |
| 452 |
JS::BigInt* bi = &cellptr.as<BigInt>(); |
| 453 |
size_t size = thingSize; |
| 454 |
if (!bi->isTenured()) { |
| 455 |
size += Nursery::nurseryCellHeaderSize(); |
| 456 |
} |
| 457 |
zStats->bigIntsGCHeap += size; |
| 458 |
zStats->bigIntsMallocHeap += |
| 459 |
bi->sizeOfExcludingThis(rtStats->mallocSizeOf_); |
| 460 |
break; |
| 461 |
} |
| 462 |
|
| 463 |
case JS::TraceKind::BaseShape: { |
| 464 |
JS::ShapeInfo info; // This zeroes all the sizes. |
| 465 |
info.shapesGCHeapBase += thingSize; |
| 466 |
// No malloc-heap measurements. |
| 467 |
|
| 468 |
zStats->shapeInfo.add(info); |
| 469 |
break; |
| 470 |
} |
| 471 |
|
| 472 |
case JS::TraceKind::GetterSetter: { |
| 473 |
zStats->getterSettersGCHeap += thingSize; |
| 474 |
break; |
| 475 |
} |
| 476 |
|
| 477 |
case JS::TraceKind::PropMap: { |
| 478 |
PropMap* map = &cellptr.as<PropMap>(); |
| 479 |
if (map->isDictionary()) { |
| 480 |
zStats->dictPropMapsGCHeap += thingSize; |
| 481 |
} else if (map->isCompact()) { |
| 482 |
zStats->compactPropMapsGCHeap += thingSize; |
| 483 |
} else { |
| 484 |
MOZ_ASSERT(map->isNormal()); |
| 485 |
zStats->normalPropMapsGCHeap += thingSize; |
| 486 |
} |
| 487 |
map->addSizeOfExcludingThis(rtStats->mallocSizeOf_, |
| 488 |
&zStats->propMapChildren, |
| 489 |
&zStats->propMapTables); |
| 490 |
break; |
| 491 |
} |
| 492 |
|
| 493 |
case JS::TraceKind::JitCode: { |
| 494 |
zStats->jitCodesGCHeap += thingSize; |
| 495 |
// The code for a script is counted in ExecutableAllocator::sizeOfCode(). |
| 496 |
break; |
| 497 |
} |
| 498 |
|
| 499 |
case JS::TraceKind::Shape: { |
| 500 |
Shape* shape = &cellptr.as<Shape>(); |
| 501 |
|
| 502 |
JS::ShapeInfo info; // This zeroes all the sizes. |
| 503 |
if (shape->isDictionary()) { |
| 504 |
info.shapesGCHeapDict += thingSize; |
| 505 |
} else { |
| 506 |
info.shapesGCHeapShared += thingSize; |
| 507 |
} |
| 508 |
shape->addSizeOfExcludingThis(rtStats->mallocSizeOf_, &info); |
| 509 |
zStats->shapeInfo.add(info); |
| 510 |
break; |
| 511 |
} |
| 512 |
|
| 513 |
case JS::TraceKind::Scope: { |
| 514 |
Scope* scope = &cellptr.as<Scope>(); |
| 515 |
zStats->scopesGCHeap += thingSize; |
| 516 |
zStats->scopesMallocHeap += |
| 517 |
scope->sizeOfExcludingThis(rtStats->mallocSizeOf_); |
| 518 |
break; |
| 519 |
} |
| 520 |
|
| 521 |
case JS::TraceKind::RegExpShared: { |
| 522 |
auto regexp = &cellptr.as<RegExpShared>(); |
| 523 |
zStats->regExpSharedsGCHeap += thingSize; |
| 524 |
zStats->regExpSharedsMallocHeap += |
| 525 |
regexp->sizeOfExcludingThis(rtStats->mallocSizeOf_); |
| 526 |
break; |
| 527 |
} |
| 528 |
|
| 529 |
case JS::TraceKind::SmallBuffer: { |
| 530 |
// Note that this overlaps with memory that is also reported as part of |
| 531 |
// the owning cell. |
| 532 |
zStats->smallBuffersGCHeap += thingSize; |
| 533 |
break; |
| 534 |
} |
| 535 |
|
| 536 |
default: |
| 537 |
MOZ_CRASH("invalid traceKind in StatsCellCallback"); |
| 538 |
} |
| 539 |
|
| 540 |
// Yes, this is a subtraction: see StatsArenaCallback() for details. |
| 541 |
zStats->unusedGCThings.addToKind(kind, -thingSize); |
| 542 |
} |
| 543 |
|
| 544 |
void ZoneStats::initStrings() { |
| 545 |
isTotals = false; |
| 546 |
allStrings.emplace(); |
| 547 |
} |
| 548 |
|
| 549 |
void RealmStats::initClasses() { |
| 550 |
isTotals = false; |
| 551 |
allClasses.emplace(); |
| 552 |
} |
| 553 |
|
| 554 |
static bool FindNotableStrings(ZoneStats& zStats) { |
| 555 |
using namespace JS; |
| 556 |
|
| 557 |
// We should only run FindNotableStrings once per ZoneStats object. |
| 558 |
MOZ_ASSERT(zStats.notableStrings.empty()); |
| 559 |
|
| 560 |
for (ZoneStats::StringsHashMap::Range r = zStats.allStrings->all(); |
| 561 |
!r.empty(); r.popFront()) { |
| 562 |
JSString* str = r.front().key(); |
| 563 |
StringInfo& info = r.front().value(); |
| 564 |
|
| 565 |
if (!info.isNotable()) { |
| 566 |
continue; |
| 567 |
} |
| 568 |
|
| 569 |
if (!zStats.notableStrings.emplaceBack(str, info)) { |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
// We're moving this string from a non-notable to a notable bucket, so |
| 574 |
// subtract it out of the non-notable tallies. |
| 575 |
zStats.stringInfo.subtract(info); |
| 576 |
} |
| 577 |
// Release |allStrings| now, rather than waiting for zStats's destruction, to |
| 578 |
// reduce peak memory consumption during reporting. |
| 579 |
zStats.allStrings.reset(); |
| 580 |
return true; |
| 581 |
} |
| 582 |
|
| 583 |
static bool FindNotableClasses(RealmStats& realmStats) { |
| 584 |
using namespace JS; |
| 585 |
|
| 586 |
// We should only run FindNotableClasses once per ZoneStats object. |
| 587 |
MOZ_ASSERT(realmStats.notableClasses.empty()); |
| 588 |
|
| 589 |
for (RealmStats::ClassesHashMap::Range r = realmStats.allClasses->all(); |
| 590 |
!r.empty(); r.popFront()) { |
| 591 |
const char* className = r.front().key(); |
| 592 |
ClassInfo& info = r.front().value(); |
| 593 |
|
| 594 |
// If this class isn't notable, or if we can't grow the notableStrings |
| 595 |
// vector, skip this string. |
| 596 |
if (!info.isNotable()) { |
| 597 |
continue; |
| 598 |
} |
| 599 |
|
| 600 |
if (!realmStats.notableClasses.emplaceBack(className, info)) { |
| 601 |
return false; |
| 602 |
} |
| 603 |
|
| 604 |
// We're moving this class from a non-notable to a notable bucket, so |
| 605 |
// subtract it out of the non-notable tallies. |
| 606 |
realmStats.classInfo.subtract(info); |
| 607 |
} |
| 608 |
// Release |allClasses| now, rather than waiting for zStats's destruction, to |
| 609 |
// reduce peak memory consumption during reporting. |
| 610 |
realmStats.allClasses.reset(); |
| 611 |
return true; |
| 612 |
} |
| 613 |
|
| 614 |
static bool FindNotableScriptSources(JS::RuntimeSizes& runtime) { |
| 615 |
using namespace JS; |
| 616 |
|
| 617 |
// We should only run FindNotableScriptSources once per RuntimeSizes. |
| 618 |
MOZ_ASSERT(runtime.notableScriptSources.empty()); |
| 619 |
|
| 620 |
for (RuntimeSizes::ScriptSourcesHashMap::Range r = |
| 621 |
runtime.allScriptSources->all(); |
| 622 |
!r.empty(); r.popFront()) { |
| 623 |
const char* filename = r.front().key(); |
| 624 |
ScriptSourceInfo& info = r.front().value(); |
| 625 |
|
| 626 |
if (!info.isNotable()) { |
| 627 |
continue; |
| 628 |
} |
| 629 |
|
| 630 |
if (!runtime.notableScriptSources.emplaceBack(filename, info)) { |
| 631 |
return false; |
| 632 |
} |
| 633 |
|
| 634 |
// We're moving this script source from a non-notable to a notable |
| 635 |
// bucket, so subtract its sizes from the non-notable tallies. |
| 636 |
runtime.scriptSourceInfo.subtract(info); |
| 637 |
} |
| 638 |
// Release |allScriptSources| now, rather than waiting for zStats's |
| 639 |
// destruction, to reduce peak memory consumption during reporting. |
| 640 |
runtime.allScriptSources.reset(); |
| 641 |
return true; |
| 642 |
} |
| 643 |
|
| 644 |
static bool CollectRuntimeStatsHelper(JSContext* cx, RuntimeStats* rtStats, |
| 645 |
ObjectPrivateVisitor* opv, bool anonymize, |
| 646 |
IterateCellCallback statsCellCallback) { |
| 647 |
// Finish any ongoing incremental GC that may change the data we're gathering |
| 648 |
// and ensure that we don't do anything that could start another one. |
| 649 |
gc::FinishGC(cx); |
| 650 |
JS::AutoAssertNoGC nogc(cx); |
| 651 |
|
| 652 |
// Wait for any background tasks to finish. |
| 653 |
WaitForAllHelperThreads(); |
| 654 |
|
| 655 |
JSRuntime* rt = cx->runtime(); |
| 656 |
if (!rtStats->realmStatsVector.reserve(rt->numRealms)) { |
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
size_t totalZones = rt->gc.zones().length(); |
| 661 |
if (!rtStats->zoneStatsVector.reserve(totalZones)) { |
| 662 |
return false; |
| 663 |
} |
| 664 |
|
| 665 |
rtStats->gcHeapChunkTotal = |
| 666 |
size_t(JS_GetGCParameter(cx, JSGC_TOTAL_CHUNKS)) * gc::ChunkSize; |
| 667 |
|
| 668 |
rtStats->gcHeapUnusedChunks = |
| 669 |
size_t(JS_GetGCParameter(cx, JSGC_UNUSED_CHUNKS)) * gc::ChunkSize; |
| 670 |
|
| 671 |
if (js::gc::DecommitEnabled()) { |
| 672 |
IterateChunks(cx, &rtStats->gcHeapDecommittedPages, |
| 673 |
DecommittedPagesChunkCallback); |
| 674 |
} |
| 675 |
|
| 676 |
// Take the per-compartment measurements. |
| 677 |
StatsClosure closure(rtStats, opv, anonymize); |
| 678 |
IterateHeapUnbarriered(cx, &closure, StatsZoneCallback, StatsRealmCallback, |
| 679 |
StatsArenaCallback, statsCellCallback); |
| 680 |
|
| 681 |
// Take the "explicit/js/runtime/" measurements. |
| 682 |
rt->addSizeOfIncludingThis(rtStats->mallocSizeOf_, &rtStats->runtime); |
| 683 |
|
| 684 |
if (!FindNotableScriptSources(rtStats->runtime)) { |
| 685 |
return false; |
| 686 |
} |
| 687 |
|
| 688 |
JS::ZoneStatsVector& zs = rtStats->zoneStatsVector; |
| 689 |
ZoneStats& zTotals = rtStats->zTotals; |
| 690 |
|
| 691 |
// We don't look for notable strings for zTotals. So we first sum all the |
| 692 |
// zones' measurements to get the totals. Then we find the notable strings |
| 693 |
// within each zone. |
| 694 |
for (size_t i = 0; i < zs.length(); i++) { |
| 695 |
zTotals.addSizes(zs[i]); |
| 696 |
} |
| 697 |
|
| 698 |
for (size_t i = 0; i < zs.length(); i++) { |
| 699 |
if (!FindNotableStrings(zs[i])) { |
| 700 |
return false; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
MOZ_ASSERT(!zTotals.allStrings); |
| 705 |
|
| 706 |
JS::RealmStatsVector& realmStats = rtStats->realmStatsVector; |
| 707 |
RealmStats& realmTotals = rtStats->realmTotals; |
| 708 |
|
| 709 |
// As with the zones, we sum all realms first, and then get the |
| 710 |
// notable classes within each zone. |
| 711 |
for (size_t i = 0; i < realmStats.length(); i++) { |
| 712 |
realmTotals.addSizes(realmStats[i]); |
| 713 |
} |
| 714 |
|
| 715 |
for (size_t i = 0; i < realmStats.length(); i++) { |
| 716 |
if (!FindNotableClasses(realmStats[i])) { |
| 717 |
return false; |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
MOZ_ASSERT(!realmTotals.allClasses); |
| 722 |
|
| 723 |
rtStats->gcHeapGCThings = rtStats->zTotals.sizeOfLiveGCThings() + |
| 724 |
rtStats->realmTotals.sizeOfLiveGCThings(); |
| 725 |
|
| 726 |
#ifdef DEBUG |
| 727 |
// Check that the in-arena measurements look ok. |
| 728 |
size_t totalArenaSize = rtStats->zTotals.gcHeapArenaAdmin + |
| 729 |
rtStats->zTotals.unusedGCThings.totalSize() + |
| 730 |
rtStats->gcHeapGCThings; |
| 731 |
MOZ_ASSERT(totalArenaSize % gc::ArenaSize == 0); |
| 732 |
#endif |
| 733 |
|
| 734 |
for (RealmsIter realm(rt); !realm.done(); realm.next()) { |
| 735 |
realm->nullRealmStats(); |
| 736 |
} |
| 737 |
|
| 738 |
size_t numDirtyChunks = |
| 739 |
(rtStats->gcHeapChunkTotal - rtStats->gcHeapUnusedChunks) / gc::ChunkSize; |
| 740 |
size_t perChunkAdmin = |
| 741 |
sizeof(gc::ArenaChunk) - (sizeof(gc::Arena) * gc::ArenasPerChunk); |
| 742 |
rtStats->gcHeapChunkAdmin = numDirtyChunks * perChunkAdmin; |
| 743 |
|
| 744 |
// |gcHeapUnusedArenas| is the only thing left. Compute it in terms of |
| 745 |
// all the others. See the comment in RuntimeStats for explanation. |
| 746 |
rtStats->gcHeapUnusedArenas = |
| 747 |
rtStats->gcHeapChunkTotal - rtStats->gcHeapDecommittedPages - |
| 748 |
rtStats->gcHeapUnusedChunks - |
| 749 |
rtStats->zTotals.unusedGCThings.totalSize() - rtStats->gcHeapChunkAdmin - |
| 750 |
rtStats->zTotals.gcHeapArenaAdmin - rtStats->gcHeapGCThings; |
| 751 |
return true; |
| 752 |
} |
| 753 |
|
| 754 |
JS_PUBLIC_API bool JS::CollectGlobalStats(GlobalStats* gStats) { |
| 755 |
AutoLockHelperThreadState lock; |
| 756 |
|
| 757 |
// HelperThreadState holds data that is not part of a Runtime. This does |
| 758 |
// not include data is is currently being processed by a HelperThread. |
| 759 |
if (IsHelperThreadStateInitialized()) { |
| 760 |
HelperThreadState().addSizeOfIncludingThis(gStats, lock); |
| 761 |
} |
| 762 |
|
| 763 |
return true; |
| 764 |
} |
| 765 |
|
| 766 |
JS_PUBLIC_API bool JS::CollectRuntimeStats(JSContext* cx, RuntimeStats* rtStats, |
| 767 |
ObjectPrivateVisitor* opv, |
| 768 |
bool anonymize) { |
| 769 |
return CollectRuntimeStatsHelper(cx, rtStats, opv, anonymize, |
| 770 |
StatsCellCallback<FineGrained>); |
| 771 |
} |
| 772 |
|
| 773 |
JS_PUBLIC_API size_t JS::SystemCompartmentCount(JSContext* cx) { |
| 774 |
size_t n = 0; |
| 775 |
for (CompartmentsIter comp(cx->runtime()); !comp.done(); comp.next()) { |
| 776 |
if (IsSystemCompartment(comp)) { |
| 777 |
++n; |
| 778 |
} |
| 779 |
} |
| 780 |
return n; |
| 781 |
} |
| 782 |
|
| 783 |
JS_PUBLIC_API size_t JS::UserCompartmentCount(JSContext* cx) { |
| 784 |
size_t n = 0; |
| 785 |
for (CompartmentsIter comp(cx->runtime()); !comp.done(); comp.next()) { |
| 786 |
if (!IsSystemCompartment(comp)) { |
| 787 |
++n; |
| 788 |
} |
| 789 |
} |
| 790 |
return n; |
| 791 |
} |
| 792 |
|
| 793 |
JS_PUBLIC_API size_t JS::SystemRealmCount(JSContext* cx) { |
| 794 |
size_t n = 0; |
| 795 |
for (RealmsIter realm(cx->runtime()); !realm.done(); realm.next()) { |
| 796 |
if (realm->isSystem()) { |
| 797 |
++n; |
| 798 |
} |
| 799 |
} |
| 800 |
return n; |
| 801 |
} |
| 802 |
|
| 803 |
JS_PUBLIC_API size_t JS::UserRealmCount(JSContext* cx) { |
| 804 |
size_t n = 0; |
| 805 |
for (RealmsIter realm(cx->runtime()); !realm.done(); realm.next()) { |
| 806 |
if (!realm->isSystem()) { |
| 807 |
++n; |
| 808 |
} |
| 809 |
} |
| 810 |
return n; |
| 811 |
} |
| 812 |
|
| 813 |
JS_PUBLIC_API size_t JS::PeakSizeOfTemporary(const JSContext* cx) { |
| 814 |
return cx->tempLifoAlloc().peakSizeOfExcludingThis(); |
| 815 |
} |
| 816 |
|
| 817 |
namespace JS { |
| 818 |
|
| 819 |
class SimpleJSRuntimeStats : public JS::RuntimeStats { |
| 820 |
public: |
| 821 |
explicit SimpleJSRuntimeStats(MallocSizeOf mallocSizeOf) |
| 822 |
: JS::RuntimeStats(mallocSizeOf) {} |
| 823 |
|
| 824 |
virtual void initExtraZoneStats(JS::Zone* zone, JS::ZoneStats* zStats, |
| 825 |
const JS::AutoRequireNoGC& nogc) override {} |
| 826 |
|
| 827 |
virtual void initExtraRealmStats(Realm* realm, JS::RealmStats* realmStats, |
| 828 |
const JS::AutoRequireNoGC& nogc) override {} |
| 829 |
}; |
| 830 |
|
| 831 |
JS_PUBLIC_API bool AddSizeOfTab(JSContext* cx, HandleObject obj, |
| 832 |
MallocSizeOf mallocSizeOf, |
| 833 |
ObjectPrivateVisitor* opv, TabSizes* sizes) { |
| 834 |
SimpleJSRuntimeStats rtStats(mallocSizeOf); |
| 835 |
|
| 836 |
JS::Zone* zone = GetObjectZone(obj); |
| 837 |
|
| 838 |
size_t numRealms = 0; |
| 839 |
for (CompartmentsInZoneIter comp(zone); !comp.done(); comp.next()) { |
| 840 |
numRealms += comp->realms().length(); |
| 841 |
} |
| 842 |
|
| 843 |
if (!rtStats.realmStatsVector.reserve(numRealms)) { |
| 844 |
return false; |
| 845 |
} |
| 846 |
|
| 847 |
if (!rtStats.zoneStatsVector.reserve(1)) { |
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
// Take the per-compartment measurements. No need to anonymize because |
| 852 |
// these measurements will be aggregated. |
| 853 |
StatsClosure closure(&rtStats, opv, /* anonymize = */ false); |
| 854 |
IterateHeapUnbarrieredForZone(cx, zone, &closure, StatsZoneCallback, |
| 855 |
StatsRealmCallback, StatsArenaCallback, |
| 856 |
StatsCellCallback<CoarseGrained>); |
| 857 |
|
| 858 |
MOZ_ASSERT(rtStats.zoneStatsVector.length() == 1); |
| 859 |
rtStats.zTotals.addSizes(rtStats.zoneStatsVector[0]); |
| 860 |
|
| 861 |
for (size_t i = 0; i < rtStats.realmStatsVector.length(); i++) { |
| 862 |
rtStats.realmTotals.addSizes(rtStats.realmStatsVector[i]); |
| 863 |
} |
| 864 |
|
| 865 |
for (RealmsInZoneIter realm(zone); !realm.done(); realm.next()) { |
| 866 |
realm->nullRealmStats(); |
| 867 |
} |
| 868 |
|
| 869 |
rtStats.zTotals.addToTabSizes(sizes); |
| 870 |
rtStats.realmTotals.addToTabSizes(sizes); |
| 871 |
|
| 872 |
return true; |
| 873 |
} |
| 874 |
|
| 875 |
JS_PUBLIC_API bool AddServoSizeOf(JSContext* cx, MallocSizeOf mallocSizeOf, |
| 876 |
ObjectPrivateVisitor* opv, |
| 877 |
ServoSizes* sizes) { |
| 878 |
SimpleJSRuntimeStats rtStats(mallocSizeOf); |
| 879 |
|
| 880 |
// No need to anonymize because the results will be aggregated. |
| 881 |
if (!CollectRuntimeStatsHelper(cx, &rtStats, opv, /* anonymize = */ false, |
| 882 |
StatsCellCallback<CoarseGrained>)) |
| 883 |
return false; |
| 884 |
|
| 885 |
#ifdef DEBUG |
| 886 |
size_t gcHeapTotalOriginal = sizes->gcHeapUsed + sizes->gcHeapUnused + |
| 887 |
sizes->gcHeapAdmin + sizes->gcHeapDecommitted; |
| 888 |
#endif |
| 889 |
|
| 890 |
rtStats.addToServoSizes(sizes); |
| 891 |
rtStats.zTotals.addToServoSizes(sizes); |
| 892 |
rtStats.realmTotals.addToServoSizes(sizes); |
| 893 |
|
| 894 |
#ifdef DEBUG |
| 895 |
size_t gcHeapTotal = sizes->gcHeapUsed + sizes->gcHeapUnused + |
| 896 |
sizes->gcHeapAdmin + sizes->gcHeapDecommitted; |
| 897 |
MOZ_ASSERT(rtStats.gcHeapChunkTotal == gcHeapTotal - gcHeapTotalOriginal); |
| 898 |
#endif |
| 899 |
|
| 900 |
return true; |
| 901 |
} |
| 902 |
|
| 903 |
} // namespace JS |