rusage and pthreads

Mark Terribile materribile at yahoo.com
Wed Jan 19 01:01:18 UTC 2011


Chuck,

> > 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?
> 
> Probably.
> 
> > 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.

I'm looking at kern_clock.c::statclock(int usermode)

The code in question begins

        struct rusage* ru;
        struct vmspace* vm;
        struct thread *td;
        struct proc *p;
  ...
        td = curthread;
        p = td->td_proc;

and continues further down

        ru = &td->td_ru;
        ru->ru_ixrss += pgtok(vm->vm_tsize);
        ru->ru_idrss += pgtok(vm->vm_dsize);
        ru->ru_isrss += pgtok(vm->vm_ssize);

This 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.

    Mark Terribile



      


More information about the freebsd-questions mailing list