[gnome-system-monitor] Added new functions with Linux-only code



commit 1ae31332cec740c6c2bc18af21078c343fa11f14
Author: Jacob Barkdull <jacobbarkdull gmail com>
Date:   Tue Oct 20 22:04:41 2020 -0700

    Added new functions with Linux-only code
    
    I have now moved my code from libgtop into GNOME System Monitor. This code, and the code for executing 
taskset, are now in new functions with code that is wrapped in #ifdef statements so it only gets compiled on 
Linux. This commit also includes a few lines of code to hide the Set Affinity menu items on all platforms 
except Linux.

 src/interface.cpp   | 17 +++++++++++++-
 src/setaffinity.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 67 insertions(+), 14 deletions(-)
---
diff --git a/src/interface.cpp b/src/interface.cpp
index 02f677a6..f137db5d 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -74,6 +74,19 @@ search_text_changed (GtkEditable *entry, gpointer data)
                                     GTK_TREE_VIEW (app->tree))))));
 }
 
+static void
+set_affinity_visiblity (GtkWidget *widget, gpointer user_data)
+{
+#ifndef __linux__
+    GtkMenuItem *item = GTK_MENU_ITEM (widget);
+    const gchar *name = gtk_menu_item_get_label (item);
+
+    if (strcmp (name, "Set _Affinity") == 0) {
+        gtk_widget_set_visible (widget, false);
+    }
+#endif
+}
+
 static void 
 create_proc_view(GsmApplication *app, GtkBuilder * builder)
 {
@@ -91,7 +104,9 @@ create_proc_view(GsmApplication *app, GtkBuilder * builder)
     GMenuModel *menu_model = G_MENU_MODEL (gtk_builder_get_object (builder, "process-popup-menu"));
     app->popup_menu = GTK_MENU (gtk_menu_new_from_model (menu_model));
     gtk_menu_attach_to_widget (app->popup_menu, GTK_WIDGET (app->main_window), NULL);
-    
+
+    gtk_container_foreach (GTK_CONTAINER (app->popup_menu), set_affinity_visiblity, NULL);
+
     app->search_bar = GTK_SEARCH_BAR (gtk_builder_get_object (builder, "proc_searchbar"));
     app->search_entry = GTK_SEARCH_ENTRY (gtk_builder_get_object (builder, "proc_searchentry"));
     
diff --git a/src/setaffinity.cpp b/src/setaffinity.cpp
index 2bc3977f..e8c1546d 100644
--- a/src/setaffinity.cpp
+++ b/src/setaffinity.cpp
@@ -148,6 +148,53 @@ set_affinity_error (void)
                               dialog);
 }
 
+static guint16 *
+gsm_set_proc_affinity (glibtop_proc_affinity *buf, GArray *cpus, pid_t pid)
+{
+#ifdef __linux__
+    guint i;
+    cpu_set_t set;
+    guint16 cpu;
+
+    CPU_ZERO (&set);
+
+    for (i = 0; i < cpus->len; i++) {
+        cpu = g_array_index (cpus, guint16, i);
+        CPU_SET (cpu, &set);
+    }
+
+    if (sched_setaffinity (pid, sizeof (set), &set) != -1) {
+        return glibtop_get_proc_affinity (buf, pid);
+    }
+#endif
+
+    return NULL;
+}
+
+static void
+execute_taskset_command (gchar **cpu_list, pid_t pid)
+{
+#ifdef __linux__
+    gchar *pc;
+    gchar *command;
+
+    /* Join CPU number strings by commas for taskset command CPU list */
+    pc = g_strjoinv (",", cpu_list);
+
+    /* Construct taskset command */
+    command = g_strdup_printf ("taskset -pc %s %d", pc, pid);
+
+    /* Execute taskset command; show error on failure */
+    if (!multi_root_check (command)) {
+        set_affinity_error ();
+    }
+
+    /* Free memory for taskset command */
+    g_free (command);
+    g_free (pc);
+#endif
+}
+
 static void
 set_affinity (GtkToggleButton *button,
               gpointer         data)
@@ -161,8 +208,6 @@ set_affinity (GtkToggleButton *button,
     GArray   *cpuset;
     guint32   i;
     gint      taskset_cpu = 0;
-    gchar    *pc;
-    gchar    *command;
 
     /* Create string array for taskset command CPU list */
     cpu_list = g_new0 (gchar *, affinity->cpu_count);
@@ -188,18 +233,12 @@ set_affinity (GtkToggleButton *button,
             }
         }
 
-        /* Construct taskset command */
-        pc = g_strjoinv (",", cpu_list);
-        command = g_strdup_printf ("taskset -pc %s %d", pc, affinity->pid);
-
         /* Set process affinity; Show message dialog upon error */
-        if (glibtop_set_proc_affinity (&set_affinity, cpuset, affinity->pid) == NULL) {
+        if (gsm_set_proc_affinity (&set_affinity, cpuset, affinity->pid) == NULL) {
             /* If so, check whether an access error occurred */
             if (errno == EPERM or errno == EACCES) {
                 /* If so, attempt to run taskset as root, show error on failure */
-                if (!multi_root_check (command)) {
-                    set_affinity_error ();
-                }
+                execute_taskset_command (cpu_list, affinity->pid);
             } else {
                 /* If not, show error immediately */
                 set_affinity_error ();
@@ -211,9 +250,8 @@ set_affinity (GtkToggleButton *button,
              g_free (cpu_list[i]);
         }
 
-        /* Free memory for taskset command */
-        g_free (command);
-        g_free (pc);
+        /* Free CPU array memory */
+        g_array_free (cpuset, TRUE);
     } else {
         /* If not, show error message dialog */
         set_affinity_error ();


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