char * format_next_io(caddr_t handle, char *(*get_userid) (uid_t)) { static char fmt[MAX_COLS]; /* static area where result is built */ static struct io_node *head = NULL; struct top_proc *p = *nextactive++; struct io_node *node = NULL; head = upsert_io_stats(head, p->pid, p->rchar, p->wchar, p->syscr, p->syscw, p->read_bytes, p->write_bytes, p->cancelled_write_bytes); if (mode_stats == STATS_DIFF) { node = get_io_stats(head, p->pid); if (node == NULL) { snprintf(fmt, sizeof(fmt), "%5d %5d %5d %7d %7d %5d %6d %7d %s", p->pid, 0, 0, 0, 0, 0, 0, 0, p->name); } else { snprintf(fmt, sizeof(fmt), "%5d %5s %5s %7lld %7lld %5s %6s %7s %s", p->pid, format_b(node->diff_rchar), format_b(node->diff_wchar), node->diff_syscr, node->diff_syscw, format_b(node->diff_read_bytes), format_b(node->diff_write_bytes), format_b(node->diff_cancelled_write_bytes), p->name); } } else { snprintf(fmt, sizeof(fmt), "%5d %5s %5s %7lld %7lld %5s %6s %7s %s", p->pid, format_b(p->rchar), format_b(p->wchar), p->syscr, p->syscw, format_b(p->read_bytes), format_b(p->write_bytes), format_b(p->cancelled_write_bytes), p->name); } return (fmt); }
void get_stats() { DIR *dir = opendir(PROC); struct dirent *ent; char filename[BUFFERLEN + 1]; char buffer[BUFFERLEN + 1]; char value[BUFFERLEN + 1]; /* Display column headers. */ if (hr_flag == 1) printf("%5s %5s %5s %8s %8s %5s %6s %7s %s\n", "pid", "rchar", "wchar", "syscr", "syscw", "reads", "writes", "cwrites", "command"); else if (kb_flag == 1) printf("%5s %8s %8s %8s %8s %8s %8s %8s %s\n", "pid", "rchar", "wchar", "syscr", "syscw", "rkb", "wkb", "cwkb", "command"); else if (mb_flag == 1) printf("%5s %8s %8s %8s %8s %8s %8s %8s %s\n", "pid", "rchar", "wchar", "syscr", "syscw", "rmb", "wmb", "cwmb", "command"); else printf("%5s %8s %8s %8s %8s %8s %8s %8s %s\n", "pid", "rchar", "wchar", "syscr", "syscw", "rbytes", "wbytes", "cwbytes", "command"); /* Loop through the process table and display a line per pid. */ while ((ent = readdir(dir)) != NULL) { int rc; int fd; int length; char *p; char *q; struct io_node *ion; struct io_node *old_ion; long long rchar; long long wchar; long long syscr; long long syscw; long long read_bytes; long long write_bytes; long long cancelled_write_bytes; if (!isdigit(ent->d_name[0])) continue; ion = new_ion(ent->d_name); if (command_flag == 1) rc = get_cmdline(ion); if (command_flag == 0 || rc != 0) /* If the full command line is not asked for or is empty... */ rc = get_tcomm(ion); if (rc != 0) { free(ion); continue; } /* Read 'io' file. */ length = snprintf(filename, BUFFERLEN, "%s/%s/io", PROC, ent->d_name); if (length == BUFFERLEN) printf("WARNING - filename length may be too big for buffer: %d\n", __LINE__); fd = open(filename, O_RDONLY); if (fd == -1) { free(ion); continue; } length = read(fd, buffer, sizeof(buffer) - 1); close(fd); buffer[length] = '\0'; /* Parsing the io file data. */ p = buffer; GET_VALUE(ion->rchar); GET_VALUE(ion->wchar); GET_VALUE(ion->syscr); GET_VALUE(ion->syscw); GET_VALUE(ion->read_bytes); GET_VALUE(ion->write_bytes); GET_VALUE(ion->cancelled_write_bytes); old_ion = get_ion(ion->pid); /* Display the pid's io data. */ if (old_ion != NULL) { rchar = ion->rchar - old_ion->rchar; wchar = ion->wchar - old_ion->wchar; syscr = ion->syscr - old_ion->syscr; syscw = ion->syscw - old_ion->syscw; read_bytes = ion->read_bytes - old_ion->read_bytes; write_bytes = ion->write_bytes - old_ion->write_bytes; cancelled_write_bytes = ion->cancelled_write_bytes - old_ion->cancelled_write_bytes; if (kb_flag == 1 && hr_flag == 0) { rchar = BTOKB(rchar); wchar = BTOKB(wchar); syscr = BTOKB(syscr); syscw = BTOKB(syscw); read_bytes = BTOKB(read_bytes); write_bytes = BTOKB(write_bytes); cancelled_write_bytes = BTOKB(cancelled_write_bytes); } else if (mb_flag == 1 && hr_flag == 0) { rchar = BTOMB(rchar); wchar = BTOMB(wchar); syscr = BTOMB(syscr); syscw = BTOMB(syscw); read_bytes = BTOMB(read_bytes); write_bytes = BTOMB(write_bytes); cancelled_write_bytes = BTOMB(cancelled_write_bytes); } if (!(idle_flag == 1 && rchar == 0 && wchar == 0 && syscr == 0 && syscw == 0 && read_bytes == 0 && write_bytes == 0 && cancelled_write_bytes == 0)) { if (hr_flag == 0) printf("%5d %8lld %8lld %8lld %8lld %8lld %8lld %8lld %s\n", ion->pid, rchar, wchar, syscr, syscw, read_bytes, write_bytes, cancelled_write_bytes, ion->command); else printf("%5d %5s %5s %8lld %8lld %5s %6s %7s %s\n", ion->pid, format_b(rchar), format_b(wchar), syscr, syscw, format_b(read_bytes), format_b(write_bytes), format_b(cancelled_write_bytes), ion->command); } } else if (idle_flag != 1) /* * No previous data, show 0's instead of calculating negatives * only if we are shoring idle processes. */ printf("%5d %8d %8d %8d %8d %8d %8d %8d %s\n", ion->pid, 0, 0, 0, 0, 0, 0, 0, ion->command); upsert_data(ion); } closedir(dir); return; }