/* pio (Process I/O): Originally written based on Jim Mauro and Richard McDougall's msacct published in Appendix C of Solaris Internals. But the core of msacct, Microstate Accounting, is not used in this program. (C) Yong Huang, 2002 yong321.freeshell.org/freeware/pio.html */ #include #include #include #include #include #include #include #include #include static char *command; static int probe_one(pid_t); void print_usage(void); void probe_all(void); int nl = 1; main(int argc, char **argv) { pid_t pid; int hdr = 1, probeall = 0; int i; if ((command = strrchr(argv[0], '/')) != NULL) command++; else command = argv[0]; if (argc <= 1) { print_usage(); exit(1); } while ((i = getopt(argc, argv, "HhnAp:")) != EOF) { switch(i) { case 'H': /* No header */ hdr = 0; break; case 'h': /* Show Help or Usage */ print_usage(); exit(1); break; case 'n': /* No Newline at line end */ nl = 0; break; case 'p': /* Process to be Probed */ pid = atoi(optarg); break; case 'A': probeall = 1; /* All processes to be probed */ pid = 0; hdr = 0; break; } } if (hdr == 1) printf("PID\tInpBlk\tOutpBlk\tRWChar\n"); if (pid) probe_one(pid); else if (probeall) probe_all(); exit(0); } int probe_one(pid_t pid) { char pathname[100]; int rval = 0; int fd; prusage_t prusage; prusage_t *pup = &prusage; (void) sprintf(pathname, "/proc/%d/usage", (int)pid); if ((fd = open(pathname, O_RDONLY)) < 0) { perror("open usage"); return (1); } if (read(fd, &prusage, sizeof (prusage)) != sizeof (prusage)) { perror("read usage"); return (2); } else { printf("%d\t", pid); printf("%lu\t", pup->pr_inblk); /* input blocks */ printf("%lu\t", pup->pr_oublk); /* output blocks */ printf("%lu", pup->pr_ioch); /* chars read and written */ } (void) close(fd); if (nl == 1) printf("\n"); return (rval); } void print_usage() { fprintf(stderr, "usage: %s [-Hhn] -p -H: no header -h: help -n: no newline at line end -p: process ID follows -A: print I/O stats for all processes\n", command); } void probe_all() { pid_t pid = 1; DIR *dp; struct dirent *d; if ((dp = opendir("/proc")) == NULL) { perror("/proc"); exit(2); /* Can't open /proc */ } while ((d = readdir(dp)) != NULL) { pid = atoi(d->d_name); probe_one(pid); } }