lib: Confine the copy_no_nl memcpy to debug_gpfs_log()
authorVolker Lendecke <vl@samba.org>
Tue, 19 Dec 2023 14:34:50 +0000 (15:34 +0100)
committerVolker Lendecke <vl@samba.org>
Thu, 4 Jan 2024 17:06:19 +0000 (17:06 +0000)
gpfswrap_add_trace() seems not to have a format string that could
understand the %.*s notation.

While there this removes >4k of r/w memory from every smbd.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Thu Jan  4 17:06:19 UTC 2024 on atb-devel-224

lib/util/debug.c

index 4c10fde1b8e1430786797f017a0cdda7a376c747..86f13f181cf00f059eee4f9ecb205bab0e4e7062 100644 (file)
@@ -100,9 +100,7 @@ static struct {
        debug_callback_fn callback;
        void *callback_private;
        char header_str[300];
-       char header_str_no_nl[300];
        size_t hs_len;
-       char msg_no_nl[FORMAT_BUFR_SIZE];
 } state = {
        .settings = {
                .timestamp_logs = true
@@ -244,48 +242,6 @@ static int debug_level_to_priority(int level)
 }
 #endif
 
-/* -------------------------------------------------------------------------- **
- * Produce a version of the given buffer without any trailing newlines.
- */
-#if defined(HAVE_LIBSYSTEMD_JOURNAL) || defined(HAVE_LIBSYSTEMD) || \
-       defined(HAVE_LTTNG_TRACEF) || defined(HAVE_GPFS)
-static void copy_no_nl(char *out,
-                      size_t out_size,
-                      const char *in,
-                      size_t in_len)
-{
-       size_t len;
-       /*
-        * Some backends already add an extra newline, so also provide
-        * a buffer without the newline character.
-        */
-       len = MIN(in_len, out_size - 1);
-       if ((len > 0) && (in[len - 1] == '\n')) {
-               len--;
-       }
-
-       memcpy(out, in, len);
-       out[len] = '\0';
-}
-
-static void ensure_copy_no_nl(char *out,
-                             size_t out_size,
-                             const char *in,
-                             size_t in_len)
-{
-       /*
-        * Assume out is a static buffer that is reused as a cache.
-        * If it isn't empty then this has already been done with the
-        * same input.
-        */
-       if (out[0] != '\0') {
-               return;
-       }
-
-       copy_no_nl(out, out_size, in, in_len);
-}
-#endif
-
 /* -------------------------------------------------------------------------- **
  * Debug backends. When logging to DEBUG_FILE, send the log entries to
  * all active backends.
@@ -445,19 +401,39 @@ static void debug_gpfs_reload(bool enabled, bool previously_enabled,
        }
 }
 
+static void copy_no_nl(char *out,
+                      size_t out_size,
+                      const char *in,
+                      size_t in_len)
+{
+       size_t len;
+       /*
+        * Some backends already add an extra newline, so also provide
+        * a buffer without the newline character.
+        */
+       len = MIN(in_len, out_size - 1);
+       if ((len > 0) && (in[len - 1] == '\n')) {
+               len--;
+       }
+
+       memcpy(out, in, len);
+       out[len] = '\0';
+}
+
 static void debug_gpfs_log(int msg_level, const char *msg, size_t msg_len)
 {
+       char no_nl[FORMAT_BUFR_SIZE];
+
        if (state.hs_len > 0) {
-               ensure_copy_no_nl(state.header_str_no_nl,
-                                 sizeof(state.header_str_no_nl),
-                                 state.header_str,
-                                 state.hs_len);
-               gpfswrap_add_trace(msg_level, state.header_str_no_nl);
+               copy_no_nl(no_nl,
+                          sizeof(no_nl),
+                          state.header_str,
+                          state.hs_len);
+               gpfswrap_add_trace(msg_level, no_nl);
        }
-       ensure_copy_no_nl(state.msg_no_nl,
-                         sizeof(state.msg_no_nl),
-                         msg, msg_len);
-       gpfswrap_add_trace(msg_level, state.msg_no_nl);
+
+       copy_no_nl(no_nl, sizeof(no_nl), msg, msg_len);
+       gpfswrap_add_trace(msg_level, no_nl);
 }
 #endif /* HAVE_GPFS */
 
@@ -712,13 +688,6 @@ static void debug_backends_log(const char *msg, size_t msg_len, int msg_level)
 {
        size_t i;
 
-       /*
-        * Some backends already add an extra newline, so initialize a
-        * buffer without the newline character.  It will be filled by
-        * the first backend that needs it.
-        */
-       state.msg_no_nl[0] = '\0';
-
        for (i = 0; i < ARRAY_SIZE(debug_backends); i++) {
                if (msg_level <= debug_backends[i].log_level) {
                        debug_backends[i].log(msg_level, msg, msg_len);
@@ -1943,8 +1912,6 @@ full:
                state.hs_len = sizeof(state.header_str) - 1;
        }
 
-       state.header_str_no_nl[0] = '\0';
-
        errno = old_errno;
        return( true );
 }