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:
phk 2002-11-01 18:58:12 +00:00
parent 5d54248492
commit b798527b8d
2 changed files with 17 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);