This source file includes following definitions.
- gettime_ns
- gettime_up
- log_uptime
- bufgetline
- pid_cmdline_strscpy
- log_sample
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include "util.h"
#include "time-util.h"
#include "strxcpyx.h"
#include "store.h"
#include "bootchart.h"
#include "cgroup-util.h"
static char smaps_buf[4096];
static int skip = 0;
DIR *proc;
int procfd = -1;
double gettime_ns(void) {
struct timespec n;
clock_gettime(CLOCK_MONOTONIC, &n);
return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
}
static double gettime_up(void) {
struct timespec n;
clock_gettime(CLOCK_BOOTTIME, &n);
return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
}
void log_uptime(void) {
if (arg_relative)
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) {
char *c;
if (!buf)
return NULL;
c = strchr(buf, '\n');
if (c)
c++;
return c;
}
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;
n = read(fd, buffer, buf_len-1);
if (n > 0) {
int i;
for (i = 0; i < n; i++)
if (buffer[i] == '\0')
buffer[i] = ' ';
buffer[n] = '\0';
}
return 0;
}
void log_sample(int sample, struct list_sample_data **ptr) {
static int vmstat;
static int schedstat;
char buf[4096];
char key[256];
char val[256];
char rt[256];
char wt[256];
char *m;
int c;
int p;
int mod;
static int e_fd;
ssize_t s;
ssize_t n;
struct dirent *ent;
int fd;
struct list_sample_data *sampledata;
struct ps_sched_struct *ps_prev = NULL;
sampledata = *ptr;
if (!proc) {
proc = opendir("/proc");
if (!proc)
return;
procfd = dirfd(proc);
} else {
rewinddir(proc);
}
if (!vmstat) {
vmstat = openat(procfd, "vmstat", O_RDONLY);
if (vmstat == -1) {
log_error("Failed to open /proc/vmstat: %m");
exit(EXIT_FAILURE);
}
}
n = pread(vmstat, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
close(vmstat);
return;
}
buf[n] = '\0';
m = buf;
while (m) {
if (sscanf(m, "%s %s", key, val) < 2)
goto vmstat_next;
if (streq(key, "pgpgin"))
sampledata->blockstat.bi = atoi(val);
if (streq(key, "pgpgout")) {
sampledata->blockstat.bo = atoi(val);
break;
}
vmstat_next:
m = bufgetline(m);
if (!m)
break;
}
if (!schedstat) {
schedstat = openat(procfd, "schedstat", O_RDONLY);
if (schedstat == -1) {
log_error("Failed to open /proc/schedstat: %m");
exit(EXIT_FAILURE);
}
}
n = pread(schedstat, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
close(schedstat);
return;
}
buf[n] = '\0';
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")) {
r = safe_atoi((const char*)(key+3), &c);
if (r < 0 || c > MAXCPUS -1)
break;
sampledata->runtime[c] = atoll(rt);
sampledata->waittime[c] = atoll(wt);
if (c == cpus)
cpus = c + 1;
}
schedstat_next:
m = bufgetline(m);
if (!m)
break;
}
if (arg_entropy) {
if (!e_fd) {
e_fd = openat(procfd, "sys/kernel/random/entropy_avail", O_RDONLY);
}
if (e_fd) {
n = pread(e_fd, buf, sizeof(buf) - 1, 0);
if (n > 0) {
buf[n] = '\0';
sampledata->entropy_avail = atoi(buf);
}
}
}
while ((ent = readdir(proc)) != NULL) {
char filename[PATH_MAX];
int pid;
struct ps_struct *ps;
if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
continue;
pid = atoi(ent->d_name);
if (pid >= MAXPIDS)
continue;
ps = ps_first;
while (ps->next_ps) {
ps = ps->next_ps;
if (ps->pid == pid)
break;
}
if (ps->pid != pid) {
_cleanup_fclose_ FILE *st = NULL;
char t[32];
struct ps_struct *parent;
int r;
ps->next_ps = new0(struct ps_struct, 1);
if (!ps->next_ps) {
log_oom();
exit (EXIT_FAILURE);
}
ps = ps->next_ps;
ps->pid = pid;
ps->sample = new0(struct ps_sched_struct, 1);
if (!ps->sample) {
log_oom();
exit (EXIT_FAILURE);
}
ps->sample->sampledata = sampledata;
pscount++;
ps->first = ps->last = ps->sample;
ps->sample->runtime = atoll(rt);
ps->sample->waittime = atoll(wt);
if (!ps->sched) {
sprintf(filename, "%d/sched", pid);
ps->sched = openat(procfd, filename, O_RDONLY);
if (ps->sched == -1)
continue;
}
s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
if (s <= 0) {
close(ps->sched);
continue;
}
buf[s] = '\0';
if (!sscanf(buf, "%s %*s %*s", key))
continue;
strscpy(ps->name, sizeof(ps->name), key);
if (arg_show_cmdline)
pid_cmdline_strscpy(ps->name, sizeof(ps->name), pid);
m = bufgetline(buf);
if (!m)
continue;
m = bufgetline(m);
if (!m)
continue;
if (!sscanf(m, "%*s %*s %s", t))
continue;
r = safe_atod(t, &ps->starttime);
if (r < 0)
continue;
ps->starttime /= 1000.0;
if (arg_show_cgroup)
cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER,
ps->pid, &ps->cgroup);
sprintf(filename, "%d/stat", pid);
fd = openat(procfd, filename, O_RDONLY);
st = fdopen(fd, "r");
if (!st)
continue;
if (!fscanf(st, "%*s %*s %*s %i", &p)) {
continue;
}
ps->ppid = p;
if (pid == 1)
continue;
if (ps->ppid == 0)
ps->ppid = 1;
parent = ps_first;
while ((parent->next_ps && parent->pid != ps->ppid))
parent = parent->next_ps;
if (parent->pid != ps->ppid) {
ps->ppid = 1;
parent = ps_first->next_ps;
}
ps->parent = parent;
if (!parent->children) {
parent->children = ps;
} else {
struct ps_struct *children;
children = parent->children;
while (children->next)
children = children->next;
children->next = ps;
}
}
if (!ps->schedstat) {
sprintf(filename, "%d/schedstat", pid);
ps->schedstat = openat(procfd, filename, O_RDONLY);
if (ps->schedstat == -1)
continue;
}
s = pread(ps->schedstat, buf, sizeof(buf) - 1, 0);
if (s <= 0) {
close(ps->schedstat);
if (ps->sched)
close(ps->sched);
continue;
}
buf[s] = '\0';
if (!sscanf(buf, "%s %s %*s", rt, wt))
continue;
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;
if (!ps->smaps) {
sprintf(filename, "%d/smaps", pid);
fd = openat(procfd, filename, O_RDONLY);
ps->smaps = fdopen(fd, "r");
if (!ps->smaps)
continue;
setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
}
else {
rewind(ps->smaps);
}
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;
if (fgets(buf, sizeof(buf), ps->smaps) == NULL) {
break;
}
if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14) {
break;
}
pss_kb = atoi(&buf[61]);
ps->sample->pss += pss_kb;
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:
mod = (arg_hz < 4.0) ? 4.0 : (arg_hz / 4.0);
if (((samples - ps->pid) + pid) % (int)(mod) == 0) {
if (!ps->sched) {
sprintf(filename, "%d/sched", pid);
ps->sched = openat(procfd, filename, O_RDONLY);
if (ps->sched == -1)
continue;
}
s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
if (s <= 0) {
close(ps->sched);
if (ps->schedstat)
close(ps->schedstat);
continue;
}
buf[s] = '\0';
if (!sscanf(buf, "%s %*s %*s", key))
continue;
strscpy(ps->name, sizeof(ps->name), key);
if (arg_show_cmdline)
pid_cmdline_strscpy(ps->name, sizeof(ps->name), pid);
}
}
}