LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2003, 05:16 PM   #1
rasselin
LQ Newbie
 
Registered: Aug 2003
Location: Pennsylvania
Posts: 7

Rep: Reputation: 0
Question accurately measure time


Hi,

I'm trying to accurately measure time, and found the following very useful web page:

www . mostang . com
/~davidm/papers/expo97/paper/doc004.html

(sorry...i haven't posted enough messages to put the hyperlink "correctly"...just remove the spaces....)

Here the author talks about: " most modern CPUs provide a register that is incremented either at the clock frequency of the CPU or an integer fraction thereof. The Alpha architecture provides the rpcc (read processor cycle count) instruction." And also provides two simple functions to access this register.

I would like to implement both of these, except that I am using and Intel Pentium 4. Any help is appreciated!

Ramy
 
Old 09-05-2003, 05:18 PM   #2
rasselin
LQ Newbie
 
Registered: Aug 2003
Location: Pennsylvania
Posts: 7

Original Poster
Rep: Reputation: 0
...part of the webpage.....

REFERENCED: http://www.mostang.com/~davidm/paper...er/doc004.html


3.1 Accurately Measuring Time
The Unix way of measuring time is by calling gettimeofday(). This returns the current real time at a resolution of typically one timer tick (about 1ms on the Alpha). The advantage of this function is that it's completely portable across all Linux platforms. The disadvantage is its relatively poor resolution (1ms corresponds to 500,000 CPU cycles on a 500MHz CPU!) and, more severely, it involves a system call. A system call is relatively slow and has the tendency to mess up your memory system. E.g., the cache gets loaded with kernel code so when your program resumes execution, it sees many cache misses that it wouldn't see without the call to gettimeofday(). This is all right for measuring times on the order of seconds or minutes, but for finer-grained measurements, something better is needed.

Fortunately, most modern CPUs provide a register that is incremented either at the clock frequency of the CPU or an integer fraction thereof. The Alpha architecture provides the rpcc (read processor cycle count) instruction. It gives access to a 64 bit register that contains a 32 bit counter in the lower half of the register. This counter is incremented once every N clock cycles. All current chips use N=1, so the register gets incremented at the full clock frequency (but there may be future Alpha processors where N>1). The top half of the value returned by rpcc is operating system dependent. Linux and Digital Unix return a correction value that makes it easy to implement a cycle counter that runs only when the calling process is executing (i.e., this allows to measure the process's virtual cycle count). With gcc, it's very easy to write inlined functions that provide access to the cycle counters:

static inline u_int realcc (void) {
u_long cc;
/* read the 64 bit process cycle counter into variable cc: */
asm volatile("rpcc %0" : "=r"(cc) : : "memory");
return cc; /* return the lower 32 bits */
}

static inline unsigned int virtcc (void) {
u_long cc;
asm volatile("rpcc %0" : "=r"(cc) : : "memory");
return (cc + (cc<<32)) >> 32; /* add process offset and count */
}

With this code in place, function realcc() returns the 32 bit real-time cycle count whereas function virtcc() returns the 32 bit virtual cycle count (which is like the real-time count except that it doesn't count when the process isn't running).
Calling these functions involves very small overheads: the slowdown is on the order of 1-2 cycles per call and adds only one or two instructions (which is less than the overhead for a function call). A good way of using these functions is to create an execution time histogram. For example, the function below measures individual execution times of calls to sqrt(2.0) and prints the results to standard output (as usual, care must be taken to ensure that the compiler doesn't optimize away the actual computation). Printing the individual execution times makes it easy to create a histogram with a little post-processing.

void measure_sqrt (void) {
u_int start, stop, time[10]; int i; double x = 2.0;
for (i = 0; i < 10; ++i) {
start = realcc(); sqrt(x); stop = realcc();
time[i] = stop - start;
}
for (i = 0; i < 10; ++i) printf(" %u", time[i]); printf("\n");
}

Note that the results are printed in a separate loop---this is important since printf is a rather big and complicated function that may even result in a system call or two. If printf were part of the main loop, the results would be much less reliable. A sample run of the above code might produce output like this:
120 101 101 101 101 101 101 101 101 101

Since this output was obtained on a 333MHz Alpha, 120 cycles corresponds to 36ns and 101 cycles corresponds to 30ns. The output shows nicely how the first call is quite a bit slower since the memory system (instruction cache in particular) is cold at that point. Since the square-root function is small enough to easily fit in the first-level instruction cache, all but the first calls execute at exactly the same time.
You may wonder why the above code uses realcc() instead of virtcc(). The reason for this is simple: we want to know the results that were affected by a context switch. By using realcc(), a call that suffers a context switch will be much slower than any of the other calls. This makes it easy to identify and discard such unwanted outliers.

The cycle counter provides a very low-overhead method of measuring individual clock cycles. On the down side, it cannot measure very long intervals. On an Alpha chip running at 500MHz, a 32 bit cycle counter overflows after just eight and a half seconds! This is not normally a problem when making fine-grained measurements, but it is important to keep the limit in mind.


3.2 Performance Counters
The Alpha chips, like most other modern CPUs, provide a variety of performance counters. These allow measuring various event counts or rates such as the number of cache misses, instruction issue-rate, branch-mispredicts, or instruction frequency. Unfortunately, the author is not aware of any Linux API that would provide access to these counters. This is particularly unfortunate since both the Pentium and the Pentium Pro chips provide similar counters. Digital Unix gives access to these counters via the uprofile and kprofile programs and an ioctl-based interface documented in the pfm(7) man page. Hopefully, something similar (but more general) will eventually become available for Linux. With the proper tools, these counters can provide a wealth of information.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I measure how much real time is used to run an executable? markhod Linux - General 1 04-07-2005 04:41 AM
CD Writer, Floppy Drive don't write accurately (Fedora Core 1) scrubmonkey Linux - Hardware 3 10-26-2004 11:43 PM
Can I somehow accurately measure the time a operation takes? Like 'unzip -x foo.zip'? brynjarh Linux - Newbie 9 08-18-2004 09:26 PM
How to measure the time to build a package? mullog Linux From Scratch 10 05-11-2004 11:23 AM
how to accurately measure processing time rasselin Programming 3 09-01-2003 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration