Created
December 8, 2014 22:33
-
-
Save dobrokot/3321085c1b1daa901894 to your computer and use it in GitHub Desktop.
allocate memory to test system behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// malloc some number of gigabytes, to test impact on system | |
// g++ -O3 alloc-mem-test.cpp && ./a.out | |
size_t WIDTH = 75; | |
int progress_bar(size_t i, size_t size) { | |
return i * WIDTH / size; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "argument expected, size in Gb\n"); | |
return 1; | |
} | |
size_t size_gb = atoi(argv[1]); | |
if (size_gb == 0) { | |
fprintf(stderr, "invalid size\n"); | |
return 1; | |
} | |
size_t size = size_gb * 1024 * 1024 * 1024; | |
char *p = (char*)malloc(size); | |
if (!p) { | |
fprintf(stderr, "malloc returns null\n"); | |
return 1; | |
} | |
printf("["); | |
for (int i = 0; i < WIDTH-2; ++i) | |
printf("-"); | |
printf("]\n"); | |
int npass = 100; | |
for (int pass = 0; pass < npass; ++pass) { | |
printf("pass %d/%d\n", pass+1, npass); | |
const size_t page_size = 4096; | |
for (size_t i = 0; i < size; i += page_size) { | |
p[i] = 0; | |
if (progress_bar(i - page_size, size) != progress_bar(i, size)) { | |
printf("."); | |
fflush(stdout); | |
} | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment