Skip to content

deps: V8: backport --perf-prof for macOS #58010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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.12',
'v8_embedder_string': '-node.13',

##### V8 defaults for Node.js #####

Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/compiler/backend/code-generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ void CodeGenerator::AssembleCode() {
}
}

// The LinuxPerfJitLogger logs code up until here, excluding the safepoint
// The PerfJitLogger logs code up until here, excluding the safepoint
// table. Resolve the unwinding info now so it is aware of the same code
// size as reported by perf.
unwinding_info_writer_.Finish(masm()->pc_offset());
Expand Down
76 changes: 39 additions & 37 deletions deps/v8/src/diagnostics/perf-jit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#include "src/common/assert-scope.h"
#include "src/flags/flags.h"

// Only compile the {LinuxPerfJitLogger} on Linux.
#if V8_OS_LINUX
// Only compile the {PerfJitLogger} on Linux & Darwin.
#if V8_OS_LINUX || V8_OS_DARWIN

#include <fcntl.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -118,22 +118,22 @@ struct PerfJitCodeUnwindingInfo : PerfJitBase {
// Followed by size_ - sizeof(PerfJitCodeUnwindingInfo) bytes of data.
};

const char LinuxPerfJitLogger::kFilenameFormatString[] = "%s/jit-%d.dump";
const char PerfJitLogger::kFilenameFormatString[] = "%s/jit-%d.dump";

// Extra padding for the PID in the filename
const int LinuxPerfJitLogger::kFilenameBufferPadding = 16;
const int PerfJitLogger::kFilenameBufferPadding = 16;

static const char kStringTerminator[] = {'\0'};

// The following static variables are protected by
// GetFileMutex().
int LinuxPerfJitLogger::process_id_ = 0;
uint64_t LinuxPerfJitLogger::reference_count_ = 0;
void* LinuxPerfJitLogger::marker_address_ = nullptr;
uint64_t LinuxPerfJitLogger::code_index_ = 0;
FILE* LinuxPerfJitLogger::perf_output_handle_ = nullptr;
int PerfJitLogger::process_id_ = 0;
uint64_t PerfJitLogger::reference_count_ = 0;
void* PerfJitLogger::marker_address_ = nullptr;
uint64_t PerfJitLogger::code_index_ = 0;
FILE* PerfJitLogger::perf_output_handle_ = nullptr;

