Skip to content

Commit

Permalink
[JSC] Make PerfLog work on Darwin
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=260626
rdar://problem/114345611

Reviewed by Justin Michaud.

This patch makes PerfLog work on Darwin, which generates JITDump[1] on Darwin.
While this is not performance efficient enough, we can later optimize the way to dump this, and try using it for a prototype.

[1]: https://raw.githubusercontent.com/torvalds/linux/master/tools/perf/Documentation/jitdump-specification.txt

* Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj:
* Source/JavaScriptCore/assembler/LinkBuffer.cpp:
(JSC::LinkBuffer::finalizeCodeWithDisassemblyImpl):
* Source/JavaScriptCore/assembler/LinkBuffer.h:
* Source/JavaScriptCore/assembler/PerfLog.cpp:
(JSC::PerfLog::singleton):
(JSC::getCurrentThreadID):
* Source/JavaScriptCore/assembler/PerfLog.h:
* Source/JavaScriptCore/jit/ExecutableAllocator.cpp:
(JSC::initializeJITPageReservation):
* Source/JavaScriptCore/runtime/Options.cpp:
(JSC::Options::isAvailable):

Canonical link: https://commits.webkit.org/267211@main
  • Loading branch information
Constellation committed Aug 24, 2023
1 parent 5dad017 commit 7f950e0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,7 @@
E386FD7E26E867B800E4C28B /* TemporalPlainTime.h in Headers */ = {isa = PBXBuildFile; fileRef = E386FD7826E867B800E4C28B /* TemporalPlainTime.h */; };
E386FD7F26E867B800E4C28B /* TemporalPlainTimePrototype.h in Headers */ = {isa = PBXBuildFile; fileRef = E386FD7926E867B800E4C28B /* TemporalPlainTimePrototype.h */; };
E389D854291226BC0085C3DC /* PageCount.h in Headers */ = {isa = PBXBuildFile; fileRef = E389D852291226BC0085C3DC /* PageCount.h */; settings = {ATTRIBUTES = (Private, ); }; };
E38DAB532A95D23A0050B7A8 /* PerfLog.h in Headers */ = {isa = PBXBuildFile; fileRef = E38DAB512A95D23A0050B7A8 /* PerfLog.h */; };
E38DB2E727F588F80027BD3F /* ScriptExecutableInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = E38DB2E627F588F70027BD3F /* ScriptExecutableInlines.h */; };
E38E8790254B978400F6F9E4 /* JSDateMath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9788FC221471AD0C0068CE2D /* JSDateMath.cpp */; };
E38F091A288D35CF009FD386 /* ObjectAdaptiveStructureWatchpoint.h in Headers */ = {isa = PBXBuildFile; fileRef = E38F0919288D35CF009FD386 /* ObjectAdaptiveStructureWatchpoint.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -5650,6 +5651,8 @@
E38D060B1F8E814100649CF2 /* JSScriptFetchParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSScriptFetchParameters.h; sourceTree = "<group>"; };
E38D060C1F8E814100649CF2 /* ScriptFetchParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptFetchParameters.h; sourceTree = "<group>"; };
E38D060D1F8E814100649CF2 /* JSScriptFetchParameters.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSScriptFetchParameters.cpp; sourceTree = "<group>"; };
E38DAB512A95D23A0050B7A8 /* PerfLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PerfLog.h; sourceTree = "<group>"; };
E38DAB522A95D23A0050B7A8 /* PerfLog.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PerfLog.cpp; sourceTree = "<group>"; };
E38DB2E627F588F70027BD3F /* ScriptExecutableInlines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptExecutableInlines.h; sourceTree = "<group>"; };
E38F0919288D35CF009FD386 /* ObjectAdaptiveStructureWatchpoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjectAdaptiveStructureWatchpoint.h; sourceTree = "<group>"; };
E39006202208BFC3001019CF /* SubspaceAccess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SubspaceAccess.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9259,6 +9262,8 @@
65860177185A8F5E00030EEE /* MaxFrameExtentForSlowPathCall.h */,
86C568DF11A213EE0007F7F0 /* MIPSAssembler.h */,
86C568DF11A213EE0007F7FF /* MIPSRegisters.h */,
E38DAB522A95D23A0050B7A8 /* PerfLog.cpp */,
E38DAB512A95D23A0050B7A8 /* PerfLog.h */,
FE63DD551EA9BC5D00103A69 /* Printer.cpp */,
FE63DD531EA9B60E00103A69 /* Printer.h */,
FE10AAF31F46826D009DEDC5 /* ProbeContext.cpp */,
Expand Down Expand Up @@ -11388,6 +11393,7 @@
65303D641447B9E100D3F904 /* ParserTokens.h in Headers */,
2BDF4F7129E9E8BB0056BF50 /* PASReportCrashPrivate.h in Headers */,
792CB34A1C4EED5C00D13AF3 /* PCToCodeOriginMap.h in Headers */,
E38DAB532A95D23A0050B7A8 /* PerfLog.h in Headers */,
A5AB49DD1BEC8086007020FB /* PerGlobalObjectWrapperWorld.h in Headers */,
0FE834181A6EF97B00D04847 /* PolymorphicCallStubRoutine.h in Headers */,
521131F71F82BF14007CCEEE /* PolyProtoAccessChain.h in Headers */,
Expand Down
9 changes: 4 additions & 5 deletions Source/JavaScriptCore/assembler/LinkBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
#include "Disassembler.h"
#include "JITCode.h"
#include "Options.h"

#if OS(LINUX)
#include "PerfLog.h"
#endif

namespace JSC {

Expand All @@ -57,18 +54,20 @@ LinkBuffer::CodeRef<LinkBufferPtrTag> LinkBuffer::finalizeCodeWithDisassemblyImp
{
CodeRef<LinkBufferPtrTag> result = finalizeCodeWithoutDisassemblyImpl();

#if OS(LINUX)
#if OS(LINUX) || OS(DARWIN)
if (Options::logJITCodeForPerf()) {
StringPrintStream out;
va_list argList;
va_start(argList, format);
va_start(argList, format);
out.vprintf(format, argList);
va_end(argList);
PerfLog::log(out.toCString(), result.code().untaggedPtr<const uint8_t*>(), result.size());
}
#endif

if (!dumpDisassembly && !Options::logJIT())
return result;

bool justDumpingHeader = !dumpDisassembly || m_alreadyDisassembled;

StringPrintStream out;
Expand Down
11 changes: 1 addition & 10 deletions Source/JavaScriptCore/assembler/LinkBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,10 @@ ALLOW_NONLITERAL_FORMAT_END
static size_t s_profileCummulativeLinkedCounts[numberOfProfiles];
};

#if OS(LINUX)
#define FINALIZE_CODE_IF(condition, linkBufferReference, resultPtrTag, ...) \
(UNLIKELY((condition) || JSC::Options::logJIT()) \
? (linkBufferReference).finalizeCodeWithDisassembly<resultPtrTag>((condition), __VA_ARGS__) \
: (UNLIKELY(JSC::Options::logJITCodeForPerf()) \
? (linkBufferReference).finalizeCodeWithDisassembly<resultPtrTag>(false, __VA_ARGS__) \
: (linkBufferReference).finalizeCodeWithoutDisassembly<resultPtrTag>()))
#else
#define FINALIZE_CODE_IF(condition, linkBufferReference, resultPtrTag, ...) \
(UNLIKELY((condition) || JSC::Options::logJIT()) \
(UNLIKELY((condition) || JSC::Options::logJIT() || JSC::Options::logJITCodeForPerf()) \
? (linkBufferReference).finalizeCodeWithDisassembly<resultPtrTag>((condition), __VA_ARGS__) \
: (linkBufferReference).finalizeCodeWithoutDisassembly<resultPtrTag>())
#endif

#define FINALIZE_CODE_FOR(codeBlock, linkBufferReference, resultPtrTag, ...) \
FINALIZE_CODE_IF((shouldDumpDisassemblyFor(codeBlock) || Options::asyncDisassembly()), linkBufferReference, resultPtrTag, __VA_ARGS__)
Expand Down
41 changes: 26 additions & 15 deletions Source/JavaScriptCore/assembler/PerfLog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2018 Yusuke Suzuki <yusukesuzuki@slowstart.org>.
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand All @@ -26,10 +27,9 @@
#include "config.h"
#include "PerfLog.h"

#if ENABLE(ASSEMBLER) && OS(LINUX)
#if ENABLE(ASSEMBLER) && (OS(LINUX) || OS(DARWIN))

#include <array>
#include <elf.h>
#include <fcntl.h>
#include <mutex>
#include <sys/mman.h>
Expand Down Expand Up @@ -63,22 +63,23 @@ static constexpr uint32_t magic = 0x4a695444;
static constexpr uint32_t magic = 0x4454694a;
#endif

// https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
#if CPU(X86)
static constexpr uint32_t elfMachine = EM_386;
static constexpr uint32_t elfMachine = 0x03;
#elif CPU(X86_64)
static constexpr uint32_t elfMachine = EM_X86_64;
static constexpr uint32_t elfMachine = 0x3E;
#elif CPU(ARM64)
static constexpr uint32_t elfMachine = EM_AARCH64;
static constexpr uint32_t elfMachine = 0xB7;
#elif CPU(ARM)
static constexpr uint32_t elfMachine = EM_ARM;
static constexpr uint32_t elfMachine = 0x28;
#elif CPU(MIPS)
#if CPU(LITTLE_ENDIAN)
static constexpr uint32_t elfMachine = EM_MIPS_RS3_LE;
static constexpr uint32_t elfMachine = 0x0A;
#else
static constexpr uint32_t elfMachine = EM_MIPS;
static constexpr uint32_t elfMachine = 0x08;
#endif
#elif CPU(RISCV64)
static constexpr uint32_t elfMachine = EM_RISCV;
static constexpr uint32_t elfMachine = 0xF3;
#endif

} // namespace Constants
Expand Down Expand Up @@ -126,22 +127,32 @@ struct CodeLoadRecord {

PerfLog& PerfLog::singleton()
{
static PerfLog* logger;
static LazyNeverDestroyed<PerfLog> logger;
static std::once_flag onceKey;
std::call_once(onceKey, [] {
logger = new PerfLog;
logger.construct();
});
return *logger;
return logger.get();
}

static inline uint64_t generateTimestamp()
{
return MonotonicTime::now().secondsSinceEpoch().nanosecondsAs<uint64_t>();
}

static inline pid_t getCurrentThreadID()
static inline uint32_t getCurrentThreadID()
{
return static_cast<pid_t>(syscall(__NR_gettid));
#if OS(LINUX)
return static_cast<uint32_t>(syscall(__NR_gettid));
#elif OS(DARWIN)
// Ideally we would like to use pthread_threadid_np. But this is 64bit, while required one is 32bit.
// For now, as a workaround, we only report lower 32bit of thread ID.
uint64_t thread = 0;
pthread_threadid_np(NULL, &thread);
return static_cast<uint32_t>(thread);
#else
#error unsupported platform
#endif
}

PerfLog::PerfLog()
Expand Down Expand Up @@ -212,4 +223,4 @@ void PerfLog::log(CString&& name, const uint8_t* executableAddress, size_t size)

} // namespace JSC

#endif // ENABLE(ASSEMBLER) && OS(LINUX)
#endif // ENABLE(ASSEMBLER) && (OS(LINUX) || OS(DARWIN))
7 changes: 5 additions & 2 deletions Source/JavaScriptCore/assembler/PerfLog.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2018 Yusuke Suzuki <yusukesuzuki@slowstart.org>.
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand All @@ -25,17 +26,19 @@

#pragma once

#if ENABLE(ASSEMBLER) && OS(LINUX)
#if ENABLE(ASSEMBLER) && (OS(LINUX) || OS(DARWIN))

#include <stdio.h>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/CString.h>

namespace JSC {

class PerfLog {
WTF_MAKE_FAST_ALLOCATED;
WTF_MAKE_NONCOPYABLE(PerfLog);
friend class LazyNeverDestroyed<PerfLog>;
public:
static void log(CString&&, const uint8_t* executableAddress, size_t);

Expand All @@ -55,4 +58,4 @@ class PerfLog {

} // namespace JSC

#endif // ENABLE(ASSEMBLER) && OS(LINUX)
#endif // ENABLE(ASSEMBLER) && (OS(LINUX) || OS(DARWIN))
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/jit/ExecutableAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ static ALWAYS_INLINE JITReservation initializeJITPageReservation()

auto tryCreatePageReservation = [] (size_t reservationSize) {
#if OS(LINUX)
// If we use uncommitted reservation, mmap operation is recorded with small page size in perf command's output.
// On Linux, if we use uncommitted reservation, mmap operation is recorded with small page size in perf command's output.
// This makes the following JIT code logging broken and some of JIT code is not recorded correctly.
// To avoid this problem, we use committed reservation if we need perf JITDump logging.
if (Options::logJITCodeForPerf())
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/runtime/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ bool Options::isAvailable(Options::ID id, Options::Availability availability)
if (id == maxSingleAllocationSizeID)
return true;
#endif
#if ENABLE(ASSEMBLER) && OS(LINUX)
#if ENABLE(ASSEMBLER) && (OS(LINUX) || OS(DARWIN))
if (id == logJITCodeForPerfID)
return true;
#endif
Expand Down

0 comments on commit 7f950e0

Please sign in to comment.