1

I've read this link on Measure time in Linux - getrusage vs clock_gettime vs clock vs gettimeofday? which provides a great breakdown of timing functions available in C

I'm very confused, however, to how the different notions of "time" are maintained by the OS/hardware.

This is a quote from the Linux man pages,

RTCs should not be confused with the system clock, which is a software clock maintained by the kernel and used to implement gettimeofday(2) and time(2), as well as setting timestamps on files, and so on. The system clock reports seconds and microseconds since a start point, defined to be the POSIX Epoch: 1970-01-01 00:00:00 +0000 (UTC). (One common implementation counts timer interrupts, once per "jiffy", at a frequency of 100, 250, or 1000 Hz.) That is, it is supposed to report wall clock time, which RTCs also do.

A key difference between an RTC and the system clock is that RTCs run even when the system is in a low power state (including "off"), and the system clock can't. Until it is initialized, the system clock can only report time since system boot ... not since the POSIX Epoch. So at boot time, and after resuming from a system low power state, the system clock will often be set to the current wall clock time using an RTC. Systems without an RTC need to set the system clock using another clock, maybe across the network or by entering that data manually.

The Arch Linux docs indicate that the RTC and system clock are independent after bootup. My questions then are:

  1. What causes the interrupts that increments the system clock???
  2. If wall time = time interval using the system clock, what does the process time depend on??
  3. Is any of this all related to the CPU frequency? Or is that a totally orthogonal time-keeping business
Community
  • 1
  • 1
Joney
  • 309
  • 1
  • 2
  • 8
  • It varies per CPU platform. The architecture-specific part of the kernel tree each has specific handlers. Perhaps you could narrow the question? – wallyk Aug 15 '14 at 16:53
  • @wallyk I've reworded the whole thing lol – Joney Aug 15 '14 at 17:10

1 Answers1

0

On Linux, from the application point of view, the time(7) man page gives a good explanation.

Linux provides also the (linux specific) timerfd_create(2) and related syscalls.

You should not care about interrupts (they are the kernel's business, and are configured dynamically, e.g. thru application timers -timer_create(2), poll(2) and many other syscalls- and by the scheduler), but only about application visible time related syscalls.

Probably, if some process is making a timer with a tiny period of e.g. 10ms, the kernel will increase the frequency of timer interrupts to 100Hz

On recent kernels, you probably want the

CONFIG_HIGH_RES_TIMERS=y
CONFIG_TIMERFD=y
CONFIG_HPET_TIMER=y
CONFIG_PREEMPT=y

options in your kernel's .config file.

BTW, you could do cat /proc/interrupts twice with 10 seconds interval. On my laptop with a home-built 3.16 kernel -with mostly idle processes, but a firefox browser and an emacs, I'm getting 25 interrupts per second. Try also cat /proc/timer_list and cat /proc/timer_stats

Look also in the Documentation/timers/ directory of a recent (e.g. 3.16) Linux kernel tree.

The kernel probably use hardware devices like -for PC laptops and desktops- on-chip HPET (or the TSC) which are much better than the old battery saved RTC timer. Of course, details are hardware specific. So, on ARM based Linux systems (e.g. your Android smartphone) it is different.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Im asking this question whilst trying to learn more about operating systems .. so I really *do* care about the interrupts – Joney Aug 15 '14 at 17:30
  • No, the kernel will configure the interrupts according to requirements. – Basile Starynkevitch Aug 15 '14 at 17:31
  • I don't quite understand that last bit. I really just want to understand how the system clock is maintained primarily. It's interrupt-driven, so something must be causing the interrupts .. but what? I don't see how the kernel configures this? – Joney Aug 15 '14 at 17:38
  • 1
    The process scheduler does that for the kernel! – Am_I_Helpful Aug 15 '14 at 17:39
  • 1
    @shekharsuman the process schedular relies on a programmable interval timer, which in turn relies on the system clock no? – Joney Aug 15 '14 at 18:16
  • @BasileStarynkevitch I've read them over, and still don't get it. The software/system clock (or the timers/schedulers ect) are **software**. There has to be ... some .. HW that is ultimately responsible for "marking time" and incrementing these clocks. And apparently its not the RTC .. so what is it? – Joney Aug 15 '14 at 18:36
  • @BasileStarynkevitch thank you! the links to the HPET and TSC were very helpful .. just what I was looking for! – Joney Aug 15 '14 at 22:58