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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include <chrono>
#include <cstdlib>
#include <string>
#include <unordered_set>
#include "common/common_types.h"
#include "common/logging.h"
#include "common/cpu_features.h"
// EDEN_NX2_GPU_PROFILE: time where the GPU thread spends its time. Each zone counts its own
// (exclusive) time, nested zones excluded; every 10 s the thread logs each zone's share.
namespace VideoCommon::Nx2Profile {
enum class Zone : u32 {
Other, ///< GPU thread time outside every zone below (register writes, command parsing)
Idle,
PendingSleep,
ResumePending,
Push,
Tick,
Run,
FlushInvalidate,
Macro,
Draw,
Clear,
Compute,
FlushWork,
PipelineLookup,
Configure,
SyncDescriptors,
ReadHandles,
FillImageViews,
TextureBuffers,
UpdateBuffers,
DescriptorAcquire,
BindStageBuffers,
PushDescriptors,
RenderTargets,
ConfigureDraw,
DynamicState,
DrawRecord,
TextureUpload,
Fence, ///< Fences made (SignalFence)
FenceSkipped, ///< Empty fences not made
Method, ///< Engine method calls (what a split GPU thread's back half would run)
SplitDrain, ///< Split GPU thread: the front waits for the back (gpu_split.h)
SplitWait, ///< Split GPU thread: the back waits for work
Count,
};
inline constexpr std::array<const char*, static_cast<size_t>(Zone::Count)> zone_names{
"other", "idle", "pending sleep", "resume pending", "push",
"tick", "run", "flush/inval", "macro", "draw",
"clear", "compute", "flush work", "pipeline lookup", "configure",
"sync descriptors", "read handles", "fill image views", "texture buffers", "update buffers",
"descriptor acquire", "bind stage buffers", "push descriptors", "render targets",
"configure draw", "dynamic state", "draw record", "texture upload", "fence",
"fence skipped", "method", "split drain", "split wait",
};
// Read once at startup: Enabled() runs several times per method write (no guard to check).
inline const bool g_enabled = std::getenv("EDEN_NX2_GPU_PROFILE") != nullptr;
inline bool Enabled() {
return g_enabled;
}
// Nanoseconds; from the host's own counter where the wall clock reads it directly (the zones
// run ~150,000 times a second, and QueryPerformanceCounter cost ~5% of the GPU thread).
inline u64 Now() {
return static_cast<u64>(Common::g_wall_clock.GetTimeNS().count());
}
struct ThreadData {
std::array<u64, static_cast<size_t>(Zone::Count)> exclusive_ns{};
std::array<u64, static_cast<size_t>(Zone::Count)> count{};
std::array<u64, 8> counters{}; ///< See Counter
u64 child_ns{}; ///< Time of the zones nested in the current one
u64 window_start{};
};
inline thread_local ThreadData t_data;
class Scope {
public:
explicit Scope(Zone zone_) : zone{zone_}, enabled{Enabled()} {
if (!enabled) {
return;
}
parent_child_ns = t_data.child_ns;
t_data.child_ns = 0;
start = Now();
}
~Scope() {
if (!enabled) {
return;
}
const u64 elapsed = Now() - start;
auto& data = t_data;
const auto index = static_cast<size_t>(zone);
data.exclusive_ns[index] += elapsed - data.child_ns;
++data.count[index];
data.child_ns = parent_child_ns + elapsed;
}
Scope(const Scope&) = delete;
Scope& operator=(const Scope&) = delete;
private:
Zone zone;
bool enabled;
u64 start{};
u64 parent_child_ns{};
};
/// Amounts counted on the GPU thread, logged per second with the zones.
enum class Counter : u32 {
DownloadBytes, ///< Buffer data copied back from the host GPU at fences
DownloadCopies, ///< Buffer ranges copied back
DmaReadBytes, ///< Guest memory the native MME read with DMA
DownloadBytesOnDmaPages, ///< Of DownloadBytes: on pages the MME has DMA-read
MethodWrites, ///< 3D register writes (Maxwell3D::CallMethod below the macro range)
SameValueWrites, ///< Of MethodWrites: the register already held the value
AcquireChecks, ///< Semaphore acquires evaluated
AcquireWaits, ///< Of AcquireChecks: not met yet (the channel waits)
};
/// Device pages (4 KiB) the native MME has DMA-read since start (GPU thread only; filled only
/// while profiling): which buffer downloads anything on the GPU thread reads back.
inline std::unordered_set<u64>& DmaReadPages() {
static std::unordered_set<u64> pages;
return pages;
}
inline void Add(Counter counter, u64 amount) {
if (Enabled()) {
t_data.counters[static_cast<size_t>(counter)] += amount;
}
}
/// Called by the GPU thread (and the split's back thread) between commands: logs and resets
/// the totals every 10 s.
inline void Report(const char* thread_name = "GPU thread") {
if (!Enabled()) {
return;
}
auto& data = t_data;
const u64 now = Now();
if (data.window_start == 0) {
data.window_start = now;
data.child_ns = 0;
return;
}
const u64 window = now - data.window_start;
if (window < 10'000'000'000ULL) {
return;
}
// Time in no zone at all is the wall time minus the zones' totals.
u64 zoned{};
for (size_t i = 1; i < data.exclusive_ns.size(); ++i) {
zoned += data.exclusive_ns[i];
}
data.exclusive_ns[0] = window > zoned ? window - zoned : 0;
std::string line;
for (size_t i = 0; i < data.exclusive_ns.size(); ++i) {
if (data.exclusive_ns[i] == 0 && data.count[i] == 0) {
continue;
}
// Per second of wall time: milliseconds spent and calls made.
const double ms = static_cast<double>(data.exclusive_ns[i]) * 1000.0 / window;
const double calls = static_cast<double>(data.count[i]) * 1e9 / window;
line += fmt::format(" | {} {:.1f} ms ({:.0f}/s)", zone_names[i], ms, calls);
}
line += fmt::format(" | buffer downloads {:.0f} KiB in {:.0f} copies ({:.0f} KiB on pages "
"MME DMA reads) | MME DMA reads {:.0f} KiB",
static_cast<double>(data.counters[0]) * 1e9 / 1024.0 / window,
static_cast<double>(data.counters[1]) * 1e9 / window,
static_cast<double>(data.counters[3]) * 1e9 / 1024.0 / window,
static_cast<double>(data.counters[2]) * 1e9 / 1024.0 / window);
line += fmt::format(" | 3D register writes {:.0f} ({:.0f} same value)",
static_cast<double>(data.counters[4]) * 1e9 / window,
static_cast<double>(data.counters[5]) * 1e9 / window);
line += fmt::format(" | acquires {:.0f} ({:.0f} waited)",
static_cast<double>(data.counters[6]) * 1e9 / window,
static_cast<double>(data.counters[7]) * 1e9 / window);
LOG_INFO(HW_GPU, "{} per second:{}", thread_name, line);
data = {};
data.window_start = now;
}
} // namespace VideoCommon::Nx2Profile
|