Skip to content

Commit a5c0bc4

Browse files
psmarshalltargos
authored andcommitted
deps: backport 4 CPU profiler commits from upstream V8
[cpu-profiler] Add a new profiling mode with a more detailed call tree. https://chromium.googlesource.com/v8/v8.git/+/ecae80cdb350dde1e654c531b56f5b6c44dc8c77 [cpu-profiler] Reuse free slots in code_entries_ https://chromium.googlesource.com/v8/v8.git/+/3e1126bf15e62c433c4e9cb21316d182f691c63a [cpu-profiler] Only store deopt inline frames for functions that need it https://chromium.googlesource.com/v8/v8.git/+/0bfcbdd4726920755e51dab28c18ab93e050819b [cpu-profiler] Use instruction start as the key for the CodeMap https://chromium.googlesource.com/v8/v8.git/+/ba752ea4c50713dff1e94f45a79db3ba968a8d66 PR-URL: #22028 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benedikt Meurer <[email protected]>
1 parent 7a70dce commit a5c0bc4

21 files changed

+469
-204
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Reset this number to 0 on major V8 upgrades.
3131
# Increment by one for each non-official patch applied to deps/v8.
32-
'v8_embedder_string': '-node.22',
32+
'v8_embedder_string': '-node.23',
3333

3434
# Enable disassembler for `--print-code` v8 options
3535
'v8_enable_disassembler': 1,

deps/v8/include/v8-profiler.h

+17
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ class V8_EXPORT CpuProfile {
273273
void Delete();
274274
};
275275

276+
enum CpuProfilingMode {
277+
// In the resulting CpuProfile tree, intermediate nodes in a stack trace
278+
// (from the root to a leaf) will have line numbers that point to the start
279+
// line of the function, rather than the line of the callsite of the child.
280+
kLeafNodeLineNumbers,
281+
// In the resulting CpuProfile tree, nodes are separated based on the line
282+
// number of their callsite in their parent.
283+
kCallerLineNumbers,
284+
};
285+
276286
/**
277287
* Interface for controlling CPU profiling. Instance of the
278288
* profiler can be created using v8::CpuProfiler::New method.
@@ -316,6 +326,13 @@ class V8_EXPORT CpuProfiler {
316326
* |record_samples| parameter controls whether individual samples should
317327
* be recorded in addition to the aggregated tree.
318328
*/
329+
void StartProfiling(Local<String> title, CpuProfilingMode mode,
330+
bool record_samples = false);
331+
/**
332+
* The same as StartProfiling above, but the CpuProfilingMode defaults to
333+
* kLeafNodeLineNumbers mode, which was the previous default behavior of the
334+
* profiler.
335+
*/
319336
void StartProfiling(Local<String> title, bool record_samples = false);
320337

