chiark / gitweb /
bootchart: add control group option
[elogind.git] / src / bootchart / bootchart.c
index fec63721a8246404a25cbf3dd2348bcd40ae0b6f..a73418af7ee4736831f22d301e59736eced0a772 100644 (file)
@@ -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", "ControlGroup",     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'},
+                {"control-group", no_argument,    NULL,  'c'},
                 {"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:FCchx: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 'c':
+                        arg_show_cgroup = true;
+                        break;
                 case 'n':
                         r = safe_atoi(optarg, &arg_samples_len);
                         if (r < 0)
@@ -217,10 +223,10 @@ 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, " --control-group, -c      Display process control group\n");
                         fprintf(stderr, " --help,      -h          Display this message\n");
                         fprintf(stderr, "See bootchart.conf for more information.\n");
                         exit (EXIT_SUCCESS);
-                        break;
                 default:
                         break;
                 }
@@ -259,14 +265,14 @@ static void do_journal_append(char *file) {
 
         memcpy(p, "BOOTCHART=", 10);
 
-        f = open(file, O_RDONLY);
+        f = open(file, O_RDONLY|O_CLOEXEC);
         if (f < 0) {
-                log_error("Failed to read bootchart data: %m\n");
+                log_error("Failed to read bootchart data: %m");
                 return;
         }
         n = loop_read(f, p + 10, BOOTCHART_MAX, false);
         if (n < 0) {
-                log_error("Failed to read bootchart data: %s\n", strerror(-n));
+                log_error("Failed to read bootchart data: %s", strerror(-n));
                 close(f);
                 return;
         }
@@ -318,10 +324,10 @@ int main(int argc, char *argv[]) {
         (void) setrlimit(RLIMIT_NOFILE, &rlim);
 
         /* start with empty ps LL */
-        ps_first = calloc(1, sizeof(struct ps_struct));
+        ps_first = new0(struct ps_struct, 1);
         if (!ps_first) {
-                perror("calloc(ps_struct)");
-                exit(EXIT_FAILURE);
+                log_oom();
+                return EXIT_FAILURE;
         }
 
         /* handle TERM/INT nicely */
@@ -356,11 +362,11 @@ int main(int argc, char *argv[]) {
                         t = time(NULL);
                         strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
                         snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
-                        of = fopen(output_file, "w");
+                        of = fopen(output_file, "we");
                 }
 
                 if (sysfd < 0)
-                        sysfd = open("/sys", O_RDONLY);
+                        sysfd = open("/sys", O_RDONLY|O_CLOEXEC);
 
                 if (!build)
                         parse_env_file("/etc/os-release", NEWLINE,
@@ -397,8 +403,8 @@ int main(int argc, char *argv[]) {
                                         /* caught signal, probably HUP! */
                                         break;
                                 }
-                                perror("nanosleep()");
-                                exit (EXIT_FAILURE);
+                                log_error("nanosleep() failed: %m");
+                                exit(EXIT_FAILURE);
                         }
                 } else {
                         overrun++;
@@ -424,7 +430,7 @@ int main(int argc, char *argv[]) {
                 t = time(NULL);
                 strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
                 snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
-                of = fopen(output_file, "w");
+                of = fopen(output_file, "we");
         }
 
         if (!of) {
@@ -459,9 +465,11 @@ int main(int argc, char *argv[]) {
                         old->sample = old->sample->next;
                         free(oldsample);
                 }
+                free(old->cgroup);
                 free(old->sample);
                 free(old);
         }
+        free(ps->cgroup);
         free(ps->sample);
         free(ps);