Serenity Operating System
at portability 70 lines 2.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Types.h> 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31#include <sys/mman.h> 32#include <unistd.h> 33 34int main(int argc, char** argv) 35{ 36 size_t limit = 0; 37 size_t bite_size = 0; 38 int interval = 0; 39 if (argc == 1) { 40 limit = 42 * MB; 41 bite_size = 1 * MB; 42 interval = 200000; 43 } else if (argc == 4) { 44 bite_size = atoi(argv[1]); 45 limit = atoi(argv[2]); 46 interval = atoi(argv[3]); 47 } else { 48 printf("usage: munch [bite_size limit interval]\n"); 49 return 1; 50 } 51 52 size_t munched = 0; 53 printf("Munching %zu bytes every %d ms, stopping at %zu\n", bite_size, interval / 1000, limit); 54 for (;;) { 55 auto* ptr = mmap(nullptr, bite_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); 56 if (ptr == MAP_FAILED) { 57 perror("mmap"); 58 return 1; 59 } 60 memset(ptr, 0, bite_size); 61 munched += bite_size; 62 printf("Allocated: %zu\n", munched); 63 if (limit && munched >= limit) { 64 printf("All done!\n"); 65 break; 66 } 67 usleep(interval); 68 } 69 return 0; 70}