Skip to content

Commit aa6ce3e

Browse files
hashseedDaniel Beckert
authored andcommitted
[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}
1 parent 7633479 commit aa6ce3e

File tree

15 files changed

+694
-200
lines changed

15 files changed

+694
-200
lines changed

include/v8-profiler.h

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

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

10131083
} // namespace v8
10141084

src/api.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10162,6 +10162,70 @@ void CpuProfiler::SetIdle(bool is_idle) {
1016210162
isolate->SetIdle(is_idle);
1016310163
}
1016410164

10165+
uintptr_t CodeEvent::GetCodeStartAddress() {
10166+
return reinterpret_cast<i::CodeEvent*>(this)->code_start_address;
10167+
}
10168+
10169+
size_t CodeEvent::GetCodeSize() {
10170+
return reinterpret_cast<i::CodeEvent*>(this)->code_size;
10171+
}
10172+
10173+
Local<String> CodeEvent::GetFunctionName() {
10174+
return ToApiHandle<String>(
10175+
reinterpret_cast<i::CodeEvent*>(this)->function_name);
10176+
}
10177+
10178+
Local<String> CodeEvent::GetScriptName() {
10179+
return ToApiHandle<String>(
10180+
reinterpret_cast<i::CodeEvent*>(this)->script_name);
10181+
}
10182+
10183+
int CodeEvent::GetScriptLine() {
10184+
return reinterpret_cast<i::CodeEvent*>(this)->script_line;
10185+
}
10186+
10187+
int CodeEvent::GetScriptColumn() {
10188+
return reinterpret_cast<i::CodeEvent*>(this)->script_column;
10189+
}
10190+
10191+
CodeEventType CodeEvent::GetCodeType() {
10192+
return reinterpret_cast<i::CodeEvent*>(this)->code_type;
10193+
}
10194+
10195+
const char* CodeEvent::GetComment() {
10196+
return reinterpret_cast<i::CodeEvent*>(this)->comment;
10197+
}
10198+
10199+
const char* CodeEvent::GetCodeEventTypeName(CodeEventType code_event_type) {
10200+
switch (code_event_type) {
10201+
case kUnknownType:
10202+
return "Unknown";
10203+
#define V(Name) \
10204+
case k##Name##Type: \
10205+
return #Name;
10206+
CODE_EVENTS_LIST(V)
10207+
#undef V
10208+
}
10209+
}
10210+
10211+
CodeEventHandler::CodeEventHandler(Isolate* isolate) {
10212+
internal_listener_ =
10213+
new i::ExternalCodeEventListener(reinterpret_cast<i::Isolate*>(isolate));
10214+
}
10215+
10216+
CodeEventHandler::~CodeEventHandler() {
10217+
delete reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_);
10218+
}
10219+
10220+
void CodeEventHandler::Enable() {
10221+
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10222+
->StartListening(this);
10223+
}
10224+
10225+
void CodeEventHandler::Disable() {
10226+
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10227+
->StopListening();
10228+
}
1016510229

1016610230
static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
1016710231
return const_cast<i::HeapGraphEdge*>(

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_); \

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

src/compiler/wasm-compiler.cc

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

39493949
namespace {
39503950
bool must_record_function_compilation(Isolate* isolate) {
3951-
return isolate->logger()->is_logging_code_events() || isolate->is_profiling();
3951+
return isolate->logger()->is_listening_to_code_events() ||
3952+
isolate->is_profiling();
39523953
}
39533954

39543955
PRINTF_FORMAT(4, 5)

src/heap/mark-compact.cc

Lines changed: 1 addition & 1 deletion
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());

src/isolate.cc

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

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

src/isolate.h

Lines changed: 1 addition & 0 deletions
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)