Serenity Operating System
1/*
2 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
3 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/JsonArray.h>
9#include <AK/JsonObject.h>
10#include <LibCore/File.h>
11#include <LibCore/System.h>
12#include <LibMain/Main.h>
13
14ErrorOr<int> serenity_main(Main::Arguments)
15{
16 TRY(Core::System::pledge("stdio rpath"));
17 TRY(Core::System::unveil("/sys/kernel/interrupts", "r"));
18 TRY(Core::System::unveil(nullptr, nullptr));
19
20 auto proc_interrupts = TRY(Core::File::open("/sys/kernel/interrupts"sv, Core::File::OpenMode::Read));
21
22 TRY(Core::System::pledge("stdio"));
23
24 auto file_contents = TRY(proc_interrupts->read_until_eof());
25 auto json = TRY(JsonValue::from_string(file_contents));
26
27 auto cpu_count = json.as_array().at(0).as_object().get_array("per_cpu_call_counts"sv)->size();
28
29 out(" "sv);
30 for (size_t i = 0; i < cpu_count; ++i) {
31 out("{:>10}", DeprecatedString::formatted("CPU{}", i));
32 }
33 outln("");
34
35 json.as_array().for_each([cpu_count](JsonValue const& value) {
36 auto& handler = value.as_object();
37 auto purpose = handler.get_deprecated_string("purpose"sv).value_or({});
38 auto interrupt = handler.get_deprecated_string("interrupt_line"sv).value_or({});
39 auto controller = handler.get_deprecated_string("controller"sv).value_or({});
40 auto call_counts = handler.get_array("per_cpu_call_counts"sv).value();
41
42 out("{:>4}: ", interrupt);
43
44 for (size_t i = 0; i < cpu_count; ++i)
45 out("{:>10}", call_counts[i].to_deprecated_string());
46
47 outln(" {:10} {:30}", controller, purpose);
48 });
49
50 return 0;
51}