kernel: new DEBUG_RACE option. try to provoke race conditions between processes.
[minix.git] / kernel / clock.c
blobe55d6624bd0b7252299294bec53b2f9a1da26457
1 /* This file contains the clock task, which handles time related functions.
2 * Important events that are handled by the CLOCK include setting and
3 * monitoring alarm timers and deciding when to (re)schedule processes.
4 * The CLOCK offers a direct interface to kernel processes. System services
5 * can access its services through system calls, such as sys_setalarm(). The
6 * CLOCK task thus is hidden from the outside world.
8 * Changes:
9 * Aug 18, 2006 removed direct hardware access etc, MinixPPC (Ingmar Alting)
10 * Oct 08, 2005 reordering and comment editing (A. S. Woodhull)
11 * Mar 18, 2004 clock interface moved to SYSTEM task (Jorrit N. Herder)
12 * Sep 30, 2004 source code documentation updated (Jorrit N. Herder)
13 * Sep 24, 2004 redesigned alarm timers (Jorrit N. Herder)
15 * Clock task is notified by the clock's interrupt handler when a timer
16 * has expired.
18 * In addition to the main clock_task() entry point, which starts the main
19 * loop, there are several other minor entry points:
20 * clock_stop: called just before MINIX shutdown
21 * get_uptime: get realtime since boot in clock ticks
22 * set_timer: set a watchdog timer (+)
23 * reset_timer: reset a watchdog timer (+)
24 * read_clock: read the counter of channel 0 of the 8253A timer
26 * (+) The CLOCK task keeps tracks of watchdog timers for the entire kernel.
27 * It is crucial that watchdog functions not block, or the CLOCK task may
28 * be blocked. Do not send() a message when the receiver is not expecting it.
29 * Instead, notify(), which always returns, should be used.
32 #include "kernel.h"
33 #include "proc.h"
34 #include <minix/endpoint.h>
35 #include <assert.h>
37 #include "clock.h"
38 #include "debug.h"
40 #ifdef CONFIG_WATCHDOG
41 #include "watchdog.h"
42 #endif
44 /* Function prototype for PRIVATE functions.
45 */
46 FORWARD _PROTOTYPE( void load_update, (void));
48 /* The CLOCK's timers queue. The functions in <timers.h> operate on this.
49 * Each system process possesses a single synchronous alarm timer. If other
50 * kernel parts want to use additional timers, they must declare their own
51 * persistent (static) timer structure, which can be passed to the clock
52 * via (re)set_timer().
53 * When a timer expires its watchdog function is run by the CLOCK task.
55 PRIVATE timer_t *clock_timers; /* queue of CLOCK timers */
56 PRIVATE clock_t next_timeout; /* realtime that next timer expires */
58 /* The time is incremented by the interrupt handler on each clock tick.
60 PRIVATE clock_t realtime = 0; /* real time clock */
63 * The boot processor timer interrupt handler. In addition to non-boot cpus it
64 * keeps real time and notifies the clock task if need be
66 PUBLIC int bsp_timer_int_handler(void)
68 unsigned ticks;
70 if(minix_panicing)
71 return 0;
73 /* Get number of ticks and update realtime. */
74 ticks = lost_ticks + 1;
75 lost_ticks = 0;
76 realtime += ticks;
78 ap_timer_int_handler();
79 assert(!proc_is_runnable(proc_ptr) || proc_ptr->p_ticks_left > 0);
81 /* if a timer expired, notify the clock task */
82 if ((next_timeout <= realtime)) {
83 tmrs_exptimers(&clock_timers, realtime, NULL);
84 next_timeout = (clock_timers == NULL) ?
85 TMR_NEVER : clock_timers->tmr_exp_time;
88 if (do_serial_debug)
89 do_ser_debug();
91 return(1); /* reenable interrupts */
94 /*===========================================================================*
95 * get_uptime *
96 *===========================================================================*/
97 PUBLIC clock_t get_uptime(void)
99 /* Get and return the current clock uptime in ticks. */
100 return(realtime);
103 /*===========================================================================*
104 * set_timer *
105 *===========================================================================*/
106 PUBLIC void set_timer(tp, exp_time, watchdog)
107 struct timer *tp; /* pointer to timer structure */
108 clock_t exp_time; /* expiration realtime */
109 tmr_func_t watchdog; /* watchdog to be called */
111 /* Insert the new timer in the active timers list. Always update the
112 * next timeout time by setting it to the front of the active list.
114 tmrs_settimer(&clock_timers, tp, exp_time, watchdog, NULL);
115 next_timeout = clock_timers->tmr_exp_time;
118 /*===========================================================================*
119 * reset_timer *
120 *===========================================================================*/
121 PUBLIC void reset_timer(tp)
122 struct timer *tp; /* pointer to timer structure */
124 /* The timer pointed to by 'tp' is no longer needed. Remove it from both the
125 * active and expired lists. Always update the next timeout time by setting
126 * it to the front of the active list.
128 tmrs_clrtimer(&clock_timers, tp, NULL);
129 next_timeout = (clock_timers == NULL) ?
130 TMR_NEVER : clock_timers->tmr_exp_time;
133 /*===========================================================================*
134 * load_update *
135 *===========================================================================*/
136 PRIVATE void load_update(void)
138 u16_t slot;
139 int enqueued = 0, q;
140 struct proc *p;
142 /* Load average data is stored as a list of numbers in a circular
143 * buffer. Each slot accumulates _LOAD_UNIT_SECS of samples of
144 * the number of runnable processes. Computations can then
145 * be made of the load average over variable periods, in the
146 * user library (see getloadavg(3)).
148 slot = (realtime / system_hz / _LOAD_UNIT_SECS) % _LOAD_HISTORY;
149 if(slot != kloadinfo.proc_last_slot) {
150 kloadinfo.proc_load_history[slot] = 0;
151 kloadinfo.proc_last_slot = slot;
154 /* Cumulation. How many processes are ready now? */
155 for(q = 0; q < NR_SCHED_QUEUES; q++)
156 for(p = rdy_head[q]; p; p = p->p_nextready)
157 enqueued++;
159 kloadinfo.proc_load_history[slot] += enqueued;
161 /* Up-to-dateness. */
162 kloadinfo.last_clock = realtime;
166 * Timer interupt handler. This is the only thing executed on non boot
167 * processors. It is called by bsp_timer_int_handler() on the boot processor
169 PUBLIC int ap_timer_int_handler(void)
172 /* Update user and system accounting times. Charge the current process
173 * for user time. If the current process is not billable, that is, if a
174 * non-user process is running, charge the billable process for system
175 * time as well. Thus the unbillable process' user time is the billable
176 * user's system time.
179 const unsigned ticks = 1;
180 struct proc * p, * billp;
182 #ifdef CONFIG_WATCHDOG
184 * we need to know whether local timer ticks are happening or whether
185 * the kernel is locked up. We don't care about overflows as we only
186 * need to know that it's still ticking or not
188 watchdog_local_timer_ticks++;
189 #endif
191 /* Update user and system accounting times. Charge the current process
192 * for user time. If the current process is not billable, that is, if a
193 * non-user process is running, charge the billable process for system
194 * time as well. Thus the unbillable process' user time is the billable
195 * user's system time.
198 /* FIXME prepared for get_cpu_local_var() */
199 p = proc_ptr;
200 billp = bill_ptr;
202 p->p_user_time += ticks;
204 #if DEBUG_RACE
205 /* With DEBUG_RACE, every process gets interrupted. */
206 p->p_ticks_left = 0;
207 #else
208 if (priv(p)->s_flags & PREEMPTIBLE) {
209 p->p_ticks_left -= ticks;
211 #endif
212 if (! (priv(p)->s_flags & BILLABLE)) {
213 billp->p_sys_time += ticks;
216 /* Decrement virtual timers, if applicable. We decrement both the
217 * virtual and the profile timer of the current process, and if the
218 * current process is not billable, the timer of the billed process as
219 * well. If any of the timers expire, do_clocktick() will send out
220 * signals.
222 if ((p->p_misc_flags & MF_VIRT_TIMER)){
223 p->p_virt_left -= ticks;
225 if ((p->p_misc_flags & MF_PROF_TIMER)){
226 p->p_prof_left -= ticks;
228 if (! (priv(p)->s_flags & BILLABLE) &&
229 (billp->p_misc_flags & MF_PROF_TIMER)){
230 billp->p_prof_left -= ticks;
234 * Check if a process-virtual timer expired. Check current process, but
235 * also bill_ptr - one process's user time is another's system time, and
236 * the profile timer decreases for both!
238 vtimer_check(p);
240 if (p != billp)
241 vtimer_check(billp);
243 /* Update load average. */
244 load_update();
246 /* check if the processes still have some ticks left */
247 check_ticks_left(p);
249 return 1;
252 PUBLIC int boot_cpu_init_timer(unsigned freq)
254 if (arch_init_local_timer(freq))
255 return -1;
257 if (arch_register_local_timer_handler(
258 (irq_handler_t) bsp_timer_int_handler))
259 return -1;
261 return 0;