v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
code-stats.cc
Go to the documentation of this file.
1// Copyright 2016 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
8#include "src/heap/heap-inl.h"
10#include "src/heap/paged-spaces-inl.h" // For PagedSpaceObjectIterator.
12
13namespace v8 {
14namespace internal {
15
16// Record code statistics.
18 Isolate* isolate) {
19 PtrComprCageBase cage_base(isolate);
20 if (IsScript(object, cage_base)) {
21 Tagged<Script> script = Cast<Script>(object);
22 // Log the size of external source code.
23 Tagged<Object> source = script->source(cage_base);
24 if (IsExternalString(source, cage_base)) {
25 Tagged<ExternalString> external_source_string =
27 int size = isolate->external_script_source_size();
28 size += external_source_string->ExternalPayloadSize();
29 isolate->set_external_script_source_size(size);
30 }
31 } else if (IsAbstractCode(object, cage_base)) {
32 // Record code+metadata statistics.
33 Tagged<AbstractCode> abstract_code = Cast<AbstractCode>(object);
34 int size = abstract_code->SizeIncludingMetadata(cage_base);
35 if (IsCode(abstract_code, cage_base)) {
36 size += isolate->code_and_metadata_size();
37 isolate->set_code_and_metadata_size(size);
38 } else {
39 size += isolate->bytecode_and_metadata_size();
40 isolate->set_bytecode_and_metadata_size(size);
41 }
42
43#ifdef DEBUG
44 CodeKind code_kind = abstract_code->kind(cage_base);
45 isolate->code_kind_statistics()[static_cast<int>(code_kind)] +=
46 abstract_code->Size(cage_base);
47#endif
48 }
49}
50
52 isolate->set_code_and_metadata_size(0);
53 isolate->set_bytecode_and_metadata_size(0);
54 isolate->set_external_script_source_size(0);
55#ifdef DEBUG
56 ResetCodeStatistics(isolate);
57#endif
58}
59
60// Collects code size statistics:
61// - code and metadata size
62// - by code kind (only in debug mode)
64 Isolate* isolate) {
65 PagedSpaceObjectIterator obj_it(isolate->heap(), space);
66 for (Tagged<HeapObject> obj = obj_it.Next(); !obj.is_null();
67 obj = obj_it.Next()) {
69 }
70}
71
72// Collects code size statistics in OldLargeObjectSpace:
73// - code and metadata size
74// - by code kind (only in debug mode)
76 Isolate* isolate) {
78 for (Tagged<HeapObject> obj = obj_it.Next(); !obj.is_null();
79 obj = obj_it.Next()) {
81 }
82}
83
84#ifdef DEBUG
85void CodeStatistics::ReportCodeStatistics(Isolate* isolate) {
86 // Report code kind statistics
87 int* code_kind_statistics = isolate->code_kind_statistics();
88 PrintF("\n Code kind histograms: \n");
89 for (int i = 0; i < kCodeKindCount; i++) {
90 if (code_kind_statistics[i] > 0) {
91 PrintF(" %-20s: %10d bytes\n",
92 CodeKindToString(static_cast<CodeKind>(i)),
93 code_kind_statistics[i]);
94 }
95 }
96 PrintF("\n");
97
98 // Report code and metadata statistics
99 if (isolate->code_and_metadata_size() > 0) {
100 PrintF("Code size including metadata : %10d bytes\n",
101 isolate->code_and_metadata_size());
102 }
103 if (isolate->bytecode_and_metadata_size() > 0) {
104 PrintF("Bytecode size including metadata: %10d bytes\n",
105 isolate->bytecode_and_metadata_size());
106 }
107
108 PrintF("\n");
109}
110
111void CodeStatistics::ResetCodeStatistics(Isolate* isolate) {
112 // Clear code kind statistics
113 int* code_kind_statistics = isolate->code_kind_statistics();
114 for (int i = 0; i < kCodeKindCount; i++) {
115 code_kind_statistics[i] = 0;
116 }
117}
118#endif
119
120} // namespace internal
121} // namespace v8
static void RecordCodeAndMetadataStatistics(Tagged< HeapObject > object, Isolate *isolate)
Definition code-stats.cc:17
static void ResetCodeAndMetadataStatistics(Isolate *isolate)
Definition code-stats.cc:51
static void CollectCodeStatistics(PagedSpace *space, Isolate *isolate)
Definition code-stats.cc:63
Tagged< HeapObject > Next() override
Tagged< HeapObject > Next() override
V8_INLINE constexpr bool is_null() const
Definition tagged.h:502
static constexpr int kCodeKindCount
Definition code-kind.h:42
void PrintF(const char *format,...)
Definition utils.cc:39
const char * CodeKindToString(CodeKind kind)
Definition code-kind.cc:10
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150