[gnome-system-monitor: 3/7] Added option for showing memory graph in logarithmic scale.




commit 82d493078746cbff0c413f7fa455fd07dcd87e33
Author: Pawel Chalkowski <chauek gmail com>
Date:   Fri Jan 1 11:44:13 2021 +0100

    Added option for showing memory graph in logarithmic scale.

 data/preferences.ui                               | 22 +++++-
 src/application.cpp                               | 24 +++++--
 src/application.h                                 |  2 +
 src/load-graph.cpp                                | 81 ++++++++++++++++++-----
 src/load-graph.h                                  |  3 +
 src/org.gnome.gnome-system-monitor.gschema.xml.in | 11 ++-
 src/prefsdialog.cpp                               | 19 ++++--
 src/settings-keys.h                               |  1 +
 8 files changed, 130 insertions(+), 33 deletions(-)
---
diff --git a/data/preferences.ui b/data/preferences.ui
index 64f90e8b..edafa9e1 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -439,6 +439,24 @@
                                 <property name="height">1</property>
                               </packing>
                             </child>
+                            <child>
+                              <object class="GtkCheckButton" id="logarithmic_scale_button">
+                                <property name="label" translatable="yes">Show memory in logarithmic 
scale</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="receives_default">False</property>
+                                <property name="hexpand">True</property>
+                                <property name="use_underline">True</property>
+                                <property name="halign">start</property>
+                                <property name="draw_indicator">True</property>
+                              </object>
+                              <packing>
+                                <property name="left_attach">0</property>
+                                <property name="top_attach">2</property>
+                                <property name="width">1</property>
+                                <property name="height">1</property>
+                              </packing>
+                            </child>
                             <child>
                               <object class="GtkCheckButton" id="draw_stacked_button">
                                 <property name="label" translatable="yes">_Draw CPU chart as stacked area 
chart</property>
@@ -452,7 +470,7 @@
                               </object>
                               <packing>
                                 <property name="left_attach">0</property>
-                                <property name="top_attach">2</property>
+                                <property name="top_attach">3</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
@@ -470,7 +488,7 @@
                               </object>
                               <packing>
                                 <property name="left_attach">0</property>
-                                <property name="top_attach">3</property>
+                                <property name="top_attach">4</property>
                                 <property name="width">1</property>
                                 <property name="height">1</property>
                               </packing>
diff --git a/src/application.cpp b/src/application.cpp
index 247f7a64..191934d0 100644
--- a/src/application.cpp
+++ b/src/application.cpp
@@ -32,13 +32,20 @@ cb_solaris_mode_changed (Gio::Settings& settings, Glib::ustring key, GsmApplicat
 }
 
 static void
-cb_process_memory_in_iec_changed (Gio::Settings& settings, Glib::ustring key, GsmApplication* app)
+cb_process_memory_in_iec_changed (Gio::Settings& settings, Glib::ustring key, GsmApplication* app) {
+       app->config.process_memory_in_iec = settings.get_boolean(key);
+       app->cpu_graph->clear_background();
+       if (app->timeout) {
+               proctable_update(app);
+       }
+}
+
+static void
+cb_logarithmic_scale_changed (Gio::Settings& settings, Glib::ustring key, GsmApplication* app)
 {
-    app->config.process_memory_in_iec = settings.get_boolean(key);
-    app->cpu_graph->clear_background();
-    if (app->timeout) {
-        proctable_update (app);
-    }
+    app->config.logarithmic_scale = settings.get_boolean(key);
+    app->mem_graph->clear_background();
+    load_graph_reset(app->mem_graph);
 }
 
 static void
@@ -222,6 +229,11 @@ GsmApplication::load_settings()
     config.process_memory_in_iec = this->settings->get_boolean (GSM_SETTING_PROCESS_MEMORY_IN_IEC);
     this->settings->signal_changed (GSM_SETTING_PROCESS_MEMORY_IN_IEC).connect ([this](const Glib::ustring& 
key) {
         cb_process_memory_in_iec_changed (*this->settings.operator->(), key, this);
+       });
+
+    config.logarithmic_scale = this->settings->get_boolean (GSM_SETTING_LOGARITHMIC_SCALE);
+    this->settings->signal_changed(GSM_SETTING_LOGARITHMIC_SCALE).connect ([this](const Glib::ustring& key) {
+        cb_logarithmic_scale_changed (*this->settings.operator->(), key, this);
     });
 
     config.draw_stacked = this->settings->get_boolean (GSM_SETTING_DRAW_STACKED);
