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
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-06-12 19:07:52 +00:00
|
|
|
#include <AK/Optional.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2021-11-25 21:04:13 +00:00
|
|
|
#include <LibMain/Main.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
|
|
|
{
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("usage: allocate [number [unit (B/KiB/MiB)]]");
|
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
|
|
|
|
2021-11-25 21:04:13 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-06-11 11:10:55 +00:00
|
|
|
{
|
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
|
|
|
|
2021-11-25 21:04:13 +00:00
|
|
|
if (arguments.argc >= 2) {
|
|
|
|
auto number = arguments.strings[1].to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-25 21:04:13 +00:00
|
|
|
if (arguments.argc >= 3) {
|
|
|
|
if (arguments.strings[2] == "B")
|
2020-07-25 14:00:26 +00:00
|
|
|
unit = Unit::Bytes;
|
2021-11-25 21:04:13 +00:00
|
|
|
else if (arguments.strings[2] == "KiB")
|
2020-08-15 18:28:15 +00:00
|
|
|
unit = Unit::KiB;
|
2021-11-25 21:04:13 +00:00
|
|
|
else if (arguments.strings[2] == "MiB")
|
2020-08-15 18:28:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("allocating memory ({} bytes)...", count);
|
2021-09-12 15:47:57 +00:00
|
|
|
auto timer = Core::ElapsedTimer::start_new();
|
2019-06-11 11:10:55 +00:00
|
|
|
char* ptr = (char*)malloc(count);
|
|
|
|
if (!ptr) {
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("failed.");
|
2019-06-11 11:10:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("done in {}ms", timer.elapsed());
|
2019-06-11 11:10:55 +00:00
|
|
|
|
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;
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("writing one byte to each page of allocated memory...");
|
2019-06-11 11:10:55 +00:00
|
|
|
timer.start();
|
2021-09-12 15:47:57 +00:00
|
|
|
auto timer2 = Core::ElapsedTimer::start_new();
|
2019-06-11 11:10:55 +00:00
|
|
|
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
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("step took {}ms ({}MiB/s)", ms, bps / MiB);
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
timer2.start();
|
|
|
|
}
|
|
|
|
}
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("done in {}ms", timer.elapsed());
|
2019-06-11 11:10:55 +00:00
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("sleeping for ten seconds...");
|
2019-06-11 11:10:55 +00:00
|
|
|
for (int i = 0; i < 10; i++) {
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{}", i);
|
2019-06-11 11:10:55 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("done.");
|
2019-06-11 11:10:55 +00:00
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("freeing memory...");
|
2019-06-11 11:10:55 +00:00
|
|
|
timer.start();
|
|
|
|
free(ptr);
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("done in {}ms", timer.elapsed());
|
2019-06-11 11:10:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|