2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
#include <AK/Optional.h>
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2019-06-11 11:10:55 +00:00
|
|
|
#include <stdio.h>
|
2020-03-08 11:05:14 +00:00
|
|
|
#include <string.h>
|
2019-06-11 11:10:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-12-20 23:09:48 +00:00
|
|
|
static void usage()
|
2019-06-11 11:10:55 +00:00
|
|
|
{
|
2020-08-15 18:28:15 +00:00
|
|
|
printf("usage: allocate [number [unit (B/KiB/MiB)]]\n");
|
2019-06-11 11:10:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-08-15 18:28:15 +00:00
|
|
|
enum class Unit {
|
|
|
|
Bytes,
|
|
|
|
KiB,
|
|
|
|
MiB,
|
|
|
|
};
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2019-06-22 14:13:47 +00:00
|
|
|
int count = 50;
|
2020-08-15 18:28:15 +00:00
|
|
|
auto unit = Unit::MiB;
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
if (argc >= 2) {
|
2020-06-12 19:07:52 +00:00
|
|
|
auto number = String(argv[1]).to_uint();
|
|
|
|
if (!number.has_value()) {
|
2019-06-11 11:10:55 +00:00
|
|
|
usage();
|
|
|
|
}
|
2020-06-12 19:07:52 +00:00
|
|
|
count = number.value();
|
2019-06-11 11:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc >= 3) {
|
|
|
|
if (strcmp(argv[2], "B") == 0)
|
2020-07-25 14:00:26 +00:00
|
|
|
unit = Unit::Bytes;
|
2020-08-15 18:28:15 +00:00
|
|
|
else if (strcmp(argv[2], "KiB") == 0)
|
|
|
|
unit = Unit::KiB;
|
|
|
|
else if (strcmp(argv[2], "MiB") == 0)
|
|
|
|
unit = Unit::MiB;
|
2019-06-11 11:10:55 +00:00
|
|
|
else
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (unit) {
|
2020-07-25 14:00:26 +00:00
|
|
|
case Unit::Bytes:
|
2019-06-11 11:10:55 +00:00
|
|
|
break;
|
2020-08-15 18:28:15 +00:00
|
|
|
case Unit::KiB:
|
|
|
|
count *= KiB;
|
2019-06-11 11:10:55 +00:00
|
|
|
break;
|
2020-08-15 18:28:15 +00:00
|
|
|
case Unit::MiB:
|
|
|
|
count *= MiB;
|
2019-06-11 11:10:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ElapsedTimer timer;
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
printf("allocating memory (%d bytes)...\n", count);
|
|
|
|
timer.start();
|
|
|
|
char* ptr = (char*)malloc(count);
|
|
|
|
if (!ptr) {
|
|
|
|
printf("failed.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("done in %dms\n", timer.elapsed());
|
|
|
|
|
2020-08-15 18:28:15 +00:00
|
|
|
auto pages = count / PAGE_SIZE;
|
2019-06-11 11:10:55 +00:00
|
|
|
auto step = pages / 10;
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ElapsedTimer timer2;
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
printf("writing one byte to each page of allocated memory...\n");
|
|
|
|
timer.start();
|
|
|
|
timer2.start();
|
|
|
|
for (int i = 0; i < pages; ++i) {
|
2020-08-15 18:28:15 +00:00
|
|
|
ptr[i * PAGE_SIZE] = 1;
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
if (i != 0 && (i % step) == 0) {
|
|
|
|
auto ms = timer2.elapsed();
|
|
|
|
if (ms == 0)
|
|
|
|
ms = 1;
|
|
|
|
|
2020-08-15 18:28:15 +00:00
|
|
|
auto bps = double(step * PAGE_SIZE) / (double(ms) / 1000);
|
2019-06-11 11:10:55 +00:00
|
|
|
|
2020-08-15 18:28:15 +00:00
|
|
|
printf("step took %dms (%fMiB/s)\n", ms, bps / MiB);
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
timer2.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("done in %dms\n", timer.elapsed());
|
|
|
|
|
|
|
|
printf("sleeping for ten seconds...\n");
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
printf("%d\n", i);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
printf("done.\n");
|
|
|
|
|
|
|
|
printf("freeing memory...\n");
|
|
|
|
timer.start();
|
|
|
|
free(ptr);
|
|
|
|
printf("done in %dms\n", timer.elapsed());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|