common: implement bt_common_g_string_append_printf
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 19 Jun 2019 18:21:57 +0000 (14:21 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 21 Jul 2019 15:39:24 +0000 (11:39 -0400)
g_string_append_printf() internally allocates a temporary buffer
through use of vasnprintf for each call. This clearly appears at
the top of perf report.

Implement our own private bt_g_string_append_printf which operates
directly on the GString buffer.

See proof of memory allocation/free near the top of this perf report
when converting a 400M LTTng trace to text (piped to /dev/null):

  14.76%  babeltrace2  libc-2.24.so                [.] vfprintf
   7.92%  babeltrace2  [kernel.kallsyms]           [k] vmacache_find
   6.39%  babeltrace2  libglib-2.0.so.0.5000.3     [.] g_string_insert_len
   4.77%  babeltrace2  babeltrace-plugin-ctf.so    [.] bt_bfcr_start
   4.23%  babeltrace2  libc-2.24.so                [.] _int_malloc
   4.06%  babeltrace2  libc-2.24.so                [.] _IO_default_xsputn
   3.60%  babeltrace2  libc-2.24.so                [.] strlen
   3.60%  babeltrace2  libc-2.24.so                [.] __strchrnul
   3.60%  babeltrace2  libbabeltrace2.so.0.0.0     [.] bt_attributes_borrow_field_by_name
   3.51%  babeltrace2  libc-2.24.so                [.] _int_free
   2.88%  babeltrace2  libc-2.24.so                [.] __vasprintf_chk
   2.88%  babeltrace2  libc-2.24.so                [.] _IO_str_init_static_internal
   2.88%  babeltrace2  libc-2.24.so                [.] __strcmp_sse2_unaligned
   2.16%  babeltrace2  libc-2.24.so                [.] malloc
   2.16%  babeltrace2  libc-2.24.so                [.] __tzstring_len

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I956305ac8891d244dbdab6a5b0dde82ecc960e25
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1514
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/common/common.c
src/common/common.h

index d092768a147cf9a18bcabd001229d5966ff5fdfa..dacb5054fa04280e04a0f44b0bc7af2a1bb7eb20 100644 (file)
@@ -1822,3 +1822,35 @@ end:
        return ret;
 }
 #endif /* __MINGW32__ */
+
+BT_HIDDEN
+int bt_common_g_string_append_printf(GString *str, const char *fmt, ...)
+{
+       va_list ap;
+       gsize len, allocated_len, available_len;
+       int print_len;
+
+       /* str->len excludes \0. */
+       len = str->len;
+       /* Explicitly exclude \0. */
+       allocated_len = str->allocated_len - 1;
+       available_len = allocated_len - len;
+
+       str->len = allocated_len;
+       va_start(ap, fmt);
+       print_len = vsnprintf(str->str + len, available_len + 1, fmt, ap);
+       va_end(ap);
+       if (print_len < 0) {
+               return print_len;
+       }
+       if (G_UNLIKELY(available_len < print_len)) {
+               /* Resize. */
+               g_string_set_size(str, len + print_len);
+               va_start(ap, fmt);
+               print_len = vsprintf(str->str + len, fmt, ap);
+               va_end(ap);
+       } else {
+               str->len = len + print_len;
+       }
+       return print_len;
+}
index b7245275832466c196ab464a54c3aa7e6ade6f01..220ec61b4ed9a09433e495881d6c4d1b62e9bff4 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <babeltrace2/babeltrace.h>
 
@@ -720,4 +721,11 @@ end:
        return ret;
 }
 
+/*
+ * bt_g_string_append_printf cannot be inlined because it expects a
+ * variadic argument list.
+ */
+BT_HIDDEN
+int bt_common_g_string_append_printf(GString *str, const char *fmt, ...);
+
 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.025997 seconds and 4 git commands to generate.