Skip to content

Commit

Permalink
deps: cherry-pick aa6ce3e from upstream V8
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
targos authored and rvagg committed Aug 15, 2018
1 parent 8b9a956 commit 6df5feb
Show file tree
Hide file tree
Showing 16 changed files with 695 additions and 201 deletions.
2 changes: 1 addition & 1 deletion common.gypi
Expand Up @@ -29,7 +29,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.2',
'v8_embedder_string': '-node.3',

# Enable disassembler for `--print-code` v8 options
'v8_enable_disassembler': 1,
Expand Down
70 changes: 70 additions & 0 deletions deps/v8/include/v8-profiler.h
Expand Up @@ -992,6 +992,76 @@ struct HeapStatsUpdate {
uint32_t size; // New value of size field for the interval with this index.
};

#define CODE_EVENTS_LIST(V) \
V(Builtin) \
V(Callback) \
V(Eval) \
V(Function) \
V(InterpretedFunction) \
V(Handler) \
V(BytecodeHandler) \
V(LazyCompile) \
V(RegExp) \
V(Script) \
V(Stub)

/**
* Note that this enum may be extended in the future. Please include a default
* case if this enum is used in a switch statement.
*/
enum CodeEventType {
kUnknownType = 0
#define V(Name) , k##Name##Type
CODE_EVENTS_LIST(V)
#undef V
};

/**
* Representation of a code creation event
*/
class V8_EXPORT CodeEvent {
public:
uintptr_t GetCodeStartAddress();
size_t GetCodeSize();
Local<String> GetFunctionName();
Local<String> GetScriptName();
int GetScriptLine();
int GetScriptColumn();
/**
* NOTE (mmarchini): We can't allocate objects in the heap when we collect
* existing code, and both the code type and the comment are not stored in the
* heap, so we return those as const char*.
*/
CodeEventType GetCodeType();
const char* GetComment();

static const char* GetCodeEventTypeName(CodeEventType code_event_type);
};

/**
* Interface to listen to code creation events.
*/
class V8_EXPORT CodeEventHandler {
public:
/**
* Creates a new listener for the |isolate|. The isolate must be initialized.
* The listener object must be disposed after use by calling |Dispose| method.
* Multiple listeners can be created for the same isolate.
*/
explicit CodeEventHandler(Isolate* isolate);
virtual ~CodeEventHandler();

virtual void Handle(CodeEvent* code_event) = 0;

void Enable();
void Disable();

private:
CodeEventHandler();
CodeEventHandler(const CodeEventHandler&);
CodeEventHandler& operator=(const CodeEventHandler&);
void* internal_listener_;
};

} // namespace v8

Expand Down
64 changes: 64 additions & 0 deletions deps/v8/src/api.cc
Expand Up @@ -10104,6 +10104,70 @@ void CpuProfiler::SetIdle(bool is_idle) {
isolate->SetIdle(is_idle);
}

uintptr_t CodeEvent::GetCodeStartAddress() {
return reinterpret_cast<i::CodeEvent*>(this)->code_start_address;
}

size_t CodeEvent::GetCodeSize() {
return reinterpret_cast<i::CodeEvent*>(this)->code_size;
}

Local<String> CodeEvent::GetFunctionName() {
return ToApiHandle<String>(
reinterpret_cast<i::CodeEvent*>(this)->function_name);
}

Local<String> CodeEvent::GetScriptName() {
return ToApiHandle<String>(
reinterpret_cast<i::CodeEvent*>(this)->script_name);
}

int CodeEvent::GetScriptLine() {
return reinterpret_cast<i::CodeEvent*>(this)->script_line;
}

int CodeEvent::GetScriptColumn() {
return reinterpret_cast<i::CodeEvent*>(this)->script_column;
}

CodeEventType CodeEvent::GetCodeType() {
return reinterpret_cast<i::CodeEvent*>(this)->code_type;
}

const char* CodeEvent::GetComment() {
return reinterpret_cast<i::CodeEvent*>(this)->comment;
}

const char* CodeEvent::GetCodeEventTypeName(CodeEventType code_event_type) {
switch (code_event_type) {
case kUnknownType:
return "Unknown";
#define V(Name) \
case k##Name##Type: \
return #Name;
CODE_EVENTS_LIST(V)
#undef V
}
}

CodeEventHandler::CodeEventHandler(Isolate* isolate) {
internal_listener_ =
new i::ExternalCodeEventListener(reinterpret_cast<i::Isolate*>(isolate));
}

CodeEventHandler::~CodeEventHandler() {
delete reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_);
}

void CodeEventHandler::Enable() {
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
->StartListening(this);
}

void CodeEventHandler::Disable() {
reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
->StopListening();
}

static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
return const_cast<i::HeapGraphEdge*>(
Expand Down
62 changes: 39 additions & 23 deletions deps/v8/src/code-events.h
Expand Up @@ -24,32 +24,38 @@ class WasmCode;
using WasmName = Vector<const char>;
} // namespace wasm