diff --git a/src/application.h b/src/application.h
index 9569a060..c298f4c2 100644
--- a/src/application.h
+++ b/src/application.h
@@ -43,6 +43,7 @@ struct ProcConfig
        num_cpus(0),
        solaris_mode(false),
        process_memory_in_iec(true),
+    logarithmic_scale(false),
        draw_stacked(false),
        draw_smooth(true),
        resources_memory_in_iec(true),
@@ -67,6 +68,7 @@ struct ProcConfig
     gint            num_cpus;
     bool solaris_mode;
     bool process_memory_in_iec;
+    bool logarithmic_scale;
     bool draw_stacked;
     bool draw_smooth;
     bool resources_memory_in_iec;
diff --git a/src/load-graph.cpp b/src/load-graph.cpp
index 6b7d5382..d85ec1b4 100644
--- a/src/load-graph.cpp
+++ b/src/load-graph.cpp
@@ -1,6 +1,8 @@
 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 #include <config.h>
 
+#include <math.h>
+
 #include <glib/gi18n.h>
 
 #include <glibtop.h>
@@ -24,6 +26,11 @@ void LoadGraph::clear_background()
     }
 }
 
+bool LoadGraph::is_logarithmic_scale() const
+{
+    // logarithmic scale is used only for memory graph
+    return this->type == LOAD_GRAPH_MEM && GsmApplication::get()->config.logarithmic_scale;
+}
 
 unsigned LoadGraph::num_bars() const
 {
@@ -43,13 +50,61 @@ unsigned LoadGraph::num_bars() const
         case 4:
             n = 4;
             break;
+        case 5:
+            n = 5;
+            if (this->is_logarithmic_scale())
+                n = 4;
+            break;
         default:
             n = 5;
+            if (this->is_logarithmic_scale())
+                n = 6;
     }
 
     return n;
 }
 
