diff options
Diffstat (limited to 'src/kpi/dlt-kpi-common.c')
-rw-r--r-- | src/kpi/dlt-kpi-common.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/kpi/dlt-kpi-common.c b/src/kpi/dlt-kpi-common.c index c0c7767..26c4e28 100644 --- a/src/kpi/dlt-kpi-common.c +++ b/src/kpi/dlt-kpi-common.c @@ -26,6 +26,8 @@ #include "dlt-kpi-common.h" +static int dlt_kpi_cpu_count = -1; + DltReturnValue dlt_kpi_read_file_compact(char *filename, char **target) { char buffer[BUFFER_SIZE]; @@ -66,3 +68,49 @@ DltReturnValue dlt_kpi_read_file(char* filename, char* buffer, uint maxLength) return DLT_RETURN_OK; } + +int dlt_kpi_read_cpu_count() +{ + char buffer[BUFFER_SIZE]; + int ret = dlt_kpi_read_file("/proc/cpuinfo", buffer, sizeof(buffer)); + if(ret != 0) + { + fprintf(stderr, "dlt_kpi_get_cpu_count(): Could not read /proc/cpuinfo\n"); + return -1; + } + + char* delim = "[] \t\n"; + char* tok = strtok(buffer, delim); + if(tok == NULL) + { + fprintf(stderr, "dlt_kpi_get_cpu_count(): Could not extract token\n"); + return -1; + } + + int num = 0; + do + { + if(strcmp(tok, "processor") == 0) + num++; + + tok = strtok(NULL, delim); + } + while(tok != NULL); + + return num; +} + +int dlt_kpi_get_cpu_count() +{ + if(dlt_kpi_cpu_count <= 0) + { + dlt_kpi_cpu_count = dlt_kpi_read_cpu_count(); + if(dlt_kpi_cpu_count <= 0) + { + fprintf(stderr, "Could not get CPU count\n"); + dlt_kpi_cpu_count = -1; + } + } + + return dlt_kpi_cpu_count; +} |