// Copyright 2014 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include "Common/JitRegister.h" #include #include #include #include #include "Common/CommonTypes.h" #include "Common/IOFile.h" #include "Common/PerformanceTrace.h" #ifdef _WIN32 #include #else #include #endif #if defined USE_VTUNE #include #pragma comment(lib, "jitprofiling.lib") #endif static File::IOFile s_perf_map_file; namespace Common::JitRegister { static bool s_is_enabled = false; void Init(const std::string& perf_dir) { #ifdef USE_VTUNE s_is_enabled = true; #endif if (!perf_dir.empty() || getenv("PERF_BUILDID_DIR")) { const std::string dir = perf_dir.empty() ? "/tmp" : perf_dir; const std::string filename = fmt::format("{}/perf-{}.map", dir, getpid()); if (s_perf_map_file.Open(filename, "w")) { // Disable buffering in order to avoid missing some mappings // in the event of a crash: std::setvbuf(s_perf_map_file.GetHandle(), nullptr, _IONBF, 0); s_is_enabled = true; } } } void Shutdown() { #ifdef USE_VTUNE iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN, nullptr); #endif if (s_perf_map_file.IsOpen()) s_perf_map_file.Close(); s_is_enabled = false; } bool IsEnabled() { return s_is_enabled; } bool IsTraceEnabled() { return PerformanceTrace::Enabled(); } void Register(const void* base_address, u32 code_size, const std::string& symbol_name) { // Block mappings (including invalidation) are emitted by JitCache itself. if (!symbol_name.starts_with("JIT_PPC_")) { // Vertex decoding also uses JitRegister. Do not attribute it to the guest CPU. const char* domain = symbol_name.starts_with("JIT_") ? "PPC" : symbol_name.starts_with("VertexLoader") ? "VertexDecode" : "OtherGenerated"; PerformanceTrace::CodeMap(domain, nullptr, base_address, static_cast(base_address) + code_size, 0, 0, symbol_name.c_str()); } #ifdef USE_VTUNE iJIT_Method_Load jmethod = {0}; jmethod.method_id = iJIT_GetNewMethodID(); jmethod.method_load_address = const_cast(base_address); jmethod.method_size = code_size; jmethod.method_name = const_cast(symbol_name.c_str()); iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, (void*)&jmethod); #endif // Linux perf /tmp/perf-$pid.map: if (!s_perf_map_file) return; const auto entry = fmt::format("{} {:x} {}\n", fmt::ptr(base_address), code_size, symbol_name); s_perf_map_file.WriteBytes(entry.data(), entry.size()); } } // namespace Common::JitRegister