-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathProcessManager.vala
More file actions
317 lines (264 loc) · 12.6 KB
/
ProcessManager.vala
File metadata and controls
317 lines (264 loc) · 12.6 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
*/
namespace Monitor {
public class ProcessManager : GLib.Object {
private static GLib.Once<ProcessManager> instance;
public static unowned ProcessManager get_default () {
return instance.once (() => { return new ProcessManager (); });
}
public double cpu_load { get; private set; }
public double[] cpu_loads { get; private set; }
uint64 cpu_last_used = 0;
uint64 cpu_last_total = 0;
uint64[] cpu_last_useds = new uint64[32];
uint64[] cpu_last_totals = new uint64[32];
private Gee.TreeMap<int, Process> process_list;
private Gee.HashSet<int> kernel_process_blacklist;
private Gee.HashMap<string, AppInfo> apps_info_list;
public signal void process_added (Process process);
public signal void process_removed (int pid);
public signal void updated ();
// Construct a new ProcessManager
public ProcessManager () {
process_list = new Gee.TreeMap<int, Process> ();
kernel_process_blacklist = new Gee.HashSet<int> ();
apps_info_list = new Gee.HashMap<string, AppInfo> ();
populate_apps_info ();
update ();
}
public void populate_apps_info () {
var _apps_info = AppInfo.get_all ();
foreach (AppInfo app_info in _apps_info) {
string commandline = (app_info.get_commandline ());
// GLib.DesktopAppInfo? dai = info as GLib.DesktopAppInfo;
// if (dai != null) {
// string id = dai.get_string ("X-Flatpak");
// if (id != null)
// appid_map.insert (id, info);
// }
if (commandline == null)
continue;
apps_info_list.set (commandline, app_info);
}
}
/**
* Gets a process by its pid, making sure that it's updated.
*/
public Process ? get_process (int pid) {
// if the process is in the kernel blacklist, we don't want to deal with it.
if (kernel_process_blacklist.contains (pid)) {
return null;
}
// else, return our cached version.
if (process_list.has_key (pid)) {
return process_list[pid];
}
// else return the result of add_process
// make sure to lazily call the callback since this is a greedy add
// this way we don't interrupt whatever this method is being called for
// with a handle_add_process
return add_process (pid, true);
}
/**
* Returns all direct sub processes of this process
*/
public Gee.Set<int> get_sub_processes (int pid) {
var sub_processes = new Gee.HashSet<int> ();
// go through and add all of the processes with PPID set to this one
foreach (var process in process_list.values) {
if (process.stat.ppid == pid) {
sub_processes.add (process.stat.pid);
}
}
return sub_processes;
}
/**
* Gets a read only map of the processes currently cached
*/
public Gee.Map<int, Process> get_process_list () {
return process_list.read_only_view;
}
/**
* Gets all new process and adds them
*/
public void update_processes () {
/* CPU */
GTop.Cpu cpu_data;
GTop.get_cpu (out cpu_data);
var used = cpu_data.user + cpu_data.nice + cpu_data.sys;
cpu_load = ((double) (used - cpu_last_used)) / (cpu_data.total - cpu_last_total);
cpu_loads = new double[cpu_data.xcpu_user.length];
var useds = new uint64[cpu_data.xcpu_user.length];
for (int i = 0; i < cpu_data.xcpu_user.length; i++) {
useds[i] = cpu_data.xcpu_user[i] + cpu_data.xcpu_nice[i] + cpu_data.xcpu_sys[i];
}
for (int i = 0; i < cpu_data.xcpu_user.length; i++) {
cpu_loads[i] = ((double) (useds[i] - cpu_last_useds[i])) /
(cpu_data.xcpu_total[i] - cpu_last_totals[i]);
}
var remove_me = new Gee.HashSet<int> ();
/* go through each process and update it, removing the old ones */
foreach (var process in process_list.values) {
if (!process.update (cpu_data.total, cpu_last_total)) {
/* process doesn't exist any more, flag it for removal! */
remove_me.add (process.stat.pid);
}
}
/* remove everything from flags */
foreach (var pid in remove_me) {
remove_process (pid);
}
var uid = Posix.getuid ();
GTop.ProcList proclist;
// var pids = GTop.get_proclist (out proclist, GTop.GLIBTOP_KERN_PROC_UID, uid);
var pids = GTop.get_proclist (out proclist, GTop.GLIBTOP_KERN_PROC_ALL, uid);
for (int i = 0; i < proclist.number; i++) {
int pid = pids[i];
if (!process_list.has_key (pid) && !kernel_process_blacklist.contains (pid)) {
add_process (pid);
}
}
cpu_last_used = used;
cpu_last_total = cpu_data.total;
cpu_last_useds = useds;
cpu_last_totals = cpu_data.xcpu_total;
/* emit the updated signal so that subscribers can update */
updated ();
}
public void update () {
new Thread<bool> ("update-processes", () => {
Idle.add (() => {
update_processes ();
return false;
});
return true;
});
/* emit the updated signal so that subscribers can update */
updated ();
}
/** Sets name and icon for a process that is a Flatpak app and its children. */
private void set_flatpak_name_icon (Process process, GLib.Icon icon, string name) {
process.application_name = name;
process.icon = icon;
foreach (int pid in process.children) {
var _process = this.get_process (pid);
if (_process != null) {
set_flatpak_name_icon (_process, icon, name);
}
}
}
private bool match_process_app (Process process) {
var command_sanitized = ProcessUtils.sanitize_commandline (process.command);
var command_sanitized_basename = Path.get_basename (command_sanitized);
process.application_name = command_sanitized_basename;
foreach (var flatpak_app in Flatpak.Instance.get_all ()) {
if (flatpak_app.get_pid () == process.stat.pid || flatpak_app.get_child_pid () == process.stat.pid) {
debug ("Found Flatpak app: %s ", flatpak_app.get_app ());
foreach (var key in apps_info_list.keys) {
if (apps_info_list.get (key).get_id ().replace (".desktop", "") == flatpak_app.get_app ()) {
set_flatpak_name_icon (process, apps_info_list.get (key).get_icon (), apps_info_list.get (key).get_name ());
}
}
}
}
foreach (var key in apps_info_list.keys) {
if (apps_info_list.get (key).get_executable () == command_sanitized) {
process.application_name = apps_info_list.get (key).get_name ();
process.icon = apps_info_list.get (key).get_icon ();
} else if (apps_info_list.get (key).get_executable () == process.command.split (" ")[1]) {
process.application_name = apps_info_list.get (key).get_name ();
process.icon = apps_info_list.get (key).get_icon ();
} else if (key.split (" ")[1] != null) {
if (key.split (" ")[1].contains ("%") || key.split (" ")[1].contains ("--")) {
if (apps_info_list.get (key).get_executable () == command_sanitized_basename) {
process.application_name = apps_info_list.get (key).get_name ();
process.icon = apps_info_list.get (key).get_icon ();
return true;
}
}
// Steam case
// Must match a proper executable and not game executable e.g. steam steam://rungameid/210770
if ((Path.get_basename (key.split (" ")[0]) == command_sanitized_basename) && !key.split (" ")[1].contains ("steam")) {
process.application_name = apps_info_list.get (key).get_name ();
process.icon = apps_info_list.get (key).get_icon ();
return true;
}
// workaround for some flatpak apps
if (process.command.contains (apps_info_list.get (key).get_id ().replace (".desktop", ""))) {
process.application_name = apps_info_list.get (key).get_name ();
process.icon = apps_info_list.get (key).get_icon ();
return true;
}
} else if (apps_info_list.get (key).get_commandline () == process.command) {
process.application_name = apps_info_list.get (key).get_name ();
process.icon = apps_info_list.get (key).get_icon ();
return true;
} else if (process.command.split (" ")[0].contains (".exe")) {
var splitted = process.command.split (" ")[0].split ("\\");
process.application_name = splitted[splitted.length - 1].chomp ();
process.icon = new ThemedIcon ("application-x-ms-dos-executable");
return true;
}
// some processes have semicolon in command
// do not sanitizing to improve readability
else if (process.command.split (" ")[0].contains (":")) {
process.application_name = process.command;
return true;
}
}
if (ProcessUtils.is_shell (command_sanitized_basename)) {
process.icon = new ThemedIcon ("bash");
debug ("app name is " + process.application_name);
}
if (command_sanitized_basename == "docker" || command_sanitized_basename == "dockerd" || command_sanitized_basename == "docker-proxy") {
process.icon = new ThemedIcon ("docker");
process.application_name = command_sanitized_basename;
debug ("app name is " + process.application_name);
}
// process.application_name = process.command;
return true;
}
/**
* Parses a pid and adds a Process to our `process_list` or to the `kernel_blacklist`
*
* returns the created process
*/
private Process ? add_process (int pid, bool lazy_signal = false) {
// create the process
var process = new Process (pid);
if (!process.exists) {
return null;
}
this.match_process_app (process);
if (process.exists) {
if (process.stat.pgrp != 0) {
// regular process, add it to our cache
process_list.set (pid, process);
// call the signal, lazily if needed
if (lazy_signal)
Idle.add (() => { process_added (process); return false; });
else
process_added (process);
return process;
} else {
// add it to our kernel processes blacklist
kernel_process_blacklist.add (pid);
}
}
return null;
}
/**
* Remove the process from all lists and broadcast the process_removed signal if removed.
*/
private void remove_process (int pid) {
if (process_list.has_key (pid)) {
process_list.unset (pid);
process_removed (pid);
} else if (kernel_process_blacklist.contains (pid)) {
kernel_process_blacklist.remove (pid);
}
}
}
}