void LinuxPerfJitLogger::OpenJitDumpFile() {
void PerfJitLogger::OpenJitDumpFile() {
// Open the perf JIT dump file.
perf_output_handle_ = nullptr;

Expand All @@ -153,22 +153,31 @@ void LinuxPerfJitLogger::OpenJitDumpFile() {
if (v8_flags.perf_prof_delete_file)
CHECK_EQ(0, unlink(perf_dump_name.begin()));

// On Linux, call OpenMarkerFile so that perf knows about the file path via
// an MMAP record.
// On macOS, don't call OpenMarkerFile because samply has already detected
// the file path during the call to `open` above (it interposes `open` with
// a preloaded library), and because the mmap call can be slow.
#if V8_OS_DARWIN
marker_address_ = nullptr;
#else
marker_address_ = OpenMarkerFile(fd);
if (marker_address_ == nullptr) return;
#endif

perf_output_handle_ = fdopen(fd, "w+");
if (perf_output_handle_ == nullptr) return;

setvbuf(perf_output_handle_, nullptr, _IOFBF, kLogBufferSize);
}

void LinuxPerfJitLogger::CloseJitDumpFile() {
void PerfJitLogger::CloseJitDumpFile() {
if (perf_output_handle_ == nullptr) return;
base::Fclose(perf_output_handle_);
perf_output_handle_ = nullptr;
}

void* LinuxPerfJitLogger::OpenMarkerFile(int fd) {
void* PerfJitLogger::OpenMarkerFile(int fd) {
long page_size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
if (page_size == -1) return nullptr;

Expand All @@ -180,15 +189,14 @@ void* LinuxPerfJitLogger::OpenMarkerFile(int fd) {
return (marker_address == MAP_FAILED) ? nullptr : marker_address;
}

void LinuxPerfJitLogger::CloseMarkerFile(void* marker_address) {
void PerfJitLogger::CloseMarkerFile(void* marker_address) {
if (marker_address == nullptr) return;
long page_size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
if (page_size == -1) return;
munmap(marker_address, page_size);
}

LinuxPerfJitLogger::LinuxPerfJitLogger(Isolate* isolate)
: CodeEventLogger(isolate) {
PerfJitLogger::PerfJitLogger(Isolate* isolate) : CodeEventLogger(isolate) {
base::LockGuard<base::RecursiveMutex> guard_file(GetFileMutex().Pointer());
process_id_ = base::OS::GetCurrentProcessId();

Expand All @@ -201,7 +209,7 @@ LinuxPerfJitLogger::LinuxPerfJitLogger(Isolate* isolate)
}
}

LinuxPerfJitLogger::~LinuxPerfJitLogger() {
PerfJitLogger::~PerfJitLogger() {
base::LockGuard<base::RecursiveMutex> guard_file(GetFileMutex().Pointer());

reference_count_--;
Expand All @@ -211,16 +219,11 @@ LinuxPerfJitLogger::~LinuxPerfJitLogger() {
}
}

uint64_t LinuxPerfJitLogger::GetTimestamp() {
struct timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
DCHECK_EQ(0, result);
USE(result);
static const uint64_t kNsecPerSec = 1000000000;
return (ts.tv_sec * kNsecPerSec) + ts.tv_nsec;
uint64_t PerfJitLogger::GetTimestamp() {
return base::TimeTicks::Now().since_origin().InNanoseconds();
}

void LinuxPerfJitLogger::LogRecordedBuffer(
void PerfJitLogger::LogRecordedBuffer(
Tagged<AbstractCode> abstract_code,
MaybeHandle<SharedFunctionInfo> maybe_sfi, const char* name, int length) {
DisallowGarbageCollection no_gc;
Expand Down Expand Up @@ -263,8 +266,8 @@ void LinuxPerfJitLogger::LogRecordedBuffer(
}

#if V8_ENABLE_WEBASSEMBLY
void LinuxPerfJitLogger::LogRecordedBuffer(const wasm::WasmCode* code,
const char* name, int length) {
void PerfJitLogger::LogRecordedBuffer(const wasm::WasmCode* code,
const char* name, int length) {
base::LockGuard<base::RecursiveMutex> guard_file(GetFileMutex().Pointer());

if (perf_output_handle_ == nullptr) return;
Expand All @@ -276,10 +279,9 @@ void LinuxPerfJitLogger::LogRecordedBuffer(const wasm::WasmCode* code,
}
#endif // V8_ENABLE_WEBASSEMBLY

void LinuxPerfJitLogger::WriteJitCodeLoadEntry(const uint8_t* code_pointer,
uint32_t code_size,
const char* name,
int name_length) {
void PerfJitLogger::WriteJitCodeLoadEntry(const uint8_t* code_pointer,
uint32_t code_size, const char* name,
int name_length) {
PerfJitCodeLoad code_load;
code_load.event_ = PerfJitCodeLoad::kLoad;
code_load.size_ = sizeof(code_load) + name_length + 1 + code_size;
Expand Down Expand Up @@ -342,8 +344,8 @@ SourcePositionInfo GetSourcePositionInfo(Isolate* isolate, Tagged<Code> code,

} // namespace

void LinuxPerfJitLogger::LogWriteDebugInfo(Tagged<Code> code,
Handle<SharedFunctionInfo> shared) {
void PerfJitLogger::LogWriteDebugInfo(Tagged<Code> code,
Handle<SharedFunctionInfo> shared) {
// Line ends of all scripts have been initialized prior to this.
DisallowGarbageCollection no_gc;
// The WasmToJS wrapper stubs have source position entries.
Expand Down Expand Up @@ -426,7 +428,7 @@ void LinuxPerfJitLogger::LogWriteDebugInfo(Tagged<Code> code,
}

#if V8_ENABLE_WEBASSEMBLY
void LinuxPerfJitLogger::LogWriteDebugInfo(const wasm::WasmCode* code) {
void PerfJitLogger::LogWriteDebugInfo(const wasm::WasmCode* code) {
if (code->IsAnonymous()) {
return;
}
Expand Down Expand Up @@ -498,7 +500,7 @@ void LinuxPerfJitLogger::LogWriteDebugInfo(const wasm::WasmCode* code) {
}
#endif // V8_ENABLE_WEBASSEMBLY

void LinuxPerfJitLogger::LogWriteUnwindingInfo(Tagged<Code> code) {
void PerfJitLogger::LogWriteUnwindingInfo(Tagged<Code> code) {
PerfJitCodeUnwindingInfo unwinding_info_header;
unwinding_info_header.event_ = PerfJitCodeLoad::kUnwindingInfo;
unwinding_info_header.time_stamp_ = GetTimestamp();
Expand Down Expand Up @@ -533,13 +535,13 @@ void LinuxPerfJitLogger::LogWriteUnwindingInfo(Tagged<Code> code) {
LogWriteBytes(padding_bytes, static_cast<int>(padding_size));
}

void LinuxPerfJitLogger::LogWriteBytes(const char* bytes, int size) {
void PerfJitLogger::LogWriteBytes(const char* bytes, int size) {
size_t rv = fwrite(bytes, 1, size, perf_output_handle_);
DCHECK(static_cast<size_t>(size) == rv);
USE(rv);
}

void LinuxPerfJitLogger::LogWriteHeader() {
void PerfJitLogger::LogWriteHeader() {
DCHECK_NOT_NULL(perf_output_handle_);
PerfJitHeader header;

Expand All @@ -560,4 +562,4 @@ void LinuxPerfJitLogger::LogWriteHeader() {
} // namespace internal
} // namespace v8

#endif // V8_OS_LINUX
#endif // V8_OS_LINUX || V8_OS_DARWIN
12 changes: 6 additions & 6 deletions deps/v8/src/diagnostics/perf-jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@

#include "include/v8config.h"

// {LinuxPerfJitLogger} is only implemented on Linux.
#if V8_OS_LINUX
// {PerfJitLogger} is only implemented on Linux & Darwin.
#if V8_OS_LINUX || V8_OS_DARWIN

#include "src/logging/log.h"

namespace v8 {
namespace internal {

// Linux perf tool logging support.
class LinuxPerfJitLogger : public CodeEventLogger {
class PerfJitLogger : public CodeEventLogger {
public:
explicit LinuxPerfJitLogger(Isolate* isolate);
~LinuxPerfJitLogger() override;
explicit PerfJitLogger(Isolate* isolate);
~PerfJitLogger() override;

void CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) override {
Expand Down Expand Up @@ -142,6 +142,6 @@ class LinuxPerfJitLogger : public CodeEventLogger {
} // namespace internal
} // namespace v8

#endif // V8_OS_LINUX
#endif // V8_OS_LINUX || V8_OS_DARWIN

#endif // V8_DIAGNOSTICS_PERF_JIT_H_
8 changes: 4 additions & 4 deletions deps/v8/src/flags/flag-definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3072,7 +3072,7 @@ DEFINE_IMPLICATION(prof, log_code)

DEFINE_BOOL(ll_prof, false, "Enable low-level linux profiler.")

#if V8_OS_LINUX
#if V8_OS_LINUX || V8_OS_DARWIN
#define DEFINE_PERF_PROF_BOOL(nam, cmt) DEFINE_BOOL(nam, false, cmt)
#define DEFINE_PERF_PROF_IMPLICATION DEFINE_IMPLICATION
#else
Expand All @@ -3089,7 +3089,7 @@ DEFINE_BOOL(ll_prof, false, "Enable low-level linux profiler.")
#endif

DEFINE_PERF_PROF_BOOL(perf_basic_prof,
"Enable perf linux profiler (basic support).")
"Enable basic support for perf profiler.")
DEFINE_NEG_IMPLICATION(perf_basic_prof, compact_code_space)
DEFINE_STRING(perf_basic_prof_path, DEFAULT_PERF_BASIC_PROF_PATH,
"directory to write perf-<pid>.map symbol file to")
Expand All @@ -3098,8 +3098,8 @@ DEFINE_PERF_PROF_BOOL(
"Only report function code ranges to perf (i.e. no stubs).")
DEFINE_PERF_PROF_IMPLICATION(perf_basic_prof_only_functions, perf_basic_prof)

DEFINE_PERF_PROF_BOOL(
perf_prof, "Enable perf linux profiler (experimental annotate support).")
DEFINE_PERF_PROF_BOOL(perf_prof,
"Enable experimental annotate support for perf profiler.")
DEFINE_STRING(perf_prof_path, DEFAULT_PERF_PROF_PATH,
"directory to write jit-<pid>.dump symbol file to")
DEFINE_PERF_PROF_BOOL(
Expand Down
Loading
Loading