34#include <unordered_map>
97 auto age = std::chrono::steady_clock::now() -
timestamp;
98 return std::chrono::duration_cast<std::chrono::milliseconds>(age).count() < max_age_ms;
108 std::snprintf(buf,
sizeof(buf),
"%.1f",
fps);
119 std::snprintf(buf,
sizeof(buf),
"%.2f ms",
frametime_ms);
132 std::snprintf(buf,
sizeof(buf),
"%.1f",
fps_1_low);
145 std::snprintf(buf,
sizeof(buf),
"%.1f ms",
latency_ms);
158 std::snprintf(buf,
sizeof(buf),
"%.1f",
fps_0_1_low);
168 auto age = std::chrono::steady_clock::now() -
timestamp;
169 return std::chrono::duration_cast<std::chrono::milliseconds>(age).count();
181 std::snprintf(buf,
sizeof(buf),
"%.1f",
fps_avg);
263 GamescopeMetricsReader();
264 ~GamescopeMetricsReader()
override;
266 bool start(
int pid)
override;
267 void stop()
override;
278 bool connectToMessageQueue();
280 std::thread readerThread_;
281 std::atomic<bool> shouldStop_{
false};
282 std::atomic<bool> isActive_{
false};
283 mutable std::mutex metricsMutex_;
284 FrameMetrics currentMetrics_;
286 int msgQueueId_ = -1;
290 static constexpr size_t SAMPLE_BUFFER_SIZE = 300;
291 std::vector<double> frametimeBuffer_;
292 size_t bufferIndex_ = 0;
293 size_t bufferCount_ = 0;
295 void updatePercentiles();
322 bool start(
int pid)
override;
323 void stop()
override;
366 const std::string& userConfigFile = {});
377 bool parseLogLine(
const std::string& line);
378 bool parseHeader(
const std::string& line);
380 std::thread readerThread_;
381 std::atomic<bool> shouldStop_{
false};
382 std::atomic<bool> isActive_{
false};
383 mutable std::mutex metricsMutex_;
384 FrameMetrics currentMetrics_;
386 std::string logDirectory_;
387 std::string currentLogFile_;
389 std::streampos lastReadPos_ = 0;
393 int frametimeColumn_ = -1;
396 static constexpr size_t SAMPLE_BUFFER_SIZE = 300;
397 std::vector<double> frametimeBuffer_;
398 size_t bufferIndex_ = 0;
399 size_t bufferCount_ = 0;
401 int checkCounter_ = 0;
403 void updatePercentiles();
432class FrameMetricsService
439 static FrameMetricsService&
instance();
449 bool startCollection(
const std::string& gameId,
int pid,
bool useGamescope =
false,
450 bool enableMangoHudLogging =
true);
507 const std::string& userConfigFile = {})
const;
520 FrameMetricsService();
521 ~FrameMetricsService();
522 FrameMetricsService(
const FrameMetricsService&) =
delete;
523 FrameMetricsService& operator=(
const FrameMetricsService&) =
delete;
525 struct GameMetricsContext
527 std::unique_ptr<IFrameMetricsReader> reader;
531 mutable std::mutex contextsMutex_;
532 std::unordered_map<std::string, GameMetricsContext> contexts_;
bool startCollection(const std::string &gameId, int pid, bool useGamescope=false, bool enableMangoHudLogging=true)
Start collecting metrics for a game.
bool isCollecting(const std::string &gameId) const
Check if collection is active for a game.
std::string getMangoHudLoggingConfig() const
Get MangoHud config for enabling logging.
FrameMetrics getMetrics(const std::string &gameId) const
Get current metrics for a game.
static FrameMetricsService & instance()
Get the singleton instance.
int cleanupOldLogs(int maxAgeHours=24)
Clean up old MangoHud log files.
bool isGamescopeAvailable() const
Check if gamescope is available.
std::string buildMangoHudLaunchConfigFile(const std::string &gameId, const std::string &userConfigFile={}) const
Materialise a complete MangoHud config file for a launch.
bool isMangoHudLoggingAvailable() const
Check if MangoHud logging is available.
void stopAll()
Stop all active collections.
void stopCollection(const std::string &gameId)
Stop collecting metrics for a game.
Abstract interface for frame metrics sources.
virtual FrameMetrics getMetrics() const =0
Get the latest metrics.
virtual bool isAvailable() const =0
Check if this reader is available on the system.
virtual ~IFrameMetricsReader()=default
virtual std::string getSourceName() const =0
Get the source name for this reader.
virtual void stop()=0
Stop collecting metrics.
virtual bool start(int pid)=0
Start collecting metrics for a process.
virtual bool isActive() const =0
Check if reader is actively collecting.
bool isActive() const override
Check if reader is actively collecting.
std::string getSourceName() const override
Get the source name for this reader.
static std::string getDefaultLogDirectory()
Get the default log directory.
bool start(int pid) override
Start collecting metrics for a process.
FrameMetrics getMetrics() const override
Get the latest metrics.
static std::string getLoggingConfig()
Get the MangoHud config string for logging.
void setLogDirectory(const std::string &path)
Set the log directory to monitor.
~MangoHudLogReader() override
static std::string buildLaunchConfigFile(const std::string &gameId, const std::string &userConfigFile={})
Materialise a complete MangoHud config file for a launch.
void stop() override
Stop collecting metrics.
bool isAvailable() const override
Check if this reader is available on the system.
Real-time frame timing and performance metrics.
uint32_t output_width
Output resolution width.
double fps_min
Minimum FPS in sample window.
std::string getFrametimeString() const
Get formatted frametime string.
double fps_0_1_low
0.1% low FPS (99.9th percentile frametime)
uint32_t output_height
Output resolution height.
std::vector< double > frametimeHistory
Recent frametimes for graph.
std::string getFPSAvgString() const
Get formatted average FPS string.
double frametime_ms
Current frame time in milliseconds.
double frametime_min_ms
Minimum frametime in sample window.
std::string getFPSString() const
Get formatted FPS string.
static constexpr size_t GRAPH_HISTORY_SIZE
std::chrono::steady_clock::time_point timestamp
std::string get01LowString() const
Get formatted 0.1% low string.
std::string getLatencyString() const
Get formatted latency string.
bool isValid(int max_age_ms=2000) const
Check if metrics are valid/fresh.
double frametime_avg_ms
Average frametime over sample window.
int64_t getAgeMs() const
Get age of metrics in milliseconds.
double fps
Current frames per second.
double fps_max
Maximum FPS in sample window.
double latency_ms
Input-to-display latency in ms.
std::string engine_name
Game engine name (if detected).
double fps_1_low
1% low FPS (99th percentile frametime)
double fps_avg
Average FPS over sample window.
std::string get1LowString() const
Get formatted 1% low string.
std::string source
"gamescope", "mangohud", or empty
double frametime_max_ms
Maximum frametime in sample window.
uint16_t display_refresh
Monitor refresh rate (Hz).