diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-05-14 04:58:28 +0300 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-05-14 04:58:28 +0300 |
commit | 96b60afcf4293db2e6841b58500fd12d0344e75f (patch) | |
tree | a0aecd475c1f9e73ff02f406d4ca761bcc2615f7 /src/memory.c | |
parent | 6333590e0822ccb5e707e4784bfd19ecbae60840 (diff) | |
download | sciteco-96b60afcf4293db2e6841b58500fd12d0344e75f.tar.gz |
FreeBSD: fixed the poll-thread memory limiting implementation - it's the default now
* On FreeBSD both the dlmalloc replacement and poll-thread via sysctl() work
but the poll-thread has been benchmarked to be significantly faster,
at least on my machine.
You can still ./configure --enable-malloc-replacement of course.
* Interestingly, the RSS of the process visible via htop does not decrease
after OOMs or command-line terminations - with neither of the implementations.
Diffstat (limited to 'src/memory.c')
-rw-r--r-- | src/memory.c | 21 |
1 files changed, 17 insertions, 4 deletions
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 |