diff options
-rw-r--r-- | configure.ac | 8 | ||||
-rw-r--r-- | src/memory.c | 21 |
2 files changed, 22 insertions, 7 deletions
diff --git a/configure.ac b/configure.ac index b8e6e38..18b878d 100644 --- a/configure.ac +++ b/configure.ac @@ -377,8 +377,10 @@ AC_ARG_ENABLE(malloc-replacement, [malloc_replacement=$enableval], [malloc_replacement=check]) if [[ $malloc_replacement = check ]]; then # We currently do not support dlmalloc on Windows and Mac OS. + # It works on FreeBSD, but is slower than the polling fallback, + # it has been disabled by default. case $host in - *-*-darwin* | *-mingw*) malloc_replacement=no;; + *-*-*bsd* | *-*-darwin* | *-mingw*) malloc_replacement=no;; *) malloc_replacement=yes;; esac fi @@ -422,8 +424,8 @@ else AC_DEFINE(HAVE_PROCFS, 1, [Whether procfs (/proc) is supported]) ;; *-*-*bsd*) - AC_CHECK_HEADERS([sys/types.h sys/sysctl.h]) - AC_CHECK_FUNCS([sysctl]) + AC_CHECK_HEADERS([sys/types.h sys/user.h sys/sysctl.h]) + AC_CHECK_FUNCS([sysconf sysctl]) ;; *-*-darwin*) AC_CHECK_HEADERS([mach/mach.h mach/message.h mach/kern_return.h mach/task_info.h]) diff --git a/src/memory.c b/src/memory.c index 6b02a25..6d7645c 100644 --- a/src/memory.c +++ b/src/memory.c @@ -60,6 +60,9 @@ #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> #endif +#ifdef HAVE_SYS_USER_H +#include <sys/user.h> +#endif #ifdef HAVE_SYS_SYSCTL_H #include <sys/sysctl.h> #endif @@ -463,23 +466,33 @@ teco_memory_get_usage(void) #define NEED_POLL_THREAD -#elif defined(G_OS_UNIX) && defined(HAVE_SYSCTL) +#elif defined(G_OS_UNIX) && defined(HAVE_SYSCONF) && defined(HAVE_SYSCTL) /* * Practically only for FreeBSD. * - * FIXME: Is this even critical or can we link in dlmalloc? + * The malloc replacement via dlmalloc also works on FreeBSD, + * but this implementation has been benchmarked to be up to 4 times faster + * (but only if we poll in a separate thread). + * On the downside, this will of course be less precise. */ static gsize teco_memory_get_usage(void) { + static long page_size = 0; + + if (G_UNLIKELY(!page_size)) + page_size = sysconf(_SC_PAGESIZE); + struct kinfo_proc procstk; size_t len = sizeof(procstk); int pidinfo[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; - sysctl(pidinfo, G_N_ELEMENTS(pidinfo), &procstk, &len, NULL, 0); + if (G_UNLIKELY(sysctl(pidinfo, G_N_ELEMENTS(pidinfo), + &procstk, &len, NULL, 0) < 0)) + return 0; - return procstk.ki_rssize; // FIXME: Which unit? + return procstk.ki_rssize * page_size; } #define NEED_POLL_THREAD |