From 5256763777b4f8ebcc4180cfbfb5e01fdead5916 Mon Sep 17 00:00:00 2001 From: stsdc <6031763+stsdc@users.noreply.github.com> Date: Tue, 12 Dec 2023 17:13:13 +0100 Subject: [PATCH] get list of processes --- flatpak/workaround/process_monitor.py | 25 ++++++++++-------- src/Managers/Process.vala | 38 ++++++++++++++++++++------- src/Managers/ProcessProvider.vala | 6 ++++- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/flatpak/workaround/process_monitor.py b/flatpak/workaround/process_monitor.py index 02519e0c..49136698 100644 --- a/flatpak/workaround/process_monitor.py +++ b/flatpak/workaround/process_monitor.py @@ -43,17 +43,20 @@ def GetProcesses(self, name, sender=None, conn=None): "io": "" } with open(f'/proc/{pid}/cmdline', 'rb') as file: - process["cmdline"] = (file.read().decode('utf-8', 'ignore').replace('\0', '')) - # with open(f'/proc/{pid}/stat', 'rb') as file: - # process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) - # with open(f'/proc/{pid}/statm', 'rb') as file: - # process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) - # try: - # with open(f'/proc/{pid}/io', 'r') as file: - # process.append(file.read()) - # except PermissionError as err: - # # print(err) - # pass + process["cmdline"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' ')) + + with open(f'/proc/{pid}/stat', 'rb') as file: + process["stat"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' ')) + + with open(f'/proc/{pid}/statm', 'rb') as file: + process["statm"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' ')) + try: + with open(f'/proc/{pid}/io', 'rb') as file: + process["io"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' ')) + + except PermissionError as err: + # print(err) + pass processes.append(process) return processes diff --git a/src/Managers/Process.vala b/src/Managers/Process.vala index dafcca68..cf5637f0 100644 --- a/src/Managers/Process.vala +++ b/src/Managers/Process.vala @@ -109,9 +109,9 @@ public class Monitor.Process : GLib.Object { exists = parse_stat (); if (exists) { get_usage (cpu_total, cpu_last_total); - // parse_io (); - // parse_statm (); - // get_open_files (); + parse_io (); + parse_statm (); + get_open_files (); } return exists; } @@ -201,12 +201,18 @@ public class Monitor.Process : GLib.Object { // Reads the /proc/%pid%/stat file and updates the process with the information therein. private bool parse_stat () { - string ? stat_contents = ProcessUtils.read_file ("/proc/%d/stat".printf (stat.pid)); - - if (stat_contents == null) { - return false; + string ? stat_contents; + if (ProcessUtils.is_flatpak_env ()) { + var process_provider = ProcessProvider.get_default (); + stat_contents = process_provider.pids_stat.get (this.stat.pid); + } else { + stat_contents = ProcessUtils.read_file ("/proc/%d/stat".printf (stat.pid)); } + if (stat_contents == null) return false; + + // debug (stat_contents); + // Split the contents into an array and parse each value that we care about // But first we have to extract the command name, since it might include spaces @@ -238,7 +244,13 @@ public class Monitor.Process : GLib.Object { } private bool parse_statm () { - string ? statm_contents = ProcessUtils.read_file ("/proc/%d/statm".printf (stat.pid)); + string ? statm_contents; + if (ProcessUtils.is_flatpak_env ()) { + var process_provider = ProcessProvider.get_default (); + statm_contents = process_provider.pids_stat.get (this.stat.pid); + } else { + statm_contents = ProcessUtils.read_file ("/proc/%d/statm".printf (stat.pid)); + } if (statm_contents == null) return false; @@ -282,7 +294,13 @@ public class Monitor.Process : GLib.Object { * Reads the /proc/%pid%/cmdline file and updates from the information contained therein. */ private bool read_cmdline () { - string ? cmdline = ProcessUtils.read_file ("/proc/%d/cmdline".printf (stat.pid)); + string ? cmdline; + if (ProcessUtils.is_flatpak_env ()) { + var process_provider = ProcessProvider.get_default (); + cmdline = process_provider.pids_cmdline.get (this.stat.pid); + } else { + cmdline = ProcessUtils.read_file ("/proc/%d/cmdline".printf (stat.pid)); + } if (cmdline == null) { return false; @@ -292,11 +310,11 @@ public class Monitor.Process : GLib.Object { // if cmdline has 0 length we look into stat file // useful for kworker processes command = stat.comm; - return true; } command = cmdline; + return true; } diff --git a/src/Managers/ProcessProvider.vala b/src/Managers/ProcessProvider.vala index 5ec54669..63cdf449 100644 --- a/src/Managers/ProcessProvider.vala +++ b/src/Managers/ProcessProvider.vala @@ -6,6 +6,8 @@ namespace Monitor { } public Gee.HashSet pids = new Gee.HashSet (); + public Gee.HashMap pids_cmdline = new Gee.HashMap (); + public Gee.HashMap pids_stat = new Gee.HashMap (); DBusWorkaroundClient dbus_workaround_client; @@ -23,8 +25,10 @@ namespace Monitor { pids = new int[procs.length]; debug ("Workaround: pids: %d", procs.length); for (int i = 0; i < procs.length; i++) { - debug (procs[i]["pid"]); + // debug (procs[i]["pid"]); pids[i] = int.parse (procs[i]["pid"]); + pids_cmdline.set (pids[i], procs[i]["cmdline"]); + pids_stat.set (pids[i], procs[i]["stat"]); } } catch (Error e) { warning (e.message);