2014-10-31 13:12:54 -06:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2014-10-31 13:12:54 -06:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-06-14 08:53:46 -06:00
|
|
|
#include "Common/JitRegister.h"
|
|
|
|
|
2014-10-31 13:12:54 -06:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
2015-10-10 08:42:53 -06:00
|
|
|
#include <cstdlib>
|
2014-10-31 13:12:54 -06:00
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
2019-06-14 08:53:46 -06:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2014-10-31 13:12:54 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-09-15 04:29:41 -06:00
|
|
|
#include "Common/IOFile.h"
|
2014-10-31 13:12:54 -06:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <process.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined USE_OPROFILE && USE_OPROFILE
|
|
|
|
#include <opagent.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined USE_VTUNE
|
|
|
|
#include <jitprofiling.h>
|
|
|
|
#pragma comment(lib, "jitprofiling.lib")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined USE_OPROFILE && USE_OPROFILE
|
|
|
|
static op_agent_t s_agent = nullptr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static File::IOFile s_perf_map_file;
|
|
|
|
|
|
|
|
namespace JitRegister
|
|
|
|
{
|
2017-08-25 12:59:31 -06:00
|
|
|
static bool s_is_enabled = false;
|
|
|
|
|
2015-05-02 14:30:56 -06:00
|
|
|
void Init(const std::string& perf_dir)
|
2014-10-31 13:12:54 -06:00
|
|
|
{
|
|
|
|
#if defined USE_OPROFILE && USE_OPROFILE
|
|
|
|
s_agent = op_open_agent();
|
2017-08-25 12:59:31 -06:00
|
|
|
s_is_enabled = true;
|
2014-10-31 13:12:54 -06:00
|
|
|
#endif
|
|
|
|
|
2015-10-10 08:42:53 -06:00
|
|
|
if (!perf_dir.empty() || getenv("PERF_BUILDID_DIR"))
|
2014-10-31 13:12:54 -06:00
|
|
|
{
|
2019-06-14 08:53:46 -06:00
|
|
|
const std::string dir = perf_dir.empty() ? "/tmp" : perf_dir;
|
|
|
|
const std::string filename = fmt::format("{}/perf-{}.map", dir, getpid());
|
2014-10-31 13:12:54 -06:00
|
|
|
s_perf_map_file.Open(filename, "w");
|
|
|
|
// Disable buffering in order to avoid missing some mappings
|
|
|
|
// if the event of a crash:
|
2015-03-14 19:20:41 -06:00
|
|
|
std::setvbuf(s_perf_map_file.GetHandle(), nullptr, _IONBF, 0);
|
2017-08-25 12:59:31 -06:00
|
|
|
s_is_enabled = true;
|
2014-10-31 13:12:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
#if defined USE_OPROFILE && USE_OPROFILE
|
|
|
|
op_close_agent(s_agent);
|
|
|
|
s_agent = nullptr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_VTUNE
|
|
|
|
iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN, nullptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (s_perf_map_file.IsOpen())
|
|
|
|
s_perf_map_file.Close();
|
2017-08-25 12:59:31 -06:00
|
|
|
|
|
|
|
s_is_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEnabled()
|
|
|
|
{
|
|
|
|
return s_is_enabled;
|
2014-10-31 13:12:54 -06:00
|
|
|
}
|
|
|
|
|
2015-01-05 12:24:37 -07:00
|
|
|
void RegisterV(const void* base_address, u32 code_size, const char* format, va_list args)
|
2014-10-31 13:12:54 -06:00
|
|
|
{
|
|
|
|
#if !(defined USE_OPROFILE && USE_OPROFILE) && !defined(USE_VTUNE)
|
|
|
|
if (!s_perf_map_file.IsOpen())
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2014-12-28 17:09:07 -07:00
|
|
|
std::string symbol_name = StringFromFormatV(format, args);
|
2014-10-31 13:12:54 -06:00
|
|
|
|
|
|
|
#if defined USE_OPROFILE && USE_OPROFILE
|
2019-06-14 08:53:46 -06:00
|
|
|
op_write_native_code(s_agent, symbol_name.c_str(), (u64)base_address, base_address, code_size);
|
2014-10-31 13:12:54 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_VTUNE
|
|
|
|
iJIT_Method_Load jmethod = {0};
|
|
|
|
jmethod.method_id = iJIT_GetNewMethodID();
|
2015-02-11 17:36:29 -07:00
|
|
|
jmethod.method_load_address = const_cast<void*>(base_address);
|
2014-10-31 13:12:54 -06:00
|
|
|
jmethod.method_size = code_size;
|
2019-06-14 08:53:46 -06:00
|
|
|
jmethod.method_name = const_cast<char*>(symbol_name.c_str());
|
2014-10-31 13:12:54 -06:00
|
|
|
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, (void*)&jmethod);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Linux perf /tmp/perf-$pid.map:
|
2019-06-14 08:53:46 -06:00
|
|
|
if (!s_perf_map_file.IsOpen())
|
|
|
|
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());
|
2014-10-31 13:12:54 -06:00
|
|
|
}
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace JitRegister
|