memprof: add memory profiling using LD_PRELOAD

This brings over some of the techniques from the old memprof design.
Sysprof and memprof shared a lot of code, so it is pretty natural to
bring back the same callgraph view based on memory allocations.

This reuses the StackStash just like it did in memprof. While it
would be nice to reuse some existing tools out there, the fit of
memprof with sysprof is so naturally aligned, it's not really a
big deal to bring back the LD_PRELOAD. The value really comes
from seeing all this stuff together instead of multiple apps.

There are plenty of things we can implement on top of this that
we are not doing yet such as temporary allocations, cross-thread
frees, graphing the heap, and graphing differences between the
heap at to points in time. I'd like all of these things, given
enough time to make them useful.

This is still a bit slow though due to the global lock we take
to access the writer. To improve the speed here we need to get
rid of that lock and head towards a design that allows a thread
to request a new writer from Sysprof and save it in TLS (to be
destroyed when the thread exits).
This commit is contained in:
Christian Hergert
2020-01-30 18:24:04 -08:00
parent cae70498da
commit 33c81a3a9c
41 changed files with 6817 additions and 193 deletions

View File

@ -0,0 +1,90 @@
/* memory-stack-stash.c
*
* Copyright 2020 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include <glib/gi18n.h>
#include <stddef.h>
#include <rax.h>
#include <sysprof.h>
#include "../stackstash.h"
#include "../stackstash.c"
static void
memory_stack_stash (SysprofCaptureReader *reader)
{
SysprofCaptureFrameType type;
StackStash *stash = stack_stash_new (NULL);
while (sysprof_capture_reader_peek_type (reader, &type))
{
if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
{
const SysprofCaptureAllocation *ev = sysprof_capture_reader_read_allocation (reader);
if (ev == NULL)
break;
if (ev->alloc_size > 0)
stack_stash_add_trace (stash, ev->addrs, ev->n_addrs, ev->alloc_size);
}
else
{
if (!sysprof_capture_reader_skip (reader))
break;
}
}
stack_stash_unref (stash);
}
gint
main (gint argc,
gchar *argv[])
{
SysprofCaptureReader *reader;
const gchar *filename = argv[1];
g_autoptr(GError) error = NULL;
if (argc < 2)
{
g_printerr ("usage: %s FILENAME\n", argv[0]);
return EXIT_FAILURE;
}
/* Set up gettext translations */
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
if (!(reader = sysprof_capture_reader_new (filename, &error)))
{
g_printerr ("%s\n", error->message);
return EXIT_FAILURE;
}
memory_stack_stash (reader);
sysprof_capture_reader_unref (reader);
return EXIT_SUCCESS;
}