Mark Gritter (markgritter) wrote,
Mark Gritter

While I'm Ranting

getrusage() is a system call that tells you (among other things) how much CPU your process has been using. This is useful for making performance measurements. Until it was 'fixed' to match Posix behavior, Linux would actually give you the CPU usage of the current thread. This was also useful, because in a multi-threaded application, which thread is burning up CPU becomes an interesting question.

A patch was put in place (somwhere around 2.6.25) to add the RUSAGE_THREAD flag, to return per-thread usage. In the discussion leading up to it, I found this gem from one of the maintainers, which is very nearly the quintessence of what I hate about the Linux development community:

Yes, for intra-process lookups relying on /proc is no good idea. But
then, I have not seen any reason so far why such an API is needed and
why a thread cannot just be responsible for reading its own usage data.

in response to a patch which had a more general pthread_getrusage which allowed you to look up the CPU usage of any thread from any other thread.

So, is it hopeless? No, because you can in fact read the desired information out of /proc/{pid}/task/{tid}/stat (by extracting the appropriate fields, converting them from text to binary and from jiffies to milliseconds.)

To be fair, getrusage() would be made obsolete by a larger performance-measurement framework within the Linux kernel (and I'm sure there are 2 or 3 in progress). And the original patch did open up a wider range of issues than it really needed to. The "I can't think of a way you would use this" answer really rubs me the wrong way, though. There's a load of difference between writing a data-collection routine which inventories the state of the system, versus intrusively asking each thread to update its own record of CPU time.

On the third hand, Linux getrusage doesn't support much of the standard anyway even though some of the fields are available in /proc.
Tags: linux, programming, rant, software
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.