From cb5e3137da1366ce7f6ccff41a12fc7ce1934477 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Wed, 20 Nov 2024 17:09:22 +0100 Subject: [PATCH] include/fsm.h, src/fsm/*: reworked memory management of the logging system This commits reworks the way that memory is allocated for the logging system. We basically use a double-linked list, with the oldest and the latest elements stored in a static variable. Most recent log entries are added at the latest position in this structure. A new function to clear and free a log unit has been implemented, and can be used at many points. We take care that each log unit is freed at closing time. Tested with valgrind: ==6161== LEAK SUMMARY: ==6161== definitely lost: 5,008 bytes in 54 blocks ==6161== indirectly lost: 16,119 bytes in 672 blocks We can consider that as a basis for future work, as these leaks are due to external causes (e.g. libraries). Signed-off-by: Jean Sirmai Signed-off-by: Adrien Bourmault --- include/fsm.h | 52 ++++++++++++------------- src/fsm/log/appendix.c | 41 ++++++++++++-------- src/fsm/log/manager.c | 4 +- src/fsm/log/oper.c | 88 +++++++++++++++++++++++++----------------- 4 files changed, 104 insertions(+), 81 deletions(-) diff --git a/include/fsm.h b/include/fsm.h index bfd74e7..c7c5c44 100644 --- a/include/fsm.h +++ b/include/fsm.h @@ -178,19 +178,18 @@ enum fsm_enum_log_source { * A log unit must include the followings: * * @callergraph - * @see fsm_log_struct + * @see fsm_log_t */ -typedef struct fsm_log_unit_struct - { - long yy_dd_mm; /**< * date of the event reported in the log */ - long usec; /**< * with microseconds precision */ - char *file_source; /**< * emitter file */ - char *function_source; /**< * emitter function */ - char *string_value; /**< * any event descriptors */ - struct fsm_log_unit_struct *prev; /**< * chained list */ - struct fsm_log_unit_struct *next; /**< * chained list */ - } - fsm_log_unit_struct; +struct fsm_log_unit_t +{ + long yy_dd_mm; /**< * date of the event reported in the log */ + long usec; /**< * with microseconds precision */ + char *file_source; /**< * emitter file */ + char *function_source; /**< * emitter function */ + char *string_value; /**< * any event descriptors */ + struct fsm_log_unit_t *prev; /**< * chained list */ + struct fsm_log_unit_t *next; /**< * chained list */ +}; /** @@ -198,7 +197,7 @@ typedef struct fsm_log_unit_struct * are required to initialize and manage it. * * @callgraph - * @see fsm_log_struct_unit + * @see fsm_log_t_unit * * @callergraph * @see fsm_init_log() @@ -209,30 +208,31 @@ typedef struct fsm_log_unit_struct * @see fsm_seek_log() * @see fsm_remove_log() */ -typedef struct { - fsm_log_unit_struct *first; /**< * required */ - fsm_log_unit_struct *last; /**< * required */ - } - fsm_log_struct; +struct fsm_log_t +{ + struct fsm_log_unit_t *oldest; /**< * required */ + struct fsm_log_unit_t *latest; /**< * required */ +}; //----------------------------------------------------------------------------- -void fsm_init_log (fsm_log_struct *jj); -void fsm_publish_log (fsm_log_struct jj); -void fsm_clear_log (fsm_log_struct *jj); +void fsm_init_log (struct fsm_log_t *); +void fsm_publish_log (struct fsm_log_t *); +void fsm_clear_log (struct fsm_log_t *); +void fsm_clear_log_unit (struct fsm_log_unit_t *); -void fsm_add_log_event (fsm_log_struct *jj, +void fsm_add_log_event (struct fsm_log_t *gg_logs, char *file_source, char *function_source, char *string_value); -int fsm_get_log_length(fsm_log_struct jj); -void fsm_seek_log (fsm_log_struct jj, +int fsm_get_log_length(struct fsm_log_t *); +void fsm_seek_log (struct fsm_log_t *gg_logs, long usec, char *file_source, char *function_source, char *string_value); -long fsm_remove_log (fsm_log_struct *jj, +long fsm_remove_log (struct fsm_log_t *gg_logs, char *file_source, char *function_source, char *string_value); @@ -248,5 +248,3 @@ void fsm_relay_close_log(); void fsm_init (char *initial_message_from_main); void fsm_close (char *final_message_from_main); - - diff --git a/src/fsm/log/appendix.c b/src/fsm/log/appendix.c index 57032f8..16ae2ed 100644 --- a/src/fsm/log/appendix.c +++ b/src/fsm/log/appendix.c @@ -39,26 +39,35 @@ * * @since 2024-09 * - * @param *jj + * @param *gg_logs * @param *file_source * @param *function_source * @param *string_value * * @returns the removed event date microseconds */ -long fsm_remove_log (fsm_log_struct *jj, +long fsm_remove_log (struct fsm_log_t *gg_logs, char *file_source, char *function_source, char *string_value) { - long usec; - fsm_log_unit_struct *tmp = jj->last; - if (! tmp) return -1; - usec = tmp->usec; - jj->last = tmp->prev; - if (jj->last) jj->last->next = NULL; - else jj->first = NULL; - free (tmp); + // save latest log ref in tmp + struct fsm_log_unit_t *tmp = gg_logs->latest; + long usec = tmp->usec; + + // if there is no log, get out + if (! tmp) + return 0; + + // remove the log from history + gg_logs->latest = tmp->prev; + + // update gg_logs references if needed + if (gg_logs->latest) gg_logs->latest->next = NULL; + else gg_logs->latest = NULL; + + // free the unit memory + fsm_clear_log_unit(tmp); return usec; } @@ -69,12 +78,12 @@ long fsm_remove_log (fsm_log_struct *jj, * * @since 2024-09 * - * @param *jj + * @param *gg_logs * @returns log length */ -int fsm_get_log_length (fsm_log_struct jj) +int fsm_get_log_length (struct fsm_log_t *gg_logs) { - fsm_log_unit_struct *a_unit = jj.first; + struct fsm_log_unit_t *a_unit = gg_logs->oldest; int nb = 0; while (a_unit) { @@ -90,19 +99,19 @@ int fsm_get_log_length (fsm_log_struct jj) * * @since 2024-09 * - * @param *jj + * @param *gg_logs * @param usec * @param *file_source * @param *function_source * @param *string_value */ -void fsm_seek_log (fsm_log_struct jj, +void fsm_seek_log (struct fsm_log_t *gg_logs, long usec, char *file_source, char *function_source, char *string_value) { - fsm_log_unit_struct *a_unit = jj.first; + struct fsm_log_unit_t *a_unit = gg_logs->oldest; int nb = 0; while (a_unit) { diff --git a/src/fsm/log/manager.c b/src/fsm/log/manager.c index d865bc4..b2a8469 100644 --- a/src/fsm/log/manager.c +++ b/src/fsm/log/manager.c @@ -47,7 +47,7 @@ * * Limiting the access to gg_logs helps to avoid uncontrolled operations on it. */ -static fsm_log_struct gg_logs = { 0 }; +static struct fsm_log_t gg_logs = { 0 }; /** @@ -148,7 +148,7 @@ void fsm_relay_init_log() */ void fsm_relay_close_log() { - fsm_publish_log (gg_logs); + fsm_publish_log (&gg_logs); fsm_clear_log (&gg_logs); } diff --git a/src/fsm/log/oper.c b/src/fsm/log/oper.c index 288eeba..da7f3a9 100644 --- a/src/fsm/log/oper.c +++ b/src/fsm/log/oper.c @@ -54,12 +54,12 @@ static int string_3_size = 50; * * @since 2024-09 * - * @param *jj + * @param *gg_logs */ -void fsm_init_log (fsm_log_struct *jj) +void fsm_init_log (struct fsm_log_t *gg_logs) { - jj->first = NULL; - jj->last = NULL; + gg_logs->oldest = NULL; + gg_logs->latest = NULL; } @@ -68,22 +68,38 @@ void fsm_init_log (fsm_log_struct *jj) * * @since 2024-09 * - * @param *jj + * @param *gg_logs */ -void fsm_clear_log (fsm_log_struct *jj) +void fsm_clear_log (struct fsm_log_t *gg_logs) { - fsm_log_unit_struct *tmp; - fsm_log_unit_struct *a_unit = jj->first; + struct fsm_log_unit_t *tmp; + struct fsm_log_unit_t *a_unit = gg_logs->oldest; + while(a_unit) { tmp = a_unit; a_unit = a_unit->next; - free (tmp); + fsm_clear_log_unit(tmp); } - jj->first = NULL; - jj->last = NULL; + gg_logs->oldest = NULL; + gg_logs->latest = NULL; } +/** + * Deletes all the contents of a log unit and takes care + * of freeing it. + * + * @since 2024-11 + * + * @param *unit + */ +void fsm_clear_log_unit (struct fsm_log_unit_t *unit) +{ + g_free(unit->file_source); + g_free(unit->function_source); + g_free(unit->string_value); + g_free(unit); +} /** * Adds a log unit (an event) to the log list. @@ -94,12 +110,12 @@ void fsm_clear_log (fsm_log_struct *jj) * * @since 2024-09 * - * @param *jj + * @param *gg_logs * @param *file_source * @param *function_source * @param *string_value */ -void fsm_add_log_event (fsm_log_struct *jj, +void fsm_add_log_event (struct fsm_log_t *gg_logs, char *file_source, char *function_source, char *string_value) @@ -107,7 +123,7 @@ void fsm_add_log_event (fsm_log_struct *jj, struct timeval tv; gettimeofday (&tv, NULL); - fsm_log_unit_struct *new_unit = malloc (sizeof(fsm_log_unit_struct)); + struct fsm_log_unit_t *new_unit = g_malloc0 (sizeof(struct fsm_log_unit_t)); if (! new_unit) exit (EXIT_FAILURE); new_unit->yy_dd_mm = tv.tv_sec; @@ -121,18 +137,17 @@ void fsm_add_log_event (fsm_log_struct *jj, strncpy (new_unit->function_source, function_source, string_2_size - 1); strncpy (new_unit->string_value, string_value, string_3_size - 1); - new_unit->next = jj->first; - new_unit->prev = NULL; + // first time we log something (don't touch new_unit->prev) + if (gg_logs->oldest == NULL) + gg_logs->oldest = new_unit; // record the first reference (once) + // not the first time, just update + else { + new_unit->prev = gg_logs->latest; + new_unit->prev->next = new_unit; + } - if (jj->first) jj->first->prev = new_unit; - else jj->last = new_unit; - - jj->first = new_unit; - - free (new_unit->file_source); - free (new_unit->function_source); - free (new_unit->string_value); -// free (new_unit); + // in any case, update the latest reference + gg_logs->latest = new_unit; } @@ -146,29 +161,30 @@ void fsm_add_log_event (fsm_log_struct *jj, * * @since 2024-09 * - * @param *jj + * @param *gg_logs */ -void fsm_publish_log (fsm_log_struct jj) +void fsm_publish_log (struct fsm_log_t *gg_logs) { - fsm_log_unit_struct *a_unit = jj.last; - char buf [LOG_MAX_LENGTH] = { 0 }; + struct fsm_log_unit_t *a_unit = gg_logs->oldest; + char *timestamp = g_malloc0 (LOG_MAX_LENGTH * sizeof(char)); int nb = 0; while (a_unit) { - strftime(buf, sizeof(buf), "%D %T", localtime(&a_unit->yy_dd_mm)); -/* - g_message ("%s + %-6ld %6d %-28s %-32s %-40s", - buf, + strftime(timestamp, LOG_MAX_LENGTH * sizeof(char), + "%D %T", localtime(&a_unit->yy_dd_mm)); + + g_message ("%s + %-6ld %6d %-28s %-32s %-45s", + timestamp, a_unit->usec, nb, a_unit->file_source, a_unit->function_source, a_unit->string_value); -*/ - printf ("%d\n", nb); - a_unit = a_unit->prev; + + a_unit = a_unit->next; nb ++; } + g_free(timestamp); } /*