analyze: add 'malloc' verb to dump malloc_info()

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>
This commit is contained in:
Luca Boccassi 2023-01-26 13:28:15 +00:00
parent fb22861da1
commit f50535afad
8 changed files with 108 additions and 15 deletions

View file

@ -139,6 +139,12 @@
<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>

View 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;

View file

@ -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;
}

View file

@ -0,0 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
int verb_malloc(int argc, char *argv[], void *userdata);

View 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 },
{}
};

View 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);

View 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',

View file

@ -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