[gnome-usage] system-monitor: Simplify SystemMonitor
- From: Petr Štětka <pstetka src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-usage] system-monitor: Simplify SystemMonitor
- Date: Wed, 19 Dec 2018 12:49:09 +0000 (UTC)
commit 2d2932d598f7a73b834813b2cc471d0208e81bea
Author: Petr Štětka <pstetka redhat com>
Date: Sun May 20 14:18:45 2018 +0200
system-monitor: Simplify SystemMonitor
src/process.vala | 54 +++++---
src/system-monitor.vala | 319 ++++++------------------------------------------
2 files changed, 75 insertions(+), 298 deletions(-)
---
diff --git a/src/process.vala b/src/process.vala
index 478ad33..a31bc2c 100644
--- a/src/process.vala
+++ b/src/process.vala
@@ -25,7 +25,6 @@ namespace Usage
public Pid pid { get; private set; }
public string cmdline { get; private set; }
public string cmdline_parameter { get; private set; } //Isn't parameters as "-p" etc, but parameter
for running app, for ex. "--writer' with libreoffice, or "privacy" with gnome-control-center
- public string display_name { get; private set; }
public uint uid { get; private set; }
public double cpu_load { get; set; default = 0; }
@@ -36,30 +35,51 @@ namespace Usage
public uint64 mem_usage { get; set; default = 0; }
- public HashTable<Pid?, Process>? sub_processes { get; set; }
+ public bool mark_as_updated { get; set; default = true; }
+ public ProcessStatus status { get; private set; default = ProcessStatus.SLEEPING; }
- public bool alive { get; set; default = true; }
- public ProcessStatus status { get; set; default = ProcessStatus.SLEEPING; }
-
- public Process(Pid pid, string cmdline, string cmdline_parameter, string display_name, uint uid)
+ public Process(Pid pid, string cmdline, string cmdline_parameter)
{
this.pid = pid;
this.cmdline = cmdline;
this.cmdline_parameter = cmdline_parameter;
- this.display_name = display_name;
- this.uid = uid;
+ this.uid = _get_uid();
+ }
+
+ public void update_status ()
+ {
+ GTop.ProcState proc_state;
+ GTop.get_proc_state (out proc_state, pid);
+
+ switch(proc_state.state)
+ {
+ case GTop.PROCESS_RUNNING:
+ case GTop.PROCESS_UNINTERRUPTIBLE:
+ status = ProcessStatus.RUNNING;
+ break;
+ case GTop.PROCESS_SWAPPING:
+ case GTop.PROCESS_INTERRUPTIBLE:
+ case GTop.PROCESS_STOPPED:
+ status = ProcessStatus.SLEEPING;
+ break;
+ case GTop.PROCESS_DEAD:
+ case GTop.PROCESS_ZOMBIE:
+ default:
+ status = ProcessStatus.DEAD;
+ break;
+ }
+
+ if(cpu_load > 0)
+ status = ProcessStatus.RUNNING;
+
+ mark_as_updated = true;
}
- public void update_from_process(Process process)
+ private uint _get_uid()
{
- this.last_processor = process.last_processor;
- this.cpu_load = process.cpu_load;
- this.x_cpu_load = process.x_cpu_load;
- this.cpu_last_used = process.cpu_last_used;
- this.x_cpu_last_used = process.x_cpu_last_used;
- this.mem_usage = process.mem_usage;
- this.alive = process.alive;
- this.status = process.status;
+ GTop.ProcUid procUid;
+ GTop.get_proc_uid(out procUid, pid);
+ return procUid.uid;
}
}
diff --git a/src/system-monitor.vala b/src/system-monitor.vala
index 280d02e..871e6bf 100644
--- a/src/system-monitor.vala
+++ b/src/system-monitor.vala
@@ -33,13 +33,8 @@ namespace Usage
private CpuMonitor cpu_monitor;
private MemoryMonitor memory_monitor;
- private HashTable<Pid?, Process> process_table;
- private HashTable<string, Process> cpu_process_table;
- private HashTable<string, Process> ram_process_table;
-
+ private HashTable<string, AppItem> app_table;
private int process_mode = GTop.KERN_PROC_ALL;
- private GLib.List<AppInfo> apps_info;
-
private static SystemMonitor system_monitor;
public static SystemMonitor get_default()
@@ -50,48 +45,14 @@ namespace Usage
return system_monitor;
}
- public List<unowned Process> get_processes()
- {
- return process_table.get_values();
- }
-
- public unowned Process get_process_by_pid(Pid pid)
- {
- return process_table.get(pid);
- }
-
- public List<unowned Process> get_cpu_processes()
- {
- return cpu_process_table.get_values();
- }
-
- public unowned Process get_cpu_process(string cmdline)
- {
- return cpu_process_table[cmdline];
- }
-
- public List<unowned Process> get_ram_processes()
+ public List<unowned AppItem> get_apps()
{
- return ram_process_table.get_values();
+ return app_table.get_values();
}
- public unowned Process get_ram_process(string cmdline)
+ public unowned AppItem get_app_by_name(string name)
{
- return ram_process_table[cmdline];
- }
-
- public unowned GLib.List<AppInfo> get_apps_info()
- {
- return apps_info;
- }
-
- public AppInfo? get_app_info(string desktop_id)
- {
- foreach(var app_info in apps_info)
- if (app_info.get_display_name() == desktop_id)
- return app_info;
-
- return null;
+ return app_table.get(name);
}
public SystemMonitor()
@@ -101,12 +62,8 @@ namespace Usage
cpu_monitor = new CpuMonitor();
memory_monitor = new MemoryMonitor();
- process_table = new HashTable<Pid?, Process>(int_hash, int_equal);
- cpu_process_table = new HashTable<string, Process>(str_hash, str_equal);
- ram_process_table = new HashTable<string, Process>(str_hash, str_equal);
-
+ app_table = new HashTable<string, AppItem>(str_hash, str_equal);
var settings = Settings.get_default();
- apps_info = AppInfo.get_all();
update_data();
Timeout.add(settings.data_update_interval, update_data);
@@ -117,7 +74,7 @@ namespace Usage
});
}
- public bool update_data()
+ private bool update_data()
{
cpu_monitor.update();
memory_monitor.update();
@@ -129,121 +86,57 @@ namespace Usage
swap_usage = memory_monitor.get_swap_usage();
swap_total = memory_monitor.get_swap_total();
- foreach(unowned Process process in process_table.get_values())
- {
- process.alive = false;
- }
-
- set_alive_false(ref cpu_process_table);
- set_alive_false(ref ram_process_table);
-
GTop.Proclist proclist;
var pids = GTop.get_proclist (out proclist, process_mode);
+ foreach(var app in app_table.get_values())
+ app.mark_as_not_updated();
+
for(uint i = 0; i < proclist.number; i++)
{
- if (!(pids[i] in process_table))
+ string cmdline_parameter;
+ string cmdline = get_full_process_cmd(pids[i], out cmdline_parameter);
+
+ if (!(cmdline in app_table))
{
- string cmdline_parameter;
- string cmdline = get_full_process_cmd(pids[i], out cmdline_parameter);
- string display_name = get_display_name(cmdline, cmdline_parameter);
- uint uid = get_uid(pids[i]);
- var process = new Process(pids[i], cmdline, cmdline_parameter, display_name, uid);
- cpu_monitor.update_process(ref process);
- process_table.insert (pids[i], (owned) process);
+ var process = new Process(pids[i], cmdline, cmdline_parameter);
+ update_process(ref process);
+ var app = new AppItem(process);
+ app_table.insert (cmdline, (owned) app);
}
else
{
- Process process = process_table[pids[i]];
- process.alive = true;
- update_process_status(ref process);
- cpu_monitor.update_process(ref process);
- memory_monitor.update_process(ref process);
- }
- }
+ AppItem app = app_table[cmdline];
- foreach(unowned Process process in process_table.get_values())
- {
- if (!process.alive)
- {
- process.status = ProcessStatus.DEAD;
- process_table.remove (process.pid);
+ if (!app.contains_process(pids[i]))
+ {
+ var process = new Process(pids[i], cmdline, cmdline_parameter);
+ update_process(ref process);
+ app.insert_process(process);
+ }
+ else
+ {
+ var process = app.get_process_by_pid(pids[i]);
+ update_process(ref process);
+ app.replace_process(process);
+ }
}
}
- var process_table_temp = new HashTable<Pid?, Process>(int_hash, int_equal);
- foreach(unowned Process process in process_table.get_values())
- {
- if(process.cpu_load >= 1)
- process_table_temp.insert(process.pid, process);
- }
- update_processes(process_table_temp, ref cpu_process_table);
-
- process_table_temp.remove_all();
- foreach(unowned Process process in process_table.get_values())
- {
- if(process.mem_usage >= 1)
- process_table_temp.insert(process.pid, process);
- }
- update_processes(process_table_temp, ref ram_process_table);
+ foreach(var app in app_table.get_values())
+ app.remove_processes();
return true;
}
- private uint get_uid(Pid pid)
+ private void update_process(ref Process process)
{
- GTop.ProcUid procUid;
- GTop.get_proc_uid(out procUid, pid);
- return procUid.uid;
+ cpu_monitor.update_process(ref process);
+ memory_monitor.update_process(ref process);
+ process.update_status();
}
- private string get_display_name(string cmdline, string cmdline_parameter)
- {
- AppInfo app_info = null;
- foreach (AppInfo info in apps_info)
- {
- string commandline = info.get_commandline();
- if(commandline != null)
- {
- for (int i = 0; i < commandline.length; i++)
- {
- if(commandline[i] == ' ' && commandline[i] == '%')
- commandline = commandline.substring(0, i);
- }
-
- commandline = Path.get_basename(commandline);
- string process_full_cmd = cmdline + " " + cmdline_parameter;
- if(commandline == process_full_cmd)
- app_info = info;
- else if(commandline.contains("google-" + cmdline + "-")) //Fix for Google Chrome naming
- app_info = info;
-
- if(app_info == null)
- {
- commandline = info.get_commandline();
- for (int i = 0; i < commandline.length; i++)
- {
- if(commandline[i] == ' ')
- commandline = commandline.substring(0, i);
- }
-
- if(info.get_commandline().has_prefix(commandline + " " + commandline + "://")) //Fix
for Steam naming
- commandline = info.get_commandline();
-
- commandline = Path.get_basename(commandline);
- if(commandline == cmdline)
- app_info = info;
- }
- }
- }
-
- if(app_info != null)
- return app_info.get_display_name();
- else
- return cmdline;
- }
-
- private string get_full_process_cmd (Pid pid, out string cmd_parameter)
+ private string get_full_process_cmd (Pid pid, out string cmd_parameter)
{
GTop.ProcArgs proc_args;
GTop.ProcState proc_state;
@@ -291,141 +184,5 @@ namespace Usage
return cmd;
}
-
- private void update_process_status (ref Process process)
- {
- GTop.ProcState proc_state;
- GTop.get_proc_state (out proc_state, process.pid);
-
- switch(proc_state.state)
- {
- case GTop.PROCESS_RUNNING:
- case GTop.PROCESS_UNINTERRUPTIBLE:
- process.status = ProcessStatus.RUNNING;
- break;
- case GTop.PROCESS_SWAPPING:
- case GTop.PROCESS_INTERRUPTIBLE:
- case GTop.PROCESS_STOPPED:
- process.status = ProcessStatus.SLEEPING;
- break;
- case GTop.PROCESS_DEAD:
- case GTop.PROCESS_ZOMBIE:
- default:
- process.status = ProcessStatus.DEAD;
- break;
- }
-
- if(process.cpu_load > 0)
- process.status = ProcessStatus.RUNNING;
- }
-
- private void set_alive_false(ref HashTable<string, Process> process_table)
- {
- foreach(unowned Process process in process_table.get_values())
- {
- if(process.sub_processes == null)
- process.alive = false;
- else
- {
- foreach(unowned Process sub_process in process.sub_processes.get_values())
- {
- sub_process.alive = false;
- }
- }
- }
- }
-
- private void update_processes(HashTable<Pid?, Process> from_table, ref HashTable<string, Process>
to_table)
- {
- foreach(unowned Process process_it in from_table.get_values())
- {
- if(process_it.cmdline in to_table) //subprocess or update process
- {
- if(to_table[process_it.cmdline].sub_processes != null) //subprocess
- {
- if(process_it.pid in to_table[process_it.cmdline].sub_processes) //update subprocess
- {
- unowned Process process =
to_table[process_it.cmdline].sub_processes[process_it.pid];
- process.update_from_process(process_it);
- }
- else //add subrow
- {
- var process = new Process(process_it.pid, process_it.cmdline,
process_it.cmdline_parameter, process_it.display_name, process_it.uid);
- process.update_from_process(process_it);
- to_table[process.cmdline].sub_processes.insert(process.pid, (owned) process);
- }
- }
- else //update process or transform to group and add subrow
- {
- if(process_it.pid == to_table[process_it.cmdline].pid) //update process
- {
- unowned Process process = to_table[process_it.cmdline];
- process.update_from_process(process_it);
- }
- else //transform to group and add subrow
- {
- to_table[process_it.cmdline].sub_processes = new HashTable<Pid?,
Process>(int_hash, int_equal);
- unowned Process process = to_table[process_it.cmdline];
-
- var sub_process_one = new Process(process.pid, process.cmdline,
process.cmdline_parameter, process.display_name, process.uid);
- sub_process_one.update_from_process(process);
- to_table[process_it.cmdline].sub_processes.insert(sub_process_one.pid, (owned)
sub_process_one);
-
- var sub_process = new Process(process_it.pid, process_it.cmdline,
process_it.cmdline_parameter, process_it.display_name, process_it.uid);
- sub_process.update_from_process(process_it);
- to_table[process_it.cmdline].sub_processes.insert(process_it.pid, (owned)
sub_process);
- }
- }
- }
- else //add process
- {
- var process = new Process(process_it.pid, process_it.cmdline,
process_it.cmdline_parameter, process_it.display_name, process_it.uid);
- process.update_from_process(process_it);
- to_table.insert(process.cmdline, (owned) process);
- }
- }
-
- foreach(unowned Process process in to_table.get_values())
- {
- if(process.sub_processes == null)
- {
- if(!process.alive)
- to_table.remove (process.cmdline);
- }
- else
- {
- double cpu_load = 0;
- uint64 mem_usage = 0;
- foreach(unowned Process sub_process in process.sub_processes.get_values())
- {
- if (!sub_process.alive)
- process.sub_processes.remove(sub_process.pid);
- else
- {
- cpu_load += sub_process.cpu_load;
- mem_usage += sub_process.mem_usage;
- }
- }
- process.cpu_load = cpu_load;
- process.mem_usage = mem_usage;
-
- if(process.sub_processes.length == 1) //tranform to process
- {
- foreach(unowned Process sub_process in process.sub_processes.get_values()) //only one
- {
- process.sub_processes = null;
- process = sub_process;
- }
- }
- else if(process.sub_processes.length == 0)
- {
- process.sub_processes = null;
- process.alive = false;
- process.status = ProcessStatus.DEAD;
- to_table.remove(process.cmdline);
- }
- }
- }
- }
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]