coverity appeasement - redundant check
[minix.git] / kernel / clock.c
blob7d478eec7203dc662c48d939ed82bbe3a604e60b
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 <minix/endpoint.h>
34 #include <assert.h>
36 #include "clock.h"
38 #ifdef USE_WATCHDOG
39 #include "watchdog.h"
40 #endif
42 /* Function prototype for PRIVATE functions.
43 */
44 static void load_update(void);
46 /* The CLOCK's timers queue. The functions in <timers.h> operate on this.
47 * Each system process possesses a single synchronous alarm timer. If other
48 * kernel parts want to use additional timers, they must declare their own
49 * persistent (static) timer structure, which can be passed to the clock
50 * via (re)set_timer().
51 * When a timer expires its watchdog function is run by the CLOCK task.
53 static timer_t *clock_timers; /* queue of CLOCK timers */
54 static clock_t next_timeout; /* realtime that next timer expires */
56 /* The time is incremented by the interrupt handler on each clock tick.
58 static clock_t realtime = 0; /* real time clock */
61 * The boot processos timer interrupt handler. In addition to non-boot cpus it
62 * keeps real time and notifies the clock task if need be
64 int timer_int_handler(void)
66 /* Update user and system accounting times. Charge the current process
67 * for user time. If the current process is not billable, that is, if a
68 * non-user process is running, charge the billable process for system
69 * time as well. Thus the unbillable process' user time is the billable
70 * user's system time.
73 struct proc * p, * billp;
75 /* FIXME watchdog for slave cpus! */
76 #ifdef USE_WATCHDOG
78 * we need to know whether local timer ticks are happening or whether
79 * the kernel is locked up. We don't care about overflows as we only
80 * need to know that it's still ticking or not
82 watchdog_local_timer_ticks++;
83 #endif
85 if (cpu_is_bsp(cpuid))
86 realtime++;
88 /* Update user and system accounting times. Charge the current process
89 * for user time. If the current process is not billable, that is, if a
90 * non-user process is running, charge the billable process for system
91 * time as well. Thus the unbillable process' user time is the billable
92 * user's system time.
95 p = get_cpulocal_var(proc_ptr);
96 billp = get_cpulocal_var(bill_ptr);
98 p->p_user_time++;
100 if (! (priv(p)->s_flags & BILLABLE)) {
101 billp->p_sys_time++;
104 /* Decrement virtual timers, if applicable. We decrement both the
105 * virtual and the profile timer of the current process, and if the
106 * current process is not billable, the timer of the billed process as
107 * well. If any of the timers expire, do_clocktick() will send out
108 * signals.
110 if ((p->p_misc_flags & MF_VIRT_TIMER)){
111 p->p_virt_left--;
113 if ((p->p_misc_flags & MF_PROF_TIMER)){
114 p->p_prof_left--;
116 if (! (priv(p)->s_flags & BILLABLE) &&
117 (billp->p_misc_flags & MF_PROF_TIMER)){
118 billp->p_prof_left--;
122 * Check if a process-virtual timer expired. Check current process, but
123 * also bill_ptr - one process's user time is another's system time, and
124 * the profile timer decreases for both!
126 vtimer_check(p);
128 if (p != billp)
129 vtimer_check(billp);
131 /* Update load average. */
132 load_update();
134 if (cpu_is_bsp(cpuid)) {
135 /* if a timer expired, notify the clock task */
136 if ((next_timeout <= realtime)) {
137 tmrs_exptimers(&clock_timers, realtime, NULL);
138 next_timeout = (clock_timers == NULL) ?
139 TMR_NEVER : clock_timers->tmr_exp_time;
142 #ifdef DEBUG_SERIAL
143 if (kinfo.do_serial_debug)
144 do_ser_debug();
145 #endif
149 return(1); /* reenable interrupts */
152 /*===========================================================================*
153 * get_uptime *
154 *===========================================================================*/
155 clock_t get_uptime(void)
157 /* Get and return the current clock uptime in ticks. */
158 return(realtime);
161 /*===========================================================================*
162 * set_timer *
163 *===========================================================================*/
164 void set_timer(tp, exp_time, watchdog)
165 struct timer *tp; /* pointer to timer structure */
166 clock_t exp_time; /* expiration realtime */
167 tmr_func_t watchdog; /* watchdog to be called */
169 /* Insert the new timer in the active timers list. Always update the
170 * next timeout time by setting it to the front of the active list.
172 tmrs_settimer(&clock_timers, tp, exp_time, watchdog, NULL);
173 next_timeout = clock_timers->tmr_exp_time;
176 /*===========================================================================*
177 * reset_timer *
178 *===========================================================================*/
179 void reset_timer(tp)
180 struct timer *tp; /* pointer to timer structure */
182 /* The timer pointed to by 'tp' is no longer needed. Remove it from both the
183 * active and expired lists. Always update the next timeout time by setting
184 * it to the front of the active list.
186 tmrs_clrtimer(&clock_timers, tp, NULL);
187 next_timeout = (clock_timers == NULL) ?
188 TMR_NEVER : clock_timers->tmr_exp_time;
191 /*===========================================================================*
192 * load_update *
193 *===========================================================================*/
194 static void load_update(void)
196 u16_t slot;
197 int enqueued = 0, q;
198 struct proc *p;
199 struct proc **rdy_head;
201 /* Load average data is stored as a list of numbers in a circular
202 * buffer. Each slot accumulates _LOAD_UNIT_SECS of samples of
203 * the number of runnable processes. Computations can then
204 * be made of the load average over variable periods, in the
205 * user library (see getloadavg(3)).
207 slot = (realtime / system_hz / _LOAD_UNIT_SECS) % _LOAD_HISTORY;
208 if(slot != kloadinfo.proc_last_slot) {
209 kloadinfo.proc_load_history[slot] = 0;
210 kloadinfo.proc_last_slot = slot;
213 rdy_head = get_cpulocal_var(run_q_head);
214 /* Cumulation. How many processes are ready now? */
215 for(q = 0; q < NR_SCHED_QUEUES; q++) {
216 for(p = rdy_head[q]; p != NULL; p = p->p_nextready) {
217 enqueued++;
221 kloadinfo.proc_load_history[slot] += enqueued;
223 /* Up-to-dateness. */
224 kloadinfo.last_clock = realtime;
227 int boot_cpu_init_timer(unsigned freq)
229 if (init_local_timer(freq))
230 return -1;
232 if (register_local_timer_handler(
233 (irq_handler_t) timer_int_handler))
234 return -1;
236 return 0;
239 int app_cpu_init_timer(unsigned freq)
241 if (init_local_timer(freq))
242 return -1;
244 return 0;