Xinqi Bao's Git
projects
/
slstatus.git
/ blobdiff
summary
|
log
|
commit
|
diff
|
tree
raw
|
inline
| side by side
uptime: Improve typing and sort headers
[slstatus.git]
/
components
/
cpu.c
diff --git
a/components/cpu.c
b/components/cpu.c
index
b9b3924
..
cd5ebe4
100644
(file)
--- a/
components/cpu.c
+++ b/
components/cpu.c
@@
-1,25
+1,30
@@
/* See LICENSE file for copyright and license details. */
/* See LICENSE file for copyright and license details. */
-#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "../util.h"
#if defined(__linux__)
#include <stdio.h>
#include <string.h>
#include "../util.h"
#if defined(__linux__)
+ #include <inttypes.h>
+ #include <stdint.h>
+
const char *
cpu_freq(void)
{
const char *
cpu_freq(void)
{
- int freq;
+ uint64_t freq;
+
+ /* in kHz */
+ if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
+ "%"SCNu64, &freq) != 1) {
+ return NULL;
+ }
- return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
- "%i", &freq) == 1) ?
- bprintf("%d", (freq + 500) / 1000) : NULL;
+ return fmt_human(freq * 1000, 1000);
}
const char *
cpu_perc(void)
{
}
const char *
cpu_perc(void)
{
- static int valid;
static long double a[7];
long double b[7];
static long double a[7];
long double b[7];
@@
-29,8
+34,7
@@
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
return NULL;
}
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
return NULL;
}
- if (!valid) {
- valid = 1;
+ if (b[0] == 0) {
return NULL;
}
return NULL;
}
@@
-56,19
+60,19
@@
size = sizeof(freq);
size = sizeof(freq);
+ /* in MHz */
if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
warn("sysctl 'HW_CPUSPEED':");
return NULL;
}
if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
warn("sysctl 'HW_CPUSPEED':");
return NULL;
}
- return
bprintf("%d", freq
);
+ return
fmt_human((size_t)freq * 1000 * 1000, 1000
);
}
const char *
cpu_perc(void)
{
int mib[2];
}
const char *
cpu_perc(void)
{
int mib[2];
- static int valid;
static long int a[CPUSTATES];
long int b[CPUSTATES];
size_t size;
static long int a[CPUSTATES];
long int b[CPUSTATES];
size_t size;
@@
-83,8
+87,7
@@
warn("sysctl 'KERN_CPTIME':");
return NULL;
}
warn("sysctl 'KERN_CPTIME':");
return NULL;
}
- if (!valid) {
- valid = 1;
+ if (b[0] == 0) {
return NULL;
}
return NULL;
}