add cpu_freq function

This commit is contained in:
Aaron Marcher 2017-06-12 23:56:21 +02:00
parent 259e967cbf
commit 832b21ca4b
No known key found for this signature in database
GPG Key ID: 74B048E5C2474F9A
2 changed files with 19 additions and 0 deletions

View File

@ -11,6 +11,7 @@
- battery_power (battery power usage) [argument: battery name] - battery_power (battery power usage) [argument: battery name]
- battery_state (battery charging state) [argument: battery name] - battery_state (battery charging state) [argument: battery name]
- cpu_perc (cpu usage in percent) [argument: NULL] - cpu_perc (cpu usage in percent) [argument: NULL]
- cpu_freq (cpu frequency) [argument: NULL]
- datetime (date and time) [argument: format] - datetime (date and time) [argument: format]
- disk_free (free disk space in GB) [argument: mountpoint] - disk_free (free disk space in GB) [argument: mountpoint]
- disk_perc (disk usage in percent) [argument: mountpoint] - disk_perc (disk usage in percent) [argument: mountpoint]

View File

@ -37,6 +37,7 @@ static char *smprintf(const char *fmt, ...);
static char *battery_perc(const char *bat); static char *battery_perc(const char *bat);
static char *battery_power(const char *bat); static char *battery_power(const char *bat);
static char *battery_state(const char *bat); static char *battery_state(const char *bat);
static char *cpu_freq(void);
static char *cpu_perc(void); static char *cpu_perc(void);
static char *datetime(const char *fmt); static char *datetime(const char *fmt);
static char *disk_free(const char *mnt); static char *disk_free(const char *mnt);
@ -167,6 +168,23 @@ battery_state(const char *bat)
} }
} }
static char *
cpu_freq(void)
{
int freq;
FILE *fp;
fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
if (fp == NULL) {
warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
return smprintf("%s", UNKNOWN_STR);
}
fscanf(fp, "%i", &freq);
fclose(fp);
return smprintf("%d", (freq + 500) / 1000);
}
static char * static char *
cpu_perc(void) cpu_perc(void)
{ {