- m = bufgetline(m);
- if (!m)
- break;
- }
-
- if (entropy) {
- if (!e_fd) {
- e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
- }
-
- if (e_fd) {
- n = pread(e_fd, buf, sizeof(buf) - 1, 0);
- if (n > 0)
- entropy_avail[sample] = atoi(buf);
- }
- }
-
- /* all the per-process stuff goes here */
- if (!proc) {
- /* find all processes */
- proc = opendir("/proc");
- if (!proc)
- return;
- } else {
- rewinddir(proc);
- }
-
- 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;
- }
-
- /* end of our LL? then append a new record */
- if (ps->pid != pid) {
- char t[32];
- struct ps_struct *parent;
-
- ps->next_ps = malloc(sizeof(struct ps_struct));
- if (!ps->next_ps) {
- perror("malloc(ps_struct)");
- exit (EXIT_FAILURE);
- }
- memset(ps->next_ps, 0, sizeof(struct ps_struct));
- ps = ps->next_ps;
- ps->pid = pid;
-
- ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
- if (!ps->sample) {
- perror("malloc(ps_struct)");
- exit (EXIT_FAILURE);
- }
- memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
-
- pscount++;
-
- /* mark our first sample */
- ps->first = sample;
-
- /* get name, start time */
- if (!ps->sched) {
- sprintf(filename, "/proc/%d/sched", pid);
- ps->sched = open(filename, O_RDONLY);
- if (ps->sched == -1)
- continue;
- }
-
- s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
- if (s <= 0) {
- close(ps->sched);
- continue;
- }
-
- if (!sscanf(buf, "%s %*s %*s", key))
- continue;
-
- strncpy(ps->name, key, 16);
- /* discard line 2 */
- m = bufgetline(buf);
- if (!m)
- continue;
-
- m = bufgetline(m);
- if (!m)
- continue;
-
- if (!sscanf(m, "%*s %*s %s", t))
- continue;
-
- ps->starttime = strtod(t, NULL) / 1000.0;
-
- /* ppid */
- sprintf(filename, "/proc/%d/stat", pid);
- st = fopen(filename, "r");
- if (!st)
- continue;
- if (!fscanf(st, "%*s %*s %*s %i", &p)) {
- fclose(st);
- continue;
- }
- fclose(st);
- ps->ppid = p;
-
- /*
- * setup child pointers
- *
- * these are used to paint the tree coherently later
- * each parent has a LL of children, and a LL of siblings
- */
- if (pid == 1)
- continue; /* nothing to do for init atm */
-
- /* kthreadd has ppid=0, which breaks our tree ordering */
- if (ps->ppid == 0)
- ps->ppid = 1;
-
- parent = ps_first;
- while ((parent->next_ps && parent->pid != ps->ppid))
- parent = parent->next_ps;
-
- if ((!parent) || (parent->pid != ps->ppid)) {
- /* orphan */
- ps->ppid = 1;
- parent = ps_first->next_ps;
- }
-
- ps->parent = parent;
-
- if (!parent->children) {
- /* it's the first child */
- parent->children = ps;
- } else {
- /* walk all children and append */
- struct ps_struct *children;
- children = parent->children;
- while (children->next)
- children = children->next;
- children->next = ps;
- }
- }
-
- /* else -> found pid, append data in ps */
-
- /* below here is all continuous logging parts - we get here on every
- * iteration */
-
- /* rt, wt */
- if (!ps->schedstat) {
- sprintf(filename, "/proc/%d/schedstat", pid);
- ps->schedstat = open(filename, O_RDONLY);
- if (ps->schedstat == -1)
- continue;
- }
-
- if (pread(ps->schedstat, buf, sizeof(buf) - 1, 0) <= 0) {
- /* clean up our file descriptors - assume that the process exited */
- close(ps->schedstat);
- if (ps->sched)
- close(ps->sched);
- //if (ps->smaps)
- // fclose(ps->smaps);
- continue;
- }
- 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;
-
- if (!pss)
- goto catch_rename;
- /* Pss */
- if (!ps->smaps) {
- sprintf(filename, "/proc/%d/smaps", pid);
- ps->smaps = fopen(filename, "r");
- setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
- if (!ps->smaps)
- continue;
- } else {
- rewind(ps->smaps);
- }
-
- while (1) {
- int pss_kb;
-
- /* 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)
- break;
-
- pss_kb = atoi(&buf[61]);
- ps->sample[sample].pss += pss_kb;
- }
-
- if (ps->sample[sample].pss > ps->pss_max)
- ps->pss_max = ps->sample[sample].pss;
+ m = bufgetline(m);
+ if (!m)
+ break;
+ }
+
+ if (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';
+ entropy_avail[sample] = 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;
+ }
+
+ /* end of our LL? then append a new record */
+ if (ps->pid != pid) {
+ FILE _cleanup_fclose_ *st = NULL;
+ char t[32];
+ struct ps_struct *parent;
+
+ ps->next_ps = calloc(1, sizeof(struct ps_struct));
+ if (!ps->next_ps) {
+ perror("calloc(ps_struct)");
+ exit (EXIT_FAILURE);
+ }
+ ps = ps->next_ps;
+ ps->pid = pid;
+
+ ps->sample = calloc(samples_len + 1, sizeof(struct ps_sched_struct));
+ if (!ps->sample) {
+ perror("calloc(ps_struct)");
+ exit (EXIT_FAILURE);
+ }
+
+ pscount++;
+
+ /* mark our first sample */
+ ps->first = sample;
+
+ /* get name, start time */
+ 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;
+
+ strncpy(ps->name, key, 256);
+
+ /* cmdline */
+ if (show_cmdline)
+ pid_cmdline_strncpy(ps->name, pid, 256);
+
+ /* discard line 2 */
+ m = bufgetline(buf);
+ if (!m)
+ continue;
+
+ m = bufgetline(m);
+ if (!m)
+ continue;
+
+ if (!sscanf(m, "%*s %*s %s", t))
+ continue;
+
+ ps->starttime = strtod(t, NULL) / 1000.0;
+
+ /* ppid */
+ 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;
+
+ /*
+ * setup child pointers
+ *
+ * these are used to paint the tree coherently later
+ * each parent has a LL of children, and a LL of siblings
+ */
+ if (pid == 1)
+ continue; /* nothing to do for init atm */
+
+ /* kthreadd has ppid=0, which breaks our tree ordering */
+ if (ps->ppid == 0)
+ ps->ppid = 1;
+
+ parent = ps_first;
+ while ((parent->next_ps && parent->pid != ps->ppid))
+ parent = parent->next_ps;
+
+ if ((!parent) || (parent->pid != ps->ppid)) {
+ /* orphan */
+ ps->ppid = 1;
+ parent = ps_first->next_ps;
+ }
+
+ ps->parent = parent;
+
+ if (!parent->children) {
+ /* it's the first child */
+ parent->children = ps;
+ } else {
+ /* walk all children and append */
+ struct ps_struct *children;
+ children = parent->children;
+ while (children->next)
+ children = children->next;
+ children->next = ps;
+ }
+ }
+
+ /* else -> found pid, append data in ps */
+
+ /* below here is all continuous logging parts - we get here on every
+ * iteration */
+
+ /* rt, wt */
+ 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) {
+ /* clean up our file descriptors - assume that the process exited */
+ close(ps->schedstat);
+ if (ps->sched)
+ close(ps->sched);
+ //if (ps->smaps)
+ // fclose(ps->smaps);
+ continue;
+ }
+ buf[s] = '\0';
+
+ 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;
+
+ if (!pss)
+ goto catch_rename;
+ /* Pss */
+ 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);
+ }
+
+ while (1) {
+ int pss_kb;
+
+ /* 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)
+ break;
+
+ pss_kb = atoi(&buf[61]);
+ ps->sample[sample].pss += pss_kb;
+ }
+
+ if (ps->sample[sample].pss > ps->pss_max)
+ ps->pss_max = ps->sample[sample].pss;