321338
/**

deps/v8/src/api.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -10005,7 +10005,7 @@ const char* CpuProfileNode::GetScriptResourceNameStr() const {
1000510005
}
1000610006

1000710007
int CpuProfileNode::GetLineNumber() const {
10008-
return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
10008+
return reinterpret_cast<const i::ProfileNode*>(this)->line_number();
1000910009
}
1001010010

1001110011

@@ -10143,9 +10143,14 @@ void CpuProfiler::CollectSample() {
1014310143

1014410144
void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) {
1014510145
reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
10146-
*Utils::OpenHandle(*title), record_samples);
10146+
*Utils::OpenHandle(*title), record_samples, kLeafNodeLineNumbers);
1014710147
}
1014810148

10149+
void CpuProfiler::StartProfiling(Local<String> title, CpuProfilingMode mode,
10150+
bool record_samples) {
10151+
reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
10152+
*Utils::OpenHandle(*title), record_samples, mode);
10153+
}
1014910154

1015010155
CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
1015110156
return reinterpret_cast<CpuProfile*>(

deps/v8/src/code-events.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class CodeEventListener {
8383
virtual void GetterCallbackEvent(Name* name, Address entry_point) = 0;
8484
virtual void SetterCallbackEvent(Name* name, Address entry_point) = 0;
8585
virtual void RegExpCodeCreateEvent(AbstractCode* code, String* source) = 0;
86-
virtual void CodeMoveEvent(AbstractCode* from, Address to) = 0;
86+
virtual void CodeMoveEvent(AbstractCode* from, AbstractCode* to) = 0;
8787
virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
8888
virtual void CodeMovingGCEvent() = 0;
8989
virtual void CodeDisableOptEvent(AbstractCode* code,
@@ -155,7 +155,7 @@ class CodeEventDispatcher {
155155
void RegExpCodeCreateEvent(AbstractCode* code, String* source) {
156156
CODE_EVENT_DISPATCH(RegExpCodeCreateEvent(code, source));
157157
}
158-
void CodeMoveEvent(AbstractCode* from, Address to) {
158+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) {
159159
CODE_EVENT_DISPATCH(CodeMoveEvent(from, to));
160160
}
161161
void SharedFunctionInfoMoveEvent(Address from, Address to) {

deps/v8/src/heap/mark-compact.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ class ProfilingMigrationObserver final : public MigrationObserver {
11361136
int size) final {
11371137
if (dest == CODE_SPACE || (dest == OLD_SPACE && dst->IsBytecodeArray())) {
11381138
PROFILE(heap_->isolate(),
1139-
CodeMoveEvent(AbstractCode::cast(src), dst->address()));
1139+
CodeMoveEvent(AbstractCode::cast(src), AbstractCode::cast(dst)));
11401140
}
11411141
heap_->OnMoveEvent(dst, src, size);
11421142
}

deps/v8/src/log.cc

+10-15
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class PerfBasicLogger : public CodeEventLogger {
270270
PerfBasicLogger();
271271
~PerfBasicLogger() override;
272272

273-
void CodeMoveEvent(AbstractCode* from, Address to) override {}
273+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) override {}
274274
void CodeDisableOptEvent(AbstractCode* code,
275275
SharedFunctionInfo* shared) override {}
276276

@@ -492,7 +492,7 @@ class LowLevelLogger : public CodeEventLogger {
492492
explicit LowLevelLogger(const char* file_name);
493493
~LowLevelLogger() override;
494494

495-
void CodeMoveEvent(AbstractCode* from, Address to) override;
495+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) override;
496496
void CodeDisableOptEvent(AbstractCode* code,
497497
SharedFunctionInfo* shared) override {}
498498
void SnapshotPositionEvent(HeapObject* obj, int pos);
@@ -610,11 +610,10 @@ void LowLevelLogger::LogRecordedBuffer(const wasm::WasmCode* code,
610610
code->instructions().length());
611611
}
612612

613-
void LowLevelLogger::CodeMoveEvent(AbstractCode* from, Address to) {
613+
void LowLevelLogger::CodeMoveEvent(AbstractCode* from, AbstractCode* to) {
614614
CodeMoveStruct event;
615615
event.from_address = from->InstructionStart();
616-
size_t header_size = from->InstructionStart() - from->address();
617-
event.to_address = to + header_size;
616+
event.to_address = to->InstructionStart();
618617
LogWriteStruct(event);
619618
}
620619

@@ -636,7 +635,7 @@ class JitLogger : public CodeEventLogger {
636635
public:
637636
explicit JitLogger(JitCodeEventHandler code_event_handler);
638637

639-
void CodeMoveEvent(AbstractCode* from, Address to) override;
638+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) override;
640639
void CodeDisableOptEvent(AbstractCode* code,
641640
SharedFunctionInfo* shared) override {}
642641
void AddCodeLinePosInfoEvent(void* jit_handler_data, int pc_offset,
@@ -694,7 +693,7 @@ void JitLogger::LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
694693
code_event_handler_(&event);
695694
}
696695

697-
void JitLogger::CodeMoveEvent(AbstractCode* from, Address to) {
696+
void JitLogger::CodeMoveEvent(AbstractCode* from, AbstractCode* to) {
698697
base::LockGuard<base::Mutex> guard(&logger_mutex_);
699698

700699
JitCodeEvent event;
@@ -703,12 +702,7 @@ void JitLogger::CodeMoveEvent(AbstractCode* from, Address to) {
703702
from->IsCode() ? JitCodeEvent::JIT_CODE : JitCodeEvent::BYTE_CODE;
704703
event.code_start = reinterpret_cast<void*>(from->InstructionStart());
705704
event.code_len = from->InstructionSize();
706-
707-
// Calculate the header size.
708-
const size_t header_size = from->InstructionStart() - from->address();
709-
710-
// Calculate the new start address of the instructions.
711-
event.new_code_start = reinterpret_cast<void*>(to + header_size);
705+
event.new_code_start = reinterpret_cast<void*>(to->InstructionStart());
712706

713707
code_event_handler_(&event);
714708
}
@@ -1450,9 +1444,10 @@ void Logger::RegExpCodeCreateEvent(AbstractCode* code, String* source) {
14501444
msg.WriteToLogFile();
14511445
}
14521446

1453-
void Logger::CodeMoveEvent(AbstractCode* from, Address to) {
1447+
void Logger::CodeMoveEvent(AbstractCode* from, AbstractCode* to) {
14541448
if (!is_listening_to_code_events()) return;
1455-
MoveEventInternal(CodeEventListener::CODE_MOVE_EVENT, from->address(), to);
1449+
MoveEventInternal(CodeEventListener::CODE_MOVE_EVENT, from->address(),
1450+
to->address());
14561451
}
14571452

14581453
namespace {

deps/v8/src/log.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class Logger : public CodeEventListener {
209209
// Emits a code create event for a RegExp.
210210
void RegExpCodeCreateEvent(AbstractCode* code, String* source);
211211
// Emits a code move event.
212-
void CodeMoveEvent(AbstractCode* from, Address to);
212+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to);
213213
// Emits a code line info record event.
214214
void CodeLinePosInfoRecordEvent(Address code_start,
215215
ByteArray* source_position_table);
@@ -466,7 +466,7 @@ class ExternalCodeEventListener : public CodeEventListener {
466466
void GetterCallbackEvent(Name* name, Address entry_point) override {}
467467
void SetterCallbackEvent(Name* name, Address entry_point) override {}
468468
void SharedFunctionInfoMoveEvent(Address from, Address to) override {}
469-
void CodeMoveEvent(AbstractCode* from, Address to) override {}
469+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) override {}
470470
void CodeDisableOptEvent(AbstractCode* code,
471471
SharedFunctionInfo* shared) override {}
472472
void CodeMovingGCEvent() override {}

deps/v8/src/perf-jit.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ void PerfJitLogger::LogWriteUnwindingInfo(Code* code) {
419419
LogWriteBytes(padding_bytes, static_cast<int>(padding_size));
420420
}
421421

422-
void PerfJitLogger::CodeMoveEvent(AbstractCode* from, Address to) {
422+
void PerfJitLogger::CodeMoveEvent(AbstractCode* from, AbstractCode* to) {
423423
// We may receive a CodeMove event if a BytecodeArray object moves. Otherwise
424424
// code relocation is not supported.
425425
CHECK(from->IsBytecodeArray());

deps/v8/src/perf-jit.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PerfJitLogger : public CodeEventLogger {
4141
PerfJitLogger();
4242
virtual ~PerfJitLogger();
4343

44-
void CodeMoveEvent(AbstractCode* from, Address to) override;
44+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) override;
4545
void CodeDisableOptEvent(AbstractCode* code,
4646
SharedFunctionInfo* shared) override {}
4747

@@ -118,7 +118,7 @@ class PerfJitLogger : public CodeEventLogger {
118118
// PerfJitLogger is only implemented on Linux
119119
class PerfJitLogger : public CodeEventLogger {
120120
public:
121-
void CodeMoveEvent(AbstractCode* from, Address to) override {
121+
void CodeMoveEvent(AbstractCode* from, AbstractCode* to) override {
122122
UNIMPLEMENTED();
123123
}
124124

deps/v8/src/profiler/cpu-profiler-inl.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,35 @@ namespace v8 {
1616
namespace internal {
1717

1818
void CodeCreateEventRecord::UpdateCodeMap(CodeMap* code_map) {
19-
code_map->AddCode(start, entry, size);
19+
code_map->AddCode(instruction_start, entry, instruction_size);
2020
}
2121

2222

2323
void CodeMoveEventRecord::UpdateCodeMap(CodeMap* code_map) {
24-
code_map->MoveCode(from, to);
24+
code_map->MoveCode(from_instruction_start, to_instruction_start);
2525
}
2626

2727

2828
void CodeDisableOptEventRecord::UpdateCodeMap(CodeMap* code_map) {
29-
CodeEntry* entry = code_map->FindEntry(start);
29+
CodeEntry* entry = code_map->FindEntry(instruction_start);
3030
if (entry != nullptr) {
3131
entry->set_bailout_reason(bailout_reason);
3232
}
3333
}
3434

3535

3636
void CodeDeoptEventRecord::UpdateCodeMap(CodeMap* code_map) {
37-
CodeEntry* entry = code_map->FindEntry(start);
38-
if (entry != nullptr) entry->set_deopt_info(deopt_reason, deopt_id);
37+
CodeEntry* entry = code_map->FindEntry(instruction_start);
38+
if (entry == nullptr) return;
39+
std::vector<CpuProfileDeoptFrame> frames_vector(
40+
deopt_frames, deopt_frames + deopt_frame_count);
41+
entry->set_deopt_info(deopt_reason, deopt_id, std::move(frames_vector));
42+
delete[] deopt_frames;
3943
}
4044

4145

4246
void ReportBuiltinEventRecord::UpdateCodeMap(CodeMap* code_map) {
43-
CodeEntry* entry = code_map->FindEntry(start);
47+
CodeEntry* entry = code_map->FindEntry(instruction_start);
4448
if (!entry) {
4549
// Code objects for builtins should already have been added to the map but
4650
// some of them have been filtered out by CpuProfiler.

deps/v8/src/profiler/cpu-profiler.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,20 @@ void CpuProfiler::CollectSample() {
345345
}
346346
}
347347

348-
void CpuProfiler::StartProfiling(const char* title, bool record_samples) {
349-
if (profiles_->StartProfiling(title, record_samples)) {
348+
void CpuProfiler::StartProfiling(const char* title, bool record_samples,
349+
ProfilingMode mode) {
350+
if (profiles_->StartProfiling(title, record_samples, mode)) {
350351
TRACE_EVENT0("v8", "CpuProfiler::StartProfiling");
351352
StartProcessorIfNotStarted();
352353
}
353354
}
354355

355-
356-
void CpuProfiler::StartProfiling(String* title, bool record_samples) {
357-
StartProfiling(profiles_->GetName(title), record_samples);
356+
void CpuProfiler::StartProfiling(String* title, bool record_samples,
357+
ProfilingMode mode) {
358+
StartProfiling(profiles_->GetName(title), record_samples, mode);
358359
isolate_->debug()->feature_tracker()->Track(DebugFeatureTracker::kProfiler);
359360
}
360361

361-
362362
void CpuProfiler::StartProcessorIfNotStarted() {
363363
if (processor_) {
364364
processor_->AddCurrentStack(isolate_);
@@ -426,7 +426,7 @@ void CpuProfiler::LogBuiltins() {
426426
CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN);
427427
ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_;
428428
Builtins::Name id = static_cast<Builtins::Name>(i);
429-
rec->start = builtins->builtin(id)->address();
429+
rec->instruction_start = builtins->builtin(id)->InstructionStart();
430430
rec->builtin_id = id;
431431
processor_->Enqueue(evt_rec);
432432
}

deps/v8/src/profiler/cpu-profiler.h

+14-9
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@ class CodeEventRecord {
5353

5454
class CodeCreateEventRecord : public CodeEventRecord {
5555
public:
56-
Address start;
56+
Address instruction_start;
5757
CodeEntry* entry;
58-
unsigned size;
58+
unsigned instruction_size;
5959

6060
INLINE(void UpdateCodeMap(CodeMap* code_map));
6161
};
6262

6363

6464
class CodeMoveEventRecord : public CodeEventRecord {
6565
public:
66-
Address from;
67-
Address to;
66+
Address from_instruction_start;
67+
Address to_instruction_start;
6868

6969
INLINE(void UpdateCodeMap(CodeMap* code_map));
7070
};
7171

7272

7373
class CodeDisableOptEventRecord : public CodeEventRecord {
7474
public:
75-
Address start;
75+
Address instruction_start;
7676
const char* bailout_reason;
7777

7878
INLINE(void UpdateCodeMap(CodeMap* code_map));
@@ -81,19 +81,21 @@ class CodeDisableOptEventRecord : public CodeEventRecord {
8181

8282
class CodeDeoptEventRecord : public CodeEventRecord {
8383
public:
84-
Address start;
84+
Address instruction_start;
8585
const char* deopt_reason;
8686
int deopt_id;
8787
Address pc;
8888
int fp_to_sp_delta;
89+
CpuProfileDeoptFrame* deopt_frames;
90+
int deopt_frame_count;
8991

9092
INLINE(void UpdateCodeMap(CodeMap* code_map));
9193
};
9294

9395

9496
class ReportBuiltinEventRecord : public CodeEventRecord {
9597
public:
96-
Address start;
98+
Address instruction_start;
9799
Builtins::Name builtin_id;
98100

99101
INLINE(void UpdateCodeMap(CodeMap* code_map));
@@ -197,10 +199,13 @@ class CpuProfiler : public CodeEventObserver {
197199

198200
static void CollectSample(Isolate* isolate);
199201

202+
typedef v8::CpuProfilingMode ProfilingMode;
203+
200204
void set_sampling_interval(base::TimeDelta value);
201205
void CollectSample();
202-
void StartProfiling(const char* title, bool record_samples = false);
203-
void StartProfiling(String* title, bool record_samples);
206+
void StartProfiling(const char* title, bool record_samples = false,
207+
ProfilingMode mode = ProfilingMode::kLeafNodeLineNumbers);
208+
void StartProfiling(String* title, bool record_samples, ProfilingMode mode);
204209
CpuProfile* StopProfiling(const char* title);
205210
CpuProfile* StopProfiling(String* title);
206211
int GetProfilesCount();

deps/v8/src/profiler/profile-generator-inl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ inline CodeEntry* ProfileGenerator::FindEntry(Address address) {
3333
}
3434

3535
ProfileNode::ProfileNode(ProfileTree* tree, CodeEntry* entry,
36-
ProfileNode* parent)
36+
ProfileNode* parent, int line_number)
3737
: tree_(tree),
3838
entry_(entry),
3939
self_ticks_(0),
40+
line_number_(line_number),
4041
parent_(parent),
4142
id_(tree->next_node_id()) {
4243
tree_->EnqueueNode(this);

0 commit comments

Comments
 (0)