head 1.4; access; symbols; locks; strict; comment @ * @; 1.4 date 2000.11.03.17.19.49; author wessels; state Exp; branches; next 1.3; 1.3 date 2000.11.03.17.19.26; author wessels; state Exp; branches; next 1.2; 1.2 date 2000.04.16.07.53.20; author wessels; state Exp; branches; next 1.1; 1.1 date 2000.04.16.07.30.25; author wessels; state Exp; branches; next ; desc @@ 1.4 log @!gc @ text @/* * $Id: test_malloc.c,v 1.3 2000/11/03 17:19:26 wessels Exp wessels $ */ /* * test_malloc * * The idea is to call malloc and free very often while maintaining a "constant" * sized total memory area. The probability of calling malloc or free is * proportional to the size of the memory area. Internally I track the sum of * all allocated blocks. Due to malloc overheads, the process size is * significantly larger. * * Each malloc'd block must be at least 4 bytes. The size of the block is stored * in the first 4 bytes. */ #include #include #include #include #include #include #include #define URANDOM "/dev/urandom" #define USAGE "usage: %s -t mbytes\n" typedef struct _hash hash; typedef struct _hashitem hashitem; struct _hashitem { void *key; hashitem *next; void *data; }; struct _hash { hashitem **buckets; int nbuckets; int nelem; }; extern hash *hashCreate(int); extern void *hashLookup(hash *, void *); extern void hashInsert(hash *, void *, void *); extern void hashDelete(hash *, void *); extern void *hashRandom(hash *); int checkdup = 0; void * hashRandom(hash * h) { int b; hashitem *i; do { b = random() % h->nbuckets; i = h->buckets[b]; } while (NULL == i); while (i->next) i = i->next; return i->data; } hash * hashCreate(int n) { hash *h = calloc(1, sizeof(*h)); h->nbuckets = n; h->buckets = calloc(h->nbuckets, sizeof(*h->buckets)); return h; } void * hashLookup(hash * h, void *key) { int b = (int) key % h->nbuckets; hashitem *i; for (i = h->buckets[b]; i; i = i->next) if (key == i->key) return i->data; return NULL; } void hashInsert(hash * h, void *data, void *key) { int b = (int) key % h->nbuckets; hashitem *i; if (checkdup) for (i = h->buckets[b]; i; i = i->next) assert(key != i->key); i = calloc(1, sizeof(*i)); i->key = key; i->data = data; i->next = h->buckets[b]; h->buckets[b] = i; h->nelem++; } void hashDelete(hash * h, void *key) { int b = (int) key % h->nbuckets; hashitem **I; hashitem *i; for (I = &h->buckets[b]; *I; I = &(*I)->next) { if ((*I)->key != key) continue; i = *I; *I = i->next; free(i); break; } h->nelem--; } int main(int argc, char *argv[]) { hash *h = hashCreate(65537); int cursize = 0; int maxsize; double p; double x; int s; void *y; const char *progname = strdup(argv[0]); int nalloc = 0; int nfree = 0; int nop = 0; int rndfd = -1; int ch; while ((ch = getopt(argc, argv, "td")) != -1) { switch (ch) { case 't': rndfd = open(URANDOM, O_RDONLY); if (rndfd < 0) { perror(URANDOM); return 1; } break; case 'd': checkdup = 1; break; case '?': default: fprintf(stderr, USAGE, progname); return 1; break; } } argc -= optind; argv += optind; if (1 != argc) { fprintf(stderr, USAGE, progname); return 1; } srandom(time(NULL)); maxsize = atoi(argv[0]) << 21; assert(0 < maxsize); for (;;) { p = (double) random() / (double) 0x7fffffff; x = (double) cursize / (double) maxsize; if (p < x) { y = hashRandom(h); assert(y); memcpy(&s, y, sizeof(s)); assert(s > 0 && s < 0x10000); cursize -= s; hashDelete(h, y); free(y); nfree++; } else { do { s = random() & 0x7FC; } while (s < sizeof(s)); y = malloc(s); if (rndfd > -1) read(rndfd, y, s); memcpy(y, &s, sizeof(s)); hashInsert(h, y, y); cursize += s; nalloc++; } nop++; if ((nop & 0xFF) == 0) if (rndfd > -1 || 0 == (nop & 0x3FFF)) printf("%d MB, %d ptrs, %d alloc, %d free\n", cursize >> 20, h->nelem, nalloc, nfree); } } @ 1.3 log @NPR bug @ text @d2 1 a2 1 * $Id: test_malloc.c,v 1.2 2000/04/16 07:53:20 wessels Exp wessels $ d24 1 @ 1.2 log @added -t option to touch allocated blocks with random data added -d option to check hash for adding duplicate pointers @ text @d2 1 a2 1 * $Id: test_malloc.c,v 1.1 2000/04/16 07:30:25 wessels Exp $ d128 1 d148 1 a148 1 fprintf(stderr, USAGE, argv[0]); d157 1 a157 1 fprintf(stderr, USAGE, argv[0]); @ 1.1 log @Initial revision @ text @d2 1 a2 1 * $Id$ d21 1 d25 3 d49 2 d89 5 a93 1 hashitem *i = calloc(1, sizeof(*i)); d131 26 a156 3 srandom(time(NULL)); if (2 != argc) { fprintf(stderr, "usage: %s mbytes\n", argv[0]); d159 2 a160 1 maxsize = atoi(argv[1]) << 21; d176 1 a176 1 s = random() & 0xFFF; d179 2 d187 4 a190 3 if ((nop & 0xFFF) == 0) printf("%d MB, %d ptrs, %d alloc, %d free\n", cursize >> 20, h->nelem, nalloc, nfree); @