Skip to content

Commit 4663d1c

Browse files
Matheus Marchinitargos
Matheus Marchini
authored andcommitted
deps: backport aa6ce3e from upstream V8
Original commit message: [log][api] introduce public CodeEventListener API Introduce a new public API called CodeEventListener to allow embedders to better support external profilers and other diagnostic tools without relying on unsupported methods like --perf-basic-prof. Bug: v8:7694 Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: I063cc965394d59401358757634c9ea84c11517e9 Co-authored-by: Daniel Beckert <[email protected]> Reviewed-on: https://chromium-review.googlesource.com/1028770 Commit-Queue: Yang Guo <[email protected]> Reviewed-by: Hannes Payer <[email protected]> Reviewed-by: Yang Guo <[email protected]> Reviewed-by: Andreas Haas <[email protected]> Cr-Commit-Position: refs/heads/master@{#53382} Refs: v8/v8@aa6ce3e PR-URL: #21126 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6b40ba1 commit 4663d1c

16 files changed

+695
-201
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Reset this number to 0 on major V8 upgrades.
2929
# Increment by one for each non-official patch applied to deps/v8.
30-
'v8_embedder_string': '-node.9',
30+
'v8_embedder_string': '-node.10',
3131

3232
# Enable disassembler for `--print-code` v8 options
3333
'v8_enable_disassembler': 1,

deps/v8/include/v8-profiler.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,76 @@ struct HeapStatsUpdate {
988988
uint32_t size; // New value of size field for the interval with this index.
989989
};
990990

991+
#define CODE_EVENTS_LIST(V) \
992+
V(Builtin) \
993+
V(Callback) \
994+
V(Eval) \
995+
V(Function) \
996+
V(InterpretedFunction) \
997+
V(Handler) \
998+
V(BytecodeHandler) \
999+
V(LazyCompile) \
1000+
V(RegExp) \
1001+
V(Script) \
1002+
V(Stub)
1003+
1004+
/**
1005+
* Note that this enum may be extended in the future. Please include a default
1006+
* case if this enum is used in a switch statement.
1007+
*/
1008+
enum CodeEventType {
1009+
kUnknownType = 0
1010+
#define V(Name) , k##Name##Type
1011+
CODE_EVENTS_LIST(V)
1012+
#undef V
1013+
};
1014+
1015+
/**
1016+
* Representation of a code creation event
1017+
*/
1018+
class V8_EXPORT CodeEvent {
1019+
public:
1020+
uintptr_t GetCodeStartAddress();
1021+
size_t GetCodeSize();
1022+
Local<String> GetFunctionName();
1023+
Local<String> GetScriptName();
1024+
int GetScriptLine();
1025+
int GetScriptColumn();
1026+
/**
1027+
* NOTE (mmarchini): We can't allocate objects in the heap when we collect
1028+
* existing code, and both the code type and the comment are not stored in the
1029+
* heap, so we return those as const char*.
1030+
*/
1031+
CodeEventType GetCodeType();
1032+
const char* GetComment();
1033+
1034+
static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1035+
};
1036+
1037+
/**
1038+
* Interface to listen to code creation events.
1039+
*/
1040+
class V8_EXPORT CodeEventHandler {
1041+
public:
1042+
/**
1043+
* Creates a new listener for the |isolate|. The isolate must be initialized.
1044+
* The listener object must be disposed after use by calling |Dispose| method.
1045+
* Multiple listeners can be created for the same isolate.
1046+
*/
1047+
explicit CodeEventHandler(Isolate* isolate);
1048+
virtual ~CodeEventHandler();
1049+
1050+
virtual void Handle(CodeEvent* code_event) = 0;
1051+
1052+
void Enable();
1053+
void Disable();
1054+
1055+
private:
1056+
CodeEventHandler();
1057+
CodeEventHandler(const CodeEventHandler&);
1058+
CodeEventHandler& operator=(const CodeEventHandler&);
1059+
void* internal_listener_;
1060+
};
9911061

9921062
} // namespace v8
9931063

deps/v8/src/api.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10211,6 +10211,70 @@ void CpuProfiler::SetIdle(bool is_idle) {
1021110211
isolate->SetIdle(is_idle);
1021210212
}
1021310213