+/*
+ Returns Y scale caption based on give index of the label.
+ Takes into account whether the scale should be logarithmic for memory graph.
+ */
+char* LoadGraph::get_caption(guint index)
+{
+    char *caption;
+    unsigned num_bars = this->num_bars();
+    guint64 max_value;
+    if (this->type == LOAD_GRAPH_NET)
+        max_value = this->net.max;
+    else
+        max_value = 100;
+
+    // operation orders matters so it's 0 if index == num_bars
+    float caption_percentage = (float)max_value - index * (float)max_value / num_bars;
+
+    if (this->is_logarithmic_scale()) {
+        float caption_value = caption_percentage == 0 ? 0 : pow(100, caption_percentage / max_value);
+        caption = g_strdup_printf("%.0f %%", caption_value);
+    } else if (this->type == LOAD_GRAPH_NET) {
+        const std::string captionstr(procman::format_network_rate((guint64)caption_percentage));
+        caption = g_strdup(captionstr.c_str());
+    } else {
+        caption = g_strdup_printf("%.0f %%", caption_percentage);
+    }
+
+    return caption;
+}
+
+/*
+ Translates y partial position to logarithmic position if set to logarithmic scale.
+*/
+float LoadGraph::translate_to_log_partial_if_needed(float position_partial)
+{
+    if (this->is_logarithmic_scale())
+        position_partial = position_partial == 0 ? 0 : log10(position_partial * 100) / 2;
+
+    return position_partial;
+}
+
 gchar* format_duration(unsigned seconds) {
     gchar* caption = NULL;
 
@@ -88,10 +143,10 @@ gchar* format_duration(unsigned seconds) {
     g_free (captionH);
     g_free (captionM);
     g_free (captionS);
+
     return caption;
 }
 
-
 const int FRAME_WIDTH = 4;
 static void draw_background(LoadGraph *graph) {
     GtkAllocation allocation;
@@ -124,7 +179,7 @@ static void draw_background(LoadGraph *graph) {
     cr = cairo_create (surface);
 
     GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (GsmApplication::get()->stack));
-    
+
     gtk_style_context_get_color (context, gtk_widget_get_state_flags (GTK_WIDGET 
(GsmApplication::get()->stack)), &fg);
 
     cairo_paint_with_alpha (cr, 0.0);
@@ -172,7 +227,7 @@ static void draw_background(LoadGraph *graph) {
     gtk_style_context_restore (context);
 
     cairo_set_line_width (cr, 1.0);
-    
+
     for (i = 0; i <= num_bars; ++i) {
         double y;
 
@@ -184,15 +239,7 @@ static void draw_background(LoadGraph *graph) {
             y = i * graph->graph_dely + graph->fontsize / 2.0;
 
         gdk_cairo_set_source_rgba (cr, &fg);
-        if (graph->type == LOAD_GRAPH_NET) {
-            // operation orders matters so it's 0 if i == num_bars
-            guint64 rate = graph->net.max - (i * graph->net.max / num_bars);
-            const std::string captionstr(procman::format_network_rate(rate));
-            caption = g_strdup(captionstr.c_str());
-        } else {
-            // operation orders matters so it's 0 if i == num_bars
-            caption = g_strdup_printf("%d %%", 100 - i * (100 / num_bars));
-        }
+        caption = graph->get_caption(i);
         pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
         pango_layout_set_text (layout, caption, -1);
         pango_layout_get_extents (layout, NULL, &extents);
@@ -215,7 +262,7 @@ static void draw_background(LoadGraph *graph) {
         cairo_line_to (cr, graph->draw_width - graph->rmargin + 0.5 + 4, i * graph->graph_dely + 0.5);
         cairo_stroke (cr);
     }
-    
+
 
     const unsigned total_seconds = graph->speed * (graph->num_points - 2) / 1000;
 
@@ -520,11 +567,11 @@ get_memory (LoadGraph *graph)
     set_memory_label_and_picker(GTK_LABEL(graph->labels.swap),
                                 GSM_COLOR_BUTTON(graph->swap_color_picker),
                                 swap.used, 0, swap.total, swappercent);
-    
+
     gtk_widget_set_sensitive (GTK_WIDGET (graph->swap_color_picker), swap.total > 0);
-    
-    graph->data[0][0] = mempercent;
-    graph->data[0][1] = swap.total>0 ? swappercent : -1.0;
+
+    graph->data[0][0] = graph->translate_to_log_partial_if_needed(mempercent);
+    graph->data[0][1] = swap.total>0 ? graph->translate_to_log_partial_if_needed(swappercent) : -1.0;
 }
 
 /* Nice Numbers for Graph Labels after Paul Heckbert
diff --git a/src/load-graph.h b/src/load-graph.h
index 01822c8d..5ec71d5d 100644
--- a/src/load-graph.h
+++ b/src/load-graph.h
@@ -44,6 +44,9 @@ struct LoadGraph
 
     unsigned num_bars() const;
     void clear_background();
+    bool is_logarithmic_scale() const;
+    char* get_caption(guint index);
+    float translate_to_log_partial_if_needed(float position_partial);
 
     double fontsize;
     double rmargin;
diff --git a/src/org.gnome.gnome-system-monitor.gschema.xml.in 
b/src/org.gnome.gnome-system-monitor.gschema.xml.in
index 1630026a..617a3578 100644
--- a/src/org.gnome.gnome-system-monitor.gschema.xml.in
+++ b/src/org.gnome.gnome-system-monitor.gschema.xml.in
@@ -177,6 +177,15 @@
       </summary>
     </key>
 
+    <key name="logarithmic-scale" type="b">
+      <default>false
+      </default>
+      <summary>Show memory in logarithmic scale
+      </summary>
+      <description>If TRUE, system-monitor shows the CPU chart as a stacked area chart instead of a line 
chart.
+      </description>
+    </key>
+
     <key name="cpu-stacked-area-chart" type="b">
       <default>false
       </default>
@@ -631,7 +640,7 @@
       <summary>Disk view columns order
       </summary>
     </key>
-    
+
     <key name="col-0-width" type="i">
       <default>100
       </default>
diff --git a/src/prefsdialog.cpp b/src/prefsdialog.cpp
index 95cf86fb..76aeacbd 100644
--- a/src/prefsdialog.cpp
+++ b/src/prefsdialog.cpp
@@ -112,8 +112,8 @@ field_toggled (const gchar *gsettings_parent, gchar *path_str, gpointer data)
 
 }
 
-static void 
-field_row_activated ( GtkTreeView *tree, GtkTreePath *path, 
+static void
+field_row_activated ( GtkTreeView *tree, GtkTreePath *path,
                       GtkTreeViewColumn *column, gpointer data)
 {
     GtkTreeModel * model = gtk_tree_view_get_model (tree);
@@ -160,13 +160,13 @@ create_field_page(GtkBuilder* builder, GtkTreeView *tree, const gchar *widgetnam
     gtk_tree_view_column_set_attributes (column, cell,
                                          "active", 0,
                                          NULL);
-    if(!g_strcmp0(widgetname, "proctree")) 
+    if(!g_strcmp0(widgetname, "proctree"))
         g_signal_connect (G_OBJECT (cell), "toggled", G_CALLBACK (proc_field_toggled), model);
-    else if(!g_strcmp0(widgetname, "disktreenew")) 
+    else if(!g_strcmp0(widgetname, "disktreenew"))
         g_signal_connect (G_OBJECT (cell), "toggled", G_CALLBACK (disk_field_toggled), model);
-        
+
     g_signal_connect (G_OBJECT (GTK_TREE_VIEW (treeview)), "row-activated", G_CALLBACK 
(field_row_activated), (gpointer)widgetname);
-    
+
     gtk_tree_view_column_set_clickable (column, TRUE);
     gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
 
@@ -279,6 +279,11 @@ create_preferences_dialog (GsmApplication *app)
                     proc_mem_in_iec_button, "active",
                     G_SETTINGS_BIND_DEFAULT);
 
+    GtkCheckButton *logarithmic_scale_button = GTK_CHECK_BUTTON (gtk_builder_get_object (builder, 
"logarithmic_scale_button"));
+    g_settings_bind (app->settings->gobj (), GSM_SETTING_LOGARITHMIC_SCALE,
+                     logarithmic_scale_button, "active",
+                     G_SETTINGS_BIND_DEFAULT);
+
     GtkCheckButton *draw_stacked_button = GTK_CHECK_BUTTON (gtk_builder_get_object (builder, 
"draw_stacked_button"));
     g_settings_bind (app->settings->gobj (), GSM_SETTING_DRAW_STACKED,
                      draw_stacked_button, "active",
@@ -297,7 +302,7 @@ create_preferences_dialog (GsmApplication *app)
     create_field_page (builder, GTK_TREE_VIEW (app->tree), "proctree");
 
     update = (gfloat) app->config.graph_update_interval;
-    spin_button = GTK_SPIN_BUTTON (gtk_builder_get_object (builder, "resources_interval_spinner"));          
                                       
+    spin_button = GTK_SPIN_BUTTON (gtk_builder_get_object (builder, "resources_interval_spinner"));
     adjustment = gtk_spin_button_get_adjustment (spin_button);
     gtk_adjustment_configure (adjustment, update / 1000.0, 0.25,
                               100.0, 0.25, 1.0, 0);
diff --git a/src/settings-keys.h b/src/settings-keys.h
index d6111c27..d1c964e3 100644
--- a/src/settings-keys.h
+++ b/src/settings-keys.h
@@ -27,6 +27,7 @@
 #define GSM_SETTING_SWAP_COLOR              "swap-color"
 #define GSM_SETTING_NET_IN_COLOR            "net-in-color"
 #define GSM_SETTING_NET_OUT_COLOR           "net-out-color"
+#define GSM_SETTING_LOGARITHMIC_SCALE       "logarithmic-scale"
 #define GSM_SETTING_DRAW_STACKED            "cpu-stacked-area-chart"
 #define GSM_SETTING_DRAW_SMOOTH             "cpu-smooth-graph"
 #define GSM_SETTING_RESOURCES_MEMORY_IN_IEC "resources-memory-in-iec"


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