[comp.unix.programmer] How to measure cpu time to ~ 1 millisecond?

rfinch@caldwr.water.ca.gov (Ralph Finch) (09/27/90)

Setup:  Sun 4/330, SunOS 4.1

We need to measure cpu time in a program down to about a 1 millisecond
resolution.  The clock call says it can only measure to 16.7 ms.  Any
other ideas on how cpu time can be measured?

Thanks,
-- 
Ralph Finch			916-445-0088
rfinch@water.ca.gov		...ucbvax!ucdavis!caldwr!rfinch
Any opinions expressed are my own; they do not represent the DWR

vendijag@aix.aix.kingston.ibm.com (Jay Goldberg) (09/29/90)

In article <237@locke.water.ca.gov> rfinch@caldwr.water.ca.gov (Ralph Finch) writes:
>
>Setup:  Sun 4/330, SunOS 4.1
>
>We need to measure cpu time in a program down to about a 1 millisecond
>resolution.  The clock call says it can only measure to 16.7 ms.  Any
>other ideas on how cpu time can be measured?

My best guess here is that your machine runs at 60 clock ticks per second.
There's a symbol in the kernel (at leat BSD derivatives) called "hz" which
contains the number of clock ticks per second on your machine.

This would mean that every clock tick, when your machine has its clock 
interrupt, the kernel takes note of what he's doing, thus giving you a
granularity of (1 sec)/(60 ticks) = 0.016666... which is a resolution
of 16.7 milliseconds.  So given that it's the kernel's responsibility
to maintain the count of CPU time per process, and he only wakes up every
16.7 milliseconds, you may be out of luck.

Your system might, however, provide a getrusage() system call (resource.h).
If you have it, that gives the "best estimation" for your system of CPU
utilization (system and user modes) with a granularity of microseconds
(see struct timeval in time.h)

Hope this helps!
-----
Jay A. Goldberg 	| Email inside IBM:    KGNAIX(VENDIJAG)
Software Contractor	|		or     vendijag@aix.aix.kingston.ibm.com
Resource One Inc. 	| Email outside IBM:   uunet!ibmsupt!jag
Phone: (914) 336 7207	| Phone: (914) 385 3384 or 8+695 3384

	I am not now, nor have I ever been, an employee of IBM.
	Had an opinion been requested, a purchase order would have
	been received by Resource One.  Who is John Galt?

thyssen@batserver.cs.uq.oz.au (Anthony Thyssen) (09/29/90)

rfinch@caldwr.water.ca.gov (Ralph Finch) writes:
>Setup:  Sun 4/330, SunOS 4.1

>We need to measure cpu time in a program down to about a 1 millisecond
>resolution.  The clock call says it can only measure to 16.7 ms.  Any
>other ideas on how cpu time can be measured?

   I was recently faced with the same problem. The Answer
There is not ^*%&^%$^%$ way!

My solution to this problem was to recomplie the program on a Amiga500
and time the part I wanted using the hardware CIA timers (1.3us per tick)

I was also informed another person in the same boat did his timing on
a Mac.

  Anthony Thyssen - (Dragon Computing!)         thyssen@batserver.cs.uq.oz.au
-------------------------------------------------------------------------------
                   I really hate this damm machine,
                    I wish that they would sell it.
                     It never does quite what I want,
                      Only what I tell it.
-------------------------------------------------------------------------------

guy@auspex.auspex.com (Guy Harris) (09/30/90)

>>Setup:  Sun 4/330, SunOS 4.1
>>
>>We need to measure cpu time in a program down to about a 1 millisecond
>>resolution.  The clock call says it can only measure to 16.7 ms.  Any
>>other ideas on how cpu time can be measured?
>
>My best guess here is that your machine runs at 60 clock ticks per second.

Actually, it's 100 clock ticks per second.  The reason why "clock()"
says it acts as if it were 60 clock ticks per second is that the code
was taken straight from S5, which uses the "times()" call, which in
SunOS maps the internal CPU time values to values that look as if they
were in 1/60ths of a second (this is inherited from 4.2BSD, which did
this for binary compatibility with 4.1BSD).

>Your system might, however, provide a getrusage() system call (resource.h).

It does, but...

>If you have it, that gives the "best estimation" for your system of CPU
>utilization (system and user modes) with a granularity of microseconds
>(see struct timeval in time.h)

...just because a "struct timeval" has a "tv_usec" field doesn't mean it
has a one-microsecond resolution.  The actual resolution is still the
underlying resolution of whatever clock is used, and on a 4/330 that's
the rate at which clock interrupts are generated, or 100 per second. 

jhpb@granjon.UUCP (Joseph H. Buehler) (10/01/90)

If you need accurate performance type information, about the only thing
I can think of is a statistical average.  Run the program lots of times,
and average the results.
--
Joe Buehler

hotz@isi.edu (Steve Hotz) (10/02/90)

Hi.

If you are looking for high resolution timing, send mail to:

	danzig@pollux.usc.edu

To gather data for his dissertation at berkeley, he and another
fellow developed a micro-second resolution timer board that sits in
an empty socket on SUN boxes (if i remember it's the DES chip socket).

I believe they are making the board available for ~$100, sending a
note to the above mailbox should get you details about how to get one.

On some systems you'll also need to get ahold of a set of PALs which
SUN just didn't include for some reason.  I believe they (the timer
folks) include the appropriate PALs for your machine.

They are fairly simply to install, but require kernel modifications
to add system calls to get time stamps from them.  This code is
included in the distribution, as well as some figure about overhead
to call the timer.

xxoo,
	steve