LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-21-2009, 04:13 AM   #1
selalerer
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Rep: Reputation: 0
Clock_realtime_hr


Hi all.

Machine details:
~~~~~~~~~~~~~~~~
We have at work a Linux server. I think it is a RedHat Enterprise 4. This path:

/usr/include/c++/4.1.2/x86_64-redhat-linux/

exists, so it will probably help identifying the version. This is the uname -a output:

Linux XXXX 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:15 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux

If it makes any difference, this is virtual server on Xen.


The problem:
~~~~~~~~~~~~
I want to use sys_clock_gettime(CLOCK_REALTIME_HR) but the compiler complains that CLOCK_REALTIME_HR is not defined.

Anybody has some experience with that? Knows what I'm doing wrong?


More details:
~~~~~~~~~~~~~
This is from 'man 2 clock_gettime':

clock_gettime returns the current timespec value of tp for the specific clock, which_clock. The values that clockid_t currently supports for POSIX.1b timers, as defined in include/linux/time.h, are:

CLOCK_REALTIME_HR
High resolution version of CLOCK_REALTIME.

But I can't find this CLOCK_REALTIME_HR in /usr/include/linux/time.h.
I tried including <linux/time.h>, it doesn't make it any better.



Thank you for any assistance,
Sela.
 
Old 07-22-2009, 02:47 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try "gettimeofday()". For example:
Code:
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
    tm->tm_sec, tv.tv_usec);
 
Old 07-23-2009, 04:42 AM   #3
selalerer
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
We need a better option.

Quote:
Originally Posted by paulsm4 View Post
Try "gettimeofday()". For example:
Code:
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
    tm->tm_sec, tv.tv_usec);
Thanks, but we rather have a higher resolution timer. The CLOCK_REALTIME_HR seems like the right solution, we are just having trouble compiling it. Any help would be appreciated. A better solution, if someone has such a solution to suggest, is also welcomed.
 
Old 07-23-2009, 05:55 AM   #4
sd9
Member
 
Registered: Apr 2009
Posts: 53

Rep: Reputation: 15
CLOCK_REALTIME_HR is removed. Use CLOCK_REALTIME_HR = CLOCK_REALTIME.

Referenced from : http://www.nabble.com/Re:-RT,-timers...-p6291002.html
 
Old 07-23-2009, 09:24 AM   #5
selalerer
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
That's a disapointment.

Quote:
Originally Posted by sd9 View Post
CLOCK_REALTIME_HR is removed. Use CLOCK_REALTIME_HR = CLOCK_REALTIME.

Referenced from : http://www.nabble.com/Re:-RT,-timers...-p6291002.html
Unfortunately the resolution of this clock is too low for us. On other Unix machines we use gethrtime(), but this function is not available in Linux. Is there some other replacement or do we have to resort to Assembly?
 
Old 07-23-2009, 10:29 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by selalerer View Post
Unfortunately the resolution of this clock is too low for us. On other Unix machines we use gethrtime(), but this function is not available in Linux. Is there some other replacement or do we have to resort to Assembly?
CLOCK_REALTIME is the same as CLOCK_REALTIME_HR, so I'm not sure why you're saying that it is insufficient. If CLOCK_REALTIME is insufficient, CLOCK_REALTIME_HR would also be insufficient.

Additionally, see this for more information on what options you'll need to enable:
http://kerneltrap.org/node/6750

-Aaron
 
Old 08-08-2009, 01:21 PM   #7
selalerer
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by orgcandman View Post
CLOCK_REALTIME is the same as CLOCK_REALTIME_HR, so I'm not sure why you're saying that it is insufficient. If CLOCK_REALTIME is insufficient, CLOCK_REALTIME_HR would also be insufficient.

Additionally, see this for more information on what options you'll need to enable:
http://kerneltrap.org/node/6750

-Aaron
Hmm.. That's disappointing. I did not think Linux will have any disadvantage on other Unix machines we use (HP-UX, AIX).

Maybe we can set it as a requirement of our product to install these high resolution timers patches. I'll have to consult my bosses on that :-). One thing is for sure, we can not do with the 1 millisecond resolution the system calls offer now.

Thanks for the info,
Sela.
 
Old 08-08-2009, 05:42 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
The CLOCK_REALTIME is correct. Use it with clock_gettime().

Example usage:
Code:
#include <time.h>
#include <stdio.h>

int main()
{
   struct timespec tv;
   clock_gettime(CLOCK_REALTIME, &tv);

   printf("seconds:     %u\n", (unsigned int) tv.tv_sec);
   printf("nanoseconds: %u\n", (unsigned int) tv.tv_nsec);

   return 0;
}
When linking the code, indicate that you want to use the real-time library; in other words, specify -lrt.

