Skip to content

Commit 6df5feb

Browse files
targosrvagg
authored andcommitted
deps: cherry-pick 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 <daniel@sthima.com.br> Reviewed-on: https://chromium-review.googlesource.com/1028770 Commit-Queue: Yang Guo <yangguo@chromium.org> Reviewed-by: Hannes Payer <hpayer@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Reviewed-by: Andreas Haas <ahaas@chromium.org> Cr-Commit-Position: refs/heads/master@{#53382} Refs: v8/v8@aa6ce3e Backport-PR-URL: #21668 PR-URL: #21079 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yang Guo <yangguo@chromium.org>
1 parent 8b9a956 commit 6df5feb

16 files changed

+695
-201
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.2',
32+
'v8_embedder_string': '-node.3',
3333

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

deps/v8/include/v8-profiler.h

+70
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,76 @@ struct HeapStatsUpdate {
992992
uint32_t size; // New value of size field for the interval with this index.
993993
};
994994

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

9961066
} // namespace v8
9971067

deps/v8/src/api.cc

+64
Original file line numberDiff line numberDiff line change
@@ -10104,6 +10104,70 @@ void CpuProfiler::SetIdle(bool is_idle) {
1010410104
isolate->SetIdle(is_idle);
1010510105
}
1010610106

10107+
uintptr_t CodeEvent::GetCodeStartAddress() {
10108+
return reinterpret_cast<i::CodeEvent*>(this)->code_start_address;
10109+
}
10110+
10111+
size_t CodeEvent::GetCodeSize() {
10112+
return reinterpret_cast<i::CodeEvent*>(this)->code_size;
10113+
}
10114+
10115+
Local<String> CodeEvent::GetFunctionName() {
10116+
return ToApiHandle<String>(
10117+
reinterpret_cast<i::CodeEvent*>(this)->function_name);
10118+
}
10119+
10120+
Local<String> CodeEvent::GetScriptName() {
10121+
return ToApiHandle<String>(
10122+
reinterpret_cast<i::CodeEvent*>(this)->script_name);
10123+
}
10124+
10125+
int CodeEvent::GetScriptLine() {
10126+
return reinterpret_cast<i::CodeEvent*>(this)->script_line;
10127+
}
10128+
10129+
int CodeEvent::GetScriptColumn() {
10130+
return reinterpret_cast<i::CodeEvent*>(this)->script_column;
10131+
}
10132+
10133+
CodeEventType CodeEvent::GetCodeType() {
10134+
return reinterpret_cast<i::CodeEvent*>(this)->code_type;
10135+
}
10136+
10137+
const char* CodeEvent::GetComment() {
10138+
return reinterpret_cast<i::CodeEvent*>(this)->comment;
10139+
}
10140+
10141+
const char* CodeEvent::GetCodeEventTypeName(CodeEventType code_event_type) {
10142+
switch (code_event_type) {
10143+
case kUnknownType:
10144+
return "Unknown";
10145+
#define V(Name) \
10146+
case k##Name##Type: \
10147+
return #Name;
10148+
CODE_EVENTS_LIST(V)
10149+
#undef V
10150+
}
10151+
}
10152+
10153+
CodeEventHandler::CodeEventHandler(Isolate* isolate) {
10154+
internal_listener_ =
10155+
new i::ExternalCodeEventListener(reinterpret_cast<i::Isolate*>(isolate));
10156+
}
10157+
10158+
CodeEventHandler::~CodeEventHandler() {
10159+
delete reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_);
10160+
}
10161+
10162+
void CodeEventHandler::Enable() {
10163+
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10164+
->StartListening(this);
10165+
}
10166+
10167+
void CodeEventHandler::Disable() {
10168+
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10169+
->StopListening();
10170+
}
1010710171

1010810172
static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
1010910173
return const_cast<i::HeapGraphEdge*>(

deps/v8/src/code-events.h

+39-23
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

+3-2
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -3952,7 +3952,8 @@ Node* WasmGraphBuilder::AtomicOp(wasm::WasmOpcode opcode, Node* const* inputs,
39523952

39533953
namespace {
39543954
bool must_record_function_compilation(Isolate* isolate) {
3955-
return isolate->logger()->is_logging_code_events() || isolate->is_profiling();
3955+
return isolate->logger()->is_listening_to_code_events() ||
3956+
isolate->is_profiling();
39563957
}
39573958

39583959
PRINTF_FORMAT(4, 5)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ void MarkCompactCollectorBase::CreateAndExecuteEvacuationTasks(
24142414

24152415
const bool profiling =
24162416
heap()->isolate()->is_profiling() ||
2417-
heap()->isolate()->logger()->is_logging_code_events() ||
2417+
heap()->isolate()->logger()->is_listening_to_code_events() ||
24182418
heap()->isolate()->heap_profiler()->is_tracking_object_moves() ||
24192419
heap()->has_heap_object_allocation_tracker();
24202420
ProfilingMigrationObserver profiling_observer(heap());

deps/v8/src/isolate.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ void CreateOffHeapTrampolines(Isolate* isolate) {
28922892
// thus collected by the GC.
28932893
builtins->set_builtin(i, *trampoline);
28942894

2895-
if (isolate->logger()->is_logging_code_events() ||
2895+
if (isolate->logger()->is_listening_to_code_events() ||
28962896
isolate->is_profiling()) {
28972897
isolate->logger()->LogCodeObject(*trampoline);
28982898
}

deps/v8/src/isolate.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class BuiltinsConstantsTableBuilder;
5757
class CallInterfaceDescriptorData;
5858
class CancelableTaskManager;
5959
class CodeEventDispatcher;
60+
class ExternalCodeEventListener;
6061
class CodeGenerator;
6162
class CodeRange;
6263
class CodeStubDescriptor;

0 commit comments

Comments
 (0)