From efade1503b44cbcd55d55705cb76e2b32554e526 Mon Sep 17 00:00:00 2001 From: Mikolaj Golub Date: Sat, 20 Apr 2013 07:50:59 +0000 Subject: [PATCH] Use procstat_getprocs(3) for retrieving thread information instead of direct sysctl calls. MFC after: 1 month --- usr.bin/procstat/procstat.c | 2 +- usr.bin/procstat/procstat.h | 2 +- usr.bin/procstat/procstat_sigs.c | 40 ++++++----------------------- usr.bin/procstat/procstat_threads.c | 39 +++++----------------------- 4 files changed, 17 insertions(+), 66 deletions(-) diff --git a/usr.bin/procstat/procstat.c b/usr.bin/procstat/procstat.c index 934e292e5de2..6b95246988cb 100644 --- a/usr.bin/procstat/procstat.c +++ b/usr.bin/procstat/procstat.c @@ -77,7 +77,7 @@ procstat(struct procstat *prstat, struct kinfo_proc *kipp) else if (sflag) procstat_cred(kipp); else if (tflag) - procstat_threads(kipp); + procstat_threads(prstat, kipp); else if (vflag) procstat_vm(kipp); else if (xflag) diff --git a/usr.bin/procstat/procstat.h b/usr.bin/procstat/procstat.h index e65436dade09..718eb476ce69 100644 --- a/usr.bin/procstat/procstat.h +++ b/usr.bin/procstat/procstat.h @@ -44,7 +44,7 @@ void procstat_files(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_kstack(struct kinfo_proc *kipp, int kflag); void procstat_rlimit(struct kinfo_proc *kipp); void procstat_sigs(struct procstat *prstat, struct kinfo_proc *kipp); -void procstat_threads(struct kinfo_proc *kipp); +void procstat_threads(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_threads_sigs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_vm(struct kinfo_proc *kipp); diff --git a/usr.bin/procstat/procstat_sigs.c b/usr.bin/procstat/procstat_sigs.c index 70df25004fdd..49e2b196e5c9 100644 --- a/usr.bin/procstat/procstat_sigs.c +++ b/usr.bin/procstat/procstat_sigs.c @@ -86,48 +86,24 @@ procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) } void -procstat_threads_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) +procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_proc *kip; pid_t pid; - int error, name[4], j; - unsigned int i; - size_t len; + int j; + unsigned int count, i; pid = kipp->ki_pid; if (!hflag) printf("%5s %6s %-16s %-7s %4s\n", "PID", "TID", "COMM", "SIG", "FLAGS"); - /* - * We need to re-query for thread information, so don't use *kipp. - */ - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; - name[3] = pid; - - len = 0; - error = sysctl(name, 4, NULL, &len, NULL, 0); - if (error < 0 && errno != ESRCH) { - warn("sysctl: kern.proc.pid: %d", pid); - return; - } - if (error < 0) - return; - - kip = malloc(len); + kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, + pid, &count); if (kip == NULL) - err(-1, "malloc"); - - if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { - warn("sysctl: kern.proc.pid: %d", pid); - free(kip); return; - } - - kinfo_proc_sort(kip, len / sizeof(*kipp)); - for (i = 0; i < len / sizeof(*kipp); i++) { + kinfo_proc_sort(kip, count); + for (i = 0; i < count; i++) { kipp = &kip[i]; for (j = 1; j <= _SIG_MAXSIG; j++) { printf("%5d ", pid); @@ -140,5 +116,5 @@ procstat_threads_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) printf("\n"); } } - free(kip); + procstat_freeprocs(procstat, kip); } diff --git a/usr.bin/procstat/procstat_threads.c b/usr.bin/procstat/procstat_threads.c index ffd659c0064c..6bd88da508e3 100644 --- a/usr.bin/procstat/procstat_threads.c +++ b/usr.bin/procstat/procstat_threads.c @@ -40,47 +40,22 @@ #include "procstat.h" void -procstat_threads(struct kinfo_proc *kipp) +procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp) { struct kinfo_proc *kip; - int error, name[4]; - unsigned int i; + unsigned int count, i; const char *str; - size_t len; if (!hflag) printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); - /* - * We need to re-query for thread information, so don't use *kipp. - */ - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; - name[3] = kipp->ki_pid; - - len = 0; - error = sysctl(name, 4, NULL, &len, NULL, 0); - if (error < 0 && errno != ESRCH) { - warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); - return; - } - if (error < 0) - return; - - kip = malloc(len); + kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, + kipp->ki_pid, &count); if (kip == NULL) - err(-1, "malloc"); - - if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { - warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); - free(kip); return; - } - - kinfo_proc_sort(kip, len / sizeof(*kipp)); - for (i = 0; i < len / sizeof(*kipp); i++) { + kinfo_proc_sort(kip, count); + for (i = 0; i < count; i++) { kipp = &kip[i]; printf("%5d ", kipp->ki_pid); printf("%6d ", kipp->ki_tid); @@ -139,5 +114,5 @@ procstat_threads(struct kinfo_proc *kipp) } printf("\n"); } - free(kip); + procstat_freeprocs(procstat, kip); }