mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
kernel: add kernel_get_cpu_frequency()
This commit is contained in:
parent
c2679fa1a4
commit
21b9ae1675
2 changed files with 63 additions and 0 deletions
|
@ -90,6 +90,9 @@ int kernel_get_page_size();
|
|||
/** Get the size of a huge page in bytes. */
|
||||
int kernel_get_hugepage_size();
|
||||
|
||||
/** Get CPU base frequency */
|
||||
int kernel_get_cpu_frequency(uint64_t *freq);
|
||||
|
||||
/** Set SMP affinity of IRQ */
|
||||
int kernel_irq_setaffinity(unsigned irq, uintmax_t aff , uintmax_t *old );
|
||||
|
||||
|
|
|
@ -281,4 +281,64 @@ int kernel_irq_setaffinity(unsigned irq, uintmax_t aff, uintmax_t *old)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int kernel_get_cpu_frequency(uint64_t *freq)
|
||||
{
|
||||
char *line = NULL, *sep, *end;
|
||||
size_t len = 0;
|
||||
double dfreq;
|
||||
int ret;
|
||||
FILE *f;
|
||||
|
||||
/* Try to get CPU frequency from cpufreq module */
|
||||
f = fopen(SYSFS_PATH "/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
|
||||
if (!f)
|
||||
goto cpuinfo;
|
||||
|
||||
ret = fscanf(f, "%" PRIu64, freq);
|
||||
fclose(f);
|
||||
if (ret != 1)
|
||||
return -1;
|
||||
|
||||
/* cpufreq reports kHz */
|
||||
*freq = *freq * 1000;
|
||||
|
||||
return 0;
|
||||
|
||||
cpuinfo:
|
||||
/* Try to read CPU frequency from /proc/cpuinfo */
|
||||
f = fopen(PROCFS_PATH "/cpuinfo", "r");
|
||||
if (!f)
|
||||
return -1; /* We give up here */
|
||||
|
||||
ret = -1;
|
||||
while (getline(&line, &len, f) >= 0) {
|
||||
if (strstr(line, "cpu MHz") == line) {
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret)
|
||||
goto out;
|
||||
|
||||
sep = strchr(line, ':');
|
||||
if (!sep) {
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
dfreq = strtod(sep+1, &end);
|
||||
|
||||
if (end == sep+1) {
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Frequency is given in MHz */
|
||||
*freq = dfreq * 1e6;
|
||||
|
||||
out: fclose(f);
|
||||
free(line);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* __linux__ */
|
||||
|
|
Loading…
Add table
Reference in a new issue