Debian Codegraph @

Source/Core/Common/JitRegister.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/JitRegister.h"
5
6 #include <cstdio>
7 #include <cstdlib>
8 #include <string>
9
10 #include <fmt/format.h>
11
12 #include "Common/CommonTypes.h"
13 #include "Common/IOFile.h"
14
15 #ifdef _WIN32
16 #include <process.h>
17 #else
18 #include <unistd.h>
19 #endif
20
21 #if defined USE_VTUNE
22 #include <jitprofiling.h>
23 #pragma comment(lib, "jitprofiling.lib")
24 #endif
25
26 static File::IOFile s_perf_map_file;
27
28 namespace Common::JitRegister
29 {
30 static bool s_is_enabled = false;
31
32 void Init(const std::string& perf_dir)
33 {
34 #ifdef USE_VTUNE
35 s_is_enabled = true;
36 #endif
37
38 if (!perf_dir.empty() || getenv("PERF_BUILDID_DIR"))
39 {
40 const std::string dir = perf_dir.empty() ? "/tmp" : perf_dir;
41 const std::string filename = fmt::format("{}/perf-{}.map", dir, getpid());
42 if (s_perf_map_file.Open(filename, "w"))
43 {
44 // Disable buffering in order to avoid missing some mappings
45 // in the event of a crash:
46 std::setvbuf(s_perf_map_file.GetHandle(), nullptr, _IONBF, 0);
47 s_is_enabled = true;
48 }
49 }
50 }
51
52 void Shutdown()
53 {
54 #ifdef USE_VTUNE
55 iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN, nullptr);
56 #endif
57
58 if (s_perf_map_file.IsOpen())
59 s_perf_map_file.Close();
60
61 s_is_enabled = false;
62 }
63
64 bool IsEnabled()
65 {
66 return s_is_enabled;
67 }
68
69 void Register(const void* base_address, u32 code_size, const std::string& symbol_name)
70 {
71 #ifdef USE_VTUNE
72 iJIT_Method_Load jmethod = {0};
73 jmethod.method_id = iJIT_GetNewMethodID();
74 jmethod.method_load_address = const_cast<void*>(base_address);
75 jmethod.method_size = code_size;
76 jmethod.method_name = const_cast<char*>(symbol_name.c_str());
77 iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, (void*)&jmethod);
78 #endif
79
80 // Linux perf /tmp/perf-$pid.map:
81 if (!s_perf_map_file)
82 return;
83
84 const auto entry = fmt::format("{} {:x} {}\n", fmt::ptr(base_address), code_size, symbol_name);
85 s_perf_map_file.WriteBytes(entry.data(), entry.size());
86 }
87 } // namespace Common::JitRegister