[gnome-usage] Add number of processes in row. In process dialog show values in Mb..etc instead of in %. In Network



commit 9e67bef32bc660bb69de48b28c715f999760bf5d
Author: Petr Štětka <pstetka redhat com>
Date:   Wed Feb 15 10:48:38 2017 +0100

    Add number of processes in row.
    In process dialog show values in Mb..etc instead of in %.
    In Network tab show actual speed of network for process.

 src/graph-block-row.vala     |   19 +++++++++--
 src/graph-block.vala         |   34 ++++++++++++++------
 src/memory-graph-table.vala  |   10 +++++-
 src/memory-monitor.vala      |   26 ++++++++++-----
 src/network-graph-table.vala |    4 +-
 src/network-monitor.vala     |   34 +++----------------
 src/process-dialog.vala      |   71 ++++++++++++++++-------------------------
 src/process-list-box.vala    |   14 ++++++++-
 src/process-row.vala         |   14 ++++++--
 src/process.vala             |   13 --------
 src/settings.vala            |    1 +
 src/sub-process-sub-row.vala |    5 ++-
 src/system-monitor.vala      |   14 +++-----
 src/utils.vala               |   14 ++++++++
 14 files changed, 149 insertions(+), 124 deletions(-)
---
diff --git a/src/graph-block-row.vala b/src/graph-block-row.vala
index 6367716..4d2f7f3 100644
--- a/src/graph-block-row.vala
+++ b/src/graph-block-row.vala
@@ -5,8 +5,9 @@ namespace Usage {
     public class GraphBlockRow : Gtk.Box
     {
         Gtk.Label value_label;
+        GraphBlockType type;
 
-        public GraphBlockRow(string label_text, string css_class)
+        public GraphBlockRow(GraphBlockType type, string label_text, string css_class)
         {
             Object(orientation: Gtk.Orientation.HORIZONTAL);
             var color_rectangle = new ColorRectangle(css_class);
@@ -21,11 +22,23 @@ namespace Usage {
             this.pack_start(color_rectangle, false, false);
             this.pack_start(label, false, true, 5);
             this.pack_end(value_label, false, true, 10);
+            this.type = type;
         }
 
-        public void update(int value)
+        public void update(uint64 value)
         {
-            value_label.set_text(value.to_string() + " %");
+            switch(type)
+            {
+                case GraphBlockType.PROCESSOR:
+                    value_label.set_text(value.to_string() + " %");
+                    break;
+                case GraphBlockType.MEMORY:
+                    value_label.set_text(Utils.format_size_values(value));
+                    break;
+                case GraphBlockType.NETWORK:
+                    value_label.set_text(Utils.format_size_speed_values(value));
+                    break;
+            }
         }
     }
 }
diff --git a/src/graph-block.vala b/src/graph-block.vala
index f9a384d..8ab1459 100644
--- a/src/graph-block.vala
+++ b/src/graph-block.vala
@@ -2,6 +2,13 @@ using Gtk;
 
 namespace Usage
 {
+    public enum GraphBlockType {
+        PROCESSOR,
+        MEMORY,
+        DISK,
+        NETWORK
+    }
+
     public class GraphBlock : Gtk.Grid
     {
         PieChart graph;
@@ -11,14 +18,16 @@ namespace Usage
         Gtk.Label label;
         string block_name;
         bool show_avaiable;
+        GraphBlockType type;
 
         class construct
         {
             set_css_name("GraphBlock");
         }
 
-        public GraphBlock(string block_name, string app_name, bool show_avaiable = true)
+        public GraphBlock(GraphBlockType type, string block_name, string app_name, bool show_avaiable = true)
         {
+            this.type = type;
             this.expand = true;
             this.block_name = block_name;
             this.show_avaiable = show_avaiable;
@@ -32,10 +41,10 @@ namespace Usage
             graph.width_request = 90;
             this.attach(graph, 0, 1, 1, 1);
 
-            application_row = new GraphBlockRow(app_name, "used");
-            others_row = new GraphBlockRow(_("Others"), "others");
+            application_row = new GraphBlockRow(type, app_name, "used");
+            others_row = new GraphBlockRow(type, _("Others"), "others");
             if(show_avaiable)
-                available_row = new GraphBlockRow(_("Available"), "available");
+                available_row = new GraphBlockRow(type, _("Available"), "available");
 
             Gtk.Box box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
             box.margin_left = 15;
@@ -47,7 +56,7 @@ namespace Usage
             this.attach(box, 1, 1, 1, 1);
         }
 
-        public void update(int processor_core, int application_percentages, int other_percentages)
+        public void update(uint64 application, uint64 other, uint64 all, int processor_core = -1)
         {
             if(processor_core != -1)
             {
@@ -60,14 +69,19 @@ namespace Usage
                 label.use_markup = true;
             }
 
-            if(other_percentages < 0)
-                other_percentages = 0;
+            int application_percentages = 0;
+            if(all != 0)
+                application_percentages = (int) (((double) application / all) * 100);
+
+            int other_percentages = 0;
+            if(all != 0)
+                other_percentages = (int) (((double) other / all) * 100);
 
             graph.update(application_percentages, other_percentages);
-            application_row.update(application_percentages);
-            others_row.update(other_percentages);
+            application_row.update(application);
+            others_row.update(other);
             if(show_avaiable)
-                available_row.update(100-other_percentages-application_percentages);
+                available_row.update(all-other-application);
         }
     }
 }
diff --git a/src/memory-graph-table.vala b/src/memory-graph-table.vala
index 0d4ad5a..bb5ca2b 100644
--- a/src/memory-graph-table.vala
+++ b/src/memory-graph-table.vala
@@ -31,10 +31,16 @@ namespace Usage {
             push (out iter, get_monotonic_time ());
 
             SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
-            double ram_usage = monitor.ram_usage;
+            double ram_usage = 0;
+            if(monitor.ram_total != 0)
+                ram_usage = (((double) monitor.ram_usage / monitor.ram_total) * 100);
+
+            double swap_usage = 0;
+            if(monitor.ram_total != 0)
+                swap_usage = (((double) monitor.swap_usage / monitor.swap_total) * 100);
 
             iter.set (column_ram_id, ram_usage, -1);
-            iter.set (column_swap_id, monitor.swap_usage, -1);
+            iter.set (column_swap_id, swap_usage, -1);
 
             if(ram_usage >= 90)
             {
diff --git a/src/memory-monitor.vala b/src/memory-monitor.vala
index c00a4b2..81ee604 100644
--- a/src/memory-monitor.vala
+++ b/src/memory-monitor.vala
@@ -4,31 +4,42 @@ namespace Usage
 {
     public class MemoryMonitor
     {
-        private double ram_usage;
-        private double swap_usage;
+        private uint64 ram_usage;
+        private uint64 ram_total;
+        private uint64 swap_usage;
+        private uint64 swap_total;
 
         public void update()
         {
             /* Memory */
             GTop.Mem mem;
             GTop.get_mem (out mem);
-            ram_usage = (((double) (mem.used - mem.buffer - mem.cached)) / mem.total) * 100;
+            ram_usage = mem.used - mem.buffer - mem.cached;
+            ram_total = mem.total;
 
             /* Swap */
             GTop.Swap swap;
             GTop.get_swap (out swap);
-            swap_usage = ((double) swap.used / swap.total) * 100;
+            swap_usage = swap.used;
+            swap_total = swap.total;
         }
 
-        public double get_ram_usage()
+        public uint64 get_ram_usage()
         {
             return ram_usage;
         }
-
-        public double get_swap_usage()
+        public uint64 get_swap_usage()
         {
             return swap_usage;
         }
+        public uint64 get_ram_total()
+        {
+            return ram_total;
+        }
+        public uint64 get_swap_total()
+        {
+            return swap_total;
+        }
 
         public void update_process_info(ref Process process)
         {
@@ -39,7 +50,6 @@ namespace Usage
             GTop.get_proc_mem (out proc_mem, process.get_pid());
 
             process.set_mem_usage(proc_mem.resident - proc_mem.share);
-            process.set_mem_usage_percentages(((double) (proc_mem.resident - proc_mem.share) / mem.total) * 
100);
         }
     }
 }
\ No newline at end of file
diff --git a/src/network-graph-table.vala b/src/network-graph-table.vala
index 1863622..a459d5e 100644
--- a/src/network-graph-table.vala
+++ b/src/network-graph-table.vala
@@ -29,8 +29,8 @@ namespace Usage {
 
             SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
 
-            double net_download = monitor.net_download_actual;
-            double net_upload = monitor.net_upload_actual;
+            double net_download = monitor.net_download;
+            double net_upload = monitor.net_upload;
 
             iter.set (column_download_id, net_download, -1);
             iter.set (column_upload_id, net_upload, -1);
diff --git a/src/network-monitor.vala b/src/network-monitor.vala
index dc4e6cb..1a88f54 100644
--- a/src/network-monitor.vala
+++ b/src/network-monitor.vala
@@ -5,9 +5,6 @@ namespace Usage
 {
     public class NetworkMonitor
     {
-        private uint64 download_usage_actual;
-        private uint64 upload_usage_actual;
-        private uint64 net_usage_actual;
         private uint64 download_usage;
         private uint64 upload_usage;
         private uint64 net_usage;
@@ -51,32 +48,13 @@ namespace Usage
             handle_error(stat.get_bytes_per_attr(null, InoutEnum.INCOMING, null, out bytes_in_unasigned));
             handle_error(stat.get_unassigned_bytes(out bytes_all_unasigned));
 
-            upload_usage_actual = bytes_out;
-            download_usage_actual = bytes_in;
-            net_usage_actual = bytes_all;
-
-            upload_usage += bytes_out - bytes_out_unasigned;
-            download_usage += bytes_in - bytes_in_unasigned;
-            net_usage += bytes_all - bytes_all_unasigned;
+            upload_usage = bytes_out - bytes_out_unasigned;
+            download_usage = bytes_in - bytes_in_unasigned;
+            net_usage = bytes_all - bytes_all_unasigned;
 
             handle_error(netinfo.clear());
         }
 
-        public uint64 get_net_download_actual()
-        {
-            return download_usage_actual;
-        }
-
-        public uint64 get_net_upload_actual()
-        {
-            return upload_usage_actual;
-        }
-
-        public uint64 get_net_usage_actual()
-        {
-            return net_usage_actual;
-        }
-
         public uint64 get_net_download()
         {
             return download_usage;
@@ -101,9 +79,9 @@ namespace Usage
             handle_error(stat.get_bytes_per_attr(process.get_pid(), InoutEnum.INCOMING, null, out bytes_in));
             handle_error(stat.get_bytes_per_pid(process.get_pid(), out bytes_all));
 
-            process.set_net_upload(process.get_net_upload() + bytes_out);
-            process.set_net_download(process.get_net_download() + bytes_in);
-            process.set_net_all(process.get_net_all() + bytes_all);
+            process.set_net_upload(bytes_out);
+            process.set_net_download(bytes_in);
+            process.set_net_all(bytes_all);
         }
 
         private void handle_error(ErrorCode e)
diff --git a/src/process-dialog.vala b/src/process-dialog.vala
index 2efc36b..432362f 100644
--- a/src/process-dialog.vala
+++ b/src/process-dialog.vala
@@ -37,11 +37,11 @@ namespace Usage
             grid.margin_start = 20;
             grid.margin_end = 20;
 
-            processor_graph_block = new GraphBlock(_("Processor"), this.title);
-            memory_graph_block = new GraphBlock(_("Memory"), this.title);
-            disk_graph_block = new GraphBlock(_("Disk I/O"), this.title);
-            downloads_graph_block = new GraphBlock(_("Downloads"), this.title, false);
-            uploads_graph_block = new GraphBlock(_("Uploads"), this.title, false);
+            processor_graph_block = new GraphBlock(GraphBlockType.PROCESSOR, _("Processor"), this.title);
+            memory_graph_block = new GraphBlock(GraphBlockType.MEMORY, _("Memory"), this.title);
+            disk_graph_block = new GraphBlock(GraphBlockType.DISK, _("Disk I/O"), this.title);
+            downloads_graph_block = new GraphBlock(GraphBlockType.NETWORK, _("Downloads"), this.title, 
false);
+            uploads_graph_block = new GraphBlock(GraphBlockType.NETWORK, _("Uploads"), this.title, false);
 
             grid.attach(processor_graph_block, 0, 0, 1, 1);
             grid.attach(memory_graph_block, 1, 0, 1, 1);
@@ -64,48 +64,33 @@ namespace Usage
        private bool update()
        {
            SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();
-           unowned Process data = monitor.get_process_by_pid(pid);
+            unowned Process data = monitor.get_process_by_pid(pid);
 
             ProcessStatus process_status = ProcessStatus.DEAD;
-           int app_cpu_load = 0;
-           int app_memory_usage = 0;
-           int app_download = 0;
-           int app_upload = 0;
-
-           int other_download = 0;
-            int other_upload = 0;
-
-           if(data != null)
-           {
-               app_cpu_load = (int) data.get_cpu_load();
-               app_memory_usage = (int) data.get_mem_usage_percentages();
-               processor_graph_block.update((int) data.get_last_processor(), app_cpu_load, (int) 
monitor.x_cpu_load[data.get_last_processor()]-app_cpu_load);
-
-                double download_one_percentage = monitor.net_download / 100;
-                if(download_one_percentage != 0)
-                {
-                    app_download = (int) (data.get_net_download() / download_one_percentage);
-                    app_download = int.min(app_download, 100);
-                    other_download = 100 - app_download;
-                }
-
-                double upload_one_percentage = monitor.net_upload / 100;
-                if(upload_one_percentage != 0)
-                {
-                    app_upload = (int) (data.get_net_upload() / upload_one_percentage);
-                    app_upload = int.min(app_upload, 100);
-                    other_upload = 100 - app_upload;
-                }
+            int app_cpu_load = 0;
+            uint64 app_memory_usage = 0;
+            uint64 app_net_download = 0;
+            uint64 app_net_upload = 0;
+
+            if(data != null)
+            {
+                app_cpu_load = (int) data.get_cpu_load();
+                app_memory_usage = data.get_mem_usage();
+                int processor_other = (int) monitor.x_cpu_load[data.get_last_processor()] - app_cpu_load;
+                processor_other = int.max(processor_other, 0);
+                processor_graph_block.update(app_cpu_load, processor_other, 100, (int) 
data.get_last_processor());
                 process_status = data.get_status();
-           }
-           else
-               processor_graph_block.update(-1, 0, (int) monitor.cpu_load);
+                app_net_download = data.get_net_download();
+                app_net_upload = data.get_net_upload();
+            }
+            else
+                processor_graph_block.update(0, (int) monitor.cpu_load, 100);
 
-           memory_graph_block.update(-1, app_memory_usage, (int) monitor.ram_usage);
-           downloads_graph_block.update(-1, app_download, other_download);
-            uploads_graph_block.update(-1, app_upload, other_upload);
+            memory_graph_block.update(app_memory_usage, monitor.ram_usage, monitor.ram_total);
+            downloads_graph_block.update(app_net_download, monitor.net_download - app_net_download, 
monitor.net_download);
+            uploads_graph_block.update(app_net_upload, monitor.net_upload - app_net_upload, 
monitor.net_upload);
             headerbar.set_process_state(process_status);
-           return true;
+            return true;
        }
     }
 
diff --git a/src/process-list-box.vala b/src/process-list-box.vala
index 2a0dde0..71b4233 100644
--- a/src/process-list-box.vala
+++ b/src/process-list-box.vala
@@ -55,7 +55,19 @@ namespace Usage
 
             var settings = (GLib.Application.get_default() as Application).settings;
 
-            Timeout.add(settings.list_update_interval_UI, update);
+            uint update_interval;
+            switch(type)
+            {
+                default:
+                case ProcessListBoxType.PROCESSOR:
+                case ProcessListBoxType.MEMORY:
+                    update_interval = settings.list_update_interval_UI;
+                    break;
+                case ProcessListBoxType.NETWORK:
+                    update_interval = settings.list_update_interval_UI_often;
+                    break;
+            }
+            Timeout.add(update_interval, update);
         }
 
         public bool update()
diff --git a/src/process-row.vala b/src/process-row.vala
index 0e1afaa..8a63147 100644
--- a/src/process-row.vala
+++ b/src/process-row.vala
@@ -6,6 +6,7 @@ namespace Usage
     public class ProcessRow : Gtk.ListBoxRow
     {
         Gtk.Label load_label;
+        Gtk.Label title_label;
         Gtk.Revealer revealer;
         SubProcessListBox sub_process_list_box;
         string display_name;
@@ -35,7 +36,7 @@ namespace Usage
 
             Gtk.Image icon;
             load_icon_and_name(out icon, out display_name);
-            var title_label = new Gtk.Label(display_name);
+            title_label = new Gtk.Label(display_name);
             row_box.pack_start(icon, false, false, 0);
             row_box.pack_start(title_label, false, true, 5);
             row_box.pack_end(load_label, false, true, 10);
@@ -160,6 +161,7 @@ namespace Usage
                         foreach(uint64 value in values)
                             values_string += "   " + value.to_string() + " %";
 
+                        title_label.label += " (" + process.get_sub_processes().size().to_string() + ")";
                         load_label.set_label(values_string);
                     }
                     else
@@ -171,6 +173,8 @@ namespace Usage
                         max_usage = false;
                     break;
                 case ProcessListBoxType.MEMORY:
+                    SystemMonitor monitor = (GLib.Application.get_default() as 
Application).get_system_monitor();
+
                     if(group)
                     {
                         string values_string = "";
@@ -181,12 +185,13 @@ namespace Usage
                         foreach(uint64 value in values)
                             values_string += "   " + Utils.format_size_values(value);
 
+                        title_label.label += " (" + process.get_sub_processes().size().to_string() + ")";
                         load_label.set_label(values_string);
                     }
                     else
                         load_label.set_label(Utils.format_size_values(process.get_mem_usage()));
 
-                    if(process.get_mem_usage_percentages() >= 90)
+                    if((((double) process.get_mem_usage() / monitor.ram_total) * 100) >= 90)
                         max_usage = true;
                     else
                         max_usage = false;
@@ -200,13 +205,14 @@ namespace Usage
                             values.insert_sorted((uint64) sub_process.get_net_all(), sort);
 
                          foreach(uint64 value in values)
-                            values_string += "   " + Utils.format_size_values(value);
+                            values_string += "   " + Utils.format_size_speed_values(value);
 
+                        title_label.label += " (" + process.get_sub_processes().size().to_string() + ")";
                         load_label.set_label(values_string);
                     }
                     else
                     {
-                        load_label.set_label(Utils.format_size_values(process.get_net_all()));
+                        load_label.set_label(Utils.format_size_speed_values(process.get_net_all()));
                     }
                     break;
             }
diff --git a/src/process.vala b/src/process.vala
index 1d52a97..21d6f12 100644
--- a/src/process.vala
+++ b/src/process.vala
@@ -2,7 +2,6 @@ using Posix;
 
 namespace Usage
 {
-    [Compact]
     public class Process : Object
     {
         pid_t pid;
@@ -14,7 +13,6 @@ namespace Usage
         uint64 x_cpu_last_used;
         uint last_processor;
         uint64 mem_usage;
-        double mem_usage_percentages;
         uint64 net_download;
         uint64 net_upload;
         uint64 net_all;
@@ -33,7 +31,6 @@ namespace Usage
             this.x_cpu_last_used = 0;
             this.last_processor = 0;
             this.mem_usage = 0;
-            this.mem_usage_percentages = 0;
             this.net_download = 0;
             this.net_upload = 0;
             this.net_all = 0;
@@ -147,16 +144,6 @@ namespace Usage
             this.mem_usage = mem_usage;
         }
 
-        public double get_mem_usage_percentages()
-        {
-            return mem_usage_percentages;
-        }
-
-        public void set_mem_usage_percentages(double mem_usage_percentages)
-        {
-            this.mem_usage_percentages = mem_usage_percentages;
-        }
-
         public uint64 get_net_download()
         {
             return net_download;
diff --git a/src/settings.vala b/src/settings.vala
index 484b9cf..f7f1f01 100644
--- a/src/settings.vala
+++ b/src/settings.vala
@@ -8,6 +8,7 @@ namespace Usage {
         public uint graph_max_samples { get; set; default = 20; }
         public uint graph_update_interval { get { return 1000; }}
         public uint list_update_interval_UI { get; set; default = 5000; }
+        public uint list_update_interval_UI_often { get; set; default = 1000; }
         public uint list_update_pie_charts_UI { get; set; default = 1000; }
         public uint data_update_interval { get; set; default = 1000; }
     }
diff --git a/src/sub-process-sub-row.vala b/src/sub-process-sub-row.vala
index cad2e37..c1d77a9 100644
--- a/src/sub-process-sub-row.vala
+++ b/src/sub-process-sub-row.vala
@@ -51,15 +51,16 @@ namespace Usage
                         max_usage = false;
                     break;
                 case ProcessListBoxType.MEMORY:
+                    SystemMonitor monitor = (GLib.Application.get_default() as 
Application).get_system_monitor();
                     load_label.set_label(Utils.format_size_values(process.get_mem_usage()));
 
-                    if(process.get_mem_usage_percentages() >= 90)
+                    if((((double) process.get_mem_usage() / monitor.ram_total) * 100) >= 90)
                         max_usage = true;
                     else
                         max_usage = false;
                     break;
                 case ProcessListBoxType.NETWORK:
-                    load_label.set_label(Utils.format_size_values(process.get_net_all()));
+                    load_label.set_label(Utils.format_size_speed_values(process.get_net_all()));
                     break;
             }
         }
diff --git a/src/system-monitor.vala b/src/system-monitor.vala
index 92082ba..2af8c29 100644
--- a/src/system-monitor.vala
+++ b/src/system-monitor.vala
@@ -7,11 +7,10 @@ namespace Usage
         public signal void cpu_processes_ready();
         public double cpu_load { get; private set; }
         public double[] x_cpu_load { get; private set; }
-        public double ram_usage { get; private set; }
-        public double swap_usage { get; private set; }
-        public uint64 net_download_actual { get; private set; }
-        public uint64 net_upload_actual { get; private set; }
-        public uint64 net_usage_actual { get; private set; }
+        public uint64 ram_usage { get; private set; }
+        public uint64 ram_total { get; private set; }
+        public uint64 swap_usage { get; private set; }
+        public uint64 swap_total { get; private set; }
         public uint64 net_download { get; private set; }
         public uint64 net_upload { get; private set; }
         public uint64 net_usage { get; private set; }
@@ -107,10 +106,9 @@ namespace Usage
             cpu_load = cpu_monitor.get_cpu_load();
             x_cpu_load = cpu_monitor.get_x_cpu_load();
             ram_usage = memory_monitor.get_ram_usage();
+            ram_total = memory_monitor.get_ram_total();
             swap_usage = memory_monitor.get_swap_usage();
-            net_download_actual = network_monitor.get_net_download_actual();
-            net_upload_actual = network_monitor.get_net_upload_actual();
-            net_usage_actual = network_monitor.get_net_usage_actual();
+            swap_total = memory_monitor.get_swap_total();
             net_download = network_monitor.get_net_download();
             net_upload = network_monitor.get_net_upload();
             net_usage = network_monitor.get_net_usage();
diff --git a/src/utils.vala b/src/utils.vala
index c3d6438..37f7138 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -17,5 +17,19 @@ namespace Usage
             else
                 return value.to_string() + " B";
         }
+
+        public static string format_size_speed_values(uint64 value)
+        {
+            if(value >= 1000000000000)
+                return "%.3f TB/s".printf((double) value / 1000000000000d);
+            else if(value >= 1000000000)
+                return "%.1f GB/s".printf((double) value / 1000000000d);
+            else if(value >= 1000000)
+                return "%.2f MB/s".printf((double) value / 1000000d);
+            else if(value >= 1000)
+                return (value / 1000).to_string() + " KB/s";
+            else
+                return value.to_string() + " B/s";
+        }
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]