Skip to content

Commit

Permalink
log: reduce # of malloc operations
Browse files Browse the repository at this point in the history
This speeds up leakcheck tests when log is enabled as well as logging
itself.
  • Loading branch information
shintaro-iwasaki committed Mar 12, 2021
1 parent c6d4739 commit 6dc14a1
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void ABTI_log_debug(FILE *fh, const char *format, ...)
ABTI_local *p_local = ABTI_local_get_local_uninlined();

const char *prefix_fmt = NULL, *prefix = NULL;
char *newfmt;
char static_buffer[256], *newfmt;
uint64_t tid;
int rank;
size_t newfmt_len;
Expand Down Expand Up @@ -49,15 +49,23 @@ void ABTI_log_debug(FILE *fh, const char *format, ...)
/* Both tid and rank are less than 42 characters in total. */
const int len_tid_rank = 50;
newfmt_len = 6 + len_tid_rank + strlen(format);
int abt_errno = ABTU_malloc(newfmt_len + 1, (void **)&newfmt);
if (abt_errno != ABT_SUCCESS)
return;
if (sizeof(static_buffer) >= newfmt_len + 1) {
newfmt = static_buffer;
} else {
int abt_errno = ABTU_malloc(newfmt_len + 1, (void **)&newfmt);
if (abt_errno != ABT_SUCCESS)
return;
}
sprintf(newfmt, prefix_fmt, tid, rank, format);
} else {
newfmt_len = strlen(prefix) + strlen(format);
int abt_errno = ABTU_malloc(newfmt_len + 1, (void **)&newfmt);
if (abt_errno != ABT_SUCCESS)
return;
if (sizeof(static_buffer) >= newfmt_len + 1) {
newfmt = static_buffer;
} else {
int abt_errno = ABTU_malloc(newfmt_len + 1, (void **)&newfmt);
if (abt_errno != ABT_SUCCESS)
return;
}
sprintf(newfmt, prefix_fmt, prefix, format);
}

Expand All @@ -72,8 +80,9 @@ void ABTI_log_debug(FILE *fh, const char *format, ...)
* function works correct (i.e., without any SEGV) but a tester does not
* need an actual log since the output can be extremely large. */
#endif

ABTU_free(newfmt);
if (newfmt != static_buffer) {
ABTU_free(newfmt);
}
}

void ABTI_log_pool_push(ABTI_pool *p_pool, ABT_unit unit)
Expand Down

0 comments on commit 6dc14a1

Please sign in to comment.