diff options
author | Christoph Lipka <clipka@de.adit-jv.com> | 2018-12-20 14:58:19 +0100 |
---|---|---|
committer | Christoph Lipka <clipka@de.adit-jv.com> | 2018-12-21 10:23:41 +0100 |
commit | dca8ab254aa0a687c32009079d85e4d8f960b213 (patch) | |
tree | e63b6a552fd9f61873892110a56a89ef354864cd /src/kpi | |
parent | 0d0c74640c8b792db37cb9f884f89f7561ea551f (diff) | |
download | DLT-daemon-dca8ab254aa0a687c32009079d85e4d8f960b213.tar.gz |
Code beautification using uncrustify
Signed-off-by: Christoph Lipka <clipka@de.adit-jv.com>
Diffstat (limited to 'src/kpi')
-rw-r--r-- | src/kpi/dlt-kpi-common.c | 59 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-common.h | 2 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-interrupt.c | 90 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-options.c | 124 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-process-list.c | 123 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-process-list.h | 4 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-process.c | 233 | ||||
-rw-r--r-- | src/kpi/dlt-kpi-process.h | 2 | ||||
-rw-r--r-- | src/kpi/dlt-kpi.c | 231 | ||||
-rw-r--r-- | src/kpi/dlt-kpi.h | 8 |
10 files changed, 408 insertions, 468 deletions
diff --git a/src/kpi/dlt-kpi-common.c b/src/kpi/dlt-kpi-common.c index fa131dc..53298b1 100644 --- a/src/kpi/dlt-kpi-common.c +++ b/src/kpi/dlt-kpi-common.c @@ -30,19 +30,18 @@ static int dlt_kpi_cpu_count = -1; DltReturnValue dlt_kpi_read_file_compact(char *filename, char **target) { - if(filename == NULL || target == NULL) - { - fprintf(stderr, "%s: Nullpointer parameter!\n",__func__); + if ((filename == NULL) || (target == NULL)) { + fprintf(stderr, "%s: Nullpointer parameter!\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - char buffer[BUFFER_SIZE]; + char buffer[BUFFER_SIZE]; int ret = dlt_kpi_read_file(filename, buffer, BUFFER_SIZE); - if(ret < DLT_RETURN_OK) + + if (ret < DLT_RETURN_OK) return ret; - if((*target = malloc(strlen(buffer) + 1)) == NULL) - { + if ((*target = malloc(strlen(buffer) + 1)) == NULL) { fprintf(stderr, "Out of memory!\n"); return DLT_RETURN_ERROR; } @@ -52,22 +51,20 @@ DltReturnValue dlt_kpi_read_file_compact(char *filename, char **target) return DLT_RETURN_OK; } -DltReturnValue dlt_kpi_read_file(char* filename, char* buffer, uint maxLength) +DltReturnValue dlt_kpi_read_file(char *filename, char *buffer, uint maxLength) { - if(filename == NULL || buffer == NULL) - { - fprintf(stderr, "%s: Nullpointer parameter!\n",__func__); + if ((filename == NULL) || (buffer == NULL)) { + fprintf(stderr, "%s: Nullpointer parameter!\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - FILE* file = fopen(filename, "r"); - if(file == NULL) - { - // fprintf(stderr, "Could not read file %s\n", filename); + FILE *file = fopen(filename, "r"); + + if (file == NULL) + /* fprintf(stderr, "Could not read file %s\n", filename); */ return DLT_RETURN_ERROR; - } - int buflen = fread(buffer, 1, maxLength-1, file); + int buflen = fread(buffer, 1, maxLength - 1, file); buffer[buflen] = '\0'; fclose(file); @@ -79,40 +76,38 @@ int dlt_kpi_read_cpu_count() { char buffer[BUFFER_SIZE]; int ret = dlt_kpi_read_file("/proc/cpuinfo", buffer, sizeof(buffer)); - if(ret != 0) - { + + 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) - { + 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) + + do { + if (strcmp(tok, "processor") == 0) num++; tok = strtok(NULL, delim); - } - while(tok != NULL); + } while (tok != NULL); return num; } int dlt_kpi_get_cpu_count() { - if(dlt_kpi_cpu_count <= 0) - { + if (dlt_kpi_cpu_count <= 0) { dlt_kpi_cpu_count = dlt_kpi_read_cpu_count(); - if(dlt_kpi_cpu_count <= 0) - { + + if (dlt_kpi_cpu_count <= 0) { fprintf(stderr, "Could not get CPU count\n"); dlt_kpi_cpu_count = -1; } diff --git a/src/kpi/dlt-kpi-common.h b/src/kpi/dlt-kpi-common.h index 4777d6f..db05b24 100644 --- a/src/kpi/dlt-kpi-common.h +++ b/src/kpi/dlt-kpi-common.h @@ -34,7 +34,7 @@ #define BUFFER_SIZE 4096 -DltReturnValue dlt_kpi_read_file(char* filename, char* buffer, uint maxLength); +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(); diff --git a/src/kpi/dlt-kpi-interrupt.c b/src/kpi/dlt-kpi-interrupt.c index b8e4e80..7274cc0 100644 --- a/src/kpi/dlt-kpi-interrupt.c +++ b/src/kpi/dlt-kpi-interrupt.c @@ -28,9 +28,8 @@ DltReturnValue dlt_kpi_log_interrupts(DltContext *ctx, DltLogLevelType log_level) { - if(ctx == NULL) - { - fprintf(stderr, "%s: Nullpointer parameter (NULL) !\n",__func__); + if (ctx == NULL) { + fprintf(stderr, "%s: Nullpointer parameter (NULL) !\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -42,52 +41,60 @@ DltReturnValue dlt_kpi_log_interrupts(DltContext *ctx, DltLogLevelType log_level int head_line = 1, first_row = 1, cpu_count = 0, column = 0, buffer_offset = 0; DltReturnValue ret; - if((ret = dlt_kpi_read_file("/proc/interrupts", file_buffer, BUFFER_SIZE)) < DLT_RETURN_OK) return ret; + if ((ret = dlt_kpi_read_file("/proc/interrupts", file_buffer, BUFFER_SIZE)) < DLT_RETURN_OK) return ret; token = strtok(file_buffer, delim); - while(token != NULL) - { - if(head_line) - { - if(strlen(token) > 3 && token[0]=='C' && token[1]=='P' && token[2]=='U') + + while (token != NULL) { + if (head_line) { + if ((strlen(token) > 3) && (token[0] == 'C') && (token[1] == 'P') && (token[2] == 'U')) { cpu_count++; - else if(cpu_count <= 0) + } + else if (cpu_count <= 0) { - fprintf(stderr, "%s: Could not parse CPU count !\n",__func__); + fprintf(stderr, "%s: Could not parse CPU count !\n", __func__); return DLT_RETURN_ERROR; } - else if(strcmp(token, "\n") == 0) + else if (strcmp(token, "\n") == 0) + { head_line = 0; + } token = strtok(NULL, delim); } - else - { + else { int tokenlen = strlen(token); - if(token[tokenlen - 1] == ':') - { + + if (token[tokenlen - 1] == ':') { column = 0; - if(first_row) + if (first_row) first_row = 0; else buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "\n"); } - if(column == 0) // IRQ number - { - buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "%.*s;", tokenlen-1, token); + if (column == 0) { /* IRQ number */ + buffer_offset += snprintf(buffer + buffer_offset, + BUFFER_SIZE - buffer_offset, + "%.*s;", + tokenlen - 1, + token); } - else if(column <= cpu_count) + else if (column <= cpu_count) { long int interrupt_count = strtol(token, &check, 10); - if(*check != '\0') - { - fprintf(stderr, "%s: Could not parse interrupt count for CPU !\n",__func__); + + if (*check != '\0') { + fprintf(stderr, "%s: Could not parse interrupt count for CPU !\n", __func__); return DLT_RETURN_ERROR; } - buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "cpu%d:%ld;", column - 1, interrupt_count); + buffer_offset += snprintf(buffer + buffer_offset, + BUFFER_SIZE - buffer_offset, + "cpu%d:%ld;", + column - 1, + interrupt_count); } column++; @@ -96,57 +103,52 @@ DltReturnValue dlt_kpi_log_interrupts(DltContext *ctx, DltLogLevelType log_level } } - // synchronization message + /* synchronization message */ DLT_LOG(*ctx, log_level, DLT_STRING("IRQ"), DLT_STRING("BEG")); DltContextData ctx_data; - if((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK) - { + + if ((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK) { fprintf(stderr, "%s: dlt_user_log_write_start() returned error\n", __func__); return ret; } - if((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK) - { + if ((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK) { fprintf(stderr, "%s: dlt_user_log_write_string() returned error\n", __func__); return ret; } token = strtok(buffer, "\n"); - while(token != NULL) - { - if(dlt_user_log_write_string(&ctx_data, token) < DLT_RETURN_OK) - { + + while (token != NULL) { + if (dlt_user_log_write_string(&ctx_data, token) < DLT_RETURN_OK) { /* message buffer full, start new one */ - if((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK) - { + if ((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK) { fprintf(stderr, "%s: dlt_user_log_write_finish() returned error\n", __func__); return ret; } - if((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK) - { + if ((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK) { fprintf(stderr, "%s: dlt_user_log_write_start() returned error\n", __func__); return ret; } - if((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK) - { + if ((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK) { fprintf(stderr, "%s: dlt_user_log_write_string() returned error\n", __func__); return ret; } } - else + else { token = strtok(NULL, "\n"); + } } - if((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK) - { + if ((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK) { fprintf(stderr, "%s: dlt_user_log_write_finish() returned error\n", __func__); return ret; } - // synchronization message + /* synchronization message */ DLT_LOG(*ctx, log_level, DLT_STRING("IRQ"), DLT_STRING("END")); return DLT_RETURN_OK; diff --git a/src/kpi/dlt-kpi-options.c b/src/kpi/dlt-kpi-options.c index 0f6938c..14f0681 100644 --- a/src/kpi/dlt-kpi-options.c +++ b/src/kpi/dlt-kpi-options.c @@ -32,13 +32,13 @@ void usage(char *prog_name) { char version[255]; - dlt_get_version(version,255); + dlt_get_version(version, 255); printf("Usage: %s [options]\n", prog_name); printf("Application to forward information from the /proc/ file system to DLT.\n"); printf("%s\n", version); printf("Options:\n"); - //printf(" -d Daemonize. Detach from terminal and run in background.\n"); + /*printf(" -d Daemonize. Detach from terminal and run in background.\n"); */ printf(" -c filename Use configuration file. \n"); printf(" Default: %s\n", DEFAULT_CONF_FILE); printf(" -h This help message.\n"); @@ -55,48 +55,46 @@ void dlt_kpi_init_cli_options(DltKpiOptions *options) void dlt_kpi_free_cli_options(DltKpiOptions *options) { - if(options->customConfigFile) + if (options->customConfigFile) free(options->configurationFileName); } DltReturnValue dlt_kpi_read_command_line(DltKpiOptions *options, int argc, char **argv) { - if(options == NULL) - { + if (options == NULL) { fprintf(stderr, "%s: Nullpointer parameter\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } + dlt_kpi_init_cli_options(options); int opt; - while((opt = getopt(argc, argv, "c:h")) != -1) - { - switch(opt) { - case 'c': - { - if((options->configurationFileName = malloc(strlen(optarg)+1)) == 0) - { - fprintf(stderr, "Out of memory!\n"); - return DLT_RETURN_ERROR; - } - strcpy(options->configurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ - options->customConfigFile = 1; - break; - } - case 'h': - { - usage(argv[0]); - exit(0); - return -1;//for parasoft - } - default: - { - fprintf(stderr, "Unknown option: %c\n", optopt); - usage(argv[0]); + while ((opt = getopt(argc, argv, "c:h")) != -1) + switch (opt) { + case 'c': + { + if ((options->configurationFileName = malloc(strlen(optarg) + 1)) == 0) { + fprintf(stderr, "Out of memory!\n"); return DLT_RETURN_ERROR; } + + strcpy(options->configurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + options->customConfigFile = 1; + break; + } + case 'h': + { + usage(argv[0]); + exit(0); + return -1; /*for parasoft */ + } + default: + { + fprintf(stderr, "Unknown option: %c\n", optopt); + usage(argv[0]); + return DLT_RETURN_ERROR; + } } - } return DLT_RETURN_OK; } @@ -123,8 +121,7 @@ DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_ char *pch, *strchk; int tmp; - if(config == NULL || file_name == NULL) - { + if ((config == NULL) || (file_name == NULL)) { fprintf(stderr, "%s: Nullpointer parameter!\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -133,17 +130,14 @@ DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_ file = fopen(file_name, "r"); - if(file == NULL) - { + if (file == NULL) { fprintf(stderr, "%s: Could not open configuration file!\n", __func__); return DLT_RETURN_ERROR; } - if(((line = malloc(COMMAND_LINE_SIZE)) == 0) || - ((token = malloc(COMMAND_LINE_SIZE)) == 0) || - ((value = malloc(COMMAND_LINE_SIZE)) == 0)) - - { + if (((line = malloc(COMMAND_LINE_SIZE)) == 0) || + ((token = malloc(COMMAND_LINE_SIZE)) == 0) || + ((value = malloc(COMMAND_LINE_SIZE)) == 0)) { fclose(file); free(line); free(token); @@ -153,66 +147,61 @@ DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_ return DLT_RETURN_ERROR; } - while(fgets(line, COMMAND_LINE_SIZE, file) != NULL) - { + while (fgets(line, COMMAND_LINE_SIZE, file) != NULL) { token[0] = '\0'; value[0] = '\0'; pch = strtok (line, " =\r\n"); - while(pch != NULL) - { - if(pch[0] == '#') + + while (pch != NULL) { + if (pch[0] == '#') break; - if(token[0] == '\0') - { - strncpy(token, pch, COMMAND_LINE_SIZE-1); - token[COMMAND_LINE_SIZE-1] = '\0'; + if (token[0] == '\0') { + strncpy(token, pch, COMMAND_LINE_SIZE - 1); + token[COMMAND_LINE_SIZE - 1] = '\0'; } - else - { - strncpy(value, pch, COMMAND_LINE_SIZE-1); - value[COMMAND_LINE_SIZE-1] = '\0'; + else { + strncpy(value, pch, COMMAND_LINE_SIZE - 1); + value[COMMAND_LINE_SIZE - 1] = '\0'; break; } pch = strtok(NULL, " =\r\n"); } - if(token[0] != '\0' && value[0] != '\0') - { - if(strcmp(token, "process_interval") == '\0') - { + if ((token[0] != '\0') && (value[0] != '\0')) { + if (strcmp(token, "process_interval") == '\0') { tmp = strtol(value, &strchk, 10); - if(strchk[0] == '\0' && tmp > 0) + if ((strchk[0] == '\0') && (tmp > 0)) config->process_log_interval = tmp; else fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token); } - else if(strcmp(token, "irq_interval") == '\0') + else if (strcmp(token, "irq_interval") == '\0') { tmp = strtol(value, &strchk, 10); - if(strchk[0] == '\0' && tmp > 0) + if ((strchk[0] == '\0') && (tmp > 0)) config->irq_log_interval = tmp; else fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token); } - else if(strcmp(token, "check_interval") == '\0') + else if (strcmp(token, "check_interval") == '\0') { tmp = strtol(value, &strchk, 10); - if(strchk[0] == '\0' && tmp > 0) + if ((strchk[0] == '\0') && (tmp > 0)) config->check_log_interval = tmp; else fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token); } - else if(strcmp(token, "log_level") == '\0') + else if (strcmp(token, "log_level") == '\0') { tmp = strtol(value, &strchk, 10); - if(strchk[0] == '\0' && tmp >= -1 && tmp <= 6) + if ((strchk[0] == '\0') && (tmp >= -1) && (tmp <= 6)) config->log_level = tmp; else fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token); @@ -234,20 +223,17 @@ DltReturnValue dlt_kpi_init(int argc, char **argv, DltKpiConfig *config) DltReturnValue ret; - if(config == NULL) - { + if (config == NULL) { fprintf(stderr, "%s: Invalid Parameter!", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if((ret = dlt_kpi_read_command_line(&options, argc, argv)) < DLT_RETURN_OK) - { + if ((ret = dlt_kpi_read_command_line(&options, argc, argv)) < DLT_RETURN_OK) { fprintf(stderr, "Failed to read command line!"); return ret; } - if((ret = dlt_kpi_read_configuration_file(config, options.configurationFileName)) < DLT_RETURN_OK) - { + if ((ret = dlt_kpi_read_configuration_file(config, options.configurationFileName)) < DLT_RETURN_OK) { fprintf(stderr, "Failed to read configuration file!"); return ret; } diff --git a/src/kpi/dlt-kpi-process-list.c b/src/kpi/dlt-kpi-process-list.c index 2d77c91..66d149e 100644 --- a/src/kpi/dlt-kpi-process-list.c +++ b/src/kpi/dlt-kpi-process-list.c @@ -29,8 +29,8 @@ DltKpiProcessList *dlt_kpi_create_process_list() { DltKpiProcessList *new_list = malloc(sizeof(DltKpiProcessList)); - if(new_list == NULL) - { + + if (new_list == NULL) { fprintf(stderr, "%s: Cannot create process list, out of memory\n", __func__); return NULL; } @@ -43,9 +43,8 @@ DltKpiProcessList *dlt_kpi_create_process_list() DltReturnValue dlt_kpi_free_process_list_soft(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -56,17 +55,16 @@ DltReturnValue dlt_kpi_free_process_list_soft(DltKpiProcessList *list) DltReturnValue dlt_kpi_free_process_list(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } DltKpiProcess *tmp; list->cursor = list->start; - while(list->cursor != NULL) - { + + while (list->cursor != NULL) { tmp = list->cursor->next; dlt_kpi_free_process(list->cursor); list->cursor = tmp; @@ -77,9 +75,8 @@ DltReturnValue dlt_kpi_free_process_list(DltKpiProcessList *list) DltKpiProcess *dlt_kpi_get_process_at_cursor(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return NULL; } @@ -88,9 +85,8 @@ DltKpiProcess *dlt_kpi_get_process_at_cursor(DltKpiProcessList *list) DltReturnValue dlt_kpi_reset_cursor(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -100,17 +96,17 @@ DltReturnValue dlt_kpi_reset_cursor(DltKpiProcessList *list) DltReturnValue dlt_kpi_set_cursor_at_end(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } list->cursor = list->start; - if(list->cursor == NULL) + + if (list->cursor == NULL) return DLT_RETURN_OK; - while(list->cursor->next != NULL) + while (list->cursor->next != NULL) dlt_kpi_increment_cursor(list); return DLT_RETURN_OK; @@ -118,13 +114,12 @@ DltReturnValue dlt_kpi_set_cursor_at_end(DltKpiProcessList *list) DltReturnValue dlt_kpi_increment_cursor(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(list->cursor == NULL) + if (list->cursor == NULL) return DLT_RETURN_ERROR; list->cursor = list->cursor->next; @@ -134,13 +129,12 @@ DltReturnValue dlt_kpi_increment_cursor(DltKpiProcessList *list) DltReturnValue dlt_kpi_decrement_cursor(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(list->cursor == NULL) + if (list->cursor == NULL) return DLT_RETURN_ERROR; list->cursor = list->cursor->prev; @@ -150,13 +144,12 @@ DltReturnValue dlt_kpi_decrement_cursor(DltKpiProcessList *list) DltReturnValue dlt_kpi_add_process_at_start(DltKpiProcessList *list, DltKpiProcess *process) { - if(list == NULL || process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((list == NULL) || (process == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(list->start != NULL) + if (list->start != NULL) list->start->prev = process; process->next = list->start; @@ -167,19 +160,17 @@ DltReturnValue dlt_kpi_add_process_at_start(DltKpiProcessList *list, DltKpiProce DltReturnValue dlt_kpi_add_process_before_cursor(DltKpiProcessList *list, DltKpiProcess *process) { - if(list == NULL || process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((list == NULL) || (process == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(list->start == NULL) // Empty list? - { + if (list->start == NULL) { /* Empty list? */ DltReturnValue ret = dlt_kpi_add_process_at_start(list, process); list->cursor = NULL; return ret; } - else if(list->cursor == NULL) + else if (list->cursor == NULL) { dlt_kpi_set_cursor_at_end(list); DltReturnValue ret = dlt_kpi_add_process_after_cursor(list, process); @@ -187,7 +178,7 @@ DltReturnValue dlt_kpi_add_process_before_cursor(DltKpiProcessList *list, DltKpi return ret; } - if(list->cursor->prev != NULL) + if (list->cursor->prev != NULL) list->cursor->prev->next = process; else list->start = process; @@ -201,16 +192,15 @@ DltReturnValue dlt_kpi_add_process_before_cursor(DltKpiProcessList *list, DltKpi DltReturnValue dlt_kpi_add_process_after_cursor(DltKpiProcessList *list, DltKpiProcess *process) { - if(list == NULL || process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((list == NULL) || (process == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_ERROR; } - if(list->cursor == NULL) + if (list->cursor == NULL) return dlt_kpi_add_process_at_start(list, process); - if(list->cursor->next != NULL) + if (list->cursor->next != NULL) list->cursor->next->prev = process; process->next = list->cursor->next; @@ -222,63 +212,58 @@ DltReturnValue dlt_kpi_add_process_after_cursor(DltKpiProcessList *list, DltKpiP DltReturnValue dlt_kpi_remove_process_at_cursor_soft(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(list->cursor == NULL) - { - fprintf(stderr, "%s: Cursor is Invalid (NULL)\n", __func__); + if (list->cursor == NULL) { + fprintf(stderr, "%s: Cursor is Invalid (NULL)\n", __func__); return DLT_RETURN_ERROR; } DltKpiProcess *tmp = list->cursor; - if(tmp->prev != NULL) - { - if(tmp->next != NULL) - { + if (tmp->prev != NULL) { + if (tmp->next != NULL) { tmp->prev->next = tmp->next; tmp->next->prev = tmp->prev; } - else + else { tmp->prev->next = NULL; + } } - else - { - if(tmp->next != NULL) - { + else { + if (tmp->next != NULL) { tmp->next->prev = NULL; list->start = tmp->next; } - else + else { list->start = NULL; + } } - list->cursor = tmp->next; // becomes NULL if list is at end + list->cursor = tmp->next; /* becomes NULL if list is at end */ return DLT_RETURN_OK; } DltReturnValue dlt_kpi_remove_process_at_cursor(DltKpiProcessList *list) { - if(list == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(list->cursor == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (list->cursor == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_ERROR; } DltKpiProcess *tmp = list->cursor; DltReturnValue ret = dlt_kpi_remove_process_at_cursor_soft(list); - if(ret < DLT_RETURN_OK) + + if (ret < DLT_RETURN_OK) return ret; dlt_kpi_free_process(tmp); diff --git a/src/kpi/dlt-kpi-process-list.h b/src/kpi/dlt-kpi-process-list.h index 52eb11f..2fc80b6 100644 --- a/src/kpi/dlt-kpi-process-list.h +++ b/src/kpi/dlt-kpi-process-list.h @@ -48,7 +48,7 @@ DltReturnValue dlt_kpi_add_process_after_cursor(DltKpiProcessList *list, DltKpiP DltReturnValue dlt_kpi_remove_process_at_cursor_soft(DltKpiProcessList *list); DltReturnValue dlt_kpi_remove_process_at_cursor(DltKpiProcessList *list); -// DltReturnValue dlt_kpi_remove_process_after_cursor(DltKpiProcessList *list); -// DltReturnValue dlt_kpi_remove_first_process(DltKpiProcessList *list); +/* DltReturnValue dlt_kpi_remove_process_after_cursor(DltKpiProcessList *list); */ +/* DltReturnValue dlt_kpi_remove_first_process(DltKpiProcessList *list); */ #endif /* SRC_KPI_DLT_KPI_PROCESS_LIST_H_ */ diff --git a/src/kpi/dlt-kpi-process.c b/src/kpi/dlt-kpi-process.c index 22a46e6..062a667 100644 --- a/src/kpi/dlt-kpi-process.c +++ b/src/kpi/dlt-kpi-process.c @@ -35,8 +35,7 @@ DltReturnValue dlt_kpi_read_process_stat_cmdline(pid_t pid, char **buffer); DltReturnValue dlt_kpi_process_update_io_wait(DltKpiProcess *process, unsigned long int time_dif_ms) { - if(process == NULL) - { + if (process == NULL) { fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -45,9 +44,10 @@ DltReturnValue dlt_kpi_process_update_io_wait(DltKpiProcess *process, unsigned l 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 && cpu_count > 0) - process->io_wait = process->io_wait * 1000 / time_dif_ms / cpu_count; // busy milliseconds per second per CPU + 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) && (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; @@ -56,8 +56,7 @@ DltReturnValue dlt_kpi_process_update_io_wait(DltKpiProcess *process, unsigned l DltReturnValue dlt_kpi_process_update_cpu_time(DltKpiProcess *process, unsigned long int time_dif_ms) { - if(process == NULL) - { + if (process == NULL) { fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -67,16 +66,18 @@ DltReturnValue dlt_kpi_process_update_cpu_time(DltKpiProcess *process, unsigned unsigned long total_cpu_time = utime + stime; - if(process->last_cpu_time > 0 && process->last_cpu_time <= total_cpu_time) - { + 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 + 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 + else { process->cpu_time = 0; + } process->last_cpu_time = total_cpu_time; @@ -85,9 +86,8 @@ DltReturnValue dlt_kpi_process_update_cpu_time(DltKpiProcess *process, unsigned DltReturnValue dlt_kpi_process_update_rss(DltKpiProcess *process) { - if(process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (process == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -98,9 +98,8 @@ DltReturnValue dlt_kpi_process_update_rss(DltKpiProcess *process) DltReturnValue dlt_kpi_process_update_ctx_switches(DltKpiProcess *process) { - if(process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (process == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -109,22 +108,21 @@ DltReturnValue dlt_kpi_process_update_ctx_switches(DltKpiProcess *process) last_tok = NULL; DltReturnValue ret; - if((ret = dlt_kpi_read_process_file_to_str(process->pid, &buffer, "status")) < DLT_RETURN_OK) return ret; + + if ((ret = dlt_kpi_read_process_file_to_str(process->pid, &buffer, "status")) < DLT_RETURN_OK) return ret; process->ctx_switches = 0; tok = strtok(buffer, delim); - while(tok != NULL) - { - if(last_tok != NULL) - { - if(strcmp(last_tok, "voluntary_ctxt_switches") == 0 || strcmp(last_tok, "nonvoluntary_ctxt_switches") == 0) - { + + while (tok != NULL) { + if (last_tok != NULL) { + if ((strcmp(last_tok, + "voluntary_ctxt_switches") == 0) || (strcmp(last_tok, "nonvoluntary_ctxt_switches") == 0)) { char *chk; process->ctx_switches += strtol(tok, &chk, 10); - if(*chk != '\0') - { + if (*chk != '\0') { fprintf(stderr, "Could not parse ctx_switches info from /proc/%d/status", process->pid); free(buffer); return DLT_RETURN_ERROR; @@ -143,9 +141,8 @@ DltReturnValue dlt_kpi_process_update_ctx_switches(DltKpiProcess *process) DltReturnValue dlt_kpi_process_update_io_bytes(DltKpiProcess *process) { - if(process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (process == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -154,23 +151,21 @@ DltReturnValue dlt_kpi_process_update_io_bytes(DltKpiProcess *process) last_tok = NULL; DltReturnValue ret; - if((ret = dlt_kpi_read_process_file_to_str(process->pid, &buffer, "io")) < DLT_RETURN_OK) + + if ((ret = dlt_kpi_read_process_file_to_str(process->pid, &buffer, "io")) < DLT_RETURN_OK) return ret; process->io_bytes = 0; tok = strtok(buffer, delim); - while(tok != NULL) - { - if(last_tok != NULL) - { - if(strcmp(last_tok, "rchar") == 0 || strcmp(last_tok, "wchar") == 0) - { + + while (tok != NULL) { + if (last_tok != NULL) { + if ((strcmp(last_tok, "rchar") == 0) || (strcmp(last_tok, "wchar") == 0)) { char *chk; process->io_bytes += strtoul(tok, &chk, 10); - if(*chk != '\0') - { + if (*chk != '\0') { fprintf(stderr, "Could not parse io_bytes info from /proc/%d/io", process->pid); free(buffer); return DLT_RETURN_ERROR; @@ -190,9 +185,8 @@ DltReturnValue dlt_kpi_process_update_io_bytes(DltKpiProcess *process) DltReturnValue dlt_kpi_update_process(DltKpiProcess *process, unsigned long int time_dif_ms) { - if(process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (process == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -208,9 +202,9 @@ DltReturnValue dlt_kpi_update_process(DltKpiProcess *process, unsigned long int DltKpiProcess *dlt_kpi_create_process(int pid) { DltKpiProcess *new_process = malloc(sizeof(DltKpiProcess)); - if(new_process == NULL) - { - fprintf(stderr, "%s: Out of Memory \n", __func__); + + if (new_process == NULL) { + fprintf(stderr, "%s: Out of Memory \n", __func__); return NULL; } @@ -220,9 +214,9 @@ DltKpiProcess *dlt_kpi_create_process(int pid) new_process->ppid = (pid_t)dlt_kpi_read_process_stat_to_ulong(pid, 4); dlt_kpi_read_process_file_to_str(pid, &(new_process->command_line), "cmdline"); - if(new_process->command_line != NULL) - if(strlen(new_process->command_line) == 0) - { + + if (new_process->command_line != NULL) + if (strlen(new_process->command_line) == 0) { free(new_process->command_line); dlt_kpi_read_process_stat_cmdline(pid, &(new_process->command_line)); } @@ -234,35 +228,35 @@ DltKpiProcess *dlt_kpi_create_process(int pid) DltKpiProcess *dlt_kpi_clone_process(DltKpiProcess *original) { - if(original == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (original == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return NULL; } - // DltKpiProcess *new_process = dlt_kpi_create_process(original->pid); + /* DltKpiProcess *new_process = dlt_kpi_create_process(original->pid); */ DltKpiProcess *new_process = malloc(sizeof(DltKpiProcess)); - if(new_process == NULL) - { - fprintf(stderr, "%s: Out of Memory\n", __func__); + + if (new_process == NULL) { + fprintf(stderr, "%s: Out of Memory\n", __func__); return NULL; } memcpy(new_process, original, sizeof(DltKpiProcess)); - if(original->command_line != NULL) - { + if (original->command_line != NULL) { new_process->command_line = malloc(strlen(original->command_line) + 1); - if(new_process->command_line == NULL) - { + + if (new_process->command_line == NULL) { fprintf(stderr, "%s: Out of Memory\n", __func__); free(new_process); return NULL; } + strncpy(new_process->command_line, original->command_line, strlen(original->command_line) + 1); } - else + else { new_process->command_line = NULL; + } new_process->next = new_process->prev = NULL; @@ -271,13 +265,12 @@ DltKpiProcess *dlt_kpi_clone_process(DltKpiProcess *original) DltReturnValue dlt_kpi_free_process(DltKpiProcess *process) { - if(process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (process == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(process->command_line != NULL) + if (process->command_line != NULL) free(process->command_line); free(process); @@ -287,9 +280,8 @@ DltReturnValue dlt_kpi_free_process(DltKpiProcess *process) DltReturnValue dlt_kpi_print_process(DltKpiProcess *process) { - if(process == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (process == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -307,23 +299,20 @@ DltReturnValue dlt_kpi_print_process(DltKpiProcess *process) DltReturnValue dlt_kpi_read_process_file_to_str(pid_t pid, char **target_str, char *subdir) { - if(target_str == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (target_str == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_ERROR; } *target_str = NULL; - if(pid <= 0) - { - fprintf(stderr, "%s: Invalid Parameter (PID)\n", __func__); + if (pid <= 0) { + fprintf(stderr, "%s: Invalid Parameter (PID)\n", __func__); return DLT_RETURN_ERROR; } - if(subdir == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (subdir == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_ERROR; } @@ -335,18 +324,17 @@ DltReturnValue dlt_kpi_read_process_file_to_str(pid_t pid, char **target_str, ch unsigned long int dlt_kpi_read_process_stat_to_ulong(pid_t pid, unsigned int index) { - if(pid <= 0) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (pid <= 0) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return 0; } char *buffer = NULL; - 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) + 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 0; @@ -355,31 +343,30 @@ unsigned long int dlt_kpi_read_process_stat_to_ulong(pid_t pid, unsigned int ind char *tok = strtok(buffer, " \t\n"); unsigned int i = 1, found = 0; - while(tok != NULL) - { - if(i == index) - { + while (tok != NULL) { + if (i == index) { found = 1; break; } + i++; tok = strtok(NULL, " \t\n"); } unsigned long int ret = 0; - if(found) - { + if (found) { char *check = NULL; ret = strtoul(tok, &check, 10); - if(*check != '\0') - { + + if (*check != '\0') { fprintf(stderr, "dlt_kpi_read_process_stat_to_ulong(): Could not extract token\n"); ret = 0; } } - else + else { fprintf(stderr, "dlt_kpi_read_process_stat_to_ulong(): Index not found\n"); + } free(buffer); @@ -388,23 +375,21 @@ unsigned long int dlt_kpi_read_process_stat_to_ulong(pid_t pid, unsigned int ind DltReturnValue dlt_kpi_read_process_stat_cmdline(pid_t pid, char **buffer) { - if(pid <= 0) - { - fprintf(stderr, "%s: Invalid Parameter (PID)\n", __func__); + if (pid <= 0) { + fprintf(stderr, "%s: Invalid Parameter (PID)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - if(buffer == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if (buffer == NULL) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } char *tmp_buffer = NULL; DltReturnValue tmp = dlt_kpi_read_process_file_to_str(pid, &tmp_buffer, "stat"); - if(tmp < DLT_RETURN_OK) - { - if(tmp_buffer != NULL) + + if (tmp < DLT_RETURN_OK) { + if (tmp_buffer != NULL) free(tmp_buffer); return tmp; @@ -413,23 +398,19 @@ DltReturnValue dlt_kpi_read_process_stat_cmdline(pid_t pid, char **buffer) char *tok = strtok(tmp_buffer, " \t\n"); unsigned int i = 1; - while(tok != NULL) - { - if(i == 2) - { + while (tok != NULL) { + if (i == 2) break; - } + i++; tok = strtok(NULL, " \t\n"); } - if(i == 2) - { + if (i == 2) { (*buffer) = malloc(strlen(tok) + 1); strncpy(*buffer, tok, strlen(tok) + 1); } - else - { + else { fprintf(stderr, "dlt_kpi_read_process_stat_cmdline(): cmdline entry not found\n"); return DLT_RETURN_ERROR; } @@ -441,22 +422,28 @@ DltReturnValue dlt_kpi_read_process_stat_cmdline(pid_t pid, char **buffer) DltReturnValue dlt_kpi_get_msg_process_update(DltKpiProcess *process, char *buffer, int maxlen) { - if(process == NULL || buffer == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((process == NULL) || (buffer == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } - snprintf(buffer, maxlen, "%d;%lu;%ld;%ld;%lu;%lu", process->pid, process->cpu_time, process->rss, process->ctx_switches, process->io_bytes, process->io_wait); + snprintf(buffer, + maxlen, + "%d;%lu;%ld;%ld;%lu;%lu", + process->pid, + process->cpu_time, + process->rss, + process->ctx_switches, + process->io_bytes, + process->io_wait); return DLT_RETURN_OK; } DltReturnValue dlt_kpi_get_msg_process_new(DltKpiProcess *process, char *buffer, int maxlen) { - if(process == NULL || buffer == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((process == NULL) || (buffer == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -467,9 +454,8 @@ DltReturnValue dlt_kpi_get_msg_process_new(DltKpiProcess *process, char *buffer, DltReturnValue dlt_kpi_get_msg_process_stop(DltKpiProcess *process, char *buffer, int maxlen) { - if(process == NULL || buffer == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((process == NULL) || (buffer == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } @@ -480,9 +466,8 @@ DltReturnValue dlt_kpi_get_msg_process_stop(DltKpiProcess *process, char *buffer DltReturnValue dlt_kpi_get_msg_process_commandline(DltKpiProcess *process, char *buffer, int maxlen) { - if(process == NULL || buffer == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((process == NULL) || (buffer == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } diff --git a/src/kpi/dlt-kpi-process.h b/src/kpi/dlt-kpi-process.h index 50d5fbc..6ad1b4e 100644 --- a/src/kpi/dlt-kpi-process.h +++ b/src/kpi/dlt-kpi-process.h @@ -31,7 +31,7 @@ #include <stdlib.h> #include "dlt-kpi-common.h" -typedef struct DltKpiEventWatch DltKpiEventWatch; // forward declaration +typedef struct DltKpiEventWatch DltKpiEventWatch; /* forward declaration */ typedef struct DltKpiProcess { diff --git a/src/kpi/dlt-kpi.c b/src/kpi/dlt-kpi.c index 8ba019e..0fbff3a 100644 --- a/src/kpi/dlt-kpi.c +++ b/src/kpi/dlt-kpi.c @@ -69,22 +69,19 @@ int main(int argc, char **argv) { printf("Launching dlt-kpi...\n"); - if(dlt_kpi_init(argc, argv, &config) < DLT_RETURN_OK) - { + if (dlt_kpi_init(argc, argv, &config) < DLT_RETURN_OK) { fprintf(stderr, "Initialization error!\n"); return -1; } dlt_kpi_init_sigterm_handler(); - if(dlt_kpi_init_process_lists() < DLT_RETURN_OK) - { + if (dlt_kpi_init_process_lists() < DLT_RETURN_OK) { fprintf(stderr, "Error occurred initializing process lists\n"); return -1; } - if(pthread_mutex_init(&process_list_mutex, NULL) < 0) - { + if (pthread_mutex_init(&process_list_mutex, NULL) < 0) { fprintf(stderr, "Error occurred initializing mutex\n"); return -1; } @@ -96,18 +93,17 @@ int main(int argc, char **argv) pthread_t irq_thread; pthread_t check_thread; - if(pthread_create(&process_thread, NULL, &dlt_kpi_start_process_thread, NULL) != 0) - { + if (pthread_create(&process_thread, NULL, &dlt_kpi_start_process_thread, NULL) != 0) { fprintf(stderr, "Could not create thread\n"); return -1; } - if(pthread_create(&irq_thread, NULL, &dlt_kpi_start_irq_thread, NULL) != 0) - { + + if (pthread_create(&irq_thread, NULL, &dlt_kpi_start_irq_thread, NULL) != 0) { fprintf(stderr, "Could not create thread\n"); return -1; } - if(pthread_create(&check_thread, NULL, &dlt_kpi_start_check_thread, NULL) != 0) - { + + if (pthread_create(&check_thread, NULL, &dlt_kpi_start_check_thread, NULL) != 0) { fprintf(stderr, "Could not create thread\n"); return -1; } @@ -124,7 +120,7 @@ int main(int argc, char **argv) dlt_kpi_free_process_lists(); printf("Done.\n"); - + return 0; } @@ -139,7 +135,7 @@ void dlt_kpi_init_sigterm_handler() void dlt_kpi_stop_loops(int sig) { - if(sig > -1) + if (sig > -1) fprintf(stderr, "dlt-kpi is now terminating due to signal %d...\n", sig); else fprintf(stderr, "dlt-kpi is now terminating due to an error...\n"); @@ -149,10 +145,13 @@ void dlt_kpi_stop_loops(int sig) DltReturnValue dlt_kpi_init_process_lists() { - if((list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; - if((new_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; - if((stopped_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; - if((update_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; + if ((list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; + + if ((new_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; + + if ((stopped_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; + + if ((update_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR; return DLT_RETURN_OK; } @@ -161,16 +160,16 @@ DltReturnValue dlt_kpi_free_process_lists() { DltReturnValue ret = DLT_RETURN_OK; - if(dlt_kpi_free_process_list(list) < DLT_RETURN_OK) + if (dlt_kpi_free_process_list(list) < DLT_RETURN_OK) ret = DLT_RETURN_ERROR; - if(dlt_kpi_free_process_list(new_process_list) < DLT_RETURN_OK) + if (dlt_kpi_free_process_list(new_process_list) < DLT_RETURN_OK) ret = DLT_RETURN_ERROR; - if(dlt_kpi_free_process_list(stopped_process_list) < DLT_RETURN_OK) + if (dlt_kpi_free_process_list(stopped_process_list) < DLT_RETURN_OK) ret = DLT_RETURN_ERROR; - if(dlt_kpi_free_process_list(update_process_list) < DLT_RETURN_OK) + if (dlt_kpi_free_process_list(update_process_list) < DLT_RETURN_OK) ret = DLT_RETURN_ERROR; return ret; @@ -178,7 +177,7 @@ DltReturnValue dlt_kpi_free_process_lists() void *dlt_kpi_start_process_thread() { - if(dlt_kpi_process_loop() < DLT_RETURN_OK) + if (dlt_kpi_process_loop() < DLT_RETURN_OK) dlt_kpi_stop_loops(-1); return NULL; @@ -190,15 +189,14 @@ DltReturnValue dlt_kpi_process_loop() old_millis = get_millis(); - while(!stop_loop) - { + while (!stop_loop) { /*DltReturnValue ret = */ dlt_kpi_update_process_list(list, config.process_log_interval); - //if(ret < DLT_RETURN_OK) - // return ret; + /*if(ret < DLT_RETURN_OK) */ + /* return ret; */ dif_millis = get_millis() - old_millis; - if(dif_millis >= (unsigned long)(config.process_log_interval)) + if (dif_millis >= (unsigned long)(config.process_log_interval)) sleep_millis = 0; else sleep_millis = config.process_log_interval - dif_millis; @@ -211,19 +209,22 @@ DltReturnValue dlt_kpi_process_loop() return DLT_RETURN_OK; } -DltReturnValue dlt_kpi_log_list(DltKpiProcessList *list, DltReturnValue(*process_callback)(DltKpiProcess*, char*, int), char *title, int delete_elements) +DltReturnValue dlt_kpi_log_list(DltKpiProcessList *list, + DltReturnValue (*process_callback)(DltKpiProcess *, char *, int), + char *title, + int delete_elements) { - if(list == NULL || process_callback == NULL || title == NULL) - { - fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); + if ((list == NULL) || (process_callback == NULL) || (title == NULL)) { + fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__); return DLT_RETURN_WRONG_PARAMETER; } dlt_kpi_reset_cursor(list); - if(list->cursor == NULL) - return DLT_RETURN_OK; // list empty; nothing to do - // Synchronization message + if (list->cursor == NULL) + return DLT_RETURN_OK; /* list empty; nothing to do */ + + /* Synchronization message */ DLT_LOG(kpi_ctx, config.log_level, DLT_STRING(title), DLT_STRING("BEG")); DltReturnValue ret; @@ -232,63 +233,53 @@ DltReturnValue dlt_kpi_log_list(DltKpiProcessList *list, DltReturnValue(*process char buffer[BUFFER_SIZE]; buffer[0] = '\0'; - if((ret = dlt_user_log_write_start(&kpi_ctx, &data, config.log_level)) < DLT_RETURN_OK) - { - fprintf(stderr, "%s: dlt_user_log_write_start() returned error.\n", __func__); + if ((ret = dlt_user_log_write_start(&kpi_ctx, &data, config.log_level)) < DLT_RETURN_OK) { + fprintf(stderr, "%s: dlt_user_log_write_start() returned error.\n", __func__); return ret; } - if((ret = dlt_user_log_write_string(&data, title)) < DLT_RETURN_OK) - { - fprintf(stderr, "%s: dlt_user_log_write_string() returned error.\n", __func__); + if ((ret = dlt_user_log_write_string(&data, title)) < DLT_RETURN_OK) { + fprintf(stderr, "%s: dlt_user_log_write_string() returned error.\n", __func__); return ret; } - do - { - if((ret = (*process_callback)(list->cursor, buffer, sizeof(buffer) - 1)) < DLT_RETURN_OK) + do { + if ((ret = (*process_callback)(list->cursor, buffer, sizeof(buffer) - 1)) < DLT_RETURN_OK) return ret; - if((ret = dlt_user_log_write_string(&data, buffer)) < DLT_RETURN_OK) - { + if ((ret = dlt_user_log_write_string(&data, buffer)) < DLT_RETURN_OK) { /* Log buffer full => Write log and start new one*/ - if((ret = dlt_user_log_write_finish(&data)) < DLT_RETURN_OK) - { - fprintf(stderr, "%s: dlt_user_log_write_finish() returned error.\n",__func__); + if ((ret = dlt_user_log_write_finish(&data)) < DLT_RETURN_OK) { + fprintf(stderr, "%s: dlt_user_log_write_finish() returned error.\n", __func__); return ret; } - if((ret = dlt_user_log_write_start(&kpi_ctx, &data, config.log_level)) < DLT_RETURN_OK) - { - fprintf(stderr, "%s: dlt_user_log_write_start() returned error.\n",__func__); + if ((ret = dlt_user_log_write_start(&kpi_ctx, &data, config.log_level)) < DLT_RETURN_OK) { + fprintf(stderr, "%s: dlt_user_log_write_start() returned error.\n", __func__); return ret; } - if((ret = dlt_user_log_write_string(&data, title)) < DLT_RETURN_OK) - { - fprintf(stderr, "%s: dlt_user_log_write_string() returned error.\n",__func__); + if ((ret = dlt_user_log_write_string(&data, title)) < DLT_RETURN_OK) { + fprintf(stderr, "%s: dlt_user_log_write_string() returned error.\n", __func__); return ret; } } - else if(delete_elements) + else if (delete_elements) { - if((ret = dlt_kpi_remove_process_at_cursor(list)) < DLT_RETURN_OK) + if ((ret = dlt_kpi_remove_process_at_cursor(list)) < DLT_RETURN_OK) return ret; } - else - { + else { list->cursor = list->cursor->next; } - } - while(list->cursor != NULL); + } while (list->cursor != NULL); - if((ret = dlt_user_log_write_finish(&data)) < DLT_RETURN_OK) - { - fprintf(stderr, "%s: dlt_user_log_write_finish() returned error.\n",__func__); + if ((ret = dlt_user_log_write_finish(&data)) < DLT_RETURN_OK) { + fprintf(stderr, "%s: dlt_user_log_write_finish() returned error.\n", __func__); return ret; } - // Synchronization message + /* Synchronization message */ DLT_LOG(kpi_ctx, config.log_level, DLT_STRING(title), DLT_STRING("END")); return DLT_RETURN_OK; @@ -301,15 +292,14 @@ DltReturnValue dlt_kpi_update_process_list(DltKpiProcessList *list, unsigned lon static struct dirent *current_dir; static pid_t current_dir_pid; - if(list == NULL) - { + if (list == NULL) { fprintf(stderr, "dlt_kpi_update_process_list(): Nullpointer parameter"); return DLT_RETURN_WRONG_PARAMETER; } DIR *proc_dir = opendir("/proc"); - if(proc_dir == NULL) - { + + if (proc_dir == NULL) { dlt_log(LOG_ERR, "Could not open /proc/ !\n"); return DLT_RETURN_ERROR; } @@ -319,21 +309,19 @@ DltReturnValue dlt_kpi_update_process_list(DltKpiProcessList *list, unsigned lon int debug_process_count = 0; - if(pthread_mutex_lock(&process_list_mutex) < 0) - { + if (pthread_mutex_lock(&process_list_mutex) < 0) { fprintf(stderr, "Can't lock mutex\n"); return DLT_RETURN_ERROR; } - while(1) - { - if(current_dir == NULL) - { + while (1) { + if (current_dir == NULL) { /* no more active processes.. delete all remaining processes in the list */ - if(list->cursor != NULL) - while(list->cursor != NULL) - { - if((tmp_ret = dlt_kpi_add_process_after_cursor(stopped_process_list, dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) + if (list->cursor != NULL) + while (list->cursor != NULL) { + if ((tmp_ret = + dlt_kpi_add_process_after_cursor(stopped_process_list, + dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) return tmp_ret; dlt_kpi_remove_process_at_cursor(list); @@ -343,80 +331,83 @@ DltReturnValue dlt_kpi_update_process_list(DltKpiProcessList *list, unsigned lon } current_dir_pid = strtol(current_dir->d_name, &strchk, 10); - if(*strchk != '\0' || current_dir_pid <= 0) - { + + if ((*strchk != '\0') || (current_dir_pid <= 0)) { /* no valid PID */ - current_dir = readdir(proc_dir); // next process in proc-fs + current_dir = readdir(proc_dir); /* next process in proc-fs */ continue; } /* compare the /proc/-filesystem with our process-list */ - if(list->cursor == NULL || current_dir_pid < list->cursor->pid) // New Process - { + if ((list->cursor == NULL) || (current_dir_pid < list->cursor->pid)) { /* New Process */ DltKpiProcess *new_process = dlt_kpi_create_process(current_dir_pid); - if(new_process == NULL) - { + + if (new_process == NULL) { fprintf(stderr, "Error: Could not create process (out of memory?)\n"); return DLT_RETURN_ERROR; } - if((tmp_ret = dlt_kpi_add_process_before_cursor(list, new_process)) < DLT_RETURN_OK) + if ((tmp_ret = dlt_kpi_add_process_before_cursor(list, new_process)) < DLT_RETURN_OK) return tmp_ret; - if((tmp_ret = dlt_kpi_add_process_before_cursor(new_process_list, dlt_kpi_clone_process(new_process))) < DLT_RETURN_OK) + if ((tmp_ret = + dlt_kpi_add_process_before_cursor(new_process_list, + dlt_kpi_clone_process(new_process))) < DLT_RETURN_OK) return tmp_ret; - current_dir = readdir(proc_dir); // next process in proc-fs + current_dir = readdir(proc_dir); /* next process in proc-fs */ debug_process_count++; } - else if(current_dir_pid > list->cursor->pid) // Process ended + else if (current_dir_pid > list->cursor->pid) /* Process ended */ { - if((tmp_ret = dlt_kpi_add_process_after_cursor(stopped_process_list, dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) + if ((tmp_ret = + dlt_kpi_add_process_after_cursor(stopped_process_list, + dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) return tmp_ret; - if((tmp_ret = dlt_kpi_remove_process_at_cursor(list)) < DLT_RETURN_OK) + if ((tmp_ret = dlt_kpi_remove_process_at_cursor(list)) < DLT_RETURN_OK) return tmp_ret; } - else if(current_dir_pid == list->cursor->pid) // Staying process + else if (current_dir_pid == list->cursor->pid) /* Staying process */ { /* update data */ - if((tmp_ret = dlt_kpi_update_process(list->cursor, time_dif_ms)) < DLT_RETURN_OK) - return tmp_ret; + if ((tmp_ret = dlt_kpi_update_process(list->cursor, time_dif_ms)) < DLT_RETURN_OK) + return tmp_ret; - if(list->cursor->cpu_time > 0) // only log active processes - if((tmp_ret = dlt_kpi_add_process_after_cursor(update_process_list, dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) - { + if (list->cursor->cpu_time > 0) /* only log active processes */ + if ((tmp_ret = + dlt_kpi_add_process_after_cursor(update_process_list, + dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) { fprintf(stderr, "dlt_kpi_update_process_list: Can't add process to list updateProcessList\n"); return tmp_ret; } - if((tmp_ret = dlt_kpi_increment_cursor(list)) < DLT_RETURN_OK) // next process in list + if ((tmp_ret = dlt_kpi_increment_cursor(list)) < DLT_RETURN_OK) /* next process in list */ return tmp_ret; - current_dir = readdir(proc_dir); // next process in proc-fs + current_dir = readdir(proc_dir); /* next process in proc-fs */ debug_process_count++; } } - if(pthread_mutex_unlock(&process_list_mutex) < 0) - { + if (pthread_mutex_unlock(&process_list_mutex) < 0) { fprintf(stderr, "Can't unlock mutex\n"); return DLT_RETURN_ERROR; } /* Log new processes */ - if((tmp_ret = dlt_kpi_log_list(new_process_list, &dlt_kpi_get_msg_process_new, "NEW", 1)) < DLT_RETURN_OK) + if ((tmp_ret = dlt_kpi_log_list(new_process_list, &dlt_kpi_get_msg_process_new, "NEW", 1)) < DLT_RETURN_OK) return tmp_ret; /* Log stopped processes */ - if((tmp_ret = dlt_kpi_log_list(stopped_process_list, &dlt_kpi_get_msg_process_stop, "STP", 1)) < DLT_RETURN_OK) + if ((tmp_ret = dlt_kpi_log_list(stopped_process_list, &dlt_kpi_get_msg_process_stop, "STP", 1)) < DLT_RETURN_OK) return tmp_ret; /* Log active processes */ - if((tmp_ret = dlt_kpi_log_list(update_process_list, &dlt_kpi_get_msg_process_update, "ACT", 1)) < DLT_RETURN_OK) + if ((tmp_ret = dlt_kpi_log_list(update_process_list, &dlt_kpi_get_msg_process_update, "ACT", 1)) < DLT_RETURN_OK) return tmp_ret; - if(closedir(proc_dir) < 0) + if (closedir(proc_dir) < 0) fprintf(stderr, "Could not close /proc/ directory\n"); return DLT_RETURN_OK; @@ -424,7 +415,7 @@ DltReturnValue dlt_kpi_update_process_list(DltKpiProcessList *list, unsigned lon void *dlt_kpi_start_irq_thread() { - if(dlt_kpi_irq_loop() < DLT_RETURN_OK) + if (dlt_kpi_irq_loop() < DLT_RETURN_OK) dlt_kpi_stop_loops(-1); return NULL; @@ -436,15 +427,14 @@ DltReturnValue dlt_kpi_irq_loop() old_millis = get_millis(); - while(!stop_loop) - { + while (!stop_loop) { /*DltReturnValue ret = */ dlt_kpi_log_interrupts(&kpi_ctx, config.log_level); - //if(ret < DLT_RETURN_OK) - // return ret; + /*if(ret < DLT_RETURN_OK) */ + /* return ret; */ dif_millis = get_millis() - old_millis; - if(dif_millis >= (unsigned long)(config.irq_log_interval)) + if (dif_millis >= (unsigned long)(config.irq_log_interval)) sleep_millis = 0; else sleep_millis = config.irq_log_interval - dif_millis; @@ -459,7 +449,7 @@ DltReturnValue dlt_kpi_irq_loop() void *dlt_kpi_start_check_thread() { - if(dlt_kpi_check_loop() < DLT_RETURN_OK) + if (dlt_kpi_check_loop() < DLT_RETURN_OK) dlt_kpi_stop_loops(-1); return NULL; @@ -471,15 +461,14 @@ DltReturnValue dlt_kpi_check_loop() old_millis = get_millis(); - while(!stop_loop) - { + while (!stop_loop) { /*DltReturnValue ret = */ dlt_kpi_log_check_commandlines(); - //if(ret < DLT_RETURN_OK) - // return ret; + /*if(ret < DLT_RETURN_OK) */ + /* return ret; */ dif_millis = get_millis() - old_millis; - if(dif_millis >= (unsigned long)(config.check_log_interval)) + if (dif_millis >= (unsigned long)(config.check_log_interval)) sleep_millis = 0; else sleep_millis = config.check_log_interval - dif_millis; @@ -494,16 +483,14 @@ DltReturnValue dlt_kpi_check_loop() DltReturnValue dlt_kpi_log_check_commandlines() { - if(pthread_mutex_lock(&process_list_mutex) < 0) - { + if (pthread_mutex_lock(&process_list_mutex) < 0) { fprintf(stderr, "Can't lock mutex\n"); return DLT_RETURN_ERROR; } DltReturnValue ret = dlt_kpi_log_list(list, dlt_kpi_get_msg_process_commandline, "CHK", 0); - if(pthread_mutex_unlock(&process_list_mutex) < 0) - { + if (pthread_mutex_unlock(&process_list_mutex) < 0) { fprintf(stderr, "Can't unlock mutex\n"); return DLT_RETURN_ERROR; } diff --git a/src/kpi/dlt-kpi.h b/src/kpi/dlt-kpi.h index bb5b48c..ff647d4 100644 --- a/src/kpi/dlt-kpi.h +++ b/src/kpi/dlt-kpi.h @@ -35,12 +35,12 @@ #include "dlt-kpi-process.h" #include "dlt-kpi-process-list.h" -// CONSTANT DEFINITIONS -#define DEFAULT_CONF_FILE ( CONFIGURATION_FILES_DIR "/dlt-kpi.conf") +/* CONSTANT DEFINITIONS */ +#define DEFAULT_CONF_FILE (CONFIGURATION_FILES_DIR "/dlt-kpi.conf") #define COMMAND_LINE_SIZE 1024 -// STRUCTURES +/* STRUCTURES */ typedef struct { char *configurationFileName; @@ -53,7 +53,7 @@ typedef struct DltLogLevelType log_level; } DltKpiConfig; -// FUNCTION DECLARATIONS: +/* FUNCTION DECLARATIONS: */ DltReturnValue dlt_kpi_read_command_line(DltKpiOptions *options, int argc, char **argv); DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_name); void dlt_kpi_free_cli_options(DltKpiOptions *options); |