How to allow userland to access per-thread CPU time information?

Olivier Certner olivier.freebsduser at free.fr
Wed Jun 13 08:58:58 UTC 2007


        Hi all,

	In the emulator project I work on currently, I need to know the time CPUs 
have spent executing a particular thread of the current process. Basically, 
this is the same functionality (actually, a subset) as provided by 
getrusage(), except I would want it at the thread-level granularity. In the 
rusage structure, I'm mostly interested by the ru_{u,s}time fields.

        I've looked around for several solutions. The POSIX threads interface 
doesn't seem to define any time reporting function. In FreeBSD, no 
non-portable extension to this interface provides the functionality. Inside 
the kernel (I'm speaking about RELENG_6 in the following), the thread 
structure has fields to keep track of the CPU usage (td_{s,uu,us}ticks). I 
agree one such structure represents a KSE context, not exactly a "thread" as 
seen from userland. But the KSE system seems to provide a solution because it 
defines different mailboxes for userland threads, each having accounting 
fields (tm_{u,s}ticks).

        So, the only thing I seem to miss here is some support to retrieve 
this information from the threading library. Hence my questions: does anyone 
have begun to work on such a functionality? If not, are you aware of any 
attempt of standardizing such an access? I would be glad to help design 
and/or implement this functionality in FreeBSD's threading libraries.

        For completeness, I've also investigated the values that the "who" 
parameter of getrusage can take in several OSes, because some of the latter 
use it as a way to return per-thread statistics. It seems that only a few of 
them implement other values than RUSAGE_SELF and RUSAGE_CHILDREN. Some (such 
as Solaris or, since recently, Linux) support the RUSAGE_THREAD value. I 
don't think there is (and will be one day) consensus on whether the N:M 
threading model is definitely inferior to the 1:1 model (even in CURRENT, the 
old N:M library is still there as libkse, although no more the default), so, 
at the moment, I don't find this solution very elegant, since the kernel is 
not necessarily aware of all userland threads, and if it is, going to the 
kernel to finally retrieve the information from the userland thread scheduler 
library isn't very efficient. I would suggest rather a (non-?)portable 
addition to the pthread interface, such as a pthread_getrusage function.

        There has been a recent discussion on this topic on the 
LKML. Here's a link to a message from Ulrich Drepper that goes in the same 
direction:
http://www.opensubscriber.com/message/linux-kernel%40vger.kernel.org/6616133.html . 
Time counters were my primary interest, but more generally, access to a 
per-thread ressource usage report seems desirable. So far, I've not looked in 
detail which rusage structure fields would be relevant for threads.

	Any directions or thoughts?

        Regards,

                Olivier


More information about the freebsd-arch mailing list