Saturday, November 24, 2012

The clock interrupt handler

The most important part of the clock driver is the clock interrupt handler. This is called every time there is a clock interrupt a.k.a clock-tick. All the basic timekeeping work is done here.

Everything done in the interrupt handler is a simple integer operation: arithmetic, comparison, logical AND/OR or assignment. There is no function call to complete the time-keeping work. So these operations are guaranteed to finish predictably and very fast.

One interesting point here is that since interrupts can be disabled by the system, clock-ticks can be lost. In some cases Minix can correct for this effect by maintaining lost clock-ticks in a global variable. Usually, when interrupts are disabled long enough that one or more clock-ticks may be lost the boot monitor is involved in the flow and it can keep track of time with the help of the BIOS. The boot monitor returns the number of ticks lost and this is used for the correction.

What does the clock driver do?

We talked about clock hardware in the last post. This post describes the different functions performed by the clock driver:

  1. The first function is to maintain the time of the day. The logic is very simple - keep a count of the clock-ticks and compute the time of the day. Since a large number of clock-ticks arrive every second the design should take into account overflow possibilities. A 32-bit counter can overflow pretty quickly for even a modest clock-rate. Multiple techniques can be used to overcome this problem: a) Use larger counters (say 64 bit counters). b) Maintain time in seconds rather than ticks using a resettable subsidiary counter that counts ticks until a whole second has been accumulated. c) Count the clock-ticks but do it relative to the time the system was booted and then compute time of day by adding the boot time to the clock-tick counter.
  2. The second function is to prevent processes from running too long. Whenever a process is started (or resumed), the scheduler initializes a counter with the value of that process' quantum in clock-ticks. At every clock-tick, the clock driver decrements this counter by 1 and when the counter reaches zero, the clock-driver calls the scheduler which switches out the currently running process and schedules another process.
  3. The third function is to do CPU accounting. The idea is to keep track of CPU utilization by each process. Various techniques exist for this, one of them being to 'charge' each clock-tick to the currently running process. But the number of clock-ticks charged to a process does not always indicate a proportional utilization because the running process can be interrupted any number of times between clock-ticks. Accurate CPU accounting is very expensive and so it is rarely done. 
  4. The fourth function is to be able to provide a timer functionality to running processes. Many times running processes may request the OS to give them a signal/warning/wake-up call after a specified time interval. This is accomplished by the clock driver by using multiple virtual clocks (since there is only one physical clock) which are basically counters in different data structures that are all updated each time the clock-driver runs. Based on which counter reaches zero, the clock driver causes a signal to be sent to the appropriate process. Parts of the OS also use timers to be called back at future times. These are called watch-dog timers and use the same mechanism.
  5. Lastly the clock-driver can be used to do profiling. It can record the position of the program counter for any process that is being profiled and over the lifetime of the process a graph of where it is spending time can be created.