mempool_test.c revision dafcb997e390efa4423883dafd100c975c4095d6
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Copyright (C) 1999-2001 Internet Software Consortium.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Permission to use, copy, modify, and distribute this software for any
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * purpose with or without fee is hereby granted, provided that the above
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * copyright notice and this permission notice appear in all copies.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * PERFORMANCE OF THIS SOFTWARE.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* $Id: mempool_test.c,v 1.15 2004/03/05 04:58:39 marka Exp $ */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#include <config.h>
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#include <isc/mem.h>
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#include <isc/util.h>
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterisc_mem_t *mctx;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterint
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostermain(int argc, char *argv[]) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster void *items1[50];
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster void *items2[50];
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster void *tmp;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_t *mp1, *mp2;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster unsigned int i, j;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mutex_t lock;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster UNUSED(argc);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster UNUSED(argv);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mem_debugging = ISC_MEM_DEBUGRECORD;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster mctx = NULL;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster mp1 = NULL;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(isc_mempool_create(mctx, 24, &mp1) == ISC_R_SUCCESS);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster mp2 = NULL;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(isc_mempool_create(mctx, 31, &mp2) == ISC_R_SUCCESS);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_associatelock(mp1, &lock);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_associatelock(mp2, &lock);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mem_stats(mctx, stderr);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_setfreemax(mp1, 10);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_setfillcount(mp1, 10);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_setmaxalloc(mp1, 30);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Allocate 30 items from the pool. This is our max.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster for (i = 0; i < 30; i++) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster items1[i] = isc_mempool_get(mp1);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(items1[i] != NULL);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Try to allocate one more. This should fail.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster tmp = isc_mempool_get(mp1);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(tmp == NULL);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Free the first 11 items. Verify that there are 10 free items on
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * the free list (which is our max).
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster for (i = 0; i < 11; i++) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_put(mp1, items1[i]);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster items1[i] = NULL;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(isc_mempool_getfreecount(mp1) == 10);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(isc_mempool_getallocated(mp1) == 19);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mem_stats(mctx, stderr);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Now, beat up on mp2 for a while. Allocate 50 items, then free
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * them, then allocate 50 more, etc.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_setfreemax(mp2, 25);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_setfillcount(mp2, 25);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster for (j = 0; j < 5000; j++) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster for (i = 0; i < 50; i++) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster items2[i] = isc_mempool_get(mp2);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RUNTIME_CHECK(items2[i] != NULL);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster for (i = 0; i < 50; i++) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_put(mp2, items2[i]);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster items2[i] = NULL;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Free all the other items and blow away this pool.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster for (i = 11; i < 30; i++) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_put(mp1, items1[i]);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster items1[i] = NULL;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
947af33fa9a6307636167a32e447bb4f9a12cd2bMareks Malnacs isc_mempool_destroy(&mp1);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mem_stats(mctx, stderr);
947af33fa9a6307636167a32e447bb4f9a12cd2bMareks Malnacs
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mempool_destroy(&mp2);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mem_stats(mctx, stderr);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster isc_mem_destroy(&mctx);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster DESTROYLOCK(&lock);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster return (0);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster}
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster