2021-11-24 16:16:25 +08:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2022-09-21 10:47:15 +08:00
|
|
|
#include "detection/cpuUsage/cpuUsage.h"
|
2022-10-07 16:52:52 +08:00
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
|
#include <synchapi.h>
|
|
|
|
|
#include <sysinfoapi.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#endif
|
2021-11-24 16:16:25 +08:00
|
|
|
|
|
|
|
|
#define FF_CPU_USAGE_MODULE_NAME "CPU Usage"
|
|
|
|
|
#define FF_CPU_USAGE_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
2022-10-07 16:52:52 +08:00
|
|
|
static inline uint64_t getTimeInMs()
|
|
|
|
|
{
|
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
|
return GetTickCount64();
|
|
|
|
|
#else
|
|
|
|
|
struct timeval timeNow;
|
|
|
|
|
gettimeofday(&timeNow, NULL);
|
|
|
|
|
return (uint64_t)((timeNow.tv_sec * 1000) + (timeNow.tv_usec / 1000));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void sleepInMs(uint32_t msec)
|
2022-09-27 22:06:53 +08:00
|
|
|
{
|
2022-10-07 16:52:52 +08:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
|
SleepEx(msec, TRUE);
|
|
|
|
|
#else
|
|
|
|
|
nanosleep(&(struct timespec){ msec / 1000, (msec % 1000) * 1000000 }, NULL);
|
|
|
|
|
#endif
|
2022-09-27 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 16:52:52 +08:00
|
|
|
static uint64_t inUseAll1, totalAll1, startTime;
|
2022-09-27 22:06:53 +08:00
|
|
|
|
|
|
|
|
void ffPrepareCPUUsage()
|
|
|
|
|
{
|
|
|
|
|
startTime = getTimeInMs();
|
|
|
|
|
ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 16:16:25 +08:00
|
|
|
void ffPrintCPUUsage(FFinstance* instance)
|
|
|
|
|
{
|
2022-09-27 22:06:53 +08:00
|
|
|
const char* error = NULL;
|
|
|
|
|
if(startTime == 0)
|
|
|
|
|
{
|
|
|
|
|
error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
|
|
|
|
if(error)
|
|
|
|
|
goto error;
|
2022-10-07 16:52:52 +08:00
|
|
|
sleepInMs(1000);
|
2022-09-27 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-07 16:52:52 +08:00
|
|
|
uint64_t duration = getTimeInMs() - startTime;
|
2022-09-27 22:06:53 +08:00
|
|
|
if(duration < 1000)
|
2022-10-07 16:52:52 +08:00
|
|
|
sleepInMs(1000 - (uint32_t) duration);
|
2022-09-27 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 16:52:52 +08:00
|
|
|
uint64_t inUseAll2, totalAll2;
|
2022-09-27 22:06:53 +08:00
|
|
|
error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2);
|
2022-09-21 10:47:15 +08:00
|
|
|
|
|
|
|
|
if(error)
|
2021-11-24 16:16:25 +08:00
|
|
|
{
|
2022-09-27 22:06:53 +08:00
|
|
|
error:
|
2022-09-21 10:47:15 +08:00
|
|
|
ffPrintError(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpu, "%s", error);
|
2021-11-24 16:16:25 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 22:06:53 +08:00
|
|
|
double cpuPercent = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
|
|
|
|
|
|
2022-06-17 18:15:36 +02:00
|
|
|
if(instance->config.cpuUsage.outputFormat.length == 0)
|
2021-11-24 16:16:25 +08:00
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
ffPrintLogoAndKey(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsage.key);
|
2021-11-24 16:16:25 +08:00
|
|
|
|
|
|
|
|
printf("%.2lf%%\n", cpuPercent);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
ffPrintFormat(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsage, FF_CPU_USAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2021-11-24 16:16:25 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpuPercent}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|