LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-27-2010, 03:16 AM   #1
arielsegal
Member
 
Registered: Jul 2007
Posts: 38

Rep: Reputation: 15
measuring microseconds with linux


I need to measure the time it takes for a program I'm writing to do Its job in microseconds resolution. I'm using Ubuntu 10.10. My Idea is, to write a device driver that directly reads the counter that produces the interrupt of RTC ticks. How do I do this, for a Linux kernel and a PC hardware?
 
Old 10-27-2010, 05:15 AM   #2
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
clock() returns microseconds in linux systems

why not read the clock when you start the program, then again when the program exits, and subtract the two?

Perhaps something like....
Code:
int t = clock();

[program]

cout << "in ms " << ((static_cast<float>(clock()-t)/
CLOCKS_PER_SEC)*1000);
 
Old 10-27-2010, 11:21 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
Oops. I posted before I was done. Stay tuned ...

Last edited by wje_lq; 10-27-2010 at 11:22 AM.
 
Old 10-27-2010, 11:48 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
Quote:
Originally Posted by arielsegal View Post
I need to measure the time it takes for a program I'm writing to do Its job in microseconds resolution. I'm using Ubuntu 10.10. My Idea is, to write a device driver that directly reads the counter that produces the interrupt of RTC ticks. How do I do this, for a Linux kernel and a PC hardware?
Quote:
Originally Posted by Simon Bridge View Post
clock() returns microseconds in linux systems

why not read the clock when you start the program, then again when the program exits, and subtract the two?

Perhaps something like....
Code:
int t = clock();

[program]

cout << "in ms " << ((static_cast<float>(clock()-t)/
CLOCKS_PER_SEC)*1000);
Hoo boy. Where do I begin?
  1. Simon didn't say this explicitly, but his code is to be inserted in your program itself, not in any driver. You don't need to mess with the kernel itself for this.
  2. Before you start, you need to decide whether you want to measure elapsed time (wall clock time) or compute time (processor time consumed).
  3. If you want to measure processor time within the program itself, you can use clock(), as Simon suggests. But since that counts starting at zero when the process begins, you don't need to call clock() twice and subtract. Just call it once, at the end of the program.
  4. If you don't want to modify your program itself, or the program is sufficiently complex that you don't want to find all the places where it can exit and place the clock() call in each of those places, you can use fork(), and use execsomething() in the child process and waitpid() in the parent process. The waitpid() should actually be in a loop which does these things:
    1. Call times(), and use the struct of returned values to get the user time and system time of the child. Add these two values together; the second one is the processor time consumed by the kernel on behalf of this user process. Read the man page for times(). It will advise you to use sysconf() to get the number of ticks per second. Heed that advice.
    2. Call waitpid() with the WNOHANG option. If the process has finished, exit the loop.
    3. sleep(1);
    If the sleep() is too hokey for you, you can do it the "right" way by catching the SIGCHLD signal.
  5. If, on the other hand, you want to measure not processor time but elapsed (wall clock) time, use gettimeofday() twice, either in the actual program or in the parent process. Subtract. Note that the precision of the time is down to the microsecond. But note that if you change the time of day on the system while the program is running, you'll probably have grossly incorrect results.
  6. If you want elapsed (wall clock) time, but you want to avoid the possibility of the wall clock changing under your feet (boy howdy, what a metaphor), go back to using times(), but use the actual returned value of the function, not the data in the struct. Once again, you'll need that conversion constant provided by sysconf().
  7. Finally, precision and accuracy are not the same thing. You'll get values in units of microseconds, but if you're running on an IBM(R) compatible PC, the accuracy is not likely to be anything near that.
Hope this helps.
 
Old 10-28-2010, 02:56 AM   #5
arielsegal
Member
 
Registered: Jul 2007
Posts: 38

Original Poster
Rep: Reputation: 15
Thank you all for the answers.
As wje_lq said: precision and accuracy are not the same thing. You'll get values in units of microseconds, but if you're running on an IBM(R) compatible PC, the accuracy is not likely to be anything near that.
This is the reason I want to write a device driver. How go I know what register to read for a acurare result?
 
  


Reply

Tags
kernel source, linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 to implement delay (microseconds) with real timer ticking at 100ms. mayankparasher Programming 1 12-24-2009 01:04 PM
[SOLVED] Inserting microseconds symbol in LaTex kushalkoolwal Linux - Software 3 10-16-2009 10:32 AM
LXer: Report: Measuring Linux and Open Source LXer Syndicated Linux News 1 06-25-2007 02:12 PM
Measuring stability of Linux distributions benigni Linux - Distributions 10 12-20-2006 04:41 AM
measuring time in linux (in C) Four Programming 4 12-30-2005 10:21 PM


All times are GMT -5. The time now is 05:58 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration