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