summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp
blob: ccb423e7c7ea2ee5fbc19c77ffbde1cec0dfb76e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/ByteBuffer.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCore/File.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <pwd.h>

namespace Core {

HashMap<uid_t, DeprecatedString> ProcessStatisticsReader::s_usernames;

ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(SeekableStream& proc_all_file, bool include_usernames)
{
    TRY(proc_all_file.seek(0, SeekMode::SetPosition));

    AllProcessesStatistics all_processes_statistics;

    auto file_contents = TRY(proc_all_file.read_until_eof());
    auto json_obj = TRY(JsonValue::from_string(file_contents)).as_object();
    json_obj.get_array("processes"sv)->for_each([&](auto& value) {
        const JsonObject& process_object = value.as_object();
        Core::ProcessStatistics process;

        // kernel data first
        process.pid = process_object.get_u32("pid"sv).value_or(0);
        process.pgid = process_object.get_u32("pgid"sv).value_or(0);
        process.pgp = process_object.get_u32("pgp"sv).value_or(0);
        process.sid = process_object.get_u32("sid"sv).value_or(0);
        process.uid = process_object.get_u32("uid"sv).value_or(0);
        process.gid = process_object.get_u32("gid"sv).value_or(0);
        process.ppid = process_object.get_u32("ppid"sv).value_or(0);
        process.nfds = process_object.get_u32("nfds"sv).value_or(0);
        process.kernel = process_object.get_bool("kernel"sv).value_or(false);
        process.name = process_object.get_deprecated_string("name"sv).value_or("");
        process.executable = process_object.get_deprecated_string("executable"sv).value_or("");
        process.tty = process_object.get_deprecated_string("tty"sv).value_or("");
        process.pledge = process_object.get_deprecated_string("pledge"sv).value_or("");
        process.veil = process_object.get_deprecated_string("veil"sv).value_or("");
        process.amount_virtual = process_object.get_u32("amount_virtual"sv).value_or(0);
        process.amount_resident = process_object.get_u32("amount_resident"sv).value_or(0);
        process.amount_shared = process_object.get_u32("amount_shared"sv).value_or(0);
        process.amount_dirty_private = process_object.get_u32("amount_dirty_private"sv).value_or(0);
        process.amount_clean_inode = process_object.get_u32("amount_clean_inode"sv).value_or(0);
        process.amount_purgeable_volatile = process_object.get_u32("amount_purgeable_volatile"sv).value_or(0);
        process.amount_purgeable_nonvolatile = process_object.get_u32("amount_purgeable_nonvolatile"sv).value_or(0);

        auto& thread_array = process_object.get_array("threads"sv).value();
        process.threads.ensure_capacity(thread_array.size());
        thread_array.for_each([&](auto& value) {
            auto& thread_object = value.as_object();
            Core::ThreadStatistics thread;
            thread.tid = thread_object.get_u32("tid"sv).value_or(0);
            thread.times_scheduled = thread_object.get_u32("times_scheduled"sv).value_or(0);
            thread.name = thread_object.get_deprecated_string("name"sv).value_or("");
            thread.state = thread_object.get_deprecated_string("state"sv).value_or("");
            thread.time_user = thread_object.get_u64("time_user"sv).value_or(0);
            thread.time_kernel = thread_object.get_u64("time_kernel"sv).value_or(0);
            thread.cpu = thread_object.get_u32("cpu"sv).value_or(0);
            thread.priority = thread_object.get_u32("priority"sv).value_or(0);
            thread.syscall_count = thread_object.get_u32("syscall_count"sv).value_or(0);
            thread.inode_faults = thread_object.get_u32("inode_faults"sv).value_or(0);
            thread.zero_faults = thread_object.get_u32("zero_faults"sv).value_or(0);
            thread.cow_faults = thread_object.get_u32("cow_faults"sv).value_or(0);
            thread.unix_socket_read_bytes = thread_object.get_u32("unix_socket_read_bytes"sv).value_or(0);
            thread.unix_socket_write_bytes = thread_object.get_u32("unix_socket_write_bytes"sv).value_or(0);
            thread.ipv4_socket_read_bytes = thread_object.get_u32("ipv4_socket_read_bytes"sv).value_or(0);
            thread.ipv4_socket_write_bytes = thread_object.get_u32("ipv4_socket_write_bytes"sv).value_or(0);
            thread.file_read_bytes = thread_object.get_u32("file_read_bytes"sv).value_or(0);
            thread.file_write_bytes = thread_object.get_u32("file_write_bytes"sv).value_or(0);
            process.threads.append(move(thread));
        });

        // and synthetic data last
        if (include_usernames) {
            process.username = username_from_uid(process.uid);
        }
        all_processes_statistics.processes.append(move(process));
    });

    all_processes_statistics.total_time_scheduled = json_obj.get_u64("total_time"sv).value_or(0);
    all_processes_statistics.total_time_scheduled_kernel = json_obj.get_u64("total_time_kernel"sv).value_or(0);
    return all_processes_statistics;
}

ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames)
{
    auto proc_all_file = TRY(Core::File::open("/sys/kernel/processes"sv, Core::File::OpenMode::Read));
    return get_all(*proc_all_file, include_usernames);
}

DeprecatedString ProcessStatisticsReader::username_from_uid(uid_t uid)
{
    if (s_usernames.is_empty()) {
        setpwent();
        while (auto* passwd = getpwent())
            s_usernames.set(passwd->pw_uid, passwd->pw_name);
        endpwent();
    }

    auto it = s_usernames.find(uid);
    if (it != s_usernames.end())
        return (*it).value;
    return DeprecatedString::number(uid);
}
}