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

@ -63,154 +63,188 @@ G_BEGIN_DECLS
typedef struct _SysprofCaptureWriter SysprofCaptureWriter;
/**
* SysprofBacktraceFunc:
* @addrs: (inout) (array length=n_addrs): an array to place addresses
* into the capture frame
* @n_addrs: the length of @addrs
* @user_data: (scope call): closure data for the callback
*
* Returns: the number of addresses filled in @addrs
*/
typedef guint (*SysprofBacktraceFunc) (SysprofCaptureAddress *addrs,
guint n_addrs,
gpointer user_data);
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureWriter *sysprof_capture_writer_new_from_env (gsize buffer_size);
SysprofCaptureWriter *sysprof_capture_writer_new_from_env (gsize buffer_size);
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureWriter *sysprof_capture_writer_new (const gchar *filename,
gsize buffer_size);
SysprofCaptureWriter *sysprof_capture_writer_new (const gchar *filename,
gsize buffer_size);
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureWriter *sysprof_capture_writer_new_from_fd (int fd,
gsize buffer_size);
SysprofCaptureWriter *sysprof_capture_writer_new_from_fd (int fd,
gsize buffer_size);
SYSPROF_AVAILABLE_IN_ALL
gsize sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self);
gsize sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self);
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureWriter *sysprof_capture_writer_ref (SysprofCaptureWriter *self);
SysprofCaptureWriter *sysprof_capture_writer_ref (SysprofCaptureWriter *self);
SYSPROF_AVAILABLE_IN_ALL
void sysprof_capture_writer_unref (SysprofCaptureWriter *self);
void sysprof_capture_writer_unref (SysprofCaptureWriter *self);
SYSPROF_AVAILABLE_IN_ALL
void sysprof_capture_writer_stat (SysprofCaptureWriter *self,
SysprofCaptureStat *stat);
void sysprof_capture_writer_stat (SysprofCaptureWriter *self,
SysprofCaptureStat *stat);
SYSPROF_AVAILABLE_IN_ALL
void sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self,
GMainContext *main_context,
guint timeout_seconds);
void sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self,
GMainContext *main_context,
guint timeout_seconds);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_file (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *path,
gboolean is_last,
const guint8 *data,
gsize data_len);
gboolean sysprof_capture_writer_add_file (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *path,
gboolean is_last,
const guint8 *data,
gsize data_len);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *path,
gint fd);
gboolean sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *path,
gint fd);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_map (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
guint64 start,
guint64 end,
guint64 offset,
guint64 inode,
const gchar *filename);
gboolean sysprof_capture_writer_add_map (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
guint64 start,
guint64 end,
guint64 offset,
guint64 inode,
const gchar *filename);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_mark (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
guint64 duration,
const gchar *group,
const gchar *name,
const gchar *message);
gboolean sysprof_capture_writer_add_mark (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
guint64 duration,
const gchar *group,
const gchar *name,
const gchar *message);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *id,
const gchar *metadata,
gssize metadata_len);
gboolean sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *id,
const gchar *metadata,
gssize metadata_len);
SYSPROF_AVAILABLE_IN_ALL
guint64 sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self,
const gchar *name);
guint64 sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self,
const gchar *name);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_process (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *cmdline);
gboolean sysprof_capture_writer_add_process (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const gchar *cmdline);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_sample (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
gint32 tid,
const SysprofCaptureAddress *addrs,
guint n_addrs);
gboolean sysprof_capture_writer_add_sample (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
gint32 tid,
const SysprofCaptureAddress *addrs,
guint n_addrs);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_fork (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
gint32 child_pid);
gboolean sysprof_capture_writer_add_fork (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
gint32 child_pid);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_exit (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid);
gboolean sysprof_capture_writer_add_exit (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid);
gboolean sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_define_counters (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const SysprofCaptureCounter *counters,
guint n_counters);
gboolean sysprof_capture_writer_define_counters (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const SysprofCaptureCounter *counters,
guint n_counters);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_set_counters (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const guint *counters_ids,
const SysprofCaptureCounterValue *values,
guint n_counters);
gboolean sysprof_capture_writer_set_counters (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
const guint *counters_ids,
const SysprofCaptureCounterValue *values,
guint n_counters);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_add_log (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
GLogLevelFlags severity,
const gchar *domain,
const gchar *message);
gboolean sysprof_capture_writer_add_log (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
GLogLevelFlags severity,
const gchar *domain,
const gchar *message);
SYSPROF_AVAILABLE_IN_3_36
gboolean sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
gint32 tid,
SysprofCaptureAddress alloc_addr,
gint64 alloc_size,
SysprofBacktraceFunc backtrace_func,
gpointer backtrace_data);
SYSPROF_AVAILABLE_IN_3_36
gboolean sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self,
gint64 time,
gint cpu,
gint32 pid,
gint32 tid,
SysprofCaptureAddress alloc_addr,
gint64 alloc_size,
const SysprofCaptureAddress *addrs,
guint n_addrs);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_flush (SysprofCaptureWriter *self);
gboolean sysprof_capture_writer_flush (SysprofCaptureWriter *self);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
const gchar *filename,
GError **error);
gboolean sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
const gchar *filename,
GError **error);
SYSPROF_AVAILABLE_IN_ALL
guint sysprof_capture_writer_request_counter (SysprofCaptureWriter *self,
guint n_counters);
guint sysprof_capture_writer_request_counter (SysprofCaptureWriter *self,
guint n_counters);
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self,
GError **error);
SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self,
GError **error);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_splice (SysprofCaptureWriter *self,
SysprofCaptureWriter *dest,
GError **error);
gboolean sysprof_capture_writer_splice (SysprofCaptureWriter *self,
SysprofCaptureWriter *dest,
GError **error);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_capture_writer_cat (SysprofCaptureWriter *self,
SysprofCaptureReader *reader,
GError **error);
gboolean sysprof_capture_writer_cat (SysprofCaptureWriter *self,
SysprofCaptureReader *reader,
GError **error);
G_GNUC_INTERNAL
gboolean _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
int fd,
GError **error) G_GNUC_INTERNAL;
gboolean _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
int fd,
GError **error) G_GNUC_INTERNAL;
G_GNUC_INTERNAL
gboolean _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self,
gint64 start_time,
gint64 end_time) G_GNUC_INTERNAL;
gboolean _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self,
gint64 start_time,
gint64 end_time) G_GNUC_INTERNAL;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureWriter, sysprof_capture_writer_unref)