]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: add 'malloc' verb to dump malloc_info() 26217/head
authorLuca Boccassi <bluca@debian.org>
Thu, 26 Jan 2023 13:28:15 +0000 (13:28 +0000)
committerLuca Boccassi <bluca@debian.org>
Thu, 23 Feb 2023 18:59:06 +0000 (18:59 +0000)
Gets the memory state of the manager:

root@image:~# systemd-analyze malloc
<malloc version=1>
<heap nr=0>
<sizes>
  <size from=33 to=33 total=396 count=12/>
  <unsorted from=20385 to=20385 total=20385 count=1/>
</sizes>
<total type=fast count=0 size=0/>
<total type=rest count=14 size=36589/>
<system type=current size=1691648/>
<system type=max size=1839104/>
<aspace type=total size=1691648/>
<aspace type=mprotect size=1691648/>
</heap>
<total type=fast count=0 size=0/>
<total type=rest count=14 size=36589/>
<total type=mmap count=0 size=0/>
<system type=current size=1691648/>
<system type=max size=1839104/>
<aspace type=total size=1691648/>
<aspace type=mprotect size=1691648/>
</malloc>

man/systemd-analyze.xml
src/analyze/analyze-dump.c
src/analyze/analyze-malloc.c [new file with mode: 0644]
src/analyze/analyze-malloc.h [new file with mode: 0644]
src/analyze/analyze.c
src/analyze/analyze.h
src/analyze/meson.build
test/units/testsuite-65.sh

index f81dcc324ae4b4f91a5674a5703f326536d80d62..8ceeccb37a0b8928bb67e03fb797234b4218b47c 100644 (file)
       <arg choice="plain">security</arg>
       <arg choice="plain" rep="repeat"><replaceable>UNIT</replaceable></arg>
     </cmdsynopsis>
+    <cmdsynopsis>
+      <command>systemd-analyze</command>
+      <arg choice="opt" rep="repeat">OPTIONS</arg>
+      <arg choice="plain">malloc</arg>
+      <arg choice="opt" rep="repeat"><replaceable>D-BUS SERVICE</replaceable></arg>
+    </cmdsynopsis>
   </refsynopsisdiv>
 
   <refsect1>
@@ -271,6 +277,16 @@ Timestamp units-load-finish: Thu 2019-03-14 23:28:07 CET
       </example>
     </refsect2>
 
+    <refsect2>
+      <title><command>systemd-analyze malloc [<replaceable>D-Bus service</replaceable>…]</command></title>
+
+      <para>This command can be used to request the output of the internal memory state (as returned by
+      <citerefentry><refentrytitle>malloc_info</refentrytitle><manvolnum>3</manvolnum></citerefentry>) of
+      a D-Bus service implementing this pattern. If no service is specified, the command will be sent to
+      <filename>org.freedesktop.systemd1</filename> (the system or user service manager). The output format
+      is subject to change without notice and should not be parsed by applications.</para>
+    </refsect2>
+
     <refsect2>
       <title><command>systemd-analyze plot</command></title>
 
index 2e838c906f31a8a0f099cde486e5c6fb6c4e54c9..2642582903b0963128733ffd353b578a47d9017c 100644 (file)
@@ -29,21 +29,6 @@ static int dump_fallback(sd_bus *bus) {
         return 0;
 }
 
-static int dump_fd_reply(sd_bus_message *message) {
-        int fd, r;
-
-        r = sd_bus_message_read(message, "h", &fd);
-        if (r < 0)
-                return bus_log_parse_error(r);
-
-        fflush(stdout);
-        r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
-        if (r < 0)
-                return r;
-
-        return 1;  /* Success */
-}
-
 static int dump(sd_bus *bus) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
