Date: Tue, 18 Jan 2011 17:01:15 -0800 (PST) From: Mark Terribile <materribile@yahoo.com> To: Chuck Swiger <cswiger@mac.com> Cc: freebsd-questions@freebsd.org Subject: Re: rusage and pthreads Message-ID: <35110.88084.qm@web110316.mail.gq1.yahoo.com> In-Reply-To: <9319A30D-9AC6-46BB-B7EE-A476C88A05ED@mac.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Chuck,=0A=0A> > I'm trying to figure out the interactions between=0A> rusag=
e and pthreads.=0A> =0A> There largely isn't any-- struct rusage is per-pro=
cess, not=0A> per thread.=0A =0A> > Peeking around in the kernel (7.2) I se=
e updates=0A> occurring in various places.=A0 kern_clock.c, for=0A> instanc=
e, appears to increment the memory occupancy (*rss)=0A> counters.=A0 This w=
ould make it appear that every thread=0A> gets part of the count, but also =
that only the process that=0A> happens to have the CPU at that moment gets =
its count=0A> updated, even if it holds the memory.=A0 Am I misreading=0A> =
this?=0A> =0A> Nope.=A0 statclock() is fired off periodically (with=0A> som=
e fuzz, to avoid clever games by processes trying to=0A> avoid being sample=
d) to update the stats for the currently=0A> running process.=0A> =0A> > An=
d the context switch counters also appear to be=0A> updated per-thread, but=
in mi_switch(), in=0A> kern_synch.c.=A0 Is this true?=0A> =0A> Probably.=
=0A> =0A> > If the answer is "yes, it's per-thread", then how does=0A> a pr=
ocess report its usages without putting the requisite=0A> code in each thre=
ad, along with the machinery to divert from=0A> whatever the thread is doin=
g (even waiting on I/O) to get=0A> the report at (nearly) the same time in =
all threads?=0A> =0A> The process doesn't have userland threads updating th=
is=0A> information.=A0 The kernel keeps track of it, and it=0A> updates the=
information periodically when the scheduler does=0A> context switches, whe=
n statclock() fires off, when disk I/O=0A> completes, etc.=0A=0AI'm looking=
at kern_clock.c::statclock(int usermode)=0A=0AThe code in question begins=
=0A=0A struct rusage* ru;=0A struct vmspace* vm;=0A st=
ruct thread *td;=0A struct proc *p;=0A ...=0A td =3D curthre=
ad;=0A p =3D td->td_proc;=0A=0Aand continues further down=0A=0A =
ru =3D &td->td_ru;=0A ru->ru_ixrss +=3D pgtok(vm->vm_tsize);=0A =
ru->ru_idrss +=3D pgtok(vm->vm_dsize);=0A ru->ru_isrss +=3D pg=
tok(vm->vm_ssize);=0A=0AThis looks to me like it's accumulating the data in=
per-thread counters. What's more, it's consistent with what I'm seeing on=
the user side. Note that this is 7.2; if 8.x behaves differently I'd like=
to know.=0A=0A Mark Terribile=0A=0A=0A=0A
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?35110.88084.qm>
