]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
optionally support glibc malloc_info via SIGCONT malloc_info
authorEric Wong <e@80x24.org>
Mon, 10 Jun 2019 09:52:13 +0000 (09:52 +0000)
committerEric Wong <e@80x24.org>
Mon, 10 Jun 2019 09:52:13 +0000 (09:52 +0000)
If run with PERL_INLINE_DIRECTORY for Inline::C support
along with INBOX_DEBUG=malloc_info, we can allow users
to opt-in to compiling extra code to support the glibc
malloc_info(3) function.

We'll also add SIGCONT handler to dump the malloc_info(3)
output to stderr on our daemons.

MANIFEST
lib/PublicInbox/Spawn.pm
t/malloc_info.t [new file with mode: 0644]

index 5085bff818383ee1c1ae7a0dfc794e7db69e8385..4a7f7ef90882d4b16f0e91d9362faa60f0572428 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -210,6 +210,7 @@ t/indexlevels-mirror.t
 t/init.t
 t/linkify.t
 t/main-bin/spamc
+t/malloc_info.t
 t/mda.t
 t/mda_filter_rubylang.t
 t/mid.t
index 66b916dfbc72d60c28c57cdddd63a88d1b8d7966..9210f11147a4e609f6e5d6ee0ee2bb37e00e0c9e 100644 (file)
@@ -149,6 +149,23 @@ int pi_fork_exec(int in, int out, int err,
 }
 VFORK_SPAWN
 
+# TODO: we may support other mallocs through this parameter
+if (($ENV{INBOX_DEBUG} // '') =~ /\bmalloc_info\b/) {
+       $vfork_spawn .= <<MALLOC_DEBUG;
+#include <malloc.h>
+
+int inbox_malloc_info(int options)
+{
+       int rc = malloc_info(options, stderr);
+
+       return rc == 0 ? TRUE : FALSE;
+}
+MALLOC_DEBUG
+
+       # dump malloc info to stderr on SIGCONT
+       $SIG{CONT} = sub { inbox_malloc_info(0) };
+}
+
 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY};
 $vfork_spawn = undef unless defined $inline_dir && -d $inline_dir && -w _;
 if (defined $vfork_spawn) {
diff --git a/t/malloc_info.t b/t/malloc_info.t
new file mode 100644 (file)
index 0000000..352ec5c
--- /dev/null
@@ -0,0 +1,25 @@
+# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use PublicInbox::Spawn ();
+
+if (!PublicInbox::Spawn->can('inbox_malloc_info')) {
+       plan skip_all => 'inbox_malloc_info not enabled';
+}
+
+open my $olderr, '>&', \*STDERR or die "dup stderr: $!";
+open my $tmp, '+>', undef or die "tmpfile: $!";
+open STDERR, '>&', $tmp or die "redirect stderr to \$tmp: $!";
+my @x = map { '0' x (1024 * 1024) } (1..128);
+my $cb = $SIG{CONT};
+$cb->();
+@x = ('hello');
+PublicInbox::Spawn::inbox_malloc_info(0);
+open STDERR, '>&', $olderr or die "restore stderr: $!";
+sysseek($tmp, 0, 0) == 0 or die "sysseek: $!";
+my @info = <$tmp>;
+like($info[0], qr/</, 'output looks like XML');
+
+done_testing;