94 lines
2.3 KiB
C++
94 lines
2.3 KiB
C++
#ifndef GPU_MONITOR_H
|
|
#define GPU_MONITOR_H
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
|
|
// Include NVML types for Linux
|
|
#ifdef __linux__
|
|
#include <nvml.h>
|
|
#endif
|
|
|
|
/// Structure containing GPU statistics
|
|
struct GpuStats {
|
|
double utilizationPercent; // GPU utilization percentage (0-100)
|
|
double memoryUsedMB; // Memory used in MB
|
|
double memoryTotalMB; // Total memory in MB
|
|
int deviceIndex; // Index of the GPU device
|
|
};
|
|
|
|
/// Class for monitoring GPU utilization and memory
|
|
class GpuMonitor {
|
|
public:
|
|
explicit GpuMonitor();
|
|
~GpuMonitor();
|
|
|
|
/// Returns true if monitor is initialized and ready
|
|
bool isReady() const;
|
|
|
|
/// Gets the latest GPU statistics
|
|
/// @param stats Reference to store the statistics
|
|
/// @return true on success, false on failure
|
|
bool getGpuStats(GpuStats &stats);
|
|
|
|
/// Enables or disables GPU monitoring
|
|
/// @param enabled Whether to enable monitoring
|
|
void setEnabled(bool enabled);
|
|
|
|
/// @return Whether GPU monitoring is enabled
|
|
bool isEnabled() const;
|
|
|
|
private:
|
|
// NVML forward declarations - will be dynamically linked
|
|
struct nvmlDevice;
|
|
struct nvmlStat;
|
|
|
|
// Internal implementation details
|
|
struct Impl {
|
|
nvmlDevice_t device_;
|
|
bool enabled_;
|
|
bool ready_;
|
|
|
|
Impl() : device_(nullptr), enabled_(true), ready_(false) {}
|
|
|
|
~Impl() {
|
|
if (device_) {
|
|
nvmlShutdown();
|
|
}
|
|
}
|
|
|
|
bool initialize() {
|
|
// Initialize NVML
|
|
nvmlReturn_t result = nvmlInit();
|
|
if (result != NVML_SUCCESS) {
|
|
return false;
|
|
}
|
|
|
|
// Get default GPU device
|
|
unsigned int deviceCount = 0;
|
|
result = nvmlDeviceGetCount(&deviceCount);
|
|
if (result != NVML_SUCCESS || deviceCount == 0) {
|
|
return false;
|
|
}
|
|
|
|
if (deviceCount > 0) {
|
|
result = nvmlDeviceGetHandleByIndex(0, &device_);
|
|
if (result != NVML_SUCCESS) {
|
|
return false;
|
|
}
|
|
ready_ = true;
|
|
}
|
|
|
|
return ready_;
|
|
}
|
|
};
|
|
|
|
std::unique_ptr<Impl> impl_;
|
|
|
|
// Configuration
|
|
bool enabled_ = true;
|
|
bool ready_ = false;
|
|
};
|
|
|
|
#endif // GPU_MONITOR_H
|