coastline 0.0.4
Loading...
Searching...
No Matches
performance.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <algorithm>
10#include <charconv>
11#include <cstdint>
12#include <memory>
13#include <optional>
14#include <span>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace coastline {
20
22inline constexpr int FRAME_RATE_STATES = 3;
23
25inline constexpr int FRAME_RATE_STATISTICS = 2;
26
37 if (active.empty()) {
38 return result;
39 }
40 for (std::size_t index = 0; index < devices.size(); ++index) {
41 if (devices[index] == active) {
42 if (result) {
43 return std::nullopt;
44 }
45 result = index;
46 }
47 }
48 return result;
49}
50
57 std::size_t at = text.find_first_not_of(' ');
58 if (at == std::string_view::npos) {
59 return std::nullopt;
60 }
61 at = text.find(' ', at);
62 if (at == std::string_view::npos) {
63 return std::nullopt;
64 }
65 text.remove_prefix(at + 1);
66 std::uint64_t pages = 0;
67 auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), pages);
68 if (error != std::errc{} || end == text.data()) {
69 return std::nullopt;
70 }
71 return pages;
72}
73
83 std::string_view executable) {
84 std::uint64_t kilobytes = 0;
85 bool counted = false;
86 bool found = false;
87 while (!text.empty()) {
88 std::size_t end = text.find('\n');
89 std::string_view line = text.substr(0, end);
90 text.remove_prefix(end == std::string_view::npos ? text.size() : end + 1);
91 if (line.starts_with("Pss:")) {
92 line.remove_prefix(4);
93 std::size_t digits = line.find_first_not_of(' ');
94 std::uint64_t value = 0;
95 if (digits == std::string_view::npos ||
96 std::from_chars(line.data() + digits, line.data() + line.size(), value).ec !=
97 std::errc{}) {
98 return std::nullopt;
99 }
100 found = true;
101 if (counted) {
102 kilobytes += value;
103 }
104 continue;
105 }
106 // The line of a mapping: its range, permissions, offset, device, inode, and path, if any.
107 std::size_t dash = line.find('-');
108 if (dash == 0 || dash == std::string_view::npos ||
109 line.find_first_not_of("0123456789abcdef") != dash) {
110 continue;
111 }
112 std::string_view rest = line;
113 for (int field = 0; field < 5; ++field) {
114 std::size_t space = rest.find(' ');
115 rest.remove_prefix(space == std::string_view::npos ? rest.size() : space);
116 rest.remove_prefix(std::min(rest.find_first_not_of(' '), rest.size()));
117 }
118 counted = rest.empty() || rest == "[heap]" || rest.starts_with("[stack") ||
119 rest.starts_with("[anon") || rest == executable;
120 }
121 if (!found) {
122 return std::nullopt;
123 }
124 return kilobytes * 1024;
125}
126
129public:
131 struct Snapshot {
133 double workMs = 0.0;
135 double intervalMs = 0.0;
137 double fps = 0.0;
139 double worstWorkMs = 0.0;
141 double worstIntervalMs = 0.0;
143 double worstFps = 0.0;
144 };
145
153 bool Record(std::uint64_t workNanoseconds, std::uint64_t now) {
154 m_workTotal += static_cast<double>(workNanoseconds);
155 ++m_workCount;
156 m_worstWork = std::max(m_worstWork, workNanoseconds);
157 if (!m_previous) {
158 m_previous = now;
159 m_start = now;
160 return false;
161 }
162 const auto interval = now - *m_previous;
163 m_previous = now;
164 m_intervalTotal += static_cast<double>(interval);
165 ++m_intervals;
166 m_worstInterval = std::max(m_worstInterval, interval);
167 if (now - m_start < 1000000000) {
168 return false;
169 }
170 const double fps =
171 static_cast<double>(m_intervals) * 1e9 / static_cast<double>(now - m_start);
172 m_snapshot = {m_workTotal / static_cast<double>(m_workCount) / 1e6,
173 m_intervalTotal / static_cast<double>(m_intervals) / 1e6,
174 fps,
175 static_cast<double>(m_worstWork) / 1e6,
176 static_cast<double>(m_worstInterval) / 1e6,
177 m_snapshot ? std::min(m_snapshot->worstFps, fps) : fps};
178 m_workTotal = 0.0;
179 m_workCount = 0;
180 m_intervalTotal = 0.0;
181 m_intervals = 0;
182 m_start = now;
183 return true;
184 }
185
191 return m_snapshot;
192 }
193
194private:
195 std::optional<Snapshot> m_snapshot;
197 std::uint64_t m_start = 0;
198 double m_workTotal = 0.0;
199 std::uint64_t m_workCount = 0;
200 double m_intervalTotal = 0.0;
201 std::uint64_t m_intervals = 0;
202 std::uint64_t m_worstWork = 0;
203 std::uint64_t m_worstInterval = 0;
204};
205
208public:
215 PerformanceStats(std::string gpuName, bool allowNvml);
222
228 void Frame(std::uint64_t workNanoseconds);
229
236
237private:
238 class Sampler;
239
240 std::unique_ptr<Sampler> m_sampler;
241 FrameTimings m_frameTimings;
242 std::uint64_t m_secondStart = 0;
243 std::optional<double> m_secondStartCpu; // The seconds of the processor at the second's start.
244 std::optional<double> m_cpuPercent; // Of one core, over the second before.
245};
246
247} // namespace coastline
One-second timing averages and worst values since collection began.
Definition performance.h:128
const std::optional< Snapshot > & Latest() const
Definition performance.h:190
bool Record(std::uint64_t workNanoseconds, std::uint64_t now)
Accumulate a frame and publish averages after at least one second of timestamp progress.
Definition performance.h:153
std::vector< std::string > Lines(std::uint64_t gameGpuBytes) const
void Frame(std::uint64_t workNanoseconds)
Record frame work and elapsed frame intervals, and sample once a second has passed.
PerformanceStats(const PerformanceStats &)=delete
Prevent copying the collector's GPU sampling thread.
PerformanceStats(std::string gpuName, bool allowNvml)
Sample the active adapter only when its identity permits NVML.
PerformanceStats & operator=(const PerformanceStats &)=delete
Prevent assignment of the collector's GPU sampling thread.
~PerformanceStats()
Stop sampling and release provider resources before destruction completes.
T data(T... args)
T empty(T... args)
T find_first_not_of(T... args)
T find(T... args)
T from_chars(T... args)
T max(T... args)
T min(T... args)
Definition assetimport.h:11
std::optional< std::uint64_t > StatmResidentPages(std::string_view text)
Definition performance.h:56
constexpr int FRAME_RATE_STATISTICS
The state of F in g_bShowFpsCounter that shows the statistics beside the frame rate.
Definition performance.h:25
std::optional< std::uint64_t > SmapsGamePssBytes(std::string_view text, std::string_view executable)
Definition performance.h:82
std::optional< std::size_t > UniqueGpuIndex(std::string_view active, std::span< const std::string > devices)
Match the active adapter to one provider device, rejecting absent and ambiguous identities.
Definition performance.h:34
constexpr int FRAME_RATE_STATES
The states of F in g_bShowFpsCounter: 0 for off, 1 for the frame rate, and 2 for both.
Definition performance.h:22
T remove_prefix(T... args)
T size(T... args)
T starts_with(T... args)
Completed sampling interval averages and extrema since the first recorded frame.
Definition performance.h:131
double intervalMs
Mean interval between frame timestamps in milliseconds, including waits.
Definition performance.h:135
double worstWorkMs
Greatest recorded CPU work time in milliseconds since collection began.
Definition performance.h:139
double worstIntervalMs
Greatest recorded interval in milliseconds since collection began.
Definition performance.h:141
double worstFps
Lowest completed sampling interval frame rate since collection began.
Definition performance.h:143
double fps
Frame intervals per second over the completed sampling interval.
Definition performance.h:137
double workMs
Mean CPU work per frame in milliseconds, excluding presentation and pacing waits.
Definition performance.h:133
T substr(T... args)