PDA

View Full Version : Where to find source code for functions introduced in time.h?



jtk
08-28-2007, 07:04 AM
I'm trying to figure out how nanosleep() function is implemented for Linux. It is declared in time.h header located at /usr/include/ -directory. But there is only the function declaration part like this:
extern int nanosleep (__const struct timespec *__requested_time,
struct timespec *__remaining);

Where to find the definition part?

matsp
08-28-2007, 07:27 AM
nanosleep is a system call, so the source of it is in the kernel sources.

look at hrtimer.c: sys_nanosleep()

--
Mats

jtk
08-28-2007, 07:42 AM
OK, thank you Mats! I'll try to check it out. Meanwhile, you can also help me telling which oscillator is used by nanosleep. Does it use processor cycle counter or software clock to count ticks?

matsp
08-28-2007, 07:49 AM
It uses the "high resolution timer", which is not based directly on the cycle counter in the processor, but some external timer - it is dependant on how you configured the system as well as the hardware in your system which timer it actually uses (PM timer, HPET are some of the choices).

Here's the "meat" of the function: http://lxr.linux.no/source/kernel/hrtimer.c#L678

--
Mats

matsp
08-28-2007, 09:18 AM
Just a note: The nanosleep system call allows you to specify a nanosecond interval to sleep. It, however, doesn't mean that even under ideal circumstances that the time from the call to the return from the call is anywhere near the number of nanoseconds you've specified if you give a small number... It is the guaranteed MINIMUM time your process will sleep, but there's no real upper bound.

--
Mats

brewbuck
08-28-2007, 11:43 AM
OK, thank you Mats! I'll try to check it out. Meanwhile, you can also help me telling which oscillator is used by nanosleep. Does it use processor cycle counter or software clock to count ticks?

Your question makes me suspect that you are looking for a real-time-capable delay function. In Linux there's no such thing, as Linux is not a real-time operating system.

matsp
08-28-2007, 12:47 PM
Your question makes me suspect that you are looking for a real-time-capable delay function. In Linux there's no such thing, as Linux is not a real-time operating system.

At least not without for example Ingo Molnar's realtime extensions, yes. Even with, I'm not entirely sure how fine-grain the timing is, for example. I don't know much at all about Ingo's real-time project...

Linux is fine for very mild real-time on a system where you understand what is running, and you don't have any REAL hard real-time limits (e.g. the hardware will NOT stop the system from doing it's job if you are taking the interrupt that comes in every 4 milliseconds after 160 ro 200 microseconds instead of within 100 microseconds).

--
Mats

jtk
08-29-2007, 01:20 AM
Thank you for help. So, I'm looking for a delay function that is not based on the system clock. In my program, I am using the network time protocol which manipulates the system clock frequency, but I need also a delay function which is not affected by NTP. That delay function should have precision of a microsecond.

matsp
08-29-2007, 03:28 AM
If you expect the sleep to return "within one microsecond of the specified time" then I think you should think again about using Linux as your OS (that is, if you actually REQUIRE this, rather than "it's ok as long as it does so most of the time").

--
Mats

jtk
08-29-2007, 04:41 AM
I think it is sufficient that the microsecond resolution is available "most of time", not all the time. So, is nanosleep capable to do that and not affected by system clock (or NTP)? If not, what other appropriate delay functions exist?

matsp
08-29-2007, 05:59 AM
I think it is sufficient that the microsecond resolution is available "most of time", not all the time. So, is nanosleep capable to do that and not affected by system clock (or NTP)? If not, what other appropriate delay functions exist?

I don't know if high-res timer will be affected by NTP - it may be.

--
Mats