From 72aef455de0ac5447765c4443a38960a5eb6567e Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Tue, 15 May 2018 07:41:26 +0200 Subject: [PATCH] procfs: implement reading cpus count Change-Id: I8083f8bf5e7fb7484502ea8f9e710f9a553b9b72 --- src/procfs.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/procfs.c b/src/procfs.c index 95478a2..763c0d8 100644 --- a/src/procfs.c +++ b/src/procfs.c @@ -22,6 +22,7 @@ #define LOADAVG_FILEPATH "/proc/loadavg" #define UPTIME_FILEPATH "/proc/uptime" +#define POSSIBLE_CPUS_FILEPATH "/sys/devices/system/cpu/possible" int procfs_read_system_load_average(struct procfs_load_average_info *info) { @@ -101,5 +102,25 @@ int procfs_read_uptime(unsigned long *uptime) int procfs_read_cpu_count(int *cpu_count) { - return -1; + ON_NULL_RETURN_VAL(cpu_count, -1); + + int cpus, dummy; + + FILE *possible_fp = fopen(POSSIBLE_CPUS_FILEPATH, "r"); + if (!possible_fp) { + ERR("failed to open " POSSIBLE_CPUS_FILEPATH); + return -1; + } + + if (fscanf(possible_fp, "%d-%d", &dummy, &cpus) != 2) { + ERR("failed to read " POSSIBLE_CPUS_FILEPATH); + fclose(possible_fp); + return -1; + } + + *cpu_count = cpus + 1; + + fclose(possible_fp); + + return 0; } -- 2.7.4