[gnome-system-monitor] Add cgroup support in the process view



commit 2d33adcbc4347c112d57082956b4e199ff7132db
Author: Jason Baron <jbaron redhat com>
Date:   Fri Nov 11 16:08:54 2011 -0500

    Add cgroup support in the process view
    
    Add a new 'Control Group' column to the process view tab.
    The format for the column is:
    
    <path name> (controller name), <path name> (controller name)...
    
    Processes that share the same path name across controllers
    are colesced. For example if a process is in the /foo cgroup
    for both the memory and cpu controllers, it would display as:
    
    /foo (memory,cpu), ...
    
    Signed-off-by: Jason Baron <jbaron redhat com>

 src/Makefile.am                                   |    1 +
 src/cgroups.cpp                                   |  103 +++++++++++++++++++++
 src/cgroups.h                                     |   14 +++
 src/org.gnome.gnome-system-monitor.gschema.xml.in |   15 +++
 src/procdialogs.cpp                               |    6 +
 src/procman.h                                     |    1 +
 src/proctable.cpp                                 |   21 ++++-
 src/proctable.h                                   |    1 +
 8 files changed, 160 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 56b6b83..cb8bdaa 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,6 +25,7 @@ gnome_system_monitor_SOURCES = \
 	defaulttable.h \
 	disks.cpp disks.h \
 	selinux.h selinux.cpp \
+	cgroups.h cgroups.cpp \
 	procman_gnomesu.h procman_gnomesu.cpp \
 	procman_gksu.h procman_gksu.cpp \
 	sysinfo.cpp sysinfo.h \
diff --git a/src/cgroups.cpp b/src/cgroups.cpp
new file mode 100644
index 0000000..010c1ff
--- /dev/null
+++ b/src/cgroups.cpp
@@ -0,0 +1,103 @@
+#include <config.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include "procman.h"
+#include "util.h"
+
+gboolean
+cgroups_enabled(void)
+{
+    static gboolean initialized = FALSE;
+    static gboolean has_cgroups;
+
+    if (!initialized) {
+        initialized = TRUE;
+        has_cgroups = g_file_test("/proc/cgroups", G_FILE_TEST_EXISTS);
+    }
+
+    return has_cgroups;
+}
+
+void
+append_cgroup_name(char *line, gchar **current_cgroup_name)
+{
+    gchar *controller, *path, *tmp;
+    int paren_offset, off, tmp_size;
+
+    controller = g_strstr_len(line, -1, ":") + 1;
+    path = g_strstr_len(controller, -1, ":") + 1;
+    *(path - 1) = '\0';
+
+    //printf("append_cgroup_name, controller: %s path: %s\n", controller, path);
+
+    if (std::strcmp(path, "/") == 0)
+        return;
+
+    if (g_strstr_len(controller, -1, "name=systemd"))
+        return;
+
+    if (*current_cgroup_name == NULL) {
+        *current_cgroup_name = g_strdup_printf("%s (%s)", path, controller);
+        return;
+    }
+
+    if ((tmp = g_strstr_len(*current_cgroup_name, -1, path))) {
+        tmp_size = strlen(*current_cgroup_name) + strlen(controller) + 1;
+        paren_offset = g_strstr_len(tmp, -1, ")") - tmp;
+        *(*current_cgroup_name + paren_offset) = '\0';
+        tmp = (gchar *)g_strnfill(tmp_size, '\0');
+        off = g_strlcat(tmp, *current_cgroup_name, tmp_size);
+        *(tmp + off) = '/';
+        off++;
+        off += g_strlcat(tmp + off, controller, tmp_size);
+        *(tmp + off) = ')';
+        off++;
+        off += g_strlcat(tmp + off, *current_cgroup_name + paren_offset + 1, tmp_size);
+        *(*current_cgroup_name + paren_offset) = ')';
+    } else  {
+        tmp = g_strdup_printf("%s, %s (%s)", *current_cgroup_name, path, controller);
+    }
+
+    g_free(*current_cgroup_name);
+    *current_cgroup_name = tmp;
+}
+
+void
+get_process_cgroup_info(ProcInfo *info)
+{
+    gchar *path;
+    GFile *file;
+    GFileInputStream *input_stream;
+    GDataInputStream *data_stream;
+    char *line;
+    gchar *cgroup_name = NULL;
+
+    if (!cgroups_enabled())
+        return;
+
+    /* read out of /proc/pid/cgroup */
+    path = g_strdup_printf("/proc/%d/cgroup", info->pid);
+    file = g_file_new_for_path(path);
+    if(!(input_stream = g_file_read(file, NULL, NULL))) {
+        goto out;
+    }
+    data_stream = g_data_input_stream_new(G_INPUT_STREAM(input_stream));
+
+    while ((line = g_data_input_stream_read_line(data_stream, NULL, NULL, NULL))) {
+        append_cgroup_name(line, &cgroup_name);
+        g_free(line);
+    }
+    if (cgroup_name) {
+        if (info->cgroup_name) {
+            g_free(info->cgroup_name);
+        }
+        info->cgroup_name = cgroup_name;
+    }
+    g_object_unref(data_stream);
+    g_object_unref(input_stream);
+out:
+    g_object_unref(file);
+    g_free(path);
+}
diff --git a/src/cgroups.h b/src/cgroups.h
new file mode 100644
index 0000000..8c42f6f
--- /dev/null
+++ b/src/cgroups.h
@@ -0,0 +1,14 @@
+#ifndef PROCMAN_CGROUP_H_20111103
+#define PROCMAN_CGROUP_H_20111103
+
+#include <glib.h>
+
+#include "procman.h"
+
+void
+get_process_cgroup_info (ProcInfo *info);
+
+gboolean
+cgroups_enabled (void);
+
+#endif /* PROCMAN_CGROUP_H_20111103 */
diff --git a/src/org.gnome.gnome-system-monitor.gschema.xml.in b/src/org.gnome.gnome-system-monitor.gschema.xml.in
index f4f51e6..a9f5e49 100644
--- a/src/org.gnome.gnome-system-monitor.gschema.xml.in
+++ b/src/org.gnome.gnome-system-monitor.gschema.xml.in
@@ -432,6 +432,21 @@
 	  <_summary>Show process 'Waiting Channel' column on startup
 	  </_summary>
 	</key>
+
+	<key name="col-17-width" type="i">
+	  <default>48
+	  </default>
+	  <_summary>Width of process 'Control Group' column
+	  </_summary>
+	</key>
+
+	<key name="col-17-visible" type="b">
+	  <default>false
+	  </default>
+	  <_summary>Show process 'Control Group' column on startup
+	  </_summary>
+	</key>
+
   </schema>
 
   <schema id="org.gnome.gnome-system-monitor.disktreenew" path="/org/gnome/gnome-system-monitor/disktreenew/">
diff --git a/src/procdialogs.cpp b/src/procdialogs.cpp
index f000d87..460bf77 100644
--- a/src/procdialogs.cpp
+++ b/src/procdialogs.cpp
@@ -34,6 +34,7 @@
 #include "settings-keys.h"
 #include "procman_gnomesu.h"
 #include "procman_gksu.h"
+#include "cgroups.h"
 
 static GtkWidget *renice_dialog = NULL;
 static GtkWidget *prefs_dialog = NULL;
@@ -465,11 +466,16 @@ create_field_page(GtkWidget *tree, const char* text)
         GtkTreeIter iter;
         const gchar *title;
         gboolean visible;
+        gint column_id;
 
         title = gtk_tree_view_column_get_title (column);
         if (!title)
             title = _("Icon");
 
+        column_id = gtk_tree_view_column_get_sort_column_id(column);
+        if ((column_id == COL_CGROUP) && (!cgroups_enabled()))
+            continue;
+
         visible = gtk_tree_view_column_get_visible (column);
 
         gtk_list_store_append (model, &iter);
diff --git a/src/procman.h b/src/procman.h
index 27c0f6c..b9cba7d 100644
--- a/src/procman.h
+++ b/src/procman.h
@@ -168,6 +168,7 @@ class ProcInfo
     gchar           *arguments;
 
     gchar           *security_context;
+    gchar           *cgroup_name;
 
     const guint     pid;
     guint           ppid;
