[gnome-system-monitor] Use two decimals precision for CPU usage (fixes #140)



commit bd6df83731c6f86718d8bb1a33cc578333fd85a0
Author: Robert Roth <robert roth off gmail com>
Date:   Sun Oct 18 12:27:25 2020 +0300

    Use two decimals precision for CPU usage (fixes #140)

 src/application.h      |  4 ++--
 src/procproperties.cpp |  2 +-
 src/proctable.cpp      | 11 ++++++++---
 src/util.cpp           | 21 +++++++++++++++++++++
 src/util.h             |  3 +++
 5 files changed, 35 insertions(+), 6 deletions(-)
---
diff --git a/src/application.h b/src/application.h
index 49efc802..57385f6a 100644
--- a/src/application.h
+++ b/src/application.h
@@ -87,7 +87,7 @@ MutableProcInfo()
     disk_read_bytes_current(0ULL),
     disk_write_bytes_current(0ULL),
     status(0U),
-    pcpu(0U),
+    pcpu(0),
     nice(0)
     {
     }
@@ -118,7 +118,7 @@ MutableProcInfo()
     guint64 disk_read_bytes_current;
     guint64 disk_write_bytes_current;
     guint status;
-    guint pcpu;
+    gdouble pcpu;
     gint nice;
     std::string cgroup_name;
 
diff --git a/src/procproperties.cpp b/src/procproperties.cpp
index 5e751a54..74764488 100644
--- a/src/procproperties.cpp
+++ b/src/procproperties.cpp
@@ -75,7 +75,7 @@ fill_proc_properties (GtkTreeView *tree, ProcInfo *info)
 #ifdef HAVE_WNCK
         { N_("X Server Memory"), format_memsize(info->memxserver)},
 #endif
-        { N_("CPU"), g_strdup_printf("%d%%", info->pcpu)},
+        { N_("CPU"), g_strdup_printf("%.2f%%", info->pcpu)},
         { N_("CPU Time"), procman::format_duration_for_display(100 * info->cpu_time / 
GsmApplication::get()->frequency) },
         { N_("Started"), procman_format_date_for_display(info->start_time) },
         { N_("Nice"), g_strdup_printf("%d", info->nice)},
diff --git a/src/proctable.cpp b/src/proctable.cpp
index 868b8afd..925e7be1 100644
--- a/src/proctable.cpp
+++ b/src/proctable.cpp
@@ -377,7 +377,7 @@ proctable_new (GsmApplication * const app)
                                 G_TYPE_ULONG,       /* Writable Memory */
                                 G_TYPE_ULONG,       /* Shared Memory */
                                 G_TYPE_ULONG,       /* X Server Memory */
-                                G_TYPE_UINT,        /* % CPU        */
+                                G_TYPE_DOUBLE,      /* % CPU        */
                                 G_TYPE_UINT64,      /* CPU time     */
                                 G_TYPE_ULONG,       /* Started      */
                                 G_TYPE_INT,         /* Nice         */
@@ -480,7 +480,12 @@ proctable_new (GsmApplication * const app)
                                                         GUINT_TO_POINTER(i),
                                                         NULL);
                 break;
-
+            case COL_CPU:
+                gtk_tree_view_column_set_cell_data_func(col, cell,
+                                                        &procman::percentage_cell_data_func,
+                                                        GUINT_TO_POINTER(i),
+                                                        NULL);
+                break;
             case COL_CPU_TIME:
                 gtk_tree_view_column_set_cell_data_func(col, cell,
                                                         &procman::duration_cell_data_func,
@@ -965,7 +970,7 @@ update_info (GsmApplication *app, ProcInfo *info)
     if (not app->config.solaris_mode)
         cpu_scale *= app->config.num_cpus;
 
-    info->pcpu = difference * cpu_scale / app->cpu_total_time;
+    info->pcpu = (gdouble)difference * cpu_scale / app->cpu_total_time;
     info->pcpu = MIN(info->pcpu, cpu_scale);
 
     app->processes.cpu_times[info->pid] = info->cpu_time = proctime.rtime;
diff --git a/src/util.cpp b/src/util.cpp
index 07f7c5fe..7431121f 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -280,6 +280,9 @@ get_size_from_column(GtkTreeModel* model, GtkTreeIter* first,
         case G_TYPE_UINT64:
             size = g_value_get_uint64(&value);
             break;
+        case G_TYPE_DOUBLE:
+            size = g_value_get_double(&value);
+            break;
         default:
             g_assert_not_reached();
     }
@@ -342,6 +345,24 @@ namespace procman
         g_free(str);
     }
 
+    void percentage_cell_data_func(GtkTreeViewColumn *, GtkCellRenderer *renderer,
+                             GtkTreeModel *model, GtkTreeIter *iter,
+                             gpointer user_data)
+    {
+        const guint index = GPOINTER_TO_UINT(user_data);
+
+        gdouble size;
+        GValue value = { 0 };
+
+        gtk_tree_model_get_value(model, iter, index, &value);
+        size = g_value_get_double(&value);
+        g_value_unset(&value);
+
+        char *str = g_strdup_printf("%.2f", size);
+        g_object_set(renderer, "text", str, NULL);
+        g_free(str);
+    }
+
 
     /*
       Same as above but handles size == 0 as not available
diff --git a/src/util.h b/src/util.h
index 8e94766c..ff3fa670 100644
--- a/src/util.h
+++ b/src/util.h
@@ -69,6 +69,9 @@ namespace procman
     void time_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                              GtkTreeModel *model, GtkTreeIter *iter,
                              gpointer user_data);
+    void percentage_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+                             GtkTreeModel *model, GtkTreeIter *iter,
+                             gpointer user_data);
 
     void status_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                                GtkTreeModel *model, GtkTreeIter *iter,


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