Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/HashMap.h>
30#include <AK/String.h>
31#include <AK/Vector.h>
32#include <LibGUI/Model.h>
33#include <unistd.h>
34
35class GraphWidget;
36
37struct PidAndTid {
38 bool operator==(const PidAndTid& other) const
39 {
40 return pid == other.pid && tid == other.tid;
41 }
42 pid_t pid;
43 int tid;
44};
45
46class ProcessModel final : public GUI::Model {
47public:
48 enum Column {
49 Icon = 0,
50 Name,
51 CPU,
52 State,
53 Priority,
54 EffectivePriority,
55 User,
56 PID,
57 TID,
58 Virtual,
59 Physical,
60 DirtyPrivate,
61 CleanInode,
62 PurgeableVolatile,
63 PurgeableNonvolatile,
64 Veil,
65 Pledge,
66 Syscalls,
67 InodeFaults,
68 ZeroFaults,
69 CowFaults,
70 FileReadBytes,
71 FileWriteBytes,
72 UnixSocketReadBytes,
73 UnixSocketWriteBytes,
74 IPv4SocketReadBytes,
75 IPv4SocketWriteBytes,
76 __Count
77 };
78
79 static ProcessModel& the();
80
81 static NonnullRefPtr<ProcessModel> create() { return adopt(*new ProcessModel); }
82 virtual ~ProcessModel() override;
83
84 virtual int row_count(const GUI::ModelIndex&) const override;
85 virtual int column_count(const GUI::ModelIndex&) const override;
86 virtual String column_name(int column) const override;
87 virtual ColumnMetadata column_metadata(int column) const override;
88 virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override;
89 virtual void update() override;
90
91 Function<void(float)> on_new_cpu_data_point;
92
93private:
94 ProcessModel();
95
96 struct ThreadState {
97 int tid;
98 pid_t pid;
99 unsigned times_scheduled;
100 String name;
101 String state;
102 String user;
103 String pledge;
104 String veil;
105 u32 priority;
106 u32 effective_priority;
107 size_t amount_virtual;
108 size_t amount_resident;
109 size_t amount_dirty_private;
110 size_t amount_clean_inode;
111 size_t amount_purgeable_volatile;
112 size_t amount_purgeable_nonvolatile;
113 unsigned syscall_count;
114 unsigned inode_faults;
115 unsigned zero_faults;
116 unsigned cow_faults;
117 unsigned unix_socket_read_bytes;
118 unsigned unix_socket_write_bytes;
119 unsigned ipv4_socket_read_bytes;
120 unsigned ipv4_socket_write_bytes;
121 unsigned file_read_bytes;
122 unsigned file_write_bytes;
123 float cpu_percent;
124 int icon_id;
125 };
126
127 struct Thread {
128 ThreadState current_state;
129 ThreadState previous_state;
130 };
131
132 HashMap<uid_t, String> m_usernames;
133 HashMap<PidAndTid, NonnullOwnPtr<Thread>> m_threads;
134 Vector<PidAndTid> m_pids;
135 RefPtr<Gfx::Bitmap> m_generic_process_icon;
136 RefPtr<Gfx::Bitmap> m_high_priority_icon;
137 RefPtr<Gfx::Bitmap> m_low_priority_icon;
138 RefPtr<Gfx::Bitmap> m_normal_priority_icon;
139};
140
141namespace AK {
142template<>
143struct Traits<PidAndTid> : public GenericTraits<PidAndTid> {
144 static unsigned hash(const PidAndTid& value) { return pair_int_hash(value.pid, value.tid); }
145};
146}