From cdce5ace256c2304e995aa149e9f8c2d9650b1ec Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Tue, 28 Oct 2014 01:03:19 +0100 Subject: [PATCH] Add Linux perf JIT support (/tmp/perf-$pid.map) 'perf' is the standard builtin tool for performance analysis on recent Linux kernel. Its source code is shipped within the kernel repository. 'perf' has basic support for JIT. For each process, it can read a file named /tmp/perf-$PID.map. This file contains mapping from address range to function name in the format: 41187e2a 1a EmuCode_804a33fc with the following entries: 1. beginning of the range (hexadecimal); 2. size of the range (hexadecimal); 3. name of the function. We supply the PowerPC address of the basic block as function name. Usage: DOLPHIN_PERF_DIR=/tmp dolphin-emu & perf record -F99 -p $(pgrep dolphin-emu) --call-graph dwarf perf script | stackcollapse-perf.pl | grep EmuCode__ | flamegraph.pl > profile.svg Issue: perf does not have support for region invalidation. It reads the file in postprocessing. It probably does not work very well if a JIT region is reused for another basic block: wrong results should be expected in this case. Currently, nothing is done to prevent this. --- .../Core/Core/PowerPC/JitCommon/JitCache.cpp | 27 +++++++++++++++++++ Source/Core/Core/PowerPC/JitCommon/JitCache.h | 3 +++ 2 files changed, 30 insertions(+) diff --git a/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp b/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp index abcdfcd656..0c680dc020 100644 --- a/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp +++ b/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp @@ -9,6 +9,14 @@ // performance hit, it's not enabled by default, but it's useful for // locating performance issues. +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + #include "disasm.h" #include "Common/CommonTypes.h" @@ -50,6 +58,15 @@ using namespace Gen; #if defined USE_OPROFILE && USE_OPROFILE agent = op_open_agent(); #endif + + const char* perf_dir = getenv("DOLPHIN_PERF_DIR"); + if (perf_dir && perf_dir[0]) + { + std::string filename = StringFromFormat("%s/perf-%d.map", + perf_dir, getpid()); + m_perf_map_file.open(filename, std::ios::trunc); + } + iCache.fill(JIT_ICACHE_INVALID_BYTE); iCacheEx.fill(JIT_ICACHE_INVALID_BYTE); iCacheVMEM.fill(JIT_ICACHE_INVALID_BYTE); @@ -69,6 +86,9 @@ using namespace Gen; #ifdef USE_VTUNE iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN, nullptr); #endif + + if (m_perf_map_file.is_open()) + m_perf_map_file.close(); } // This clears the JIT cache. It's called from JitCache.cpp when the JIT cache @@ -179,6 +199,13 @@ using namespace Gen; jmethod.method_name = b.blockName; iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, (void*)&jmethod); #endif + + if (m_perf_map_file.is_open()) + { + m_perf_map_file << StringFromFormat( + "%" PRIx64 " %x EmuCode_%x\n", + (u64)blockCodePointers[block_num], b.codeSize, b.originalAddress); + } } const u8 **JitBaseBlockCache::GetCodePointers() diff --git a/Source/Core/Core/PowerPC/JitCommon/JitCache.h b/Source/Core/Core/PowerPC/JitCommon/JitCache.h index 0a79344309..d53fa16d43 100644 --- a/Source/Core/Core/PowerPC/JitCommon/JitCache.h +++ b/Source/Core/Core/PowerPC/JitCommon/JitCache.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -119,6 +120,8 @@ class JitBaseBlockCache bool m_initialized; + std::ofstream m_perf_map_file; + bool RangeIntersect(int s1, int e1, int s2, int e2) const; void LinkBlockExits(int i); void LinkBlock(int i);