Logging: use a TLS buffer and increase its size
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 23 Feb 2018 02:57:30 +0000 (21:57 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 2 May 2019 03:32:03 +0000 (23:32 -0400)
The logging system's line buffer is on the stack, and its current size
is 512. This means any log line which is larger than 512 bytes (this can
happen with some warnings/errors with a lot of information) gets
truncated.

In order to make this buffer size arbitrarily large, use a TLS global
buffer instead.

Increase the logging system's line buffer to 16 kiB (per thread).

Drawback: the logging system had a static assertion which ensured that
the line buffer size was not greater than PIPE_BUF so that individual
write operations were atomic. With a line buffer size larger than
PIPE_BUF, different threads could write to the output stream at the same
time which could result in interlaced data. We need to add some locking
to support multithreaded logging.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
logging/log.c

index 78a0361de7c5e89d3586028fbd6e49a9a03999cf..13fcab8e843218dca263316450fb41319146644f 100644 (file)
@@ -128,11 +128,10 @@ extern unsigned long pthread_getsequence_np(pthread_t *);
 #else
        #define BT_LOG_OPTIMIZE_SIZE 0
 #endif
-/* Size of the log line buffer. The buffer is allocated on stack. It limits
- * maximum length of a log line.
+/* Size of the log line buffer. The buffer is a globally allocated per thread.
  */
 #ifndef BT_LOG_BUF_SZ
-       #define BT_LOG_BUF_SZ 512
+       #define BT_LOG_BUF_SZ (4 * 4096)
 #endif
 /* Default number of bytes in one line of memory output. For large values
  * BT_LOG_BUF_SZ also must be increased.
@@ -537,11 +536,10 @@ static void buffer_callback(bt_log_message *msg, char *buf);
 STATIC_ASSERT(eol_fits_eol_sz, sizeof(BT_LOG_EOL) <= BT_LOG_EOL_SZ);
 STATIC_ASSERT(eol_sz_greater_than_zero, 0 < BT_LOG_EOL_SZ);
 STATIC_ASSERT(eol_sz_less_than_buf_sz, BT_LOG_EOL_SZ < BT_LOG_BUF_SZ);
-#if !defined(_WIN32) && !defined(_WIN64)
-       STATIC_ASSERT(buf_sz_less_than_pipe_buf, BT_LOG_BUF_SZ <= PIPE_BUF);
-#endif
 static const char c_hex[] = "0123456789abcdef";
 
+static char __thread logging_buf[4 * 4096];
+
 static INSTRUMENTED_CONST unsigned g_buf_sz = BT_LOG_BUF_SZ - BT_LOG_EOL_SZ;
 static INSTRUMENTED_CONST time_cb g_time_cb = time_callback;
 static INSTRUMENTED_CONST pid_cb g_pid_cb = pid_callback;
@@ -1263,7 +1261,7 @@ static void _bt_log_write_imp(
                const int lvl, const char *const tag, const char *const fmt, va_list va)
 {
        bt_log_message msg;
-       char buf[BT_LOG_BUF_SZ];
+       char *buf = logging_buf;
        const unsigned mask = log->output->mask;
        msg.lvl = lvl;
        msg.tag = tag;
This page took 0.026441 seconds and 4 git commands to generate.