Code:
gcc gettime.c -lrt -o gettime
 
Old 08-09-2009, 11:49 AM   #9
selalerer
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
The CLOCK_REALTIME is correct. Use it with clock_gettime().

Example usage:
Code:
#include <time.h>
#include <stdio.h>

int main()
{
   struct timespec tv;
   clock_gettime(CLOCK_REALTIME, &tv);

   printf("seconds:     %u\n", (unsigned int) tv.tv_sec);
   printf("nanoseconds: %u\n", (unsigned int) tv.tv_nsec);

   return 0;
}
When linking the code, indicate that you want to use the real-time library; in other words, specify -lrt.

Code:
gcc gettime.c -lrt -o gettime
Already did that, the resolution is a bit below one millisecond. That's not a high enough resolution for us.

Thank you.
 
Old 08-09-2009, 02:02 PM   #10
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Just because you get a *value* in nanoseconds, it doesn't mean that it's *accurate* to nanoseconds!

It isn't.

This is equally true of Linux, HP-UX, AIX, Solaris ... and most definitely true of Windows.

IMHO .. PSM
 
Old 08-09-2009, 05:00 PM   #11
gangettan
LQ Newbie
 
Registered: Aug 2009
Posts: 7

Rep: Reputation: 0
Wow ... This is a really heated discussion ...


linux

Last edited by gangettan; 08-24-2009 at 05:50 AM.
 
Old 08-09-2009, 05:07 PM   #12
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 3
Quote:
Originally Posted by selalerer View Post
Hmm.. That's disappointing. I did not think Linux will have any disadvantage on other Unix machines we use (HP-UX, AIX).

Maybe we can set it as a requirement of our product to install these high resolution timers patches. I'll have to consult my bosses on that :-). One thing is for sure, we can not do with the 1 millisecond resolution the system calls offer now.

Thanks for the info,
Sela.
Did you read the manpage for the clock_* functions? It would answer most of your questions. clock_getres gives you the clock resolution on your system, if your kernel is tickless it will return one nanosecond.

This code prints "Clock res = 0s:1ns" on my system:
Code:
#include <stdio.h>
#include <time.h>
#include <assert.h>

int main() {
  struct timespec res;
  assert( clock_getres(CLOCK_REALTIME, &res) != -1);
  printf("Clock res = %ds:%ldns\n", res.tv_sec, res.tv_nsec);
  return 0;
}
Ah, sorry. Now that I've re-read you post I see that you are using an ancient kernel. The merging of bits from Ingo Molnar's realtime patch into the vanilla kernel started around 2.6.18 AFAIK, so I think it can be tickless. Also check out kernel preemption.

Last edited by haikan; 08-09-2009 at 06:17 PM. Reason: Added some info...
 
Old 08-09-2009, 05:16 PM   #13
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by selalerer View Post
Already did that, the resolution is a bit below one millisecond. That's not a high enough resolution for us.

Thank you.
I suspect that you do not even know what a millisecond is. FYI, it is 1/1000 of a second. A microsecond is 1/1000000 of a second. A nanosecond is 1/1000000000 of a second.

If your POS system can't even do millisecond resolution, then you and your client need serious help.
 
Old 08-09-2009, 06:55 PM   #14
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 3
Quote:
Originally Posted by dwhitney67 View Post
I suspect that you do not even know what a millisecond is. FYI, it is 1/1000 of a second. A microsecond is 1/1000000 of a second. A nanosecond is 1/1000000000 of a second.

If your POS system can't even do millisecond resolution, then you and your client need serious help.
I think you're being unfair. His kernel version is 2.6.18, and back then system calls that set timeouts was limited by the resolution of the software clock, which measures time in jiffies. In 2.6.18 jiffies could be 0.01, 0.004, or 0.001 seconds, depending on the kernel configuration. High resolution timers wasn't introduced until 2.6.21.

My advice to the OP would be to upgrade the kernel to at least version 2.6.21, or if you can't then apply the realtime patches for your kernel version.
 
Old 08-09-2009, 07:25 PM   #15
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
My point is this:

1. If you think you *need* microsecond (or nanosecond) granularity, you possibly have a design problem (for example, relying on polling instead of blocking). I would suggest revisiting the original requirements, and considering other design alternatives.

2. If you think you're actually going to *get* nanosecond resolution, you're probably mistaken (even with decent hardware, up-to-date kernels, and realtime extensions)(and *certainly* with most other, non-Linux multitasking, preemptable operating systems; including most/all flavors of Unix and Windows).

IMHO .. PSM

Last edited by paulsm4; 08-10-2009 at 02:38 PM.
 
  


Reply

Tags
clock, high, realtime, time


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



All times are GMT -5. The time now is 08:59 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