10214+
uintptr_t CodeEvent::GetCodeStartAddress() {
10215+
return reinterpret_cast<i::CodeEvent*>(this)->code_start_address;
10216+
}
10217+
10218+
size_t CodeEvent::GetCodeSize() {
10219+
return reinterpret_cast<i::CodeEvent*>(this)->code_size;
10220+
}
10221+
10222+
Local<String> CodeEvent::GetFunctionName() {
10223+
return ToApiHandle<String>(
10224+
reinterpret_cast<i::CodeEvent*>(this)->function_name);
10225+
}
10226+
10227+
Local<String> CodeEvent::GetScriptName() {
10228+
return ToApiHandle<String>(
10229+
reinterpret_cast<i::CodeEvent*>(this)->script_name);
10230+
}
10231+
10232+
int CodeEvent::GetScriptLine() {
10233+
return reinterpret_cast<i::CodeEvent*>(this)->script_line;
10234+
}
10235+
10236+
int CodeEvent::GetScriptColumn() {
10237+
return reinterpret_cast<i::CodeEvent*>(this)->script_column;
10238+
}
10239+
10240+
CodeEventType CodeEvent::GetCodeType() {
10241+
return reinterpret_cast<i::CodeEvent*>(this)->code_type;
10242+
}
10243+
10244+
const char* CodeEvent::GetComment() {
10245+
return reinterpret_cast<i::CodeEvent*>(this)->comment;
10246+
}
10247+
10248+
const char* CodeEvent::GetCodeEventTypeName(CodeEventType code_event_type) {
10249+
switch (code_event_type) {
10250+
case kUnknownType:
10251+
return "Unknown";
10252+
#define V(Name) \
10253+
case k##Name##Type: \
10254+
return #Name;
10255+
CODE_EVENTS_LIST(V)
10256+
#undef V
10257+
}
10258+
}
10259+
10260+
CodeEventHandler::CodeEventHandler(Isolate* isolate) {
10261+
internal_listener_ =
10262+
new i::ExternalCodeEventListener(reinterpret_cast<i::Isolate*>(isolate));
10263+
}
10264+
10265+
CodeEventHandler::~CodeEventHandler() {
10266+
delete reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_);
10267+
}
10268+
10269+
void CodeEventHandler::Enable() {
10270+
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10271+
->StartListening(this);
10272+
}
10273+
10274+
void CodeEventHandler::Disable() {
10275+
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10276+
->StopListening();
10277+
}
1021410278

1021510279
static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
1021610280
return const_cast<i::HeapGraphEdge*>(

deps/v8/src/code-events.h

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,38 @@ class WasmCode;
2424
using WasmName = Vector<const char>;
2525
} // namespace wasm
2626

27-
#define LOG_EVENTS_AND_TAGS_LIST(V) \
28-
V(CODE_CREATION_EVENT, "code-creation") \
29-
V(CODE_DISABLE_OPT_EVENT, "code-disable-optimization") \
30-
V(CODE_MOVE_EVENT, "code-move") \
31-
V(CODE_DELETE_EVENT, "code-delete") \
32-
V(CODE_MOVING_GC, "code-moving-gc") \
33-
V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \
34-
V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \
35-
V(TICK_EVENT, "tick") \
36-
V(BUILTIN_TAG, "Builtin") \
37-
V(CALLBACK_TAG, "Callback") \
38-
V(EVAL_TAG, "Eval") \
39-
V(FUNCTION_TAG, "Function") \
40-
V(INTERPRETED_FUNCTION_TAG, "InterpretedFunction") \
41-
V(HANDLER_TAG, "Handler") \
42-
V(BYTECODE_HANDLER_TAG, "BytecodeHandler") \
43-
V(LAZY_COMPILE_TAG, "LazyCompile") \
44-
V(REG_EXP_TAG, "RegExp") \
45-
V(SCRIPT_TAG, "Script") \
46-
V(STUB_TAG, "Stub") \
47-
V(NATIVE_FUNCTION_TAG, "Function") \
48-
V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \
49-
V(NATIVE_SCRIPT_TAG, "Script")
27+
#define LOG_EVENTS_LIST(V) \
28+
V(CODE_CREATION_EVENT, code-creation) \
29+
V(CODE_DISABLE_OPT_EVENT, code-disable-optimization) \
30+
V(CODE_MOVE_EVENT, code-move) \
31+
V(CODE_DELETE_EVENT, code-delete) \
32+
V(CODE_MOVING_GC, code-moving-gc) \
33+
V(SHARED_FUNC_MOVE_EVENT, sfi-move) \
34+
V(SNAPSHOT_CODE_NAME_EVENT, snapshot-code-name) \
35+
V(TICK_EVENT, tick)
36+
37+
#define TAGS_LIST(V) \
38+
V(BUILTIN_TAG, Builtin) \
39+
V(CALLBACK_TAG, Callback) \
40+
V(EVAL_TAG, Eval) \
41+
V(FUNCTION_TAG, Function) \
42+
V(INTERPRETED_FUNCTION_TAG, InterpretedFunction) \
43+
V(HANDLER_TAG, Handler) \
44+
V(BYTECODE_HANDLER_TAG, BytecodeHandler) \
45+
V(LAZY_COMPILE_TAG, LazyCompile) \
46+
V(REG_EXP_TAG, RegExp) \
47+
V(SCRIPT_TAG, Script) \
48+
V(STUB_TAG, Stub) \
49+
V(NATIVE_FUNCTION_TAG, Function) \
50+
V(NATIVE_LAZY_COMPILE_TAG, LazyCompile) \
51+
V(NATIVE_SCRIPT_TAG, Script)
5052
// Note that 'NATIVE_' cases for functions and scripts are mapped onto
5153
// original tags when writing to the log.
5254

