2015-12-06 11:57:54 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2021-09-26 14:54:53 +00:00
|
|
|
* Copyright (C) 2002-2021 PCSX2 Dev Team
|
2015-12-06 11:57:54 +00:00
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/Perf.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Pcsx2Defs.h"
|
2023-04-28 11:09:04 +00:00
|
|
|
#include "common/Assertions.h"
|
|
|
|
#include "common/StringUtil.h"
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#ifdef ENABLE_VTUNE
|
|
|
|
#include "jitprofiling.h"
|
2023-04-28 11:09:04 +00:00
|
|
|
#endif
|
2022-11-08 19:37:24 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <atomic>
|
|
|
|
#include <ctime>
|
|
|
|
#include <mutex>
|
|
|
|
#include <elf.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/syscall.h>
|
2016-11-25 16:46:52 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-06 11:57:54 +00:00
|
|
|
//#define ProfileWithPerf
|
2023-04-28 11:09:04 +00:00
|
|
|
//#define ProfileWithPerfJitDump
|
2015-12-06 11:57:54 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
#if defined(ENABLE_VTUNE) && defined(_WIN32)
|
2016-11-29 21:57:45 +00:00
|
|
|
#pragma comment(lib, "jitprofiling.lib")
|
|
|
|
#endif
|
2015-12-06 11:57:54 +00:00
|
|
|
|
|
|
|
namespace Perf
|
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
Group any("");
|
|
|
|
Group ee("EE");
|
|
|
|
Group iop("IOP");
|
|
|
|
Group vu0("VU0");
|
|
|
|
Group vu1("VU1");
|
|
|
|
Group vif("VIF");
|
2015-12-06 11:57:54 +00:00
|
|
|
|
|
|
|
// Perf is only supported on linux
|
2023-04-28 11:09:04 +00:00
|
|
|
#if defined(__linux__) && defined(ProfileWithPerf)
|
|
|
|
static std::FILE* s_map_file = nullptr;
|
|
|
|
static bool s_map_file_opened = false;
|
|
|
|
static std::mutex s_mutex;
|
|
|
|
static void RegisterMethod(const void* ptr, size_t size, const char* symbol)
|
|
|
|
{
|
|
|
|
std::unique_lock lock(s_mutex);
|
2015-12-06 11:57:54 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
if (!s_map_file)
|
|
|
|
{
|
|
|
|
if (s_map_file_opened)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char file[256];
|
|
|
|
snprintf(file, std::size(file), "/tmp/perf-%d.map", getpid());
|
|
|
|
s_map_file = std::fopen(file, "wb");
|
|
|
|
s_map_file_opened = true;
|
|
|
|
if (!s_map_file)
|
|
|
|
return;
|
|
|
|
}
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
std::fprintf(s_map_file, "%" PRIx64 " %zx %s\n", static_cast<u64>(reinterpret_cast<uintptr_t>(ptr)), size, symbol);
|
|
|
|
std::fflush(s_map_file);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2023-04-28 11:09:04 +00:00
|
|
|
#elif defined(__linux__) && defined(ProfileWithPerfJitDump)
|
|
|
|
enum : u32
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
JIT_CODE_LOAD = 0,
|
|
|
|
JIT_CODE_MOVE = 1,
|
|
|
|
JIT_CODE_DEBUG_INFO = 2,
|
|
|
|
JIT_CODE_CLOSE = 3,
|
|
|
|
JIT_CODE_UNWINDING_INFO = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct JITDUMP_HEADER
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
u32 magic = 0x4A695444; // JiTD
|
|
|
|
u32 version = 1;
|
|
|
|
u32 header_size = sizeof(JITDUMP_HEADER);
|
|
|
|
u32 elf_mach;
|
|
|
|
u32 pad1 = 0;
|
|
|
|
u32 pid;
|
|
|
|
u64 timestamp;
|
|
|
|
u64 flags = 0;
|
|
|
|
};
|
|
|
|
struct JITDUMP_RECORD_HEADER
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
u32 id;
|
|
|
|
u32 total_size;
|
|
|
|
u64 timestamp;
|
|
|
|
};
|
|
|
|
struct JITDUMP_CODE_LOAD
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
JITDUMP_RECORD_HEADER header;
|
|
|
|
u32 pid;
|
|
|
|
u32 tid;
|
|
|
|
u64 vma;
|
|
|
|
u64 code_addr;
|
|
|
|
u64 code_size;
|
|
|
|
u64 code_index;
|
|
|
|
// name
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
static u64 JitDumpTimestamp()
|
|
|
|
{
|
|
|
|
struct timespec ts = {};
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return (static_cast<u64>(ts.tv_sec) * 1000000000ULL) + static_cast<u64>(ts.tv_nsec);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
static FILE* s_jitdump_file = nullptr;
|
|
|
|
static bool s_jitdump_file_opened = false;
|
|
|
|
static std::mutex s_jitdump_mutex;
|
|
|
|
static u32 s_jitdump_record_id;
|
|
|
|
|
|
|
|
static void RegisterMethod(const void* ptr, size_t size, const char* symbol)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
const u32 namelen = std::strlen(symbol) + 1;
|
2016-11-25 16:46:52 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
std::unique_lock lock(s_jitdump_mutex);
|
|
|
|
if (!s_jitdump_file)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
if (!s_jitdump_file_opened)
|
|
|
|
{
|
|
|
|
char file[256];
|
|
|
|
snprintf(file, std::size(file), "jit-%d.dump", getpid());
|
|
|
|
s_jitdump_file = fopen(file, "w+b");
|
|
|
|
s_jitdump_file_opened = true;
|
|
|
|
if (!s_jitdump_file)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* perf_marker = mmap(nullptr, 4096, PROT_READ | PROT_EXEC, MAP_PRIVATE, fileno(s_jitdump_file), 0);
|
|
|
|
pxAssertRel(perf_marker != MAP_FAILED, "Map perf marker");
|
|
|
|
|
|
|
|
JITDUMP_HEADER jh = {};
|
|
|
|
jh.elf_mach = EM_X86_64;
|
|
|
|
jh.pid = getpid();
|
|
|
|
jh.timestamp = JitDumpTimestamp();
|
|
|
|
std::fwrite(&jh, sizeof(jh), 1, s_jitdump_file);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2015-12-06 11:57:54 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
JITDUMP_CODE_LOAD cl = {};
|
|
|
|
cl.header.id = JIT_CODE_LOAD;
|
|
|
|
cl.header.total_size = sizeof(cl) + namelen + static_cast<u32>(size);
|
|
|
|
cl.header.timestamp = JitDumpTimestamp();
|
|
|
|
cl.pid = getpid();
|
|
|
|
cl.tid = syscall(SYS_gettid);
|
|
|
|
cl.vma = 0;
|
|
|
|
cl.code_addr = static_cast<u64>(reinterpret_cast<uintptr_t>(ptr));
|
|
|
|
cl.code_size = static_cast<u64>(size);
|
|
|
|
cl.code_index = s_jitdump_record_id++;
|
|
|
|
std::fwrite(&cl, sizeof(cl), 1, s_jitdump_file);
|
|
|
|
std::fwrite(symbol, namelen, 1, s_jitdump_file);
|
|
|
|
std::fwrite(ptr, size, 1, s_jitdump_file);
|
|
|
|
std::fflush(s_jitdump_file);
|
|
|
|
}
|
|
|
|
#elif defined(ENABLE_VTUNE)
|
|
|
|
static void RegisterMethod(const void* ptr, size_t size, const char* symbol)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
iJIT_Method_Load_V2 ml = {};
|
2021-09-06 18:28:26 +00:00
|
|
|
ml.method_id = iJIT_GetNewMethodID();
|
2023-04-28 11:09:04 +00:00
|
|
|
ml.method_name = const_cast<char*>(symbol);
|
2023-05-04 23:12:15 +00:00
|
|
|
ml.method_load_address = const_cast<void*>(ptr);
|
2023-04-28 11:09:04 +00:00
|
|
|
ml.method_size = static_cast<unsigned int>(size);
|
2021-09-06 18:28:26 +00:00
|
|
|
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED_V2, &ml);
|
|
|
|
}
|
2023-04-28 11:09:04 +00:00
|
|
|
#endif
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
#if (defined(__linux__) && (defined(ProfileWithPerf) || defined(ProfileWithPerfJitDump))) || defined(ENABLE_VTUNE)
|
|
|
|
void Group::Register(const void* ptr, size_t size, const char* symbol)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
char full_symbol[128];
|
|
|
|
if (HasPrefix())
|
|
|
|
std::snprintf(full_symbol, std::size(full_symbol), "%s_%s", m_prefix, symbol);
|
|
|
|
else
|
|
|
|
StringUtil::Strlcpy(full_symbol, symbol, std::size(full_symbol));
|
|
|
|
RegisterMethod(ptr, size, full_symbol);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
void Group::RegisterPC(const void* ptr, size_t size, u32 pc)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
char full_symbol[128];
|
|
|
|
if (HasPrefix())
|
|
|
|
std::snprintf(full_symbol, std::size(full_symbol), "%s_%08X", m_prefix, pc);
|
|
|
|
else
|
|
|
|
std::snprintf(full_symbol, std::size(full_symbol), "%08X", pc);
|
|
|
|
RegisterMethod(ptr, size, full_symbol);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2015-12-06 11:57:54 +00:00
|
|
|
|
2023-04-28 11:09:04 +00:00
|
|
|
void Group::RegisterKey(const void* ptr, size_t size, const char* prefix, u64 key)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-04-28 11:09:04 +00:00
|
|
|
char full_symbol[128];
|
|
|
|
if (HasPrefix())
|
|
|
|
std::snprintf(full_symbol, std::size(full_symbol), "%s_%s%016" PRIX64, m_prefix, prefix, key);
|
|
|
|
else
|
|
|
|
std::snprintf(full_symbol, std::size(full_symbol), "%s%016" PRIX64, prefix, key);
|
|
|
|
RegisterMethod(ptr, size, full_symbol);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2023-04-28 11:09:04 +00:00
|
|
|
#else
|
|
|
|
void Group::Register(const void* ptr, size_t size, const char* symbol) {}
|
|
|
|
void Group::RegisterPC(const void* ptr, size_t size, u32 pc) {}
|
|
|
|
void Group::RegisterKey(const void* ptr, size_t size, const char* prefix, u64 key) {}
|
2015-12-06 11:57:54 +00:00
|
|
|
#endif
|
2021-09-06 18:28:26 +00:00
|
|
|
} // namespace Perf
|