summaryrefslogtreecommitdiff
path: root/Source/Core/Common/JitRegister.cpp
blob: f05a9e2b4dbe23ec1a3c2323fdc700d6bf38c933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/JitRegister.h"

#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>

#include <fmt/format.h>

#include "Common/CommonTypes.h"
#include "Common/IOFile.h"
#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 Common::JitRegister
{
static bool s_is_enabled = false;

void Init(const std::string& perf_dir)
{
#if defined USE_OPROFILE && USE_OPROFILE
  s_agent = op_open_agent();
  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());
    s_perf_map_file.Open(filename, "w");
    // Disable buffering in order to avoid missing some mappings
    // if the event of a crash:
    std::setvbuf(s_perf_map_file.GetHandle(), nullptr, _IONBF, 0);
    s_is_enabled = true;
  }
}

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();

  s_is_enabled = false;
}

bool IsEnabled()
{
  return s_is_enabled;
}

void Register(const void* base_address, u32 code_size, const std::string& symbol_name)
{
#if !(defined USE_OPROFILE && USE_OPROFILE) && !defined(USE_VTUNE)
  if (!s_perf_map_file.IsOpen())
    return;
#endif

#if defined USE_OPROFILE && USE_OPROFILE
  op_write_native_code(s_agent, symbol_name.c_str(), (u64)base_address, base_address, code_size);
#endif

#ifdef USE_VTUNE
  iJIT_Method_Load jmethod = {0};
  jmethod.method_id = iJIT_GetNewMethodID();
  jmethod.method_load_address = const_cast<void*>(base_address);
  jmethod.method_size = code_size;
  jmethod.method_name = const_cast<char*>(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.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());
}
}  // namespace Common::JitRegister