/***
This file is part of systemd.
- Copyright (C) 2009-2013 Intel Coproration
+ Copyright (C) 2009-2013 Intel Corporation
Authors:
Auke Kok <auke-jan.h.kok@intel.com>
#include <time.h>
#include "util.h"
+#include "time-util.h"
#include "strxcpyx.h"
#include "store.h"
#include "bootchart.h"
+#include "cgroup-util.h"
/*
* Alloc a static 4k buffer for stdio - primarily used to increase
* read() overhead.
*/
static char smaps_buf[4096];
+static int skip = 0;
DIR *proc;
int procfd = -1;
clock_gettime(CLOCK_MONOTONIC, &n);
- return (n.tv_sec + (n.tv_nsec / 1000000000.0));
+ return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
}
-void log_uptime(void) {
- _cleanup_fclose_ FILE *f = NULL;
- char str[32];
- double uptime;
-
- f = fopen("/proc/uptime", "r");
-
- if (!f)
- return;
- if (!fscanf(f, "%s %*s", str))
- return;
-
- uptime = strtod(str, NULL);
+static double gettime_up(void) {
+ struct timespec n;
- log_start = gettime_ns();
+ clock_gettime(CLOCK_BOOTTIME, &n);
+ return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
+}
- /* start graph at kernel boot time */
+void log_uptime(void) {
if (arg_relative)
- graph_start = log_start;
- else
+ graph_start = log_start = gettime_ns();
+ else {
+ double uptime = gettime_up();
+
+ log_start = gettime_ns();
graph_start = log_start - uptime;
+ }
}
static char *bufgetline(char *buf) {
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[4096];
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) {
/* block stuff */
vmstat = openat(procfd, "vmstat", O_RDONLY);
if (vmstat == -1) {
- perror("open /proc/vmstat");
- exit (EXIT_FAILURE);
+ log_error_errno(errno, "Failed to open /proc/vmstat: %m");
+ exit(EXIT_FAILURE);
}
}
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:
/* overall CPU utilization */
schedstat = openat(procfd, "schedstat", O_RDONLY);
if (schedstat == -1) {
- perror("open /proc/schedstat");
- exit (EXIT_FAILURE);
+ log_error_errno(errno, "Failed to open /proc/schedstat: %m");
+ exit(EXIT_FAILURE);
}
}
m = buf;
while (m) {
+ int r;
+
if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
goto schedstat_next;
if (strstr(key, "cpu")) {
- c = atoi((const char*)(key+3));
- if (c > MAXCPUS)
+ r = safe_atoi((const char*)(key+3), &c);
+ if (r < 0 || c > MAXCPUS -1)
/* 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;
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);
}
}
}
_cleanup_fclose_ FILE *st = NULL;
char t[32];
struct ps_struct *parent;
+ int r;
- ps->next_ps = calloc(1, sizeof(struct ps_struct));
+ ps->next_ps = new0(struct ps_struct, 1);
if (!ps->next_ps) {
- perror("calloc(ps_struct)");
+ log_oom();
exit (EXIT_FAILURE);
}
ps = ps->next_ps;
ps->pid = pid;
- ps->sample = calloc(arg_samples_len + 1, sizeof(struct ps_sched_struct));
+ ps->sample = new0(struct ps_sched_struct, 1);
if (!ps->sample) {
- perror("calloc(ps_struct)");
+ log_oom();
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) {
if (!sscanf(m, "%*s %*s %s", t))
continue;
- ps->starttime = strtod(t, NULL) / 1000.0;
+ r = safe_atod(t, &ps->starttime);
+ if (r < 0)
+ continue;
+
+ ps->starttime /= 1000.0;
+
+ if (arg_show_cgroup)
+ /* if this fails, that's OK */
+ cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER,
+ ps->pid, &ps->cgroup);
/* ppid */
sprintf(filename, "%d/stat", pid);
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;
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 = new0(struct ps_sched_struct, 1);
+ if (!ps->sample->next) {
+ log_oom();
+ 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 (!arg_pss)
goto catch_rename;
+
/* Pss */
if (!ps->smaps) {
sprintf(filename, "%d/smaps", pid);
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 = (arg_hz < 4.0) ? 4.0 : (arg_hz / 4.0);
- if (((samples - ps->first) + pid) % (int)(mod) == 0) {
+ if (((samples - ps->pid) + pid) % (int)(mod) == 0) {
/* re-fetch name */
/* get name, start time */