Skip to content

Commit

Permalink
use logger instead of perror
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Plewa <[email protected]>
  • Loading branch information
lplewa committed Mar 28, 2024
1 parent 74a2ca2 commit bdc8bc9
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 44 deletions.
3 changes: 0 additions & 3 deletions benchmark/ubench.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#define DISJOINT_POOL_MAX_POOLABLE_SIZE (2 * ALLOC_SIZE)
#define DISJOINT_POOL_CAPACITY (N_ITERATIONS + 10)
#define DISJOINT_POOL_MIN_BUCKET_SIZE (ALLOC_SIZE)
#define DISJOINT_POOL_TRACE (0)

typedef struct alloc_s {
void *ptr;
Expand Down Expand Up @@ -115,7 +114,6 @@ static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS = {
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,

// others
/* .traces = */ OS_MEMORY_PROVIDER_TRACE,
};

static void *w_umfMemoryProviderAlloc(void *provider, size_t size,
Expand Down Expand Up @@ -237,7 +235,6 @@ UBENCH_EX(simple, disjoint_pool_with_os_memory_provider) {
DISJOINT_POOL_MAX_POOLABLE_SIZE;
disjoint_memory_pool_params.Capacity = DISJOINT_POOL_CAPACITY;
disjoint_memory_pool_params.MinBucketSize = DISJOINT_POOL_MIN_BUCKET_SIZE;
disjoint_memory_pool_params.PoolTrace = DISJOINT_POOL_TRACE;

umf_memory_pool_handle_t disjoint_pool;
umf_result = umfPoolCreate(umfDisjointPoolOps(), os_memory_provider,
Expand Down
13 changes: 6 additions & 7 deletions src/provider/provider_os_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ static void print_numa_nodes(os_memory_provider_t *os_provider, void *addr,
int ret = hwloc_get_area_memlocation(os_provider->topo, addr, 1, nodeset,
HWLOC_MEMBIND_BYNODESET);
if (ret) {
LOG_DEBUG("cannot print assigned NUMA node (errno = %i)", errno);
perror("get_mempolicy()");
LOG_PDEBUG("cannot print assigned NUMA node (errno = %i)", errno);
} else {
if (hwloc_bitmap_list_snprintf(os_provider->nodeset_str_buf,
NODESET_STR_BUF_LEN, nodeset)) {
Expand Down Expand Up @@ -422,7 +421,7 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
ret = os_mmap_aligned(NULL, size, alignment, page_size, protection, &addr);
if (ret) {
os_store_last_native_error(UMF_OS_RESULT_ERROR_ALLOC_FAILED, errno);
perror("memory allocation failed");
LOG_PERR("memory allocation failed");

return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
}
Expand Down Expand Up @@ -455,7 +454,7 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,

if (ret) {
os_store_last_native_error(UMF_OS_RESULT_ERROR_BIND_FAILED, errno);
perror("binding memory to NUMA node failed");
LOG_PERR("binding memory to NUMA node failed");
// TODO: (errno == 0) when hwloc_set_area_membind() fails on Windows - ignore this temporarily
if (errno != ENOSYS &&
errno != 0) { // ENOSYS - Function not implemented
Expand Down Expand Up @@ -483,7 +482,7 @@ static umf_result_t os_free(void *provider, void *ptr, size_t size) {
// ignore error when size == 0
if (ret && (size > 0)) {
os_store_last_native_error(UMF_OS_RESULT_ERROR_FREE_FAILED, errno);
perror("memory deallocation failed");
LOG_PERR("memory deallocation failed");

return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
}
Expand Down Expand Up @@ -555,7 +554,7 @@ static umf_result_t os_purge_lazy(void *provider, void *ptr, size_t size) {
if (os_purge(ptr, size, UMF_PURGE_LAZY)) {
os_store_last_native_error(UMF_OS_RESULT_ERROR_PURGE_LAZY_FAILED,
errno);
perror("lazy purging failed");
LOG_PERR("lazy purging failed");

return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
}
Expand All @@ -571,7 +570,7 @@ static umf_result_t os_purge_force(void *provider, void *ptr, size_t size) {
if (os_purge(ptr, size, UMF_PURGE_FORCE)) {
os_store_last_native_error(UMF_OS_RESULT_ERROR_PURGE_FORCE_FAILED,
errno);
perror("force purging failed");
LOG_PERR("force purging failed");
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
}
return UMF_RESULT_SUCCESS;
Expand Down
16 changes: 7 additions & 9 deletions src/utils/utils_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef UMF_ASSERT_H
#define UMF_ASSERT_H 1

#include "utils_log.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -29,9 +30,8 @@ extern "C" {
#define ASSERT(x) \
do { \
if (!(x)) { \
fprintf(stderr, \
"Assertion failed: " #x " at " __FILE__ " line %d.\n", \
__LINE__); \
LOG_FATAL("Assertion failed: " #x " at " __FILE__ " line %d.", \
__LINE__); \
abort(); \
} \
} while (0)
Expand All @@ -40,10 +40,9 @@ extern "C" {
long X = (x); \
long Y = (y); \
if (X == Y) { \
fprintf(stderr, \
"Assertion failed: " #x " != " #y \
", both are %ld, at " __FILE__ " line %d.\n", \
X, __LINE__); \
LOG_FATAL("Assertion failed: " #x " != " #y \
", both are %ld, at " __FILE__ " line %d.", \
X, __LINE__); \
abort(); \
} \
} while (0)
Expand All @@ -52,8 +51,7 @@ extern "C" {
#define UMF_CHECK(condition, errorStatus) \
do { \
if (!(condition)) { \
fprintf(stderr, "UMF check failed: " #condition " in %s\n", \
__func__); \
LOG_FATAL("UMF check failed: " #condition " in %s", __func__); \
return errorStatus; \
} \
} while (0)
Expand Down
114 changes: 95 additions & 19 deletions src/utils/utils_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <pthread.h>
#endif

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -54,14 +55,17 @@ static const char *level_to_str(util_log_level_t l) {
return "INFO";
case LOG_WARNING:
return "WARN";
case LOG_FATAL:
return "FATAL";
default:
ASSERT(0);
return "";
}
}

void util_log(util_log_level_t level, const char *format, ...) {
if (!loggerConfig.output) {
static void util_log_internal(util_log_level_t level, int perror,
const char *format, va_list args) {
if (!loggerConfig.output && level != LOG_FATAL) {
return; //logger not enabled
}
if (level < loggerConfig.level) {
Expand All @@ -82,17 +86,73 @@ void util_log(util_log_level_t level, const char *format, ...) {
#endif

char buffer[LOG_MAX];
va_list args;
va_start(args, format);
int ret = vsnprintf(buffer, sizeof(buffer), format, args);
const char *overflow = "";
if (ret >= (intptr_t)sizeof(buffer)) {
char *b_pos = buffer;
int b_size = sizeof(buffer);

int tmp = vsnprintf(buffer, sizeof(buffer), format, args);
ASSERT(tmp > 0);

b_pos += (int)tmp;
b_size -= (int)tmp;

const char *postfix = "";

if (perror) {
if (b_size > 2) {
strncat(b_pos, ": ", b_size);
b_pos += 2;
b_size -= 2;
#if defined(_WIN32)
char err[80]; // max size according to msdn
if (strerror_s(err, sizeof(err), errno)) {
*err = '\0';
postfix = "[strerror_s failed]";
}
#elif defined(__APPLE__)
char err[1024]; // max size according to manpage.
int saveno = errno;
errno = 0;
if (strerror_r(saveno, err, sizeof(err))) {
/* should never happen */
*err = '\0';
postfix = "[strerror_r failed]";
}

if (errno == ERANGE) {
postfix = "[truncated...]";
}
errno = saveno;
#else
char err_buff[1024]; // max size according to manpage.
int saveno = errno;
errno = 0;
char *err = strerror_r(saveno, err_buff, sizeof(err_buff));
if (errno == ERANGE) {
postfix = "[truncated...]";
}
errno = saveno;
#endif
strncpy(b_pos, err, b_size);
size_t err_size = strlen(err);
b_pos += err_size;
b_size -= (int)err_size;
if (b_size <= 0) {
buffer[LOG_MAX - 1] =
'\0'; //strncpy do not add \0 in case of overflow
}
} else {
postfix = "[truncated...]";
}
}

if (b_size <= 0) {
//TODO: alloc bigger buffer with base alloc
overflow = "[truncated...]";
postfix = "[truncated...]";
}
va_end(args);

char header[LOG_HEADER];
char *h_pos = header;
int h_size = sizeof(header);
memset(header, 0, sizeof(header));

if (loggerConfig.timestamp) {
Expand All @@ -104,32 +164,48 @@ void util_log(util_log_level_t level, const char *format, ...) {
localtime_r(&now, &tm_info);
#endif

ASSERT((intptr_t)sizeof(header) > (h_pos - header));
h_pos += strftime(h_pos, sizeof(header) - (h_pos - header),
"%Y-%m-%dT%H:%M:%S ", &tm_info);
ASSERT(h_size > 0);
tmp = (int)strftime(h_pos, h_size, "%Y-%m-%dT%H:%M:%S ", &tm_info);
h_pos += tmp;
h_size -= tmp;
}

if (loggerConfig.pid) {
ASSERT((intptr_t)sizeof(header) > (h_pos - header));
h_pos += snprintf(h_pos, sizeof(header) - (h_pos - header),
"PID:%-6lu TID:%-6lu ", (unsigned long)pid,
(unsigned long)tid);
ASSERT(h_size > 0);
tmp = snprintf(h_pos, h_size, "PID:%-6lu TID:%-6lu ",
(unsigned long)pid, (unsigned long)tid);
h_pos += tmp;
h_size -= tmp;
}

// We take twice header size here to ensure that
// we have space for log level and overflow string
// we have space for log level and postfix string
// otherwise -Wformat-truncation might be thrown by compiler
char logLine[LOG_MAX + LOG_HEADER * 2];
snprintf(logLine, sizeof(logLine), "[%s%-5s UMF] %s%s\n", header,
level_to_str(level), buffer, overflow);
level_to_str(level), buffer, postfix);

fputs(logLine, loggerConfig.output);
fputs(logLine, loggerConfig.output ? loggerConfig.output : stderr);

if (level >= loggerConfig.flushLevel) {
fflush(loggerConfig.output);
}
}

void util_log(util_log_level_t level, const char *format, ...) {
va_list args;
va_start(args, format);
util_log_internal(level, 0, format, args);
va_end(args);
}

void util_plog(util_log_level_t level, const char *format, ...) {
va_list args;
va_start(args, format);
util_log_internal(level, 1, format, args);
va_end(args);
}

static const char *bool_to_str(int b) { return b ? "yes" : "no"; }

void util_log_init(void) {
Expand Down
29 changes: 24 additions & 5 deletions src/utils/utils_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,38 @@
extern "C" {
#endif

typedef enum { LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR } util_log_level_t;
typedef enum {
LOG_DEBUG,
LOG_INFO,
LOG_WARNING,
LOG_ERROR,
LOG_FATAL
} util_log_level_t;

#define LOG_DEBUG(...) util_log(LOG_DEBUG, __VA_ARGS__);
#define LOG_INFO(...) util_log(LOG_INFO, __VA_ARGS__);
#define LOG_WARN(...) util_log(LOG_WARNING, __VA_ARGS__);
#define LOG_ERR(...) util_log(LOG_ERROR, __VA_ARGS__);
#define LOG_FATAL(...) util_log(LOG_FATAL, __VA_ARGS__);

#define LOG_PDEBUG(...) util_plog(LOG_DEBUG, __VA_ARGS__);
#define LOG_PINFO(...) util_plog(LOG_INFO, __VA_ARGS__);
#define LOG_PWARN(...) util_plog(LOG_WARNING, __VA_ARGS__);
#define LOG_PERR(...) util_plog(LOG_ERROR, __VA_ARGS__);
#define LOG_PFATAL(...) util_plog(LOG_FATAL, __VA_ARGS__);

void util_log_init(void);
#ifdef _WIN32
void util_log(util_log_level_t level, const char *format, ...);
void util_plog(util_log_level_t level, const char *format, ...);
#else
void util_log(util_log_level_t level, const char *format, ...)
__attribute__((format(printf, 2, 3)));
void util_plog(util_log_level_t level, const char *format, ...)
__attribute__((format(printf, 2, 3)));
#endif
#define LOG_DEBUG(...) util_log(LOG_DEBUG, __VA_ARGS__);
#define LOG_INFO(...) util_log(LOG_INFO, __VA_ARGS__);
#define LOG_WARN(...) util_log(LOG_WARNING, __VA_ARGS__);
#define LOG_ERR(...) util_log(LOG_ERROR, __VA_ARGS__);

void util_log_init(void);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit bdc8bc9

Please sign in to comment.