55+
#define LOG_EVENTS_AND_TAGS_LIST(V) \
56+
LOG_EVENTS_LIST(V) \
57+
TAGS_LIST(V)
58+
5359
#define PROFILE(the_isolate, Call) (the_isolate)->code_event_dispatcher()->Call;
5460

5561
class CodeEventListener {
@@ -85,6 +91,8 @@ class CodeEventListener {
8591
enum DeoptKind { kSoft, kLazy, kEager };
8692
virtual void CodeDeoptEvent(Code* code, DeoptKind kind, Address pc,
8793
int fp_to_sp_delta) = 0;
94+
95+
virtual bool is_listening_to_code_events() { return false; }
8896
};
8997

9098
class CodeEventDispatcher {
@@ -101,6 +109,14 @@ class CodeEventDispatcher {
101109
base::LockGuard<base::Mutex> guard(&mutex_);
102110
listeners_.erase(listener);
103111
}
112+
bool IsListeningToCodeEvents() {
113+
for (auto it : listeners_) {
114+
if (it->is_listening_to_code_events()) {
115+
return true;
116+
}
117+
}
118+
return false;
119+
}
104120

105121
#define CODE_EVENT_DISPATCH(code) \
106122
base::LockGuard<base::Mutex> guard(&mutex_); \

deps/v8/src/compiler.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ void LogFunctionCompilation(CodeEventListener::LogEventsAndTags tag,
8484
// Log the code generation. If source information is available include
8585
// script name and line number. Check explicitly whether logging is
8686
// enabled as finding the line number is not free.
87-
if (!isolate->logger()->is_logging_code_events() &&
88-
!isolate->is_profiling() && !FLAG_log_function_events) {
87+
if (!isolate->logger()->is_listening_to_code_events() &&
88+
!isolate->is_profiling() && !FLAG_log_function_events &&
89+
!isolate->code_event_dispatcher()->IsListeningToCodeEvents()) {
8990
return;
9091
}
9192

deps/v8/src/compiler/wasm-compiler.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4625,7 +4625,8 @@ Node* WasmGraphBuilder::AtomicOp(wasm::WasmOpcode opcode, Node* const* inputs,
46254625

46264626
namespace {
46274627
bool must_record_function_compilation(Isolate* isolate) {
4628-
return isolate->logger()->is_logging_code_events() || isolate->is_profiling();
4628+
return isolate->logger()->is_listening_to_code_events() ||
4629+
isolate->is_profiling();
46294630
}
46304631

46314632
PRINTF_FORMAT(4, 5)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ void MarkCompactCollectorBase::CreateAndExecuteEvacuationTasks(
24582458

24592459
const bool profiling =
24602460
heap()->isolate()->is_profiling() ||
2461-
heap()->isolate()->logger()->is_logging_code_events() ||
2461+
heap()->isolate()->logger()->is_listening_to_code_events() ||
24622462
heap()->isolate()->heap_profiler()->is_tracking_object_moves() ||
24632463
heap()->has_heap_object_allocation_tracker();
24642464
ProfilingMigrationObserver profiling_observer(heap());

deps/v8/src/isolate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2883,7 +2883,7 @@ void CreateOffHeapTrampolines(Isolate* isolate) {
28832883
// thus collected by the GC.
28842884
builtins->set_builtin(i, *trampoline);
28852885

2886-
if (isolate->logger()->is_logging_code_events() ||
2886+
if (isolate->logger()->is_listening_to_code_events() ||
28872887
isolate->is_profiling()) {
28882888
isolate->logger()->LogCodeObject(*trampoline);
28892889
}

deps/v8/src/isolate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class BuiltinsConstantsTableBuilder;
5656
class CallInterfaceDescriptorData;
5757
class CancelableTaskManager;
5858
class CodeEventDispatcher;
59+
class ExternalCodeEventListener;
5960
class CodeGenerator;
6061
class CodeRange;
6162
class CodeStubDescriptor;

0 commit comments

Comments
 (0)