2020-07-06 17:31:40 +00:00
|
|
|
#pragma once
|
|
|
|
#ifndef MANGOHUD_CPU_H
|
|
|
|
#define MANGOHUD_CPU_H
|
|
|
|
|
2020-02-01 23:55:24 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <cstdint>
|
2020-03-14 20:59:43 +00:00
|
|
|
#include <cstdio>
|
2020-11-12 21:27:35 +00:00
|
|
|
#include <memory>
|
2022-03-13 14:51:41 +00:00
|
|
|
#include <string>
|
2023-03-12 13:44:27 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2020-11-12 21:27:35 +00:00
|
|
|
#include "timing.hpp"
|
2020-02-01 23:55:24 +00:00
|
|
|
|
|
|
|
typedef struct CPUData_ {
|
2020-03-13 14:35:11 +00:00
|
|
|
unsigned long long int totalTime;
|
|
|
|
unsigned long long int userTime;
|
|
|
|
unsigned long long int systemTime;
|
|
|
|
unsigned long long int systemAllTime;
|
|
|
|
unsigned long long int idleAllTime;
|
|
|
|
unsigned long long int idleTime;
|
|
|
|
unsigned long long int niceTime;
|
|
|
|
unsigned long long int ioWaitTime;
|
|
|
|
unsigned long long int irqTime;
|
|
|
|
unsigned long long int softIrqTime;
|
|
|
|
unsigned long long int stealTime;
|
|
|
|
unsigned long long int guestTime;
|
2020-02-01 23:55:24 +00:00
|
|
|
|
2020-03-13 14:35:11 +00:00
|
|
|
unsigned long long int totalPeriod;
|
|
|
|
unsigned long long int userPeriod;
|
|
|
|
unsigned long long int systemPeriod;
|
|
|
|
unsigned long long int systemAllPeriod;
|
|
|
|
unsigned long long int idleAllPeriod;
|
|
|
|
unsigned long long int idlePeriod;
|
|
|
|
unsigned long long int nicePeriod;
|
|
|
|
unsigned long long int ioWaitPeriod;
|
|
|
|
unsigned long long int irqPeriod;
|
|
|
|
unsigned long long int softIrqPeriod;
|
|
|
|
unsigned long long int stealPeriod;
|
|
|
|
unsigned long long int guestPeriod;
|
2022-04-25 07:25:56 +00:00
|
|
|
|
|
|
|
int cpu_id;
|
2020-03-13 14:35:11 +00:00
|
|
|
float percent;
|
|
|
|
int mhz;
|
|
|
|
int temp;
|
2020-11-11 22:11:21 +00:00
|
|
|
int cpu_mhz;
|
2022-02-07 03:34:17 +00:00
|
|
|
float power;
|
2020-02-01 23:55:24 +00:00
|
|
|
} CPUData;
|
|
|
|
|
2020-11-12 21:27:35 +00:00
|
|
|
enum {
|
|
|
|
CPU_POWER_K10TEMP,
|
|
|
|
CPU_POWER_ZENPOWER,
|
2023-07-29 17:48:38 +00:00
|
|
|
CPU_POWER_ZENERGY,
|
2022-01-14 21:41:38 +00:00
|
|
|
CPU_POWER_RAPL,
|
|
|
|
CPU_POWER_AMDGPU
|
2020-11-12 21:27:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CPUPowerData {
|
|
|
|
int source;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CPUPowerData_k10temp : public CPUPowerData {
|
|
|
|
CPUPowerData_k10temp() {
|
|
|
|
this->source = CPU_POWER_K10TEMP;
|
|
|
|
};
|
|
|
|
|
|
|
|
~CPUPowerData_k10temp() {
|
|
|
|
if(this->coreVoltageFile)
|
|
|
|
fclose(this->coreVoltageFile);
|
|
|
|
if(this->coreCurrentFile)
|
|
|
|
fclose(this->coreCurrentFile);
|
|
|
|
if(this->socVoltageFile)
|
|
|
|
fclose(this->socVoltageFile);
|
|
|
|
if(this->socCurrentFile)
|
|
|
|
fclose(this->socCurrentFile);
|
2023-07-23 19:54:26 +00:00
|
|
|
if(this->corePowerFile)
|
|
|
|
fclose(this->corePowerFile);
|
|
|
|
if(this->socPowerFile)
|
|
|
|
fclose(this->socPowerFile);
|
2020-11-12 21:27:35 +00:00
|
|
|
};
|
|
|
|
|
2020-11-28 23:24:35 +00:00
|
|
|
FILE* coreVoltageFile {nullptr};
|
|
|
|
FILE* coreCurrentFile {nullptr};
|
|
|
|
FILE* socVoltageFile {nullptr};
|
|
|
|
FILE* socCurrentFile {nullptr};
|
2023-07-23 19:54:26 +00:00
|
|
|
FILE* corePowerFile {nullptr};
|
|
|
|
FILE* socPowerFile {nullptr};
|
2020-11-12 21:27:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CPUPowerData_zenpower : public CPUPowerData {
|
|
|
|
CPUPowerData_zenpower() {
|
|
|
|
this->source = CPU_POWER_ZENPOWER;
|
|
|
|
};
|
|
|
|
|
|
|
|
~CPUPowerData_zenpower() {
|
|
|
|
if(this->corePowerFile)
|
|
|
|
fclose(this->corePowerFile);
|
|
|
|
if(this->socPowerFile)
|
|
|
|
fclose(this->socPowerFile);
|
|
|
|
};
|
|
|
|
|
2020-11-28 23:24:35 +00:00
|
|
|
FILE* corePowerFile {nullptr};
|
|
|
|
FILE* socPowerFile {nullptr};
|
2020-11-12 21:27:35 +00:00
|
|
|
};
|
|
|
|
|
2023-07-29 17:48:38 +00:00
|
|
|
struct CPUPowerData_zenergy : public CPUPowerData {
|
|
|
|
CPUPowerData_zenergy() {
|
|
|
|
this->source = CPU_POWER_ZENERGY;
|
|
|
|
this->lastCounterValue = 0;
|
|
|
|
this->lastCounterValueTime = Clock::now();
|
|
|
|
};
|
|
|
|
|
|
|
|
~CPUPowerData_zenergy() {
|
|
|
|
if(this->energyCounterFile)
|
|
|
|
fclose(this->energyCounterFile);
|
|
|
|
};
|
|
|
|
|
|
|
|
FILE* energyCounterFile {nullptr};
|
|
|
|
uint64_t lastCounterValue;
|
|
|
|
Clock::time_point lastCounterValueTime;
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:27:35 +00:00
|
|
|
struct CPUPowerData_rapl : public CPUPowerData {
|
|
|
|
CPUPowerData_rapl() {
|
|
|
|
this->source = CPU_POWER_RAPL;
|
2021-04-05 10:36:34 +00:00
|
|
|
this->lastCounterValue = 0;
|
2020-11-12 21:27:35 +00:00
|
|
|
this->lastCounterValueTime = Clock::now();
|
|
|
|
};
|
|
|
|
|
|
|
|
~CPUPowerData_rapl() {
|
|
|
|
if(this->energyCounterFile)
|
|
|
|
fclose(this->energyCounterFile);
|
|
|
|
};
|
|
|
|
|
2020-11-28 23:24:35 +00:00
|
|
|
FILE* energyCounterFile {nullptr};
|
2021-04-05 10:36:34 +00:00
|
|
|
uint64_t lastCounterValue;
|
2020-11-12 21:27:35 +00:00
|
|
|
Clock::time_point lastCounterValueTime;
|
|
|
|
};
|
|
|
|
|
2022-01-14 21:41:38 +00:00
|
|
|
struct CPUPowerData_amdgpu : public CPUPowerData {
|
|
|
|
CPUPowerData_amdgpu() {
|
|
|
|
this->source = CPU_POWER_AMDGPU;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-02-01 23:55:24 +00:00
|
|
|
class CPUStats
|
|
|
|
{
|
|
|
|
public:
|
2020-03-13 14:35:11 +00:00
|
|
|
CPUStats();
|
2020-03-14 20:59:43 +00:00
|
|
|
~CPUStats();
|
2020-03-13 14:35:11 +00:00
|
|
|
bool Init();
|
2021-01-30 13:50:02 +00:00
|
|
|
bool Reinit();
|
2020-03-13 14:35:11 +00:00
|
|
|
bool Updated()
|
|
|
|
{
|
|
|
|
return m_updatedCPUs;
|
|
|
|
}
|
2020-02-01 23:55:24 +00:00
|
|
|
|
2020-03-13 14:35:11 +00:00
|
|
|
bool UpdateCPUData();
|
|
|
|
bool UpdateCoreMhz();
|
|
|
|
bool UpdateCpuTemp();
|
2020-11-12 21:27:35 +00:00
|
|
|
bool UpdateCpuPower();
|
2023-01-24 02:06:50 +00:00
|
|
|
bool ReadcpuTempFile(int& temp);
|
2020-03-13 14:35:11 +00:00
|
|
|
bool GetCpuFile();
|
2020-11-12 21:27:35 +00:00
|
|
|
bool InitCpuPowerData();
|
2020-03-13 14:35:11 +00:00
|
|
|
double GetCPUPeriod() { return m_cpuPeriod; }
|
2020-02-01 23:55:24 +00:00
|
|
|
|
2020-03-13 14:35:11 +00:00
|
|
|
const std::vector<CPUData>& GetCPUData() const {
|
|
|
|
return m_cpuData;
|
|
|
|
}
|
|
|
|
const CPUData& GetCPUDataTotal() const {
|
|
|
|
return m_cpuDataTotal;
|
|
|
|
}
|
2022-03-13 14:51:41 +00:00
|
|
|
std::string cpu_type = "CPU";
|
2020-02-01 23:55:24 +00:00
|
|
|
private:
|
2020-03-13 14:35:11 +00:00
|
|
|
unsigned long long int m_boottime = 0;
|
|
|
|
std::vector<CPUData> m_cpuData;
|
|
|
|
CPUData m_cpuDataTotal {};
|
|
|
|
std::vector<int> m_coreMhz;
|
|
|
|
double m_cpuPeriod = 0;
|
|
|
|
bool m_updatedCPUs = false; // TODO use caching or just update?
|
|
|
|
bool m_inited = false;
|
2020-03-14 20:59:43 +00:00
|
|
|
FILE *m_cpuTempFile = nullptr;
|
2020-11-12 21:27:35 +00:00
|
|
|
std::unique_ptr<CPUPowerData> m_cpuPowerData;
|
2020-02-01 23:55:24 +00:00
|
|
|
};
|
|
|
|
|
2020-05-10 12:11:56 +00:00
|
|
|
extern CPUStats cpuStats;
|
2023-03-12 13:44:27 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
uint64_t FileTimeToInt64( const FILETIME& ft );
|
|
|
|
#endif
|
2020-07-06 17:31:40 +00:00
|
|
|
#endif //MANGOHUD_CPU_H
|