Skip to content

Commit

Permalink
Create ProcessWorkaround class and use it if in Flatpak env
Browse files Browse the repository at this point in the history
  • Loading branch information
stsdc committed Dec 24, 2023
1 parent 5a00128 commit 0a3cd3d
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 65 deletions.
10 changes: 7 additions & 3 deletions flatpak/workaround/process_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ def GetProcesses(self, name, sender=None, conn=None):
"io": "",
"children": ""
}
with open(f'/proc/{pid}/cmdline', 'rb') as file:
process["cmdline"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))

try:
with open(f'/proc/{pid}/cmdline', 'rb') as file:
process["cmdline"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))
except FileNotFoundError as err:
process["cmdline"] = None
continue

with open(f'/proc/{pid}/stat', 'rb') as file:
process["stat"] = (file.read().decode('utf-8', 'ignore').replace('\0', ' '))

Expand Down
77 changes: 16 additions & 61 deletions src/Managers/Process.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public class Monitor.Process : GLib.Object {


// Whether or not the PID leads to something
public bool exists { get; private set; }
public bool exists { get; protected set; }

// Full command from cmdline file
public string command { get; private set; }
public string command { get; protected set; }

// If process is an installed app, this will contain its name,
// otherwise it is just a trimmed command
Expand Down Expand Up @@ -51,17 +51,17 @@ public class Monitor.Process : GLib.Object {
*
* Will be 0 on first update.
*/
public double cpu_percentage { get; private set; }
public double cpu_percentage { get; protected set; }

private uint64 cpu_last_used;
protected uint64 cpu_last_used;

// Memory usage of the process, measured in KiB.
public uint64 mem_usage { get; private set; }
public double mem_percentage { get; private set; }
public uint64 mem_usage { get; protected set; }
public double mem_percentage { get; protected set; }

private uint64 last_total;
protected uint64 last_total;

const int HISTORY_BUFFER_SIZE = 30;
protected const int HISTORY_BUFFER_SIZE = 30;
public Gee.ArrayList<double ? > cpu_percentage_history = new Gee.ArrayList<double ? > ();
public Gee.ArrayList<double ? > mem_percentage_history = new Gee.ArrayList<double ? > ();

Expand Down Expand Up @@ -95,7 +95,7 @@ public class Monitor.Process : GLib.Object {
get_usage (0, 1);
}

private int get_uid () {
protected int get_uid () {
if (ProcessUtils.is_flatpak_env ()) {
var process_provider = ProcessProvider.get_default ();
string ? status = process_provider.pids_status.get (this.stat.pid);
Expand Down Expand Up @@ -139,7 +139,7 @@ public class Monitor.Process : GLib.Object {
return false;
}

private bool get_children_pids () {
protected bool get_children_pids () {
string ? children_content = ProcessUtils.read_file ("/proc/%d/task/%d/children".printf (stat.pid, stat.pid));
if (children_content == "" || children_content == null) {
return false;
Expand All @@ -152,52 +152,7 @@ public class Monitor.Process : GLib.Object {
return true;
}

private bool parse_io_workaround () {
var process_provider = ProcessProvider.get_default ();
string ? io_stats = process_provider.pids_io.get (this.stat.pid);

if (io_stats == "") return false;

foreach (string line in io_stats.split ("\n")) {
if (line == "") continue;
var splitted_line = line.split (":");
switch (splitted_line[0]) {
case "wchar":
io.wchar = uint64.parse (splitted_line[1]);
break;
case "rchar":
io.rchar = uint64.parse (splitted_line[1]);
break;
case "syscr":
io.syscr = uint64.parse (splitted_line[1]);
break;
case "syscw":
io.syscw = uint64.parse (splitted_line[1]);
break;
case "read_bytes":
io.read_bytes = uint64.parse (splitted_line[1]);
break;
case "write_bytes":
io.write_bytes = uint64.parse (splitted_line[1]);
break;
case "cancelled_write_bytes":
io.cancelled_write_bytes = uint64.parse (splitted_line[1]);
break;
default:
warning ("Unknown value in /proc/%d/io", stat.pid);
break;
}
}

return true;
}

private bool parse_io () {

if (ProcessUtils.is_flatpak_env ()) {
return parse_io_workaround ();
}

protected bool parse_io () {
var io_file = File.new_for_path ("/proc/%d/io".printf (stat.pid));

if (!io_file.query_exists ()) {
Expand Down Expand Up @@ -250,7 +205,7 @@ public class Monitor.Process : GLib.Object {
}

// Reads the /proc/%pid%/stat file and updates the process with the information therein.
private bool parse_stat () {
protected bool parse_stat () {
string ? stat_contents;
if (ProcessUtils.is_flatpak_env ()) {
var process_provider = ProcessProvider.get_default ();
Expand Down Expand Up @@ -293,7 +248,7 @@ public class Monitor.Process : GLib.Object {
return true;
}

private bool parse_statm () {
protected bool parse_statm () {
string ? statm_contents;
if (ProcessUtils.is_flatpak_env ()) {
var process_provider = ProcessProvider.get_default ();
Expand All @@ -316,7 +271,7 @@ public class Monitor.Process : GLib.Object {
return true;
}

private bool get_open_files () {
protected bool get_open_files () {
// try {
// string directory = "/proc/%d/fd".printf (stat.pid);
// Dir dir = Dir.open (directory, 0);
Expand All @@ -343,7 +298,7 @@ public class Monitor.Process : GLib.Object {
/**
* Reads the /proc/%pid%/cmdline file and updates from the information contained therein.
*/
private bool read_cmdline () {
protected bool read_cmdline () {
string ? cmdline;
if (ProcessUtils.is_flatpak_env ()) {
var process_provider = ProcessProvider.get_default ();
Expand All @@ -368,7 +323,7 @@ public class Monitor.Process : GLib.Object {
return true;
}

private void get_usage (uint64 cpu_total, uint64 cpu_last_total) {
protected void get_usage (uint64 cpu_total, uint64 cpu_last_total) {
// Get CPU usage by process
GTop.ProcTime proc_time;
GTop.get_proc_time (out proc_time, stat.pid);
Expand Down
2 changes: 1 addition & 1 deletion src/Managers/ProcessManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace Monitor {
*/
private Process ? add_process (int pid, bool lazy_signal = false) {
// create the process
var process = new Process (pid);
var process = ProcessProvider.get_default ().create_process (pid);

if (!process.exists) {
return null;
Expand Down
7 changes: 7 additions & 0 deletions src/Managers/ProcessProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ namespace Monitor {
return pids;
}

public Process create_process (int pid) {
if (ProcessUtils.is_flatpak_env ()) {
return new ProcessWorkaround (pid);
}
return new Process (pid);
}

private bool process_line (IOChannel channel, IOCondition condition, GLib.List<int> _pids) {
if (condition == IOCondition.HUP) {
// debug ("%s: The fd has been closed.\n", stream_name);
Expand Down
Loading

0 comments on commit 0a3cd3d

Please sign in to comment.