int main(int argc, char **argv) { /* Set up benchmark */ unsigned long iterations, rechance, i; int action, target, size; void * lowbreak, * highbreak; #ifdef MMAP lowbreak = endHeap(); #else lowbreak = (void *) sbrk(0); #endif if(argc != 3) { printf("Usage: %s iterations realloc\n", argv[0]); return 1; } else { iterations = atoi(argv[1]); rechance = atoi(argv[2]); } for(i = 0; i < NUM_BLOCKS; i++) { blocks[i] = NULL; } /* Start of benchmark */ for(i = 0; i < iterations; i++) { /* Random action and target */ action = rand() % 10; target = rand() % NUM_BLOCKS; size = rand() % MAX_ALLOC; /* Realloc */ if(action < rechance) { blocks[target] = realloc(blocks[target], size); /* Malloc/Free */ } else { /* Malloc */ if(blocks[target] == NULL) { blocks[target] = malloc(size); /* Free */ } else { free(blocks[target]); blocks[target] = NULL; } } } #ifdef MMAP highbreak = endHeap(); #else highbreak = (void *) sbrk(0); #endif printf("%d", (unsigned)(highbreak - lowbreak)); return 0; }
int main(int argc, char *argv[]) { int i, return_value, size; struct timeval start, end, diff;/* Holds start, end and run time */ void *memstart, *memend; if(argc <2) size = MAXSIZE; else { size = atoi(argv[1]); } if(size>MAXSIZE) size = MAXSIZE; fprintf(stderr, "size: %d. ", size); return_value = gettimeofday(&start, NULL); if(return_value == -1){ perror("Couldn't get time"); exit(1); } #ifdef MMAP memstart = endHeap(); #else memstart = (void *)sbrk(0); #endif /* Malloc ints */ for(i=0; i<size; i++) { ints[i] = malloc(sizeof(int)); } /* Malloc longs */ for(i=0; i<size; i++) { longs[i] = malloc(sizeof(long)); } /* Free everything */ for(i=0; i<size; i++) { free(longs[i]); free(ints[i]); } #ifdef MMAP memend = endHeap(); #else memend = (void *) sbrk(0); #endif return_value = gettimeofday(&end, NULL); if(return_value == -1){ perror("Couldn't get time"); exit(1); } diff = time_passed(start, end); fprintf(stderr, "Exe time: %d:%.6d s\nMemory consumed: %ld\n", (int)diff.tv_sec, (int)diff.tv_usec, (unsigned long)(memend-memstart)); return 0; }
int main(int argc, char * argv[]){ unsigned memory_size; while(scanf("%u", &memory_size) == 1) { /*fprintf(stderr, "block size: %u, pid: %u\n", memory_size, getpid());*/ /* used with MANY_RUNS.sh */ int i,j; unsigned pagesize = getpagesize(); char *a[SIZE]; void *memory_start, *memory_end; for(j = 0; j < SIZE/2; ++j) { a[j] = malloc(memory_size*pagesize*10); free(a[j]); } for(j = SIZE/2+1; j < SIZE; ++j) { a[j] = malloc(memory_size*pagesize*3); free(a[j]); } memory_start = endHeap(); for(i = 0; i < TIMES; ++i) { for(j = 0; j < SIZE; ++j) { a[j] = malloc(pagesize*3); } } memory_end = endHeap(); for(j = 0; j < SIZE; j++) { free(a[j]); } /* block size, memory and time */ fprintf(stdout,"%u %u\n", memory_size,(unsigned)(memory_end - memory_start)/1000); #ifndef SYSTEM reset_free_list(); #endif /* block size and time */ /*fprintf(stderr,"%u %d\n", memory_size,msec);*/ /* block size and memory */ /*fprintf(stderr,"%u %u\n", memory_size,(unsigned)(endHeap() - memory_start));*/ } return 0; }
void *getbreak(void){ #ifdef MMAP return endHeap(); #else return (void *)sbrk(0); #endif };
/** * Returns the current heap end position. */ void * getEndHeap(){ #ifdef STRATEGY /* We're testing custom malloc, use endHeap */ return endHeap(); #else /* We're testing built-in malloc, use sbrk */ return (void *) sbrk(0); #endif }
int main(int argc, char *argv[]) { int i, maxMem=0; void *start, *end; char *progname; if (argc > 0) progname = argv[0]; else progname = ""; MESSAGE("-- This test checks malloc(), free() and realloc()\n"); srand((unsigned int)time(NULL)); #ifdef CUSTOM_MALLOC MESSAGE("Using custom malloc\n"); #else MESSAGE("Using system malloc\n"); #endif #ifdef MMAP start = endHeap(); #else start = (void *)sbrk(0); #endif for(i=0;i<MAXPOSTS;i++) { memPosts[i].size = rand()%(MAXSIZE/2); startMeasure(); memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double)); stopMeasure(); if ( memPosts[i].size == 0 && memPosts[i].ptr!= NULL ) /* MESSAGE("* ERROR: malloc doesn't return NULL pointer on zero size\n");*/ ; else if( memPosts[i].size && memPosts[i].ptr == NULL ) { MESSAGE("* ERROR: malloc returned NULL on non-zero size request\n"); } else if( memPosts[i].ptr != NULL ) memPosts[i].ptr[0] = (double)3.14; } calcMemUsage(&maxMem); for(i=0;i<MAXITERS;i++) { int index; index = rand()%MAXPOSTS; if(ALLOCATED(index)) { if(rand()%5 < 3) { if(memPosts[index].ptr[0] != (double)3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); memPosts[index].size = rand()%MAXSIZE; startMeasure(); memPosts[index].ptr = (double*) realloc(memPosts[index].ptr, memPosts[index].size*sizeof(double)); stopMeasure(); if(memPosts[index].size && memPosts[index].ptr[0] != (double)3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); if(memPosts[index].size) memPosts[index].ptr[0] = (double)3.14; } else { if(memPosts[index].ptr[0] != (double)3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); startMeasure(); free(memPosts[index].ptr); stopMeasure(); memPosts[index].size = 0; } } else { memPosts[index].size = rand()%MAXSIZE; startMeasure(); memPosts[index].ptr = (double*) malloc(memPosts[index].size*sizeof(double)); stopMeasure(); if(memPosts[index].size) memPosts[index].ptr[0] = (double)3.14; } calcMemUsage(&maxMem); } #ifdef MMAP end = endHeap(); #else end = (void *) sbrk(0); #endif fprintf(stderr, "%s: Max memory allocated %d\n%s: Memory consumed %ld\n%s: Time in free/malloc/realloc: %f\n", progname, maxMem, progname, (unsigned long)(end-start), progname, getMeasuredTime()); return 0; }
int main(int argc, char *argv[]){ int i,j; float worst; char *a[SIZE], *b[BIGSIZE]; size_t pagesize; void * lowbreak, *highbreak, *maxbreak = 0; char *progname; if (argc > 0) progname = argv[0]; else progname = ""; /* to generate big memory refs later */ pagesize = sysconf(_SC_PAGESIZE); MESSAGE("Testing memory utility\n"); #ifdef MMAP lowbreak = endHeap(); #else lowbreak = (void *) sbrk(0); #endif MESSAGE("Getting small pieces of memory\n"); for(i = 0; i < TIMES; i++){ for(j = 0; j < SIZE; j++){ a[j] = malloc(SMALLSTRING); } #ifdef MMAP highbreak = endHeap(); #else highbreak = (void *) sbrk(0); #endif maxbreak = MAX(maxbreak, highbreak); for(j = 0; j < SIZE; j++){ free(a[j]); } if ( i % 10 == 0 ) fprintf(stderr, "%s: Using total of 0x%x of memory\n", progname, (unsigned) (highbreak - lowbreak)); } worst = (maxbreak - lowbreak)/ (SMALLSTRING * SIZE * 1.0); fprintf(stderr, "%s: Using %2.2f times worst case calculation\n", progname, worst); if ( worst > 2 ) { MESSAGE("* ERROR: Test indicates excessive memory usage\n"); } else { MESSAGE("Small memory handled OK\n"); } MESSAGE("Getting big blocks of memory\n"); for(i = 0; i < BIGTIMES; i++){ for(j = 0; j < BIGSIZE; j++){ b[j] = malloc(TIMESPAGE*pagesize); } #ifdef MMAP highbreak = endHeap(); #else highbreak = (void *) sbrk(0); #endif maxbreak = MAX(maxbreak, highbreak); for(j = 0; j < BIGSIZE; j++){ free(b[j]); } fprintf(stderr, "%s: Using total of 0x%x of memory\n", progname, (unsigned) (highbreak - lowbreak)); } MESSAGE("Allocations versus worst case memory usage:\n"); worst = (maxbreak - lowbreak)/ (BIGSIZE * pagesize * TIMESPAGE * 1.0); fprintf(stderr, "%s: Using %2.2f times worst case calculation\n", progname, worst); if ( worst > 2 ) { MESSAGE("* ERROR: Test indicates excessive memory usage\n"); } else { MESSAGE("Big memory handled OK\n"); } return 0; }