2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-06-25 21:52:47 +03:00
|
|
|
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2023-04-09 16:27:30 +03:00
|
|
|
#include <AK/Format.h>
|
2020-06-12 22:07:52 +03:00
|
|
|
#include <AK/Optional.h>
|
2023-06-25 21:52:47 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2021-11-26 00:04:13 +03:00
|
|
|
#include <LibMain/Main.h>
|
2019-06-11 14:10:55 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-08-15 21:28:15 +03:00
|
|
|
enum class Unit {
|
|
|
|
Bytes,
|
|
|
|
KiB,
|
|
|
|
MiB,
|
2023-06-25 21:52:47 +03:00
|
|
|
GiB,
|
2020-08-15 21:28:15 +03:00
|
|
|
};
|
2019-06-11 14:10:55 +03:00
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-06-11 14:10:55 +03:00
|
|
|
{
|
2023-06-25 21:52:47 +03:00
|
|
|
size_t iterations_count = 10;
|
|
|
|
size_t allocation_size = 100;
|
|
|
|
auto unit = Unit::Bytes;
|
|
|
|
StringView chosen_unit {};
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(chosen_unit, "Allocation's Size Unit in base 2 (B, KiB, MiB, GiB)", "unit", 'u', "unit");
|
|
|
|
args_parser.add_option(iterations_count, "Number of seconds to sleep before freeing memory", "sleep-time", 'n', "seconds");
|
|
|
|
args_parser.add_positional_argument(allocation_size, "Allocation Size", "size", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
|
|
|
if (!chosen_unit.is_null()) {
|
|
|
|
if (chosen_unit == "B"sv) {
|
2020-07-25 17:00:26 +03:00
|
|
|
unit = Unit::Bytes;
|
2023-06-25 21:52:47 +03:00
|
|
|
} else if (chosen_unit == "KiB"sv) {
|
2020-08-15 21:28:15 +03:00
|
|
|
unit = Unit::KiB;
|
2023-06-25 21:52:47 +03:00
|
|
|
} else if (chosen_unit == "MiB") {
|
2020-08-15 21:28:15 +03:00
|
|
|
unit = Unit::MiB;
|
2023-06-25 21:52:47 +03:00
|
|
|
} else if (chosen_unit == "GiB") {
|
|
|
|
unit = Unit::GiB;
|
|
|
|
} else {
|
|
|
|
args_parser.print_usage(stderr, arguments.strings[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-06-11 14:10:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2023-06-25 21:52:47 +03:00
|
|
|
allocation_size *= KiB;
|
2019-06-11 14:10:55 +03:00
|
|
|
break;
|
2020-08-15 21:28:15 +03:00
|
|
|
case Unit::MiB:
|
2023-06-25 21:52:47 +03:00
|
|
|
allocation_size *= MiB;
|
|
|
|
break;
|
|
|
|
case Unit::GiB:
|
|
|
|
allocation_size *= GiB;
|
2019-06-11 14:10:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-06-25 21:52:47 +03:00
|
|
|
outln("allocating memory ({} bytes)...", allocation_size);
|
2021-09-12 18:47:57 +03:00
|
|
|
auto timer = Core::ElapsedTimer::start_new();
|
2023-06-25 21:52:47 +03:00
|
|
|
auto* ptr = reinterpret_cast<char*>(malloc(allocation_size));
|
2019-06-11 14:10:55 +03:00
|
|
|
if (!ptr) {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("failed.");
|
2019-06-11 14:10:55 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2023-05-24 15:33:12 +03:00
|
|
|
outln("done in {}ms", timer.elapsed_milliseconds());
|
2019-06-11 14:10:55 +03:00
|
|
|
|
2023-06-25 21:52:47 +03:00
|
|
|
size_t pages_count = allocation_size / PAGE_SIZE;
|
|
|
|
auto step = pages_count / 10;
|
2019-06-11 14:10:55 +03:00
|
|
|
|
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();
|
2023-06-25 21:52:47 +03:00
|
|
|
for (size_t page_index = 0; page_index < pages_count; ++page_index) {
|
|
|
|
ptr[page_index * PAGE_SIZE] = 1;
|
2019-06-11 14:10:55 +03:00
|
|
|
|
2023-06-25 21:52:47 +03:00
|
|
|
if (page_index != 0 && (page_index % step) == 0) {
|
2023-05-24 15:33:12 +03:00
|
|
|
auto ms = timer2.elapsed_milliseconds();
|
2019-06-11 14:10:55 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 15:33:12 +03:00
|
|
|
outln("done in {}ms", timer.elapsed_milliseconds());
|
2019-06-11 14:10:55 +03:00
|
|
|
|
2023-06-25 21:52:47 +03:00
|
|
|
outln("sleeping for {} seconds...", iterations_count);
|
|
|
|
for (unsigned iteration_index = 0; iteration_index < iterations_count; iteration_index++) {
|
|
|
|
outln("{}", iteration_index);
|
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);
|
2023-05-24 15:33:12 +03:00
|
|
|
outln("done in {}ms", timer.elapsed_milliseconds());
|
2019-06-11 14:10:55 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|