diff options
| -rw-r--r-- | src/lib/core/include/utils.h | 2 | ||||
| -rw-r--r-- | src/lib/core/multiprocessing.c | 17 | ||||
| -rw-r--r-- | src/lib/core/utils.c | 13 | ||||
| -rw-r--r-- | tests/test_multiprocessing.c | 5 |
4 files changed, 21 insertions, 16 deletions
diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index 2476d4e..ea98faf 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -464,7 +464,7 @@ int is_git_sha(char const *hash); int check_python_package_dependencies(const char *srcdir); -char *seconds_to_human_readable(int v); +void seconds_to_human_readable(int v, char *result, size_t maxlen); #define STR_TO_TIMEOUT_NEGATIVE (-1) #define STR_TO_TIMEOUT_INVALID_TIME_SCALE (-2) diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c index f39f74b..298484a 100644 --- a/src/lib/core/multiprocessing.c +++ b/src/lib/core/multiprocessing.c @@ -244,7 +244,9 @@ void mp_pool_show_summary(struct MultiProcessingPool *pool) { strcpy(status_str, "FAIL"); } - printf("%-4s %10d %10s %-10s\n", status_str, task->parent_pid, seconds_to_human_readable(task->time_data.duration), task->ident) ; + char duration[255] = {0}; + seconds_to_human_readable(task->time_data.duration, duration, sizeof(duration)); + printf("%-4s %10d %10s %-10s\n", status_str, task->parent_pid, duration, task->ident) ; //printf("%-4s %10d %7lds %-10s\n", status_str, task->parent_pid, task->elapsed, task->ident) ; } puts(""); @@ -329,6 +331,7 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { } for (size_t i = lower_i; i < upper_i; i++) { + char duration[255] = {0}; struct MultiProcessingTask *slot = &pool->task[i]; if (slot->status == MP_POOL_TASK_STATUS_INITIAL) { slot->_startup = time(NULL); @@ -365,7 +368,8 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { if (slot->timeout) { task_timed_out = slot->time_data.duration >= (double) slot->timeout; if (task_timed_out && pid == 0 && slot->pid != 0) { - printf("%s Task timed out after %s (pid: %d)\n", progress, seconds_to_human_readable(slot->timeout), slot->pid); + seconds_to_human_readable(slot->timeout, duration, sizeof(duration)); + printf("%s Task timed out after %s (pid: %d)\n", progress, duration, slot->pid); if (kill(slot->pid, SIGKILL) == 0) { status = SIGKILL; } else { @@ -419,7 +423,8 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { semaphore_wait(&pool->semaphore); update_task_elapsed(slot); semaphore_post(&pool->semaphore); - fprintf(stderr, "%s Task failed after %s\n", progress, seconds_to_human_readable(slot->time_data.duration)); + seconds_to_human_readable(slot->time_data.duration, duration, sizeof(duration)); + fprintf(stderr, "%s Task failed after %s\n", progress, duration); failures++; if (flags & MP_POOL_FAIL_FAST && pool->num_used > 1) { @@ -427,7 +432,8 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { return -2; } } else { - printf("%s Task finished after %s\n", progress, seconds_to_human_readable(slot->time_data.duration)); + seconds_to_human_readable(slot->time_data.duration, duration, sizeof(duration)); + printf("%s Task finished after %s\n", progress, duration); } // Clean up logs and scripts left behind by the task @@ -454,8 +460,9 @@ int mp_pool_join(struct MultiProcessingPool *pool, size_t jobs, size_t flags) { slot->interval_data.duration = 0.0; } if (slot->interval_data.duration == 0.0) { + seconds_to_human_readable(slot->time_data.duration, duration, sizeof(duration)); printf("[%s:%s] Task is running (pid: %d, elapsed: %s)\n", - pool->ident, slot->ident, slot->parent_pid, seconds_to_human_readable(slot->time_data.duration)); + pool->ident, slot->ident, slot->parent_pid, duration); update_task_interval_start(slot); } diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index 0871787..a8b1c73 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -1104,21 +1104,18 @@ int str_to_timeout(char *s) { return value; } -char *seconds_to_human_readable(const int v) { - static char result[255] = {0}; +void seconds_to_human_readable(const int v, char *result, const size_t maxlen) { const int hours = v / 3600; const int minutes = (v % 3600) / 60; const int seconds = v % 60; - memset(result, '\0', sizeof(result)); + memset(result, '\0', maxlen); if (hours) { - snprintf(result + strlen(result), sizeof(result), "%dh ", hours); + snprintf(result + strlen(result), maxlen, "%dh ", hours); } if (hours || minutes) { - snprintf(result + strlen(result), sizeof(result), "%dm ", minutes); + snprintf(result + strlen(result), maxlen, "%dm ", minutes); } - snprintf(result + strlen(result), sizeof(result), "%ds", seconds); - - return result; + snprintf(result + strlen(result), maxlen, "%ds", seconds); } diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c index 4a68688..1db8716 100644 --- a/tests/test_multiprocessing.c +++ b/tests/test_multiprocessing.c @@ -185,7 +185,7 @@ static void test_mp_timeout() { } static void test_mp_seconds_to_human_readable() { - struct testcase { + const struct testcase { int seconds; const char *expected; } tc[] = { @@ -201,7 +201,8 @@ static void test_mp_seconds_to_human_readable() { {.seconds = 86400, "24h 0m 0s"}, }; for (size_t i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) { - char *result = seconds_to_human_readable(tc[i].seconds); + char result[255] = {0}; + seconds_to_human_readable(tc[i].seconds, result, sizeof(result)); printf("seconds=%d, expected: %s, got: %s\n", tc[i].seconds, tc[i].expected, result); STASIS_ASSERT(strcmp(result, tc[i].expected) == 0, "bad output"); } |