#define LOG_EVENTS_AND_TAGS_LIST(V) \
V(CODE_CREATION_EVENT, "code-creation") \
V(CODE_DISABLE_OPT_EVENT, "code-disable-optimization") \
V(CODE_MOVE_EVENT, "code-move") \
V(CODE_DELETE_EVENT, "code-delete") \
V(CODE_MOVING_GC, "code-moving-gc") \
V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \
V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \
V(TICK_EVENT, "tick") \
V(BUILTIN_TAG, "Builtin") \
V(CALLBACK_TAG, "Callback") \
V(EVAL_TAG, "Eval") \
V(FUNCTION_TAG, "Function") \
V(INTERPRETED_FUNCTION_TAG, "InterpretedFunction") \
V(HANDLER_TAG, "Handler") \
V(BYTECODE_HANDLER_TAG, "BytecodeHandler") \
V(LAZY_COMPILE_TAG, "LazyCompile") \
V(REG_EXP_TAG, "RegExp") \
V(SCRIPT_TAG, "Script") \
V(STUB_TAG, "Stub") \
V(NATIVE_FUNCTION_TAG, "Function") \
V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \
V(NATIVE_SCRIPT_TAG, "Script")
#define LOG_EVENTS_LIST(V) \
V(CODE_CREATION_EVENT, code-creation) \
V(CODE_DISABLE_OPT_EVENT, code-disable-optimization) \
V(CODE_MOVE_EVENT, code-move) \
V(CODE_DELETE_EVENT, code-delete) \
V(CODE_MOVING_GC, code-moving-gc) \
V(SHARED_FUNC_MOVE_EVENT, sfi-move) \
V(SNAPSHOT_CODE_NAME_EVENT, snapshot-code-name) \
V(TICK_EVENT, tick)

#define TAGS_LIST(V) \
V(BUILTIN_TAG, Builtin) \
V(CALLBACK_TAG, Callback) \
V(EVAL_TAG, Eval) \
V(FUNCTION_TAG, Function) \
V(INTERPRETED_FUNCTION_TAG, InterpretedFunction) \
V(HANDLER_TAG, Handler) \
V(BYTECODE_HANDLER_TAG, BytecodeHandler) \
V(LAZY_COMPILE_TAG, LazyCompile) \
V(REG_EXP_TAG, RegExp) \
V(SCRIPT_TAG, Script) \
V(STUB_TAG, Stub) \
V(NATIVE_FUNCTION_TAG, Function) \
V(NATIVE_LAZY_COMPILE_TAG, LazyCompile) \
V(NATIVE_SCRIPT_TAG, Script)
// Note that 'NATIVE_' cases for functions and scripts are mapped onto
// original tags when writing to the log.

#define LOG_EVENTS_AND_TAGS_LIST(V) \
LOG_EVENTS_LIST(V) \
TAGS_LIST(V)

#define PROFILE(the_isolate, Call) (the_isolate)->code_event_dispatcher()->Call;

class CodeEventListener {
Expand Down Expand Up @@ -85,6 +91,8 @@ class CodeEventListener {
enum DeoptKind { kSoft, kLazy, kEager };
virtual void CodeDeoptEvent(Code* code, DeoptKind kind, Address pc,
int fp_to_sp_delta) = 0;

virtual bool is_listening_to_code_events() { return false; }
};

class CodeEventDispatcher {
Expand All @@ -101,6 +109,14 @@ class CodeEventDispatcher {
base::LockGuard<base::Mutex> guard(&mutex_);
listeners_.erase(listener);
}
bool IsListeningToCodeEvents() {
for (auto it : listeners_) {
if (it->is_listening_to_code_events()) {
return true;
}
}
return false;
}

#define CODE_EVENT_DISPATCH(code) \
base::LockGuard<base::Mutex> guard(&mutex_); \
Expand Down
5 changes: 3 additions & 2 deletions deps/v8/src/compiler.cc
Expand Up @@ -84,8 +84,9 @@ void LogFunctionCompilation(CodeEventListener::LogEventsAndTags tag,
// Log the code generation. If source information is available include
// script name and line number. Check explicitly whether logging is
// enabled as finding the line number is not free.
if (!isolate->logger()->is_logging_code_events() &&
!isolate->is_profiling() && !FLAG_log_function_events) {
if (!isolate->logger()->is_listening_to_code_events() &&
!isolate->is_profiling() && !FLAG_log_function_events &&
!isolate->code_event_dispatcher()->IsListeningToCodeEvents()) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion deps/v8/src/compiler/wasm-compiler.cc
Expand Up @@ -3952,7 +3952,8 @@ Node* WasmGraphBuilder::AtomicOp(wasm::WasmOpcode opcode, Node* const* inputs,

namespace {
bool must_record_function_compilation(Isolate* isolate) {
return isolate->logger()->is_logging_code_events() || isolate->is_profiling();
return isolate->logger()->is_listening_to_code_events() ||
isolate->is_profiling();
}

PRINTF_FORMAT(4, 5)
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/heap/mark-compact.cc
Expand Up @@ -2414,7 +2414,7 @@ void MarkCompactCollectorBase::CreateAndExecuteEvacuationTasks(

const bool profiling =
heap()->isolate()->is_profiling() ||
heap()->isolate()->logger()->is_logging_code_events() ||
heap()->isolate()->logger()->is_listening_to_code_events() ||
heap()->isolate()->heap_profiler()->is_tracking_object_moves() ||
heap()->has_heap_object_allocation_tracker();
ProfilingMigrationObserver profiling_observer(heap());
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/isolate.cc
Expand Up @@ -2892,7 +2892,7 @@ void CreateOffHeapTrampolines(Isolate* isolate) {
// thus collected by the GC.
builtins->set_builtin(i, *trampoline);

if (isolate->logger()->is_logging_code_events() ||
if (isolate->logger()->is_listening_to_code_events() ||
isolate->is_profiling()) {
isolate->logger()->LogCodeObject(*trampoline);
}
Expand Down
1 change: 1 addition & 0 deletions deps/v8/src/isolate.h
Expand Up @@ -57,6 +57,7 @@ class BuiltinsConstantsTableBuilder;
class CallInterfaceDescriptorData;
class CancelableTaskManager;
class CodeEventDispatcher;
class ExternalCodeEventListener;
class CodeGenerator;
class CodeRange;
class CodeStubDescriptor;
Expand Down

0 comments on commit 6df5feb

Please sign in to comment.