rusage and pthreads

Chuck Swiger cswiger at
Wed Jan 19 00:36:05 UTC 2011

On Jan 18, 2011, at 4:07 PM, Mark Terribile wrote:
> I'm trying to figure out the interactions between rusage and pthreads.

There largely isn't any-- struct rusage is per-process, not per thread.

> Peeking around in the kernel (7.2) I see updates occurring in various places.  kern_clock.c, for instance, appears to increment the memory occupancy (*rss) counters.  This would make it appear that every thread gets part of the count, but also that only the process that happens to have the CPU at that moment gets its count updated, even if it holds the memory.  Am I misreading this?

Nope.  statclock() is fired off periodically (with some fuzz, to avoid clever games by processes trying to avoid being sampled) to update the stats for the currently running process.

> And the context switch counters also appear to be updated per-thread, but in mi_switch(), in kern_synch.c.  Is this true?


> If the answer is "yes, it's per-thread", then how does a process report its usages without putting the requisite code in each thread, along with the machinery to divert from whatever the thread is doing (even waiting on I/O) to get the report at (nearly) the same time in all threads?

The process doesn't have userland threads updating this information.  The kernel keeps track of it, and it updates the information periodically when the scheduler does context switches, when statclock() fires off, when disk I/O completes, etc.


More information about the freebsd-questions mailing list