Serenity Operating System
at hosted 125 lines 3.5 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/String.h> 28#include <LibCore/ElapsedTimer.h> 29#include <stdio.h> 30#include <string.h> 31#include <unistd.h> 32 33void usage(void) 34{ 35 printf("usage: allocate [number [unit (B/KB/MB)]]\n"); 36 exit(1); 37} 38 39enum Unit { Bytes, KiloBytes, MegaBytes }; 40 41int main(int argc, char** argv) 42{ 43 int count = 50; 44 Unit unit = MegaBytes; 45 46 if (argc >= 2) { 47 bool ok; 48 count = String(argv[1]).to_uint(ok); 49 if (!ok) { 50 usage(); 51 } 52 } 53 54 if (argc >= 3) { 55 if (strcmp(argv[2], "B") == 0) 56 unit = Bytes; 57 else if (strcmp(argv[2], "KB") == 0) 58 unit = KiloBytes; 59 else if (strcmp(argv[2], "MB") == 0) 60 unit = MegaBytes; 61 else 62 usage(); 63 } 64 65 switch (unit) { 66 case Bytes: 67 break; 68 case KiloBytes: 69 count *= 1024; 70 break; 71 case MegaBytes: 72 count *= 1024 * 1024; 73 break; 74 } 75 76 Core::ElapsedTimer timer; 77 78 printf("allocating memory (%d bytes)...\n", count); 79 timer.start(); 80 char* ptr = (char*)malloc(count); 81 if (!ptr) { 82 printf("failed.\n"); 83 return 1; 84 } 85 printf("done in %dms\n", timer.elapsed()); 86 87 auto pages = count / 4096; 88 auto step = pages / 10; 89 90 Core::ElapsedTimer timer2; 91 92 printf("writing one byte to each page of allocated memory...\n"); 93 timer.start(); 94 timer2.start(); 95 for (int i = 0; i < pages; ++i) { 96 ptr[i * 4096] = 1; 97 98 if (i != 0 && (i % step) == 0) { 99 auto ms = timer2.elapsed(); 100 if (ms == 0) 101 ms = 1; 102 103 auto bps = double(step * 4096) / (double(ms) / 1000); 104 105 printf("step took %dms (%fMB/s)\n", ms, bps / 1024 / 1024); 106 107 timer2.start(); 108 } 109 } 110 printf("done in %dms\n", timer.elapsed()); 111 112 printf("sleeping for ten seconds...\n"); 113 for (int i = 0; i < 10; i++) { 114 printf("%d\n", i); 115 sleep(1); 116 } 117 printf("done.\n"); 118 119 printf("freeing memory...\n"); 120 timer.start(); 121 free(ptr); 122 printf("done in %dms\n", timer.elapsed()); 123 124 return 0; 125}