Uptime: detect boot time

This commit is contained in:
李通洲
2023-09-08 11:08:29 +08:00
committed by 李通洲
parent a093ca2001
commit cde2ba2c18
6 changed files with 70 additions and 27 deletions
+7 -1
View File
@@ -5,6 +5,12 @@
#include "fastfetch.h"
const char* ffDetectUptime(uint64_t* result);
typedef struct FFUptimeResult
{
uint64_t bootTime;
uint64_t uptime;
} FFUptimeResult;
const char* ffDetectUptime(FFUptimeResult* result);
#endif
+20 -10
View File
@@ -4,18 +4,28 @@
#include <sys/sysctl.h>
#include <sys/time.h>
const char* ffDetectUptime(uint64_t* result)
const char* ffDetectUptime(FFUptimeResult* result)
{
struct timeval bootTime;
size_t bootTimeSize = sizeof(bootTime);
if(sysctl(
(int[]) {CTL_KERN, KERN_BOOTTIME}, 2,
&bootTime, &bootTimeSize,
NULL, 0
) != 0)
return "sysctl({CTL_KERN, KERN_BOOTTIME}) failed";
{
struct timeval bootTime;
size_t bootTimeSize = sizeof(bootTime);
if(sysctl(
(int[]) {CTL_KERN, KERN_BOOTTIME}, 2,
&bootTime, &bootTimeSize,
NULL, 0
) != 0)
return "sysctl({CTL_KERN, KERN_BOOTTIME}) failed";
*result = (uint64_t) difftime(time(NULL), bootTime.tv_sec);
result->bootTime = (uint64_t) bootTime.tv_sec * 1000 + (uint64_t) bootTime.tv_usec / 1000;
}
{
struct timespec realTime;
if(clock_gettime(CLOCK_REALTIME, &realTime) != 0)
return "clock_gettime(CLOCK_REALTIME) failed";
uint64_t now = (uint64_t) realTime.tv_sec * 1000 + (uint64_t) realTime.tv_nsec / 1000000;
result->uptime = now - result->bootTime;
}
return NULL;
}
+14 -6
View File
@@ -1,12 +1,20 @@
#include "uptime.h"
#include <sys/sysinfo.h>
#include <time.h>
const char* ffDetectUptime(uint64_t* result)
const char* ffDetectUptime(FFUptimeResult* result)
{
struct sysinfo info;
if(sysinfo(&info) != 0)
return "sysinfo() failed";
*result = (uint64_t) info.uptime;
struct timespec uptime;
if (clock_gettime(CLOCK_BOOTTIME, &uptime) != 0)
return "clock_gettime(CLOCK_BOOTTIME) failed";
result->uptime = (uint64_t) uptime.tv_sec * 1000 + (uint64_t) uptime.tv_nsec / 1000000;
struct timespec realTime;
if(clock_gettime(CLOCK_REALTIME, &realTime) != 0)
return "clock_gettime(CLOCK_REALTIME) failed";
result->bootTime = (uint64_t) realTime.tv_sec * 1000 + (uint64_t) realTime.tv_nsec / 1000000 + result->uptime;
return NULL;
}
+11 -2
View File
@@ -2,8 +2,17 @@
#include <sysinfoapi.h>
const char* ffDetectUptime(uint64_t* result)
static inline uint64_t to_ms(uint64_t ret)
{
*result = GetTickCount64() / 1000;
ret -= 116444736000000000ull;
return ret / 10000ull;
}
const char* ffDetectUptime(FFUptimeResult* result)
{
result->uptime = GetTickCount64();
FILETIME fileTime;
GetSystemTimeAsFileTime(&fileTime);
result->bootTime = to_ms(*(uint64_t*) &fileTime) - result->uptime;
return NULL;
}
+1 -1
View File
@@ -4,7 +4,7 @@
#include <windows.h>
#include <wtsapi32.h>
static uint64_t to_ms(uint64_t ret)
static inline uint64_t to_ms(uint64_t ret)
{
ret -= 116444736000000000ull;
return ret / 10000ull;
+17 -7
View File
@@ -8,9 +8,9 @@
void ffPrintUptime(FFUptimeOptions* options)
{
uint64_t uptime;
FFUptimeResult result;
const char* error = ffDetectUptime(&uptime);
const char* error = ffDetectUptime(&result);
if(error)
{
@@ -18,10 +18,17 @@ void ffPrintUptime(FFUptimeOptions* options)
return;
}
uint32_t days = (uint32_t) uptime / 86400;
uint32_t hours = (uint32_t) (uptime - (days * 86400)) / 3600;
uint32_t minutes = (uint32_t) (uptime - (days * 86400) - (hours * 3600)) / 60;
uint32_t seconds = (uint32_t) uptime - (days * 86400) - (hours * 3600) - (minutes * 60);
uint64_t uptime = result.uptime;
uint32_t milliseconds = (uint32_t) (uptime % 1000);
uptime /= 1000;
uint32_t seconds = (uint32_t) (uptime % 60);
uptime /= 60;
uint32_t minutes = (uint32_t) (uptime % 60);
uptime /= 60;
uint32_t hours = (uint32_t) (uptime % 24);
uptime /= 24;
uint32_t days = (uint32_t) uptime;
if(options->moduleArgs.outputFormat.length == 0)
{
@@ -74,7 +81,10 @@ void ffPrintUptime(FFUptimeOptions* options)
{FF_FORMAT_ARG_TYPE_UINT, &days},
{FF_FORMAT_ARG_TYPE_UINT, &hours},
{FF_FORMAT_ARG_TYPE_UINT, &minutes},
{FF_FORMAT_ARG_TYPE_UINT, &seconds}
{FF_FORMAT_ARG_TYPE_UINT, &seconds},
{FF_FORMAT_ARG_TYPE_UINT, &milliseconds},
{FF_FORMAT_ARG_TYPE_UINT64, &result.uptime},
{FF_FORMAT_ARG_TYPE_UINT64, &result.bootTime},
});
}
}