diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/kpi/dlt-kpi-common.c | 48 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-common.h | 1 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-process.c | 26 |
3 files changed, 67 insertions, 8 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; +} diff --git a/src/kpi/dlt-kpi-common.h b/src/kpi/dlt-kpi-common.h index 4e220a8..4777d6f 100644 --- a/src/kpi/dlt-kpi-common.h +++ b/src/kpi/dlt-kpi-common.h @@ -36,5 +36,6 @@ DltReturnValue dlt_kpi_read_file(char* filename, char* buffer, uint maxLength); DltReturnValue dlt_kpi_read_file_compact(char *filename, char **target); +int dlt_kpi_get_cpu_count(); #endif /* SRC_KPI_DLT_KPI_COMMON_H_ */ diff --git a/src/kpi/dlt-kpi-process.c b/src/kpi/dlt-kpi-process.c index d126551..168adf7 100644 --- a/src/kpi/dlt-kpi-process.c +++ b/src/kpi/dlt-kpi-process.c @@ -43,9 +43,11 @@ DltReturnValue dlt_kpi_process_update_io_wait(DltKpiProcess *process, unsigned l unsigned long int total_io_wait = dlt_kpi_read_process_stat_to_ulong(process->pid, 42); + int cpu_count = dlt_kpi_get_cpu_count(); + process->io_wait = (total_io_wait - process->last_io_wait) * 1000 / sysconf(_SC_CLK_TCK); // busy milliseconds since last update - if(time_dif_ms > 0) - process->io_wait = process->io_wait * 1000 / time_dif_ms; // busy milliseconds per second + if(time_dif_ms > 0 && cpu_count > 0) + process->io_wait = process->io_wait * 1000 / time_dif_ms / cpu_count; // busy milliseconds per second per CPU process->last_io_wait = total_io_wait; @@ -65,9 +67,16 @@ DltReturnValue dlt_kpi_process_update_cpu_time(DltKpiProcess *process, unsigned unsigned long total_cpu_time = utime + stime; - process->cpu_time = (total_cpu_time - process->last_cpu_time) * 1000 / sysconf(_SC_CLK_TCK); // busy milliseconds since last update - if(time_dif_ms > 0) - process->cpu_time = process->cpu_time * 1000 / time_dif_ms; // busy milliseconds per second + if(process->last_cpu_time > 0 && process->last_cpu_time <= total_cpu_time) + { + int cpu_count = dlt_kpi_get_cpu_count(); + + process->cpu_time = (total_cpu_time - process->last_cpu_time) * 1000 / sysconf(_SC_CLK_TCK); // busy milliseconds since last update + if(time_dif_ms > 0 && cpu_count > 0) + process->cpu_time = process->cpu_time * 1000 / time_dif_ms / cpu_count; // busy milliseconds per second per CPU + } + else + process->cpu_time = 0; process->last_cpu_time = total_cpu_time; @@ -319,13 +328,14 @@ unsigned long int dlt_kpi_read_process_stat_to_ulong(pid_t pid, unsigned int ind } char *buffer = NULL; - DltReturnValue tmp = dlt_kpi_read_process_file_to_str(pid, &buffer, "stat"); - if(tmp < DLT_RETURN_OK) + if(dlt_kpi_read_process_file_to_str(pid, &buffer, "stat") < DLT_RETURN_OK) { + // fprintf(stderr, "dlt_kpi_read_process_stat_to_ulong(): Error while reading process stat file. Pid: %d. Requested index: %u\n", pid, index); // can happen if process closed shortly before + if(buffer != NULL) free(buffer); - return tmp; + return 0; } char *tok = strtok(buffer, " \t\n"); |