Serenity Operating System
1/*
2 * Copyright (c) 2022, Undefine <undefine@undefine.pl>
3 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/Format.h>
9#include <AK/JsonArray.h>
10#include <AK/JsonObject.h>
11#include <AK/NumberFormat.h>
12#include <LibCore/File.h>
13#include <LibCore/System.h>
14#include <LibMain/Main.h>
15
16static void print_cache_info(StringView description, JsonObject const& cache_object)
17{
18 outln("\t{}:", description);
19 outln("\t\tSize: {}", human_readable_size(cache_object.get_u32("size"sv).value()));
20 outln("\t\tLine size: {}", human_readable_size(cache_object.get_u32("line_size"sv).value()));
21};
22
23static void print_cpu_info(JsonObject const& value)
24{
25 outln("CPU {}:", value.get_u32("processor"sv).value());
26 outln("\tVendor ID: {}", value.get_deprecated_string("vendor_id"sv).value());
27 if (value.has("hypervisor_vendor_id"sv))
28 outln("\tHypervisor Vendor ID: {}", value.get_deprecated_string("hypervisor_vendor_id"sv).value());
29 outln("\tBrand: {}", value.get_deprecated_string("brand"sv).value());
30 outln("\tFamily: {}", value.get_u32("family"sv).value());
31 outln("\tModel: {}", value.get_u32("model"sv).value());
32 outln("\tStepping: {}", value.get_u32("stepping"sv).value());
33 outln("\tType: {}", value.get_u32("type"sv).value());
34
35 auto& caches = value.get_object("caches"sv).value();
36 if (caches.has("l1_data"sv))
37 print_cache_info("L1 data cache"sv, caches.get_object("l1_data"sv).value());
38 if (caches.has("l1_instruction"sv))
39 print_cache_info("L1 instruction cache"sv, caches.get_object("l1_instruction"sv).value());
40 if (caches.has("l2"sv))
41 print_cache_info("L2 cache"sv, caches.get_object("l2"sv).value());
42 if (caches.has("l3"sv))
43 print_cache_info("L3 cache"sv, caches.get_object("l3"sv).value());
44
45 out("\tFeatures: ");
46
47 auto& features = value.get_array("features"sv).value();
48
49 for (auto const& feature : features.values())
50 out("{} ", feature.as_string());
51
52 outln();
53}
54
55ErrorOr<int> serenity_main(Main::Arguments)
56{
57 TRY(Core::System::pledge("stdio rpath"));
58
59 TRY(Core::System::unveil("/sys/kernel/cpuinfo", "r"));
60 TRY(Core::System::unveil(nullptr, nullptr));
61
62 auto file = TRY(Core::File::open("/sys/kernel/cpuinfo"sv, Core::File::OpenMode::Read));
63 auto file_contents = TRY(file->read_until_eof());
64 auto json = TRY(JsonValue::from_string(file_contents));
65 auto const& array = json.as_array();
66
67 for (size_t i = 0; i < array.size(); i++) {
68 print_cpu_info(array.at(i).as_object());
69 if (i != array.size() - 1)
70 outln();
71 }
72
73 return 0;
74}