High-resolution user/system times?

High-resolution user/system times?

Oleg Goldshmidt pub at goldshmidt.org
Wed Jul 25 12:48:45 IDT 2012


On Wed, Jul 25, 2012 at 11:07 AM, Nadav Har'El <nyh at math.technion.ac.il>wrote:

> On Wed, Jul 25, 2012, Nadav Har'El wrote about "High-resolution
> user/system times?":
> > I'm now trying to measure a process running around 3 milliseconds, less
> > than one jiffy, and I still want to understand how much of it is spent in
> > user space, and how much of it is spent in kernel space (e.g., handling
> on
>
> Thanks for all the ideas.
> I think I found an even simpler solution:
>
> It appears that while times(2) has a 4-ms resolution,


Sanity check: I assume you measured it, right? Out of curiosity I did

#include <unistd.h>
#include <sys/times.h>
#include <stdio.h>

int main(void) {
    return printf("%ld\n",sysconf(_SC_CLK_TCK));
}

on a couple of systems, and got 100 both times, which corresponds to a 10ms
resolution. This is what I'd expect if HZ is 100 in the kernel.

I recall a discussion here at work where, I believe, one of the conclusions
was that it was possible to have hi-res timers with clock_gettime(0 and
clock_getres() (which i mentioned already), but functions used to getting
system vs. user time such as getrusage() were limtied by software timer
accuracy (jiffies).

it doesn't mean
> that every time a 3-ms process is measured it returns 0. Rather, it
> seems probabilistic - the user-space count seems (but I didn't verify in
> the code...) to return 0 if the timer interrupt happened while the code
> was in the kernel or 1 if the code was in user space.
> So what I can do is to run the whole process 1,000 times, and add up the
> "1" and "0"s which I get for system time, and if I get for example 30,
> I know that the process was in the kernel for 30/1000 of the time.
>
> I no idea how accurate this will be, but it might just work, and is
> very simple...
>

So I gather you think this will be different from what you wanted to avoid
in your original post  because you think the startup code will be repeated
on each iteration? It's difficult to assess without knowing what it is that
you are trying to measure and what, in the context, the difference is.

I cannot really point to why this would not be valid, but intuitively I
have my doubts. One possible cause of mixup may be the difference between
accuracy and precision. Averaging over a set of possibly imprecise and
possibly inaccurate measurements may improve precision, but not accuracy.

-- 
Oleg Goldshmidt | oleg at goldshmidt.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20120725/d00152ae/attachment.html>


More information about the Linux-il mailing list