From f63ee3a6ce91574448645ed33a0b6a1364e3afc5 Mon Sep 17 00:00:00 2001 From: Paul Irofti Date: Thu, 12 Apr 2012 14:59:20 +0000 Subject: Add per thread accounting, mainly for usage & friends. This expands the already bloated FILL_KPROC macro to take an extra parameter that indicates if the callee is a thread or a process. The userland bits are adjusted accordingly and ps(1) and top(1) now display per thread usage times when -H is used. Also pkill(1) had to be adjusted so that duplicates don't pop up. libkvm does basically the same thing as the kernel bits. Okay guenther@. --- lib/libkvm/kvm_getprocs.3 | 8 ++++++-- lib/libkvm/kvm_proc2.c | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/libkvm/kvm_getprocs.3 b/lib/libkvm/kvm_getprocs.3 index e2acf477b22..93a4a7b4381 100644 --- a/lib/libkvm/kvm_getprocs.3 +++ b/lib/libkvm/kvm_getprocs.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: kvm_getprocs.3,v 1.17 2012/01/07 05:38:12 guenther Exp $ +.\" $OpenBSD: kvm_getprocs.3,v 1.18 2012/04/12 14:59:19 pirofti Exp $ .\" $NetBSD: kvm_getprocs.3,v 1.13 2003/08/07 16:44:37 agc Exp $ .\" .\" Copyright (c) 1992, 1993 @@ -34,7 +34,7 @@ .\" .\" @(#)kvm_getprocs.3 8.1 (Berkeley) 6/4/93 .\" -.Dd $Mdocdate: January 7 2012 $ +.Dd $Mdocdate: April 12 2012 $ .Dt KVM_GETPROCS 3 .Os .Sh NAME @@ -114,6 +114,10 @@ and .Fn kvm_close will overwrite this storage. .Pp +.Fn kvm_getprocs +sets the thread ID field accordingly for each thread except for the +process (main thread) which has it set to \-1. +.Pp .Fn kvm_getargv returns a null-terminated argument vector that corresponds to the command line arguments passed to process indicated by diff --git a/lib/libkvm/kvm_proc2.c b/lib/libkvm/kvm_proc2.c index f4306bf691b..c78a93211f6 100644 --- a/lib/libkvm/kvm_proc2.c +++ b/lib/libkvm/kvm_proc2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kvm_proc2.c,v 1.10 2012/03/23 15:51:25 guenther Exp $ */ +/* $OpenBSD: kvm_proc2.c,v 1.11 2012/04/12 14:59:19 pirofti Exp $ */ /* $NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $ */ /*- * Copyright (c) 1998 The NetBSD Foundation, Inc. @@ -282,9 +282,39 @@ kvm_proclist(kvm_t *kd, int op, int arg, struct proc *p, limp = NULL; #define do_copy_str(_d, _s, _l) kvm_read(kd, (u_long)(_s), (_d), (_l)-1) + if ((proc.p_flag & P_THREAD) == 0) { + FILL_KPROC(&kp, do_copy_str, &proc, &process, &pcred, + &ucred, &pgrp, p, proc.p_p, &sess, vmp, limp, sap, + 0); + + /* stuff that's too painful to generalize */ + kp.p_pid = process_pid; + kp.p_ppid = parent_pid; + kp.p_sid = leader_pid; + if ((process.ps_flags & PS_CONTROLT) && + sess.s_ttyp != NULL) { + kp.p_tdev = tty.t_dev; + if (tty.t_pgrp != NULL && + tty.t_pgrp != process.ps_pgrp && + KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { + _kvm_err(kd, kd->program, + "can't read tpgrp at &x", + tty.t_pgrp); + return (-1); + } + kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1; + kp.p_tsess = PTRTOINT64(tty.t_session); + } else { + kp.p_tpgid = -1; + kp.p_tdev = NODEV; + } + + memcpy(bp, &kp, esize); + bp += esize; + ++cnt; + } FILL_KPROC(&kp, do_copy_str, &proc, &process, &pcred, &ucred, - &pgrp, p, proc.p_p, &sess, vmp, limp, sap); -#undef do_copy_str + &pgrp, p, proc.p_p, &sess, vmp, limp, sap, 1); /* stuff that's too painful to generalize into the macros */ kp.p_pid = process_pid; @@ -309,6 +339,7 @@ kvm_proclist(kvm_t *kd, int op, int arg, struct proc *p, memcpy(bp, &kp, esize); bp += esize; ++cnt; +#undef do_copy_str } return (cnt); } -- cgit v1.2.3