6 #define CHUNK (128 * 1024)
8 void usage(const char *prog, FILE *out)
10 fprintf(out, "usage: %s allocsize\n", prog);
11 fprintf(out, " allocsize is kbytes, or number[KMGP] (P = pages)\n");
15 int main(int argc, char *argv[])
17 long long kbtotal = 0, kballoc;
18 int i, j, k, numchunk, alloc, sum, rc = 0;
23 kbtotal = strtoull(argv[1], &end, 0);
41 usage(argv[0], stderr);
46 if (argc != 2 || kbtotal == 0)
47 usage(argv[0], stderr);
49 numchunk = (kbtotal + CHUNK - 1) / CHUNK;
50 mem = calloc(numchunk, sizeof(*mem));
52 fprintf(stderr, "error allocating initial chunk array\n");
57 printf("[%d] allocating %lld kbytes in %u kbyte chunks\n",
58 getpid(), kbtotal, alloc);
59 for (i = kballoc = 0; i < numchunk && alloc > 0; i++, kballoc += alloc){
60 if (kbtotal - kballoc < alloc)
61 alloc = kbtotal - kballoc;
63 while (alloc > 0 && (mem[i] = malloc(alloc * 1024)) == NULL) {
64 fprintf(stderr, "malloc(%u) failed (%lld/%lld)\n",
65 alloc * 1024, kballoc, kbtotal);
71 printf("touching %p ([%lld-%lld]/%lld)\n", mem[i], kballoc,
72 kballoc + alloc - 1, kbtotal);
73 for (j = 0, tmp = mem[i]; j < alloc; j += 4) {
74 for (k = 0, sum = 0; k < 4095; k++, tmp++)
83 printf("touched %lld kbytes\n", kballoc);
86 printf("verifying %lld kbytes in %u kbyte chunks\n", kbtotal, alloc);
87 for (i = kballoc = 0; i < numchunk; i++, kballoc += alloc) {
88 if (kbtotal - kballoc < alloc)
89 alloc = kbtotal - kballoc;
93 printf("verifying %p (%lld/%lld)\n",
94 tmp, kballoc, kbtotal);
95 for (j = 0; j < alloc; j += 4) {
96 for (k = 0, sum = 0; k < 4095; k++, tmp++)
99 fprintf(stderr, "sum %x != %x at %p\n",
100 *tmp, sum, tmp - 4092);
106 printf("verified %lld kbytes\n", kballoc);