VM: munmap used by VM for itself is no longer used
[minix.git] / kernel / clock.c
blob2ea4c17d0b7ca5b3d13427821a78280227b0c17a
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 arch_timer_int_handler();
151 return(1); /* reenable interrupts */
154 /*===========================================================================*
155 * get_uptime *
156 *===========================================================================*/
157 clock_t get_uptime(void)
159 /* Get and return the current clock uptime in ticks. */
160 return(realtime);
163 /*===========================================================================*
164 * set_timer *
165 *===========================================================================*/
166 void set_timer(tp, exp_time, watchdog)
167 struct timer *tp; /* pointer to timer structure */
168 clock_t exp_time; /* expiration realtime */
169 tmr_func_t watchdog; /* watchdog to be called */
171 /* Insert the new timer in the active timers list. Always update the
172 * next timeout time by setting it to the front of the active list.
174 tmrs_settimer(&clock_timers, tp, exp_time, watchdog, NULL);
175 next_timeout = clock_timers->tmr_exp_time;
178 /*===========================================================================*
179 * reset_timer *
180 *===========================================================================*/
181 void reset_timer(tp)
182 struct timer *tp; /* pointer to timer structure */
184 /* The timer pointed to by 'tp' is no longer needed. Remove it from both the
185 * active and expired lists. Always update the next timeout time by setting
186 * it to the front of the active list.
188 tmrs_clrtimer(&clock_timers, tp, NULL);
189 next_timeout = (clock_timers == NULL) ?
190 TMR_NEVER : clock_timers->tmr_exp_time;
193 /*===========================================================================*
194 * load_update *
195 *===========================================================================*/
196 static void load_update(void)
198 u16_t slot;
199 int enqueued = 0, q;
200 struct proc *p;
201 struct proc **rdy_head;
203 /* Load average data is stored as a list of numbers in a circular
204 * buffer. Each slot accumulates _LOAD_UNIT_SECS of samples of
205 * the number of runnable processes. Computations can then
206 * be made of the load average over variable periods, in the
207 * user library (see getloadavg(3)).
209 slot = (realtime / system_hz / _LOAD_UNIT_SECS) % _LOAD_HISTORY;
210 if(slot != kloadinfo.proc_last_slot) {
211 kloadinfo.proc_load_history[slot] = 0;
212 kloadinfo.proc_last_slot = slot;
215 rdy_head = get_cpulocal_var(run_q_head);
216 /* Cumulation. How many processes are ready now? */
217 for(q = 0; q < NR_SCHED_QUEUES; q++) {
218 for(p = rdy_head[q]; p != NULL; p = p->p_nextready) {
219 enqueued++;
223 kloadinfo.proc_load_history[slot] += enqueued;
225 /* Up-to-dateness. */
226 kloadinfo.last_clock = realtime;
229 int boot_cpu_init_timer(unsigned freq)
231 if (init_local_timer(freq))
232 return -1;
234 if (register_local_timer_handler(
235 (irq_handler_t) timer_int_handler))
236 return -1;
238 return 0;
241 int app_cpu_init_timer(unsigned freq)
243 if (init_local_timer(freq))
244 return -1;
246 return 0;