From 9331379cd49a15a05949a7753b8e781b62aee2ff Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Tue, 11 Apr 2023 17:35:48 +0000 Subject: [PATCH] Bug 1827214 - Make PerfSpewer.cpp compile on macOS, and allow --enable-perf on macOS. r=dpalmeiro Emitting Jitdump files on macOS will be useful once the samply profiler supports Jitdump on macOS. Differential Revision: https://phabricator.services.mozilla.com/D175045 --- js/moz.configure | 6 ++++-- js/src/jit/PerfSpewer.cpp | 32 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/js/moz.configure b/js/moz.configure index 8f22982882bc..37e08a7ea46b 100644 --- a/js/moz.configure +++ b/js/moz.configure @@ -431,8 +431,10 @@ option("--enable-perf", env="JS_ION_PERF", help="Enable Linux perf integration") @depends("--enable-perf", target) def ion_perf(value, target): - ok_kernel = target.kernel == "Linux" and target.os == "GNU" - if value and ok_kernel: + is_linux = target.kernel == "Linux" and target.os == "GNU" + is_mac = target.kernel == "Darwin" and target.os == "OSX" + + if value and (is_linux or is_mac): return True diff --git a/js/src/jit/PerfSpewer.cpp b/js/src/jit/PerfSpewer.cpp index 92d568b5c7aa..a7804f9efc2b 100644 --- a/js/src/jit/PerfSpewer.cpp +++ b/js/src/jit/PerfSpewer.cpp @@ -22,6 +22,34 @@ # include # define gettid() static_cast(syscall(__NR_gettid)) #endif + +#if defined(JS_ION_PERF) && defined(XP_MACOSX) +# include +# include + +pid_t gettid_pthread() { + uint64_t tid; + if (pthread_threadid_np(nullptr, &tid) != 0) { + return 0; + } + // Truncate the tid to 32 bits. macOS thread IDs are usually small enough. + // And even if we do end up truncating, it doesn't matter much for Jitdump + // as long as the process ID is correct. + return pid_t(tid); +} +# define gettid() gettid_pthread() + +const char* get_current_dir_name_cwd() { + constexpr size_t CWD_MAX = 256; + char* buffer = (char*)malloc(CWD_MAX); + if (getcwd(buffer, CWD_MAX) == nullptr) { + buffer[0] = 0; + } + return buffer; +} +# define get_current_dir_name() get_current_dir_name_cwd() +#endif + #include "jit/PerfSpewer.h" #include @@ -171,7 +199,9 @@ static bool openJitDump() { if (env_dir[0] == '/') { spew_dir = JS_smprintf("%s", env_dir); } else { - spew_dir = JS_smprintf("%s/%s", get_current_dir_name(), env_dir); + const char* dir = get_current_dir_name(); + spew_dir = JS_smprintf("%s/%s", dir, env_dir); + free((void*)dir); } } else { fprintf(stderr, "Please define PERF_SPEW_DIR as an output directory.\n");