LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-01-2007, 06:09 PM   #1
Lothar Schwab
LQ Newbie
 
Registered: Aug 2007
Location: Minnesota, USA
Distribution: Lubuntu
Posts: 19

Rep: Reputation: 2
How to get current time without kernel transition


I have implemented a high performance binary logging
library that runs on Linux.

I noticed that almost all of the time spend in the log
function is in this call:
clock_gettime (CLOCK_REALTIME, &ti);

After some closer inspection I found the reason why. This
function performs a transition into kernel mode. I tried
other time functions as well (gettimeofday() and time()).

They all seem to have the same behavior: they transition into
kernel mode, get the time in kernel mode and transition back
into user mode.

From windows I am used to a different behavior. The Win32
function GetSystemTimeAsFileTime for instance runs in much
less than a microsecond. This is most likely because it picks
up the time from some shared memory location that the kernel
updates once in a while.

So - my question is this: is there a function available for
Linux applications that provides the current time with a
moderate resolution directly in user space?

If you know something about the topic please let me know.
 
Old 08-01-2007, 06:48 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Can you tolerate a relatively high overhead on the very first call, or perhaps a "before-the-first" call that sets everything up?

If so, then set things up slowly by finding out how many ticks (as defined in the following program) constitute one second (maybe several trials). Then get the time of day, followed by the value of the clock (as defined by the following program).

Each time through, get that clock and do the arithmetic.

Hope this helps.
Code:
#include <stdio.h>
#include <unistd.h>

inline unsigned long long get_clock()
{
  unsigned long long ret;
  asm volatile("rdtsc" : "=A" (ret));
  return ret;

} /* get_clock() */

int main(void)
{
  int                counter;

  unsigned long long alpha;
  unsigned long long beta;

  for(counter=0;
      counter<5;
      counter++
     )
  {
    alpha=get_clock();
    sleep(1);
    beta=get_clock();

    printf("%lld\n",beta-alpha);
  }

  return 0;

} /* main() */
 
Old 08-01-2007, 07:31 PM   #3
Lothar Schwab
LQ Newbie
 
Registered: Aug 2007
Location: Minnesota, USA
Distribution: Lubuntu
Posts: 19

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by wjevans_7d1@yahoo.co
Can you tolerate a relatively high overhead on the very first call, or perhaps a "before-the-first" call that sets everything up?]
Thanks for the answer.

Yes - I can publish the parameters of the converstion function that
calculates the actual time based on rdtsc in shared memory so that this
information must be obtained only once.

I will go along these lines for my code to run on x86 Linux - for now.

Such a calculation scheme has some disadvantages though (I repeat information
I found on wikipedia):
- x86 specific
- Not all INTEL compatible CPUs might properly support it
- Potential problems on multi-CPU systems when CPU affinity is not
guaranteed (timer value may not be guaranteed to be in sync on all
virtual or real CPUs)
- Potential problems with variable clock rate and hibernation facilities

Wikipedia cites a Microsoft site that states that these issues are the reason
why the QueryPerformanceCounter API exists in Win32. Theoretically such
an API could (should ?) be provided on Linux. It would cancel out the effects
caused by the problems mentioned above and provide a reliable high resolution
timer.

But QueryPerformanceCounter as well as a corresponding hypothetical Linux
counterpart would still not provide what I am looking for: a moderate
resolution time that is accessible in user space.

With the mindset of wishing Linux to be superior or at least equal in all
even-so-small details I wonder whether it would make sense to suggest two
enhancements to the Linux kernel:
1. Provide a function that picks up the current time in user space with a
resolution of somewhere in the 50 millisecond range. No kernel transistion
should be necessary in order to achieve a performance somewhere in the
low hundreds of nanoseconds.

2. Provide an API call that provides a "dirt-effect-corrected" high resolution
counter. A kernel transition is probably unavoidable so that this
API cannot be used excessively.

Has anybody suggested such things? How could I "file" such a suggestion?
 
Old 08-02-2007, 12:46 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Quote:
Has anybody suggested such things? How could I "file" such a suggestion?
You'd have to do more than just "file" this suggestion. You'd probably have to become part of the appropriate community. Go here:

http://en.wikipedia.org/wiki/Linux_Kernel_Mailing_List
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display the current time at a different timezone stinkytofoo Linux - Software 3 07-18-2007 03:41 AM
How do I disable daylight savings time transition? linuxrocks123 Linux - Software 2 03-11-2007 11:47 PM
dell d610 kernel 2.4.27 -> 2.6.15 transition boot problem andywest Linux - Laptop and Netbook 6 04-08-2006 09:59 AM
LXer: Debian kernel-package hooks transition LXer Syndicated Linux News 0 12-24-2005 02:46 PM
Info about current ABI transition in etch philippeP Debian 2 12-16-2005 11:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:42 AM.

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