mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2025-12-31 20:36:25 +00:00
preload: hoist preloads into src/ directory
They will get used from libsysprof-profile, but it's nice to have them a directory up going forward.
This commit is contained in:
79
src/preload/backtrace-helper.h
Normal file
79
src/preload/backtrace-helper.h
Normal file
@ -0,0 +1,79 @@
|
||||
/* backtrace-helper.h
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef HAVE_EXECINFO_H
|
||||
# include <execinfo.h>
|
||||
#endif
|
||||
#ifdef ENABLE_LIBUNWIND
|
||||
# define UNW_LOCAL_ONLY
|
||||
# include <libunwind.h>
|
||||
#endif
|
||||
|
||||
static void
|
||||
backtrace_init (void)
|
||||
{
|
||||
#ifdef ENABLE_LIBUNWIND
|
||||
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
|
||||
# ifdef HAVE_UNW_SET_CACHE_SIZE
|
||||
unw_set_cache_size (unw_local_addr_space, 1024, 0);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
backtrace_func (SysprofCaptureAddress *addrs,
|
||||
guint n_addrs,
|
||||
G_GNUC_UNUSED gpointer user_data)
|
||||
{
|
||||
#if defined(ENABLE_LIBUNWIND)
|
||||
# if GLIB_SIZEOF_VOID_P == 8
|
||||
/* We know that collector will overwrite fields *AFTER* it
|
||||
* has called the backtrace function allowing us to cheat
|
||||
* and subtract an offset from addrs to avoid having to
|
||||
* copy frame pointers around.
|
||||
*/
|
||||
return unw_backtrace ((void **)addrs - 2, n_addrs) - 2;
|
||||
# else
|
||||
static const int skip = 2;
|
||||
void **stack = alloca (n_addrs * sizeof (gpointer));
|
||||
int n = unw_backtrace (stack, n_addrs);
|
||||
for (guint i = skip; i < n; i++)
|
||||
addrs[i-skip] = GPOINTER_TO_SIZE (stack[i]);
|
||||
return MAX (0, n - skip);
|
||||
# endif
|
||||
#elif defined(HAVE_EXECINFO_H)
|
||||
# if GLIB_SIZEOF_VOID_P == 8
|
||||
/* See note on unw_backtrace() */
|
||||
return backtrace ((void **)addrs - 2, n_addrs) - 2;
|
||||
# else /* GLIB_SIZEOF_VOID_P != 8 */
|
||||
static const int skip = 2;
|
||||
void **stack = alloca (n_addrs * sizeof (gpointer));
|
||||
int n = backtrace (stack, n_addrs);
|
||||
for (guint i = skip; i < n; i++)
|
||||
addrs[i-skip] = GPOINTER_TO_SIZE (stack[i]);
|
||||
return MAX (0, n - skip);
|
||||
# endif /* GLIB_SIZEOF_VOID_P */
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
94
src/preload/gconstructor.h
Normal file
94
src/preload/gconstructor.h
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
|
||||
destructors, in a sane way, including e.g. on library unload. If not you're on
|
||||
your own.
|
||||
|
||||
Some compilers need #pragma to handle this, which does not work with macros,
|
||||
so the way you need to use this is (for constructors):
|
||||
|
||||
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
|
||||
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
|
||||
#endif
|
||||
G_DEFINE_CONSTRUCTOR(my_constructor)
|
||||
static void my_constructor(void) {
|
||||
...
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __GTK_DOC_IGNORE__
|
||||
|
||||
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
|
||||
|
||||
#define G_HAS_CONSTRUCTORS 1
|
||||
|
||||
#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
|
||||
#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
|
||||
|
||||
#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
|
||||
/* Visual studio 2008 and later has _Pragma */
|
||||
|
||||
#define G_HAS_CONSTRUCTORS 1
|
||||
|
||||
#define G_DEFINE_CONSTRUCTOR(_func) \
|
||||
static void _func(void); \
|
||||
static int _func ## _wrapper(void) { _func(); return 0; } \
|
||||
__pragma(section(".CRT$XCU",read)) \
|
||||
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
|
||||
|
||||
#define G_DEFINE_DESTRUCTOR(_func) \
|
||||
static void _func(void); \
|
||||
static int _func ## _constructor(void) { atexit (_func); return 0; } \
|
||||
__pragma(section(".CRT$XCU",read)) \
|
||||
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
|
||||
|
||||
#elif defined (_MSC_VER)
|
||||
|
||||
#define G_HAS_CONSTRUCTORS 1
|
||||
|
||||
/* Pre Visual studio 2008 must use #pragma section */
|
||||
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
|
||||
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
|
||||
|
||||
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
|
||||
section(".CRT$XCU",read)
|
||||
#define G_DEFINE_CONSTRUCTOR(_func) \
|
||||
static void _func(void); \
|
||||
static int _func ## _wrapper(void) { _func(); return 0; } \
|
||||
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
|
||||
|
||||
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
|
||||
section(".CRT$XCU",read)
|
||||
#define G_DEFINE_DESTRUCTOR(_func) \
|
||||
static void _func(void); \
|
||||
static int _func ## _constructor(void) { atexit (_func); return 0; } \
|
||||
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
|
||||
|
||||
#elif defined(__SUNPRO_C)
|
||||
|
||||
/* This is not tested, but i believe it should work, based on:
|
||||
* http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
|
||||
*/
|
||||
|
||||
#define G_HAS_CONSTRUCTORS 1
|
||||
|
||||
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
|
||||
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
|
||||
|
||||
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
|
||||
init(_func)
|
||||
#define G_DEFINE_CONSTRUCTOR(_func) \
|
||||
static void _func(void);
|
||||
|
||||
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
|
||||
fini(_func)
|
||||
#define G_DEFINE_DESTRUCTOR(_func) \
|
||||
static void _func(void);
|
||||
|
||||
#else
|
||||
|
||||
/* constructors not supported for this compiler */
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* __GTK_DOC_IGNORE__ */
|
||||
26
src/preload/meson.build
Normal file
26
src/preload/meson.build
Normal file
@ -0,0 +1,26 @@
|
||||
libdl_dep = cc.find_library('dl', required: false)
|
||||
|
||||
preload_deps = [
|
||||
dependency('glib-2.0'),
|
||||
libsysprof_capture_dep,
|
||||
libdl_dep,
|
||||
]
|
||||
|
||||
if get_option('libunwind')
|
||||
preload_deps += [libunwind_dep]
|
||||
endif
|
||||
|
||||
|
||||
libsysprof_memory_preload = shared_library('sysprof-memory-@0@'.format(libsysprof_api_version),
|
||||
['sysprof-memory-collector.c'],
|
||||
dependencies: preload_deps,
|
||||
install: true,
|
||||
install_dir: get_option('libdir'),
|
||||
)
|
||||
|
||||
libsysprof_speedtrack_preload = shared_library('sysprof-speedtrack-@0@'.format(libsysprof_api_version),
|
||||
['sysprof-speedtrack-collector.c'],
|
||||
dependencies: preload_deps,
|
||||
install: true,
|
||||
install_dir: get_option('libdir'),
|
||||
)
|
||||
232
src/preload/sysprof-memory-collector.c
Normal file
232
src/preload/sysprof-memory-collector.c
Normal file
@ -0,0 +1,232 @@
|
||||
/* sysprof-memory-collector.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 <dlfcn.h>
|
||||
#include <glib.h>
|
||||
#include <sched.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <sysprof-capture.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "backtrace-helper.h"
|
||||
|
||||
#include "gconstructor.h"
|
||||
|
||||
typedef void *(* RealMalloc) (size_t);
|
||||
typedef void (* RealFree) (void *);
|
||||
typedef void *(* RealCalloc) (size_t, size_t);
|
||||
typedef void *(* RealRealloc) (void *, size_t);
|
||||
typedef void *(* RealAlignedAlloc) (size_t, size_t);
|
||||
typedef int (* RealPosixMemalign) (void **, size_t, size_t);
|
||||
typedef void *(* RealMemalign) (size_t, size_t);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char buf[4092];
|
||||
int off;
|
||||
} ScratchAlloc;
|
||||
|
||||
static void hook_memtable (void);
|
||||
static void *scratch_malloc (size_t);
|
||||
static void *scratch_realloc (void *, size_t);
|
||||
static void *scratch_calloc (size_t, size_t);
|
||||
static void scratch_free (void *);
|
||||
|
||||
static int collector_ready;
|
||||
static int hooked;
|
||||
static ScratchAlloc scratch;
|
||||
static RealCalloc real_calloc = scratch_calloc;
|
||||
static RealFree real_free = scratch_free;
|
||||
static RealMalloc real_malloc = scratch_malloc;
|
||||
static RealRealloc real_realloc = scratch_realloc;
|
||||
static RealAlignedAlloc real_aligned_alloc;
|
||||
static RealPosixMemalign real_posix_memalign;
|
||||
static RealMemalign real_memalign;
|
||||
|
||||
#if defined (G_HAS_CONSTRUCTORS)
|
||||
# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
|
||||
# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(collector_init_ctor)
|
||||
# endif
|
||||
G_DEFINE_CONSTRUCTOR(collector_init_ctor)
|
||||
#else
|
||||
# error Your platform/compiler is missing constructor support
|
||||
#endif
|
||||
|
||||
static void
|
||||
collector_init_ctor (void)
|
||||
{
|
||||
backtrace_init ();
|
||||
sysprof_collector_init ();
|
||||
collector_ready = TRUE;
|
||||
}
|
||||
|
||||
static void *
|
||||
scratch_malloc (size_t size)
|
||||
{
|
||||
hook_memtable ();
|
||||
return real_malloc (size);
|
||||
}
|
||||
|
||||
static void *
|
||||
scratch_realloc (void *ptr,
|
||||
size_t size)
|
||||
{
|
||||
hook_memtable ();
|
||||
return real_realloc (ptr, size);
|
||||
}
|
||||
|
||||
static void *
|
||||
scratch_calloc (size_t nmemb,
|
||||
size_t size)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
/* re-entrant, but forces early hook in case calloc is
|
||||
* called before any of our other hooks.
|
||||
*/
|
||||
if (!hooked)
|
||||
hook_memtable ();
|
||||
|
||||
size *= nmemb;
|
||||
ret = &scratch.buf[scratch.off];
|
||||
scratch.off += size;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
scratch_free (void *ptr)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
hook_memtable (void)
|
||||
{
|
||||
if (hooked)
|
||||
return;
|
||||
|
||||
hooked = 1;
|
||||
|
||||
real_calloc = dlsym (RTLD_NEXT, "calloc");
|
||||
real_free = dlsym (RTLD_NEXT, "free");
|
||||
real_malloc = dlsym (RTLD_NEXT, "malloc");
|
||||
real_realloc = dlsym (RTLD_NEXT, "realloc");
|
||||
real_aligned_alloc = dlsym (RTLD_NEXT, "aligned_alloc");
|
||||
real_posix_memalign = dlsym (RTLD_NEXT, "posix_memalign");
|
||||
real_memalign = dlsym (RTLD_NEXT, "memalign");
|
||||
|
||||
unsetenv ("LD_PRELOAD");
|
||||
}
|
||||
|
||||
static inline void
|
||||
track_malloc (void *ptr,
|
||||
size_t size)
|
||||
{
|
||||
if G_LIKELY (ptr && collector_ready)
|
||||
sysprof_collector_allocate (GPOINTER_TO_SIZE (ptr),
|
||||
size,
|
||||
backtrace_func,
|
||||
NULL);
|
||||
}
|
||||
|
||||
static inline void
|
||||
track_free (void *ptr)
|
||||
{
|
||||
if G_LIKELY (ptr && collector_ready)
|
||||
sysprof_collector_allocate (GPOINTER_TO_SIZE (ptr),
|
||||
0,
|
||||
NULL,
|
||||
NULL);
|
||||
}
|
||||
|
||||
void *
|
||||
malloc (size_t size)
|
||||
{
|
||||
void *ret = real_malloc (size);
|
||||
track_malloc (ret, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *
|
||||
calloc (size_t nmemb,
|
||||
size_t size)
|
||||
{
|
||||
void *ret = real_calloc (nmemb, size);
|
||||
track_malloc (ret, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *
|
||||
realloc (void *ptr,
|
||||
size_t size)
|
||||
{
|
||||
void *ret = real_realloc (ptr, size);
|
||||
|
||||
if (ret != ptr)
|
||||
{
|
||||
track_free (ptr);
|
||||
track_malloc (ret, size);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
free (void *ptr)
|
||||
{
|
||||
if G_LIKELY (ptr < (void *)scratch.buf ||
|
||||
ptr >= (void *)&scratch.buf[sizeof scratch.buf])
|
||||
{
|
||||
real_free (ptr);
|
||||
track_free (ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void *
|
||||
aligned_alloc (size_t alignment,
|
||||
size_t size)
|
||||
{
|
||||
void *ret = real_aligned_alloc (alignment, size);
|
||||
track_malloc (ret, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
posix_memalign (void **memptr,
|
||||
size_t alignment,
|
||||
size_t size)
|
||||
{
|
||||
int ret = real_posix_memalign (memptr, alignment, size);
|
||||
track_malloc (*memptr, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *
|
||||
memalign (size_t alignment,
|
||||
size_t size)
|
||||
{
|
||||
void *ret = real_memalign (alignment, size);
|
||||
track_malloc (ret, size);
|
||||
return ret;
|
||||
}
|
||||
481
src/preload/sysprof-speedtrack-collector.c
Normal file
481
src/preload/sysprof-speedtrack-collector.c
Normal file
@ -0,0 +1,481 @@
|
||||
/* sysprof-speedtrack-collector.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 <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <sysprof-capture.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "backtrace-helper.h"
|
||||
|
||||
static void hook_func (void **addr, const char *name);
|
||||
static int hook_open (const char *filename, int flags, ...);
|
||||
static int hook_close (int fd);
|
||||
static int hook_fsync (int fd);
|
||||
static int hook_fdatasync (int fd);
|
||||
static int hook_msync (void *addr, size_t length, int flags);
|
||||
static void hook_sync (void);
|
||||
static int hook_syncfs (int fd);
|
||||
static ssize_t hook_read (int fd, void *buf, size_t nbyte);
|
||||
static ssize_t hook_write (int fd, const void *buf, size_t nbyte);
|
||||
static gboolean hook_iteration (GMainContext *context, gboolean may_block);
|
||||
|
||||
static __thread gboolean rec_guard;
|
||||
static int (*real_open) (const char *filename, int flags, ...) = hook_open;
|
||||
static int (*real_close) (int fd) = hook_close;
|
||||
static int (*real_fsync) (int fd) = hook_fsync;
|
||||
static int (*real_fdatasync) (int fd) = hook_fdatasync;
|
||||
static int (*real_msync) (void *addr, size_t length, int flags) = hook_msync;
|
||||
static ssize_t (*real_read) (int fd, void *buf, size_t nbyte) = hook_read;
|
||||
static ssize_t (*real_write) (int fd, const void *buf, size_t nbyte) = hook_write;
|
||||
static void (*real_sync) (void) = hook_sync;
|
||||
static int (*real_syncfs) (int fd) = hook_syncfs;
|
||||
static gboolean (*real_iteration) (GMainContext *context, gboolean may_block) = hook_iteration;
|
||||
|
||||
static inline gboolean
|
||||
is_capturing (void)
|
||||
{
|
||||
static __thread int tid = 0;
|
||||
static int pid = 0;
|
||||
|
||||
if (rec_guard)
|
||||
return FALSE;
|
||||
|
||||
if G_UNLIKELY (tid == 0)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
tid = syscall (__NR_gettid, 0);
|
||||
#elif defined(__APPLE__)
|
||||
uint64_t threadid;
|
||||
pthread_threadid_np (NULL, &threadid);
|
||||
tid = threadid;
|
||||
#endif
|
||||
}
|
||||
|
||||
if G_UNLIKELY (pid == 0)
|
||||
pid = getpid ();
|
||||
|
||||
return tid == pid;
|
||||
}
|
||||
|
||||
static void
|
||||
hook_func (void **addr,
|
||||
const char *name)
|
||||
{
|
||||
static GRecMutex m;
|
||||
static gboolean did_init;
|
||||
|
||||
g_rec_mutex_lock (&m);
|
||||
if (!did_init)
|
||||
{
|
||||
did_init = TRUE;
|
||||
backtrace_init ();
|
||||
}
|
||||
g_rec_mutex_unlock (&m);
|
||||
|
||||
*addr = dlsym (RTLD_NEXT, name);
|
||||
}
|
||||
|
||||
int
|
||||
open (const char *filename,
|
||||
int flags,
|
||||
...)
|
||||
{
|
||||
va_list args;
|
||||
mode_t mode;
|
||||
|
||||
va_start (args, flags);
|
||||
mode = va_arg (args, int);
|
||||
va_end (args);
|
||||
|
||||
if (is_capturing ())
|
||||
{
|
||||
gchar str[1024];
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
int ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_open (filename, flags, mode);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (str, sizeof str,
|
||||
"flags = 0x%x, mode = 0%o, filename = %s => %d",
|
||||
flags, mode, filename, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "open", str);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_open (filename, flags, mode);
|
||||
}
|
||||
|
||||
static int
|
||||
hook_open (const char *filename, int flags, ...)
|
||||
{
|
||||
va_list args;
|
||||
mode_t mode;
|
||||
|
||||
va_start (args, flags);
|
||||
mode = va_arg (args, int);
|
||||
va_end (args);
|
||||
|
||||
hook_func ((void **)&real_open, "open");
|
||||
|
||||
return real_open (filename, flags, mode);
|
||||
}
|
||||
|
||||
int
|
||||
close (int fd)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar fdstr[32];
|
||||
int ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_close (fd);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (fdstr, sizeof fdstr, "fd = %d => %d", fd, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "close", fdstr);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_close (fd);
|
||||
}
|
||||
|
||||
static int
|
||||
hook_close (int fd)
|
||||
{
|
||||
hook_func ((void **)&real_close, "close");
|
||||
return real_close (fd);
|
||||
}
|
||||
|
||||
int
|
||||
fsync (int fd)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar fdstr[32];
|
||||
int ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_fsync (fd);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (fdstr, sizeof fdstr, "fd = %d => %d", fd, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "fsync", fdstr);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_fsync (fd);
|
||||
}
|
||||
|
||||
static int
|
||||
hook_fsync (int fd)
|
||||
{
|
||||
hook_func ((void **)&real_fsync, "fsync");
|
||||
return real_fsync (fd);
|
||||
}
|
||||
|
||||
int
|
||||
fdatasync (int fd)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar fdstr[32];
|
||||
int ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_fdatasync (fd);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (fdstr, sizeof fdstr, "fd = %d => %d", fd, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "fdatasync", fdstr);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_fdatasync (fd);
|
||||
}
|
||||
|
||||
static int
|
||||
hook_fdatasync (int fd)
|
||||
{
|
||||
hook_func ((void **)&real_fdatasync, "fdatasync");
|
||||
return real_fdatasync (fd);
|
||||
}
|
||||
|
||||
int
|
||||
msync (void *addr,
|
||||
size_t length,
|
||||
int flags)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar str[64];
|
||||
int ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_msync (addr, length, flags);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (str, sizeof str, "addr = %p, length = %"G_GSIZE_FORMAT", flags = %d => %d",
|
||||
addr, length, flags, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "msync", str);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_msync (addr, length, flags);
|
||||
}
|
||||
|
||||
static int
|
||||
hook_msync (void *addr,
|
||||
size_t length,
|
||||
int flags)
|
||||
{
|
||||
hook_func ((void **)&real_msync, "msync");
|
||||
return real_msync (addr, length, flags);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
read (int fd,
|
||||
void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar str[64];
|
||||
ssize_t ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_read (fd, buf, nbyte);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (str, sizeof str, "fd = %d, buf = %p, nbyte = %"G_GSIZE_FORMAT" => %"G_GSSIZE_FORMAT,
|
||||
fd, buf, nbyte, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "read", str);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_read (fd, buf, nbyte);
|
||||
}
|
||||
|
||||
static ssize_t
|
||||
hook_read (int fd,
|
||||
void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
hook_func ((void **)&real_read, "read");
|
||||
return real_read (fd, buf, nbyte);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
write (int fd,
|
||||
const void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar str[64];
|
||||
ssize_t ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_write (fd, buf, nbyte);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (str, sizeof str, "fd = %d, buf = %p, nbyte = %"G_GSIZE_FORMAT" => %"G_GSSIZE_FORMAT,
|
||||
fd, buf, nbyte, ret);
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "write", str);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_write (fd, buf, nbyte);
|
||||
}
|
||||
|
||||
static ssize_t
|
||||
hook_write (int fd,
|
||||
const void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
hook_func ((void **)&real_write, "write");
|
||||
return real_write (fd, buf, nbyte);
|
||||
}
|
||||
|
||||
void
|
||||
sync (void)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
real_sync ();
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "sync", "");
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
real_sync ();
|
||||
}
|
||||
|
||||
static void
|
||||
hook_sync (void)
|
||||
{
|
||||
hook_func ((void **)&real_sync, "sync");
|
||||
real_sync ();
|
||||
}
|
||||
|
||||
int
|
||||
syncfs (int fd)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar str[32];
|
||||
int ret;
|
||||
|
||||
rec_guard = TRUE;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_syncfs (fd);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (str, sizeof str, "fd = %d => %d", fd, ret);
|
||||
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "syncfs", str);
|
||||
|
||||
rec_guard = FALSE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_syncfs (fd);
|
||||
}
|
||||
|
||||
static int
|
||||
hook_syncfs (int fd)
|
||||
{
|
||||
hook_func ((void **)&real_syncfs, "syncfs");
|
||||
return real_syncfs (fd);
|
||||
}
|
||||
|
||||
gboolean
|
||||
g_main_context_iteration (GMainContext *context,
|
||||
gboolean may_block)
|
||||
{
|
||||
if (is_capturing ())
|
||||
{
|
||||
gint64 begin;
|
||||
gint64 end;
|
||||
gchar str[128];
|
||||
gboolean ret;
|
||||
|
||||
begin = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
ret = real_iteration (context, may_block);
|
||||
end = SYSPROF_CAPTURE_CURRENT_TIME;
|
||||
|
||||
g_snprintf (str, sizeof str, "context = %p, may_block = %d => %d", context, may_block, ret);
|
||||
sysprof_collector_mark (begin, end - begin, "speedtrack", "g_main_context_iteration", str);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return real_iteration (context, may_block);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
hook_iteration (GMainContext *context,
|
||||
gboolean may_block)
|
||||
{
|
||||
hook_func ((void **)&real_iteration, "g_main_context_iteration");
|
||||
return real_iteration (context, may_block);
|
||||
}
|
||||
72
src/preload/sysprof-tracer.c
Normal file
72
src/preload/sysprof-tracer.c
Normal file
@ -0,0 +1,72 @@
|
||||
/* sysprof-tracer.c
|
||||
*
|
||||
* Copyright 2022 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.h>
|
||||
#include <sysprof-capture.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "backtrace-helper.h"
|
||||
|
||||
#include "gconstructor.h"
|
||||
|
||||
#if defined (G_HAS_CONSTRUCTORS)
|
||||
# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
|
||||
# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(collector_init_ctor)
|
||||
# endif
|
||||
G_DEFINE_CONSTRUCTOR(collector_init_ctor)
|
||||
#else
|
||||
# error Your platform/compiler is missing constructor support
|
||||
#endif
|
||||
|
||||
#ifndef __APPLE__
|
||||
# define profile_func_enter __cyg_profile_func_enter
|
||||
# define profile_func_exit __cyg_profile_func_exit
|
||||
#endif
|
||||
|
||||
static void
|
||||
collector_init_ctor (void)
|
||||
{
|
||||
backtrace_init ();
|
||||
sysprof_collector_init ();
|
||||
}
|
||||
|
||||
/* TODO:
|
||||
*
|
||||
* This is just an example.
|
||||
*
|
||||
* What we would really want to do is to have a new frame type for enter/exit
|
||||
* tracing so that we can only push/pop the new address to the sample. Then
|
||||
* when decoding it can recreate stack traces if necessary.
|
||||
*/
|
||||
|
||||
void
|
||||
profile_func_enter (void *func,
|
||||
void *call_site)
|
||||
{
|
||||
sysprof_collector_sample (backtrace_func, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
profile_func_exit (void *func,
|
||||
void *call_site)
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user