/* * listprusage - list various prusage statistics by process. Solaris, C. * * 01-Jul-2005, ver 0.70 (first release. check for newer versions) * * USAGE: listprusage * * COMPILE: cc -o listprusage listprusage.c * * FIELDS: * PID process ID * MINF minor faults * MAJF major faults * INBLK in blocks * OUBLK out blocks * SYSC system calls * IOCH read/write characters * * NOTES: * - MINF always reports zero (Solaris doesn't increment it). * - This is the first release, future versions will have options. * * Standard Disclaimer: This is freeware, use at your own risk. * * COPYRIGHT: Copyright (c) 2005 Brendan Gregg. * * 01-Jul-2005 Brendan Gregg Created this. */ #include #include #include #include #include static void printprusage(pid_t pid); int main() { DIR *dirp; struct dirent *dp; pid_t pid; /* open /proc for list of processes */ if ((dirp = opendir("/proc")) == NULL) { perror("ERROR1: Couldn't open /proc"); return (1); } /* print header */ (void) printf("%6s %6s %6s %6s %6s %10s %12s\n", "PID", "MINF", "MAJF", "INBLK", "OUBLK", "SYSC", "IOCH"); /* main loop */ for (;;) { /* read filename */ if ((dp = readdir(dirp)) == NULL) break; if (strcmp(dp->d_name, ".") == 0) continue; if (strcmp(dp->d_name, "..") == 0) continue; /* print process details */ pid = atoi(dp->d_name); (void) printprusage(pid); } (void) closedir(dirp); return (0); } /* * printprusage * * this function takes a PID and prints out various prusage statistics. */ static void printprusage(pid_t pid) { struct prusage pru; struct prusage *prup = &pru; char path[64]; FILE *filep; /* read prusage data */ (void) sprintf(path, "/proc/%d/usage", (int)pid); if (!(filep = fopen(path, "r"))) return; if (!(fread(prup, sizeof (struct prusage), 1, filep))) return; (void) fclose(filep); /* print output line */ (void) printf("%6d %6lu %6lu %6lu %6lu %10lu %12lu\n", pid, prup->pr_minf, prup->pr_majf, prup->pr_inblk, prup->pr_oublk, prup->pr_sysc, prup->pr_ioch); }