Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


can choose a very short repetition interval that would cause timers to expire over and over — resulting in
excessive time spent in the timer code. Put less politely, one could also call this a denial-of-service attack,
and the current approach avoids this.

15.7.3 Getting the Current Time


The current time of the system needs to be known for two reasons: First, many operations rely on time
stamps — for instance, the kernel needs to record when a file was last changed or when some log infor-
mation was produced. Second, the absolute time — that is, the real time of the outside world — of the
system is needed to inform the user with a clock, for example.

While absolute accuracy is not too important for the first purpose as long as the time flow is continuous
(i.e., the time stamps of successive operations should follow their order), it is more essential for the second
purpose. Hardware clocks are notorious for being either fast, slow, or a random combination of both.
There are various methods to solve this problem, with the most common one in the age of networked
computers being synchronization with a reliable time source (e.g., an atomic clock) via NTP. Since this is
purely a userland issue, I won’t discuss it any further.

Two means are provided to obtain timing information:


  1. The system calladjtimex. A small utility program of the same name can be used to quickly
    display the exported information. The system call allows for reading the current kernel inter-
    nal time. Other possibilities are documented in the associated manual page2(adjtimex).

  2. The device special file/dev/rtc. This source can be operated in various modes, but one of
    them delivers the current date and time to the caller.


I focus onadjtimexin the following. The entry point is as usualsys_adjtimex, but after some prepara-
tions, the real work is delegated todo_adjtimex. The function is rather lengthy, but the portion required
for our purposes is quite compact:

kernel/time.c
int do_adjtimex(struct timex *txc)
{
...
do_gettimeofday(&txc->time);
...
}

The call todo_gettimeofdayobtains the kernel’s internal time in the best possible resolution. The best
time source that was selected by the kernel as described in Section 15.4 is used for this purpose.

15.8 Managing Process Times


The task structure contains two elements related to process times that are important in our context:

<sched.h>
struct task_struct {
...
cputime_t utime, stime;
...
}
Free download pdf