Introduce malloc_last_fail() which returns the number of seconds since
malloc(9) failed last time. This is intended to help code adjust memory usage to the current circumstances. A typical use could be: if (malloc_last_fail() < 60) reduce_cache_by_one();
This commit is contained in:
parent
3ce3aae31e
commit
27173bd0dd
@ -46,6 +46,7 @@
|
||||
#include <sys/vmmeter.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <vm/vm.h>
|
||||
#include <vm/pmap.h>
|
||||
@ -134,6 +135,16 @@ static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
|
||||
|
||||
static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS);
|
||||
|
||||
/* time_uptime of last malloc(9) failure */
|
||||
static time_t t_malloc_fail;
|
||||
|
||||
int
|
||||
malloc_last_fail(void)
|
||||
{
|
||||
|
||||
return (time_uptime - t_malloc_fail);
|
||||
}
|
||||
|
||||
/*
|
||||
* malloc:
|
||||
*
|
||||
@ -191,6 +202,11 @@ malloc(size, type, flags)
|
||||
ksp->ks_maxused = ksp->ks_memuse;
|
||||
|
||||
mtx_unlock(&ksp->ks_mtx);
|
||||
if (!(flags & M_NOWAIT))
|
||||
KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
|
||||
if (va == NULL) {
|
||||
t_malloc_fail = time_uptime;
|
||||
}
|
||||
return ((void *) va);
|
||||
}
|
||||
|
||||
|
@ -108,6 +108,7 @@ void *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
|
||||
void free(void *addr, struct malloc_type *type);
|
||||
void *malloc(unsigned long size, struct malloc_type *type, int flags);
|
||||
void malloc_init(void *);
|
||||
int malloc_last_fail(void);
|
||||
void malloc_uninit(void *);
|
||||
void *realloc(void *addr, unsigned long size, struct malloc_type *type,
|
||||
int flags);
|
||||
|
Loading…
Reference in New Issue
Block a user