v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
heap-layout-tracer.cc
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <iostream>
8
12#include "src/heap/spaces-inl.h"
13
14namespace v8 {
15namespace internal {
16
17namespace {
18
19constexpr const char* TypeToCollectorName(v8::GCType gc_type) {
20 switch (gc_type) {
21 case kGCTypeScavenge:
22 return "Scavenger";
24 return "Mark-Compact";
26 return "Minor Mark-Sweep";
27 default:
28 break;
29 }
30 return "Unknown collector";
31}
32
33} // namespace
34
35// static
37 v8::GCType gc_type,
39 void* data) {
40 Heap* heap = reinterpret_cast<i::Isolate*>(isolate)->heap();
41 // gc_count_ will increase after this callback, manually add 1.
42 PrintF("Before GC:%d,", heap->gc_count() + 1);
43 PrintF("collector_name:%s\n", TypeToCollectorName(gc_type));
44 PrintHeapLayout(std::cout, heap);
45}
46
47// static
49 v8::GCType gc_type,
51 void* data) {
52 Heap* heap = reinterpret_cast<i::Isolate*>(isolate)->heap();
53 PrintF("After GC:%d,", heap->gc_count());
54 PrintF("collector_name:%s\n", TypeToCollectorName(gc_type));
55 PrintHeapLayout(std::cout, heap);
56}
57
58// static
60 const MemoryChunkMetadata& chunk,
61 const char* owner_name) {
62 os << "{owner:" << owner_name << ","
63 << "address:" << &chunk << ","
64 << "size:" << chunk.size() << ","
65 << "allocated_bytes:" << chunk.allocated_bytes() << ","
66 << "wasted_memory:" << chunk.wasted_memory() << "}" << std::endl;
67}
68
69// static
71 if (v8_flags.minor_ms) {
72 for (const PageMetadata* page : *heap->paged_new_space()) {
73 PrintMemoryChunk(os, *page, "new_space");
74 }
75 } else {
76 const SemiSpaceNewSpace* semi_space_new_space =
77 SemiSpaceNewSpace::From(heap->new_space());
78 for (const PageMetadata* page : semi_space_new_space->to_space()) {
79 PrintMemoryChunk(os, *page, "to_space");
80 }
81
82 for (const PageMetadata* page : semi_space_new_space->from_space()) {
83 PrintMemoryChunk(os, *page, "from_space");
84 }
85 }
86
89 while ((chunk = it.next()) != nullptr) {
90 PrintMemoryChunk(os, *chunk, ToString(chunk->owner()->identity()));
91 }
92
93 for (ReadOnlyPageMetadata* page : heap->read_only_space()->pages()) {
94 PrintMemoryChunk(os, *page, "ro_space");
95 }
96}
97} // namespace internal
98} // namespace v8
AllocationSpace identity() const
Definition base-space.h:32
static void GCEpiloguePrintHeapLayout(v8::Isolate *isolate, v8::GCType gc_type, v8::GCCallbackFlags flags, void *data)
static void GCProloguePrintHeapLayout(v8::Isolate *isolate, v8::GCType gc_type, v8::GCCallbackFlags flags, void *data)
static void PrintMemoryChunk(std::ostream &os, const MemoryChunkMetadata &chunk, const char *owner_name)
static void PrintHeapLayout(std::ostream &os, Heap *heap)
static SemiSpaceNewSpace * From(NewSpace *space)
Definition new-spaces.h:259
Isolate * isolate
constexpr const char * ToString(DeoptimizeKind kind)
Definition globals.h:880
void PrintF(const char *format,...)
Definition utils.cc:39
V8_EXPORT_PRIVATE FlagValues v8_flags
GCCallbackFlags
@ kGCTypeMinorMarkSweep
@ kGCTypeScavenge
@ kGCTypeMarkSweepCompact