X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbootchart%2Fstore.c;h=b6ba11356192eacd205965cd1edf67fa4927bb93;hp=ccec03f12cb18c6b85c49dcdc888df50d5d5ba07;hb=226b735a745c1e9bbab24b47460d7304def9d38f;hpb=e75dcf5737ea7c12522d31b55fe0ca193b9dcc21 diff --git a/src/bootchart/store.c b/src/bootchart/store.c index ccec03f12..b6ba11356 100644 --- a/src/bootchart/store.c +++ b/src/bootchart/store.c @@ -1,7 +1,9 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + /*** - log.c - This file is part of systemd-bootchart + This file is part of systemd. - Copyright (C) 2009-2013 Intel Coproration + Copyright (C) 2009-2013 Intel Corporation Authors: Auke Kok @@ -20,7 +22,6 @@ along with systemd; If not, see . ***/ -#define _GNU_SOURCE 1 #include #include #include @@ -32,9 +33,10 @@ #include #include - -#include "bootchart.h" #include "util.h" +#include "strxcpyx.h" +#include "store.h" +#include "bootchart.h" /* * Alloc a static 4k buffer for stdio - primarily used to increase @@ -42,11 +44,11 @@ * read() overhead. */ static char smaps_buf[4096]; +static int skip = 0; DIR *proc; -int procfd=-1; +int procfd = -1; -double gettime_ns(void) -{ +double gettime_ns(void) { struct timespec n; clock_gettime(CLOCK_MONOTONIC, &n); @@ -54,10 +56,8 @@ double gettime_ns(void) return (n.tv_sec + (n.tv_nsec / 1000000000.0)); } - -void log_uptime(void) -{ - FILE _cleanup_fclose_ *f = NULL; +void log_uptime(void) { + _cleanup_fclose_ FILE *f = NULL; char str[32]; double uptime; @@ -73,15 +73,13 @@ void log_uptime(void) log_start = gettime_ns(); /* start graph at kernel boot time */ - if (relative) + if (arg_relative) graph_start = log_start; else graph_start = log_start - uptime; } - -static char *bufgetline(char *buf) -{ +static char *bufgetline(char *buf) { char *c; if (!buf) @@ -93,17 +91,17 @@ static char *bufgetline(char *buf) return c; } -static int pid_cmdline_strncpy(char *buffer, int pid, size_t buf_len) { - char filename[PATH_MAX]; - int _cleanup_close_ fd=-1; - ssize_t n; +static int pid_cmdline_strscpy(char *buffer, size_t buf_len, int pid) { + char filename[PATH_MAX]; + _cleanup_close_ int fd=-1; + ssize_t n; - sprintf(filename, "%d/cmdline", pid); - fd = openat(procfd, filename, O_RDONLY); - if (fd < 0) - return -errno; + sprintf(filename, "%d/cmdline", pid); + fd = openat(procfd, filename, O_RDONLY); + if (fd < 0) + return -errno; - n = read(fd, buffer, buf_len-1); + n = read(fd, buffer, buf_len-1); if (n > 0) { int i; for (i = 0; i < n; i++) @@ -111,14 +109,13 @@ static int pid_cmdline_strncpy(char *buffer, int pid, size_t buf_len) { buffer[i] = ' '; buffer[n] = '\0'; } - return 0; + return 0; } -void log_sample(int sample) -{ +void log_sample(int sample, struct list_sample_data **ptr) { static int vmstat; static int schedstat; - char buf[4095]; + char buf[4096]; char key[256]; char val[256]; char rt[256]; @@ -132,6 +129,12 @@ void log_sample(int sample) ssize_t n; struct dirent *ent; int fd; + struct list_sample_data *sampledata; + struct ps_sched_struct *ps_prev = NULL; + + + + sampledata = *ptr; /* all the per-process stuff goes here */ if (!proc) { @@ -165,9 +168,9 @@ void log_sample(int sample) if (sscanf(m, "%s %s", key, val) < 2) goto vmstat_next; if (streq(key, "pgpgin")) - blockstat[sample].bi = atoi(val); + sampledata->blockstat.bi = atoi(val); if (streq(key, "pgpgout")) { - blockstat[sample].bo = atoi(val); + sampledata->blockstat.bo = atoi(val); break; } vmstat_next: @@ -202,8 +205,8 @@ vmstat_next: if (c > MAXCPUS) /* Oops, we only have room for MAXCPUS data */ break; - cpustat[c].sample[sample].runtime = atoll(rt); - cpustat[c].sample[sample].waittime = atoll(wt); + sampledata->runtime[c] = atoll(rt); + sampledata->waittime[c] = atoll(wt); if (c == cpus) cpus = c + 1; @@ -214,7 +217,7 @@ schedstat_next: break; } - if (entropy) { + if (arg_entropy) { if (!e_fd) { e_fd = openat(procfd, "sys/kernel/random/entropy_avail", O_RDONLY); } @@ -223,7 +226,7 @@ schedstat_next: n = pread(e_fd, buf, sizeof(buf) - 1, 0); if (n > 0) { buf[n] = '\0'; - entropy_avail[sample] = atoi(buf); + sampledata->entropy_avail = atoi(buf); } } } @@ -250,7 +253,7 @@ schedstat_next: /* end of our LL? then append a new record */ if (ps->pid != pid) { - FILE _cleanup_fclose_ *st = NULL; + _cleanup_fclose_ FILE *st = NULL; char t[32]; struct ps_struct *parent; @@ -262,16 +265,19 @@ schedstat_next: ps = ps->next_ps; ps->pid = pid; - ps->sample = calloc(samples_len + 1, sizeof(struct ps_sched_struct)); + ps->sample = calloc(1, sizeof(struct ps_sched_struct)); if (!ps->sample) { perror("calloc(ps_struct)"); exit (EXIT_FAILURE); } + ps->sample->sampledata = sampledata; pscount++; /* mark our first sample */ - ps->first = sample; + ps->first = ps->last = ps->sample; + ps->sample->runtime = atoll(rt); + ps->sample->waittime = atoll(wt); /* get name, start time */ if (!ps->sched) { @@ -291,11 +297,11 @@ schedstat_next: if (!sscanf(buf, "%s %*s %*s", key)) continue; - strncpy(ps->name, key, 256); + strscpy(ps->name, sizeof(ps->name), key); /* cmdline */ - if (show_cmdline) - pid_cmdline_strncpy(ps->name, pid, 256); + if (arg_show_cmdline) + pid_cmdline_strscpy(ps->name, sizeof(ps->name), pid); /* discard line 2 */ m = bufgetline(buf); @@ -339,7 +345,7 @@ schedstat_next: while ((parent->next_ps && parent->pid != ps->ppid)) parent = parent->next_ps; - if ((!parent) || (parent->pid != ps->ppid)) { + if (parent->pid != ps->ppid) { /* orphan */ ps->ppid = 1; parent = ps_first->next_ps; @@ -387,16 +393,28 @@ schedstat_next: if (!sscanf(buf, "%s %s %*s", rt, wt)) continue; - ps->last = sample; - ps->sample[sample].runtime = atoll(rt); - ps->sample[sample].waittime = atoll(wt); - - ps->total = (ps->sample[ps->last].runtime - - ps->sample[ps->first].runtime) - / 1000000000.0; + ps->sample->next = calloc(1, sizeof(struct ps_sched_struct)); + if (!ps->sample) { + perror("calloc(ps_struct)"); + exit (EXIT_FAILURE); + } + ps->sample->next->prev = ps->sample; + ps->sample = ps->sample->next; + ps->last = ps->sample; + ps->sample->runtime = atoll(rt); + ps->sample->waittime = atoll(wt); + ps->sample->sampledata = sampledata; + ps->sample->ps_new = ps; + if (ps_prev) { + ps_prev->cross = ps->sample; + } + ps_prev = ps->sample; + ps->total = (ps->last->runtime - ps->first->runtime) + / 1000000000.0; - if (!pss) + if (!arg_pss) goto catch_rename; + /* Pss */ if (!ps->smaps) { sprintf(filename, "%d/smaps", pid); @@ -405,31 +423,53 @@ schedstat_next: if (!ps->smaps) continue; setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf)); - } else { + } + else { + rewind(ps->smaps); + } + /* test to see if we need to skip another field */ + if (skip == 0) { + if (fgets(buf, sizeof(buf), ps->smaps) == NULL) { + continue; + } + if (fread(buf, 1, 28 * 15, ps->smaps) != (28 * 15)) { + continue; + } + if (buf[392] == 'V') { + skip = 2; + } + else { + skip = 1; + } rewind(ps->smaps); } - while (1) { int pss_kb; - /* skip one line, this contains the object mapped */ - if (fgets(buf, sizeof(buf), ps->smaps) == NULL) + /* skip one line, this contains the object mapped. */ + if (fgets(buf, sizeof(buf), ps->smaps) == NULL) { break; + } /* then there's a 28 char 14 line block */ - if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14) + if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14) { break; - + } pss_kb = atoi(&buf[61]); - ps->sample[sample].pss += pss_kb; - } + ps->sample->pss += pss_kb; - if (ps->sample[sample].pss > ps->pss_max) - ps->pss_max = ps->sample[sample].pss; + /* skip one more line if this is a newer kernel */ + if (skip == 2) { + if (fgets(buf, sizeof(buf), ps->smaps) == NULL) + break; + } + } + if (ps->sample->pss > ps->pss_max) + ps->pss_max = ps->sample->pss; catch_rename: /* catch process rename, try to randomize time */ - mod = (hz < 4.0) ? 4.0 : (hz / 4.0); - if (((samples - ps->first) + pid) % (int)(mod) == 0) { + mod = (arg_hz < 4.0) ? 4.0 : (arg_hz / 4.0); + if (((samples - ps->pid) + pid) % (int)(mod) == 0) { /* re-fetch name */ /* get name, start time */ @@ -454,11 +494,11 @@ catch_rename: if (!sscanf(buf, "%s %*s %*s", key)) continue; - strncpy(ps->name, key, 256); + strscpy(ps->name, sizeof(ps->name), key); /* cmdline */ - if (show_cmdline) - pid_cmdline_strncpy(ps->name, pid, 256); + if (arg_show_cmdline) + pid_cmdline_strscpy(ps->name, sizeof(ps->name), pid); } } }