Saturday, November 24, 2012

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. 

No comments:

Post a Comment