diff --git a/src/analyze/analyze-malloc.c b/src/analyze/analyze-malloc.c
new file mode 100644 (file)
index 0000000..5e6ff5b
--- /dev/null
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "sd-bus.h"
+
+#include "analyze-malloc.h"
+#include "analyze.h"
+#include "bus-error.h"
+#include "bus-internal.h"
+
+static int dump_malloc_info(sd_bus *bus, char *service) {
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+        int r;
+
+        assert(bus);
+        assert(service);
+
+        r = sd_bus_call_method(bus,
+                               service,
+                               "/org/freedesktop/MemoryAllocation1",
+                               "org.freedesktop.MemoryAllocation1",
+                               "GetMallocInfo",
+                               &error,
+                               &reply,
+                               NULL);
+        if (r < 0)
+                return log_error_errno(r, "Failed to call GetMallocInfo on '%s': %s", service, bus_error_message(&error, r));
+
+        return dump_fd_reply(reply);
+}
+
+int verb_malloc(int argc, char *argv[], void *userdata) {
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
+        char **services = STRV_MAKE("org.freedesktop.systemd1");
+        int r;
+
+        if (!strv_isempty(strv_skip(argv, 1))) {
+                services = strv_skip(argv, 1);
+                STRV_FOREACH(service, services)
+                        if (!service_name_is_valid(*service))
+                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "D-Bus service name '%s' is not valid.", *service);
+        }
+
+        r = acquire_bus(&bus, NULL);
+        if (r < 0)
+                return bus_log_connect_error(r, arg_transport);
+
+        r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
+        if (r < 0)
+                return log_error_errno(r, "Unable to determine if bus connection supports fd passing: %m");
+        if (r == 0)
+                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unable to receive FDs over D-Bus.");
+
+        pager_open(arg_pager_flags);
+
+        STRV_FOREACH(service, services) {
+                r = dump_malloc_info(bus, *service);
+                if (r < 0)
+                        return r;
+        }
+
+        return EXIT_SUCCESS;
+}
diff --git a/src/analyze/analyze-malloc.h b/src/analyze/analyze-malloc.h
new file mode 100644 (file)
index 0000000..d3feabd
--- /dev/null
@@ -0,0 +1,5 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#pragma once
+
+int verb_malloc(int argc, char *argv[], void *userdata);
index aaa7e4b706ae71ebd6d19be0ecb012051af87e3d..d7688c1a85b7a71567567260d5d6235201e1bd25 100644 (file)
@@ -25,6 +25,7 @@
 #include "analyze-filesystems.h"
 #include "analyze-inspect-elf.h"
 #include "analyze-log-control.h"
+#include "analyze-malloc.h"
 #include "analyze-plot.h"
 #include "analyze-security.h"
 #include "analyze-service-watchdogs.h"
@@ -166,6 +167,23 @@ void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timesp
                            "Use 'systemd-analyze timespan \"%s\"' instead?", p);
 }
 
+int dump_fd_reply(sd_bus_message *message) {
+        int fd, r;
+
+        assert(message);
+
+        r = sd_bus_message_read(message, "h", &fd);
+        if (r < 0)
+                return bus_log_parse_error(r);
+
+        fflush(stdout);
+        r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
+        if (r < 0)
+                return r;
+
+        return 1;  /* Success */
+}
+
 static int help(int argc, char *argv[], void *userdata) {
         _cleanup_free_ char *link = NULL, *dot_link = NULL;
         int r;
@@ -211,6 +229,7 @@ static int help(int argc, char *argv[], void *userdata) {
                "  timespan SPAN...           Validate a time span\n"
                "  security [UNIT...]         Analyze security of unit\n"
                "  inspect-elf FILE...        Parse and print ELF package metadata\n"
+               "  malloc [D-BUS SERVICE...]  Dump malloc stats of a D-Bus service\n"
                "\nOptions:\n"
                "     --recursive-errors=MODE Control which units are verified\n"
                "     --offline=BOOL          Perform a security review on unit file(s)\n"
@@ -601,6 +620,7 @@ static int run(int argc, char *argv[]) {
                 { "timespan",          2,        VERB_ANY, 0,            verb_timespan          },
                 { "security",          VERB_ANY, VERB_ANY, 0,            verb_security          },
                 { "inspect-elf",       2,        VERB_ANY, 0,            verb_elf_inspection    },
+                { "malloc",            VERB_ANY, VERB_ANY, 0,            verb_malloc            },
                 {}
         };
 
index e4af7b47e00c965c0625d0abe53cdadb95cfffa3..35b1cddb38ff8721c4da00d10e4a31e651730d13 100644 (file)
@@ -44,3 +44,5 @@ int acquire_bus(sd_bus **bus, bool *use_full_bus);
 int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv);
 
 void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan);
+
+int dump_fd_reply(sd_bus_message *message);
index 6493cb2e995ef312c9ee2a30daf130b81652d3ad..ac40600a6dedd53325fee303e35385df97cf811b 100644 (file)
@@ -14,6 +14,7 @@ systemd_analyze_sources = files(
         'analyze-filesystems.c',
         'analyze-inspect-elf.c',
         'analyze-log-control.c',
+        'analyze-malloc.c',
         'analyze-plot.c',
         'analyze-security.c',
         'analyze-service-watchdogs.c',
index 4093c5a2a7870604f67b577f791b57ee59d2a9e8..edaf667107156686c7036b4422224f2793b44ed0 100755 (executable)
@@ -56,6 +56,7 @@ systemd-analyze dump "*" >/dev/null
 systemd-analyze dump "*.socket" >/dev/null
 systemd-analyze dump "*.socket" "*.service" aaaaaaa ... >/dev/null
 systemd-analyze dump systemd-journald.service >/dev/null
+systemd-analyze malloc >/dev/null
 (! systemd-analyze dump "")
 # unit-files
 systemd-analyze unit-files >/dev/null