[systemd-devel] [RFC 2/2] bootchart: add cgroup option

WaLyong Cho walyong.cho at samsung.com
Fri Mar 28 01:07:16 PDT 2014


---
 src/bootchart/bootchart.c    |    9 ++++++++-
 src/bootchart/bootchart.conf |    1 +
 src/bootchart/bootchart.h    |    2 ++
 src/bootchart/store.c        |   25 +++++++++++++++++++++++++
 src/bootchart/svg.c          |    5 +++--
 5 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
index 9f72d98..1c87085 100644
--- a/src/bootchart/bootchart.c
+++ b/src/bootchart/bootchart.c
@@ -78,6 +78,7 @@ bool initcall = true;
 bool arg_relative = false;
 bool arg_filter = true;
 bool arg_show_cmdline = false;
+bool arg_show_cgroup = false;
 bool arg_pss = false;
 int samples;
 int arg_samples_len = 500; /* we record len+1 (1 start sample) */
@@ -113,6 +114,7 @@ static void parse_conf(void) {
                 { "Bootchart", "PlotEntropyGraph", config_parse_bool,   0, &arg_entropy     },
                 { "Bootchart", "ScaleX",           config_parse_double, 0, &arg_scale_x     },
                 { "Bootchart", "ScaleY",           config_parse_double, 0, &arg_scale_y     },
+                { "Bootchart", "Cgroup",           config_parse_bool,   0, &arg_show_cgroup },
                 { NULL, NULL, NULL, 0, NULL }
         };
         _cleanup_fclose_ FILE *f;
@@ -143,6 +145,7 @@ static int parse_args(int argc, char *argv[]) {
                 {"init",      required_argument,  NULL,  'i'},
                 {"no-filter", no_argument,        NULL,  'F'},
                 {"cmdline",   no_argument,        NULL,  'C'},
+                {"cgroup",    no_argument,        NULL,  'g'},
                 {"help",      no_argument,        NULL,  'h'},
                 {"scale-x",   required_argument,  NULL,  'x'},
                 {"scale-y",   required_argument,  NULL,  'y'},
@@ -151,7 +154,7 @@ static int parse_args(int argc, char *argv[]) {
         };
         int c;
 
-        while ((c = getopt_long(argc, argv, "erpf:n:o:i:FChx:y:", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "erpf:n:o:i:FCghx:y:", options, NULL)) >= 0) {
                 int r;
 
                 switch (c) {
@@ -170,6 +173,9 @@ static int parse_args(int argc, char *argv[]) {
                 case 'C':
                         arg_show_cmdline = true;
                         break;
+                case 'g':
+                        arg_show_cgroup = true;
+                        break;
                 case 'n':
                         r = safe_atoi(optarg, &arg_samples_len);
                         if (r < 0)
@@ -217,6 +223,7 @@ static int parse_args(int argc, char *argv[]) {
                         fprintf(stderr, "                          that are of less importance or short-lived\n");
                         fprintf(stderr, " --cmdline,   -C          Display the full command line with arguments\n");
                         fprintf(stderr, "                          of processes, instead of only the process name\n");
+                        fprintf(stderr, " --cgroup,    -g          Display process cgroup name\n");
                         fprintf(stderr, " --help,      -h          Display this message\n");
                         fprintf(stderr, "See bootchart.conf for more information.\n");
                         exit (EXIT_SUCCESS);
diff --git a/src/bootchart/bootchart.conf b/src/bootchart/bootchart.conf
index 48fad02..565fd6e 100644
--- a/src/bootchart/bootchart.conf
+++ b/src/bootchart/bootchart.conf
@@ -18,3 +18,4 @@
 #PlotEntropyGraph=no
 #ScaleX=100
 #ScaleY=20
+#Cgroup=no
diff --git a/src/bootchart/bootchart.h b/src/bootchart/bootchart.h
index 968c38d..4e72c96 100644
--- a/src/bootchart/bootchart.h
+++ b/src/bootchart/bootchart.h
@@ -77,6 +77,7 @@ struct ps_struct {
         char name[256];
         int pid;
         int ppid;
+        char cgroup[256];
 
         /* cache fd's */
         int sched;
@@ -114,6 +115,7 @@ extern int pscount;
 extern bool arg_relative;
 extern bool arg_filter;
 extern bool arg_show_cmdline;
+extern bool arg_show_cgroup;
 extern bool arg_pss;
 extern bool arg_entropy;
 extern bool initcall;
diff --git a/src/bootchart/store.c b/src/bootchart/store.c
index 78c5cf8..725fbea 100644
--- a/src/bootchart/store.c
+++ b/src/bootchart/store.c
@@ -112,6 +112,28 @@ static int pid_cmdline_strscpy(char *buffer, size_t buf_len, int pid) {
         return 0;
 }
 
+static void get_cgroup_name(int pid, struct ps_struct *ps) {
+        _cleanup_fclose_ FILE *cg = NULL;
+        char filename[PATH_MAX], *line = NULL;
+        int r, fd;
+
+        sprintf(filename, "%d/cgroup", pid);
+        fd = openat(procfd, filename, O_RDONLY);
+        if (fd == -1)
+                return;
+
+        cg = fdopen(fd, "r");
+        if (!cg)
+                return;
+
+        r = read_one_line_from_file_if_has(cg, &line, "name=systemd:/");
+        if (r < 0)
+                return;
+
+        strncpy(ps->cgroup, line, sizeof(ps->cgroup));
+        free(line);
+}
+
 void log_sample(int sample, struct list_sample_data **ptr) {
         static int vmstat;
         static int schedstat;
@@ -315,6 +337,9 @@ schedstat_next:
 
                         ps->starttime = strtod(t, NULL) / 1000.0;
 
+                        if (arg_show_cgroup)
+                                get_cgroup_name(pid, ps);
+
                         /* ppid */
                         sprintf(filename, "%d/stat", pid);
                         fd = openat(procfd, filename, O_RDONLY);
diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index 7438e47..a53f98a 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -1093,12 +1093,13 @@ static void svg_ps_bars(void) {
                         w = starttime;
 
                 /* text label of process name */
-                svg("  <text x=\"%.03f\" y=\"%.03f\"><![CDATA[%s]]> [%i]<tspan class=\"run\">%.03fs</tspan></text>\n",
+                svg("  <text x=\"%.03f\" y=\"%.03f\"><![CDATA[%s]]> [%i]<tspan class=\"run\">%.03fs</tspan> %s</text>\n",
                     time_to_graph(w - graph_start) + 5.0,
                     ps_to_graph(j) + 14.0,
                     ps->name,
                     ps->pid,
-                    (ps->last->runtime - ps->first->runtime) / 1000000000.0);
+                    (ps->last->runtime - ps->first->runtime) / 1000000000.0,
+                    arg_show_cgroup ? ps->cgroup : "");
                 /* paint lines to the parent process */
                 if (ps->parent) {
                         /* horizontal part */
-- 
1.7.9.5



More information about the systemd-devel mailing list