diff --git a/src/proctable.cpp b/src/proctable.cpp
index 160cf66..4373522 100644
--- a/src/proctable.cpp
+++ b/src/proctable.cpp
@@ -52,6 +52,7 @@
 #include "util.h"
 #include "interface.h"
 #include "selinux.h"
+#include "cgroups.h"
 
 ProcInfo::UserMap ProcInfo::users;
 ProcInfo::List ProcInfo::all;
@@ -247,6 +248,7 @@ proctable_new (ProcData * const procdata)
         N_("Memory"),
         /* xgettext: combined noun, the function the process is waiting in, see wchan ps(1) */
         N_("Waiting Channel"),
+        N_("Control Group"),
         NULL,
         "POINTER"
     };
@@ -276,6 +278,7 @@ proctable_new (ProcData * const procdata)
                                 G_TYPE_STRING,      /* Arguments    */
                                 G_TYPE_ULONG,       /* Memory       */
                                 G_TYPE_STRING,      /* wchan        */
+                                G_TYPE_STRING,      /* Cgroup       */
                                 GDK_TYPE_PIXBUF,    /* Icon         */
                                 G_TYPE_POINTER,     /* ProcInfo     */
                                 G_TYPE_STRING       /* Sexy tooltip */
@@ -319,7 +322,7 @@ proctable_new (ProcData * const procdata)
     gtk_tree_view_set_expander_column (GTK_TREE_VIEW (proctree), column);
 
 
-    for (i = COL_USER; i <= COL_WCHAN; i++) {
+    for (i = COL_USER; i <= COL_CGROUP; i++) {
 
         GtkCellRenderer *cell;
         GtkTreeViewColumn *col;
@@ -424,6 +427,14 @@ proctable_new (ProcData * const procdata)
         gtk_tree_view_column_set_visible (column, FALSE);
     }
 
+    if (!cgroups_enabled()) {
+        GtkTreeViewColumn *column;
+
+        column = my_gtk_tree_view_get_column_with_sort_column_id(GTK_TREE_VIEW(proctree), COL_CGROUP);
+        gtk_tree_view_column_set_visible(column, FALSE);
+    }
+
+
     g_signal_connect (G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (proctree))),
                       "changed",
                       G_CALLBACK (cb_row_selected), procdata);
@@ -445,6 +456,7 @@ ProcInfo::~ProcInfo()
     g_free(this->tooltip);
     g_free(this->arguments);
     g_free(this->security_context);
+    g_free(this->cgroup_name);
 }
 
 
@@ -530,7 +542,6 @@ static void get_process_memory_writable(ProcInfo *info)
     g_free(maps);
 }
 
-
 static void
 get_process_memory_info(ProcInfo *info)
 {
@@ -579,6 +590,7 @@ update_info_mutable_cols(ProcInfo *info)
     tree_store_update(model, &info->node, COL_NICE, info->nice);
     tree_store_update(model, &info->node, COL_MEM, info->mem);
     tree_store_update(model, &info->node, COL_WCHAN, info->wchan);
+    tree_store_update(model, &info->node, COL_CGROUP, info->cgroup_name);
 }
 
 
@@ -695,6 +707,9 @@ update_info (ProcData *procdata, ProcInfo *info)
     ProcInfo::cpu_times[info->pid] = info->cpu_time = proctime.rtime;
     info->nice = procuid.nice;
     info->ppid = procuid.ppid;
+
+    /* get cgroup data */
+    get_process_cgroup_info(info);
 }
 
 
@@ -739,6 +754,8 @@ ProcInfo::ProcInfo(pid_t pid)
     info->start_time = proctime.start_time;
 
     get_process_selinux_context (info);
+    info->cgroup_name = NULL;
+    get_process_cgroup_info(info);
 }
 
 
diff --git a/src/proctable.h b/src/proctable.h
index 62cb529..1a26a62 100644
--- a/src/proctable.h
+++ b/src/proctable.h
@@ -43,6 +43,7 @@ enum
     COL_ARGS,
     COL_MEM,
     COL_WCHAN,
+    COL_CGROUP,
     COL_PIXBUF,
     COL_POINTER,
     COL_TOOLTIP,



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