Commit | Line | Data |
---|---|---|
4f083fd7 | 1 | /* kern_clock.c 4.45 82/11/13 */ |
83be5fac BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
d9b8447e | 5 | #include "../h/dk.h" |
0a34b6fd | 6 | #include "../h/callout.h" |
83be5fac BJ |
7 | #include "../h/dir.h" |
8 | #include "../h/user.h" | |
f0da6d20 | 9 | #include "../h/kernel.h" |
83be5fac | 10 | #include "../h/proc.h" |
83be5fac BJ |
11 | #include "../h/psl.h" |
12 | #include "../h/vm.h" | |
83be5fac | 13 | #include "../h/text.h" |
c53dce5d RE |
14 | #ifdef MUSH |
15 | #include "../h/quota.h" | |
16 | #include "../h/share.h" | |
17 | #endif | |
83be5fac | 18 | |
76b2a182 BJ |
19 | /* |
20 | * Clock handling routines. | |
21 | * | |
22 | * This code is written for a machine with only one interval timer, | |
23 | * and does timing and resource utilization estimation statistically | |
24 | * based on the state of the machine hz times a second. A machine | |
25 | * with proper clocks (running separately in user state, system state, | |
26 | * interrupt state and idle state) as well as a time-of-day clock | |
27 | * would allow a non-approximate implementation. | |
28 | */ | |
6602c75b | 29 | |
76b2a182 BJ |
30 | /* |
31 | * TODO: | |
32 | * * Keep more accurate statistics by simulating good interval timers. | |
33 | * * Use the time-of-day clock on the VAX to keep more accurate time | |
34 | * than is possible by repeated use of the interval timer. | |
35 | * * Allocate more timeout table slots when table overflows. | |
36 | */ | |
83be5fac | 37 | |
76b2a182 BJ |
38 | /* bump a timeval by a small number of usec's */ |
39 | #define bumptime(tp, usec) \ | |
40 | (tp)->tv_usec += usec; \ | |
27b91f59 BJ |
41 | if ((tp)->tv_usec >= 1000000) { \ |
42 | (tp)->tv_usec -= 1000000; \ | |
43 | (tp)->tv_sec++; \ | |
44 | } | |
72857acf | 45 | |
76b2a182 BJ |
46 | /* |
47 | * The (single) hardware interval timer. | |
48 | * We update the events relating to real time, and then | |
49 | * make a gross assumption: that the system has been in the | |
50 | * state it is in (user state, kernel state, interrupt state, | |
51 | * or idle state) for the entire last time interval, and | |
52 | * update statistics accordingly. | |
53 | */ | |
260ea681 | 54 | /*ARGSUSED*/ |
b4e32d36 | 55 | #ifdef vax |
f403d99f | 56 | hardclock(pc, ps) |
4512b9a4 | 57 | caddr_t pc; |
460ab27f | 58 | int ps; |
83be5fac | 59 | { |
460ab27f | 60 | #endif |
b4e32d36 | 61 | #ifdef sun |
460ab27f BJ |
62 | hardclock(regs) |
63 | struct regs regs; | |
64 | { | |
65 | int ps = regs.r_sr; | |
66 | caddr_t pc = (caddr_t)regs.r_pc; | |
67 | #endif | |
0a34b6fd | 68 | register struct callout *p1; |
27b91f59 | 69 | register struct proc *p; |
f403d99f | 70 | register int s, cpstate; |
83be5fac | 71 | |
76b2a182 BJ |
72 | /* |
73 | * Update real-time timeout queue. | |
74 | * At front of queue are some number of events which are ``due''. | |
75 | * The time to these is <= 0 and if negative represents the | |
76 | * number of ticks which have passed since it was supposed to happen. | |
77 | * The rest of the q elements (times > 0) are events yet to happen, | |
78 | * where the time for each is given as a delta from the previous. | |
79 | * Decrementing just the first of these serves to decrement the time | |
80 | * to all events. | |
81 | */ | |
c4710996 | 82 | for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next) |
d01b68d6 | 83 | --p1->c_time; |
c4710996 | 84 | if (p1) |
d01b68d6 | 85 | --p1->c_time; |
5da67d35 | 86 | |
76b2a182 BJ |
87 | /* |
88 | * If the cpu is currently scheduled to a process, then | |
89 | * charge it with resource utilization for a tick, updating | |
90 | * statistics which run in (user+system) virtual time, | |
91 | * such as the cpu time limit and profiling timers. | |
92 | * This assumes that the current process has been running | |
93 | * the entire last tick. | |
94 | */ | |
83be5fac BJ |
95 | if (!noproc) { |
96 | s = u.u_procp->p_rssize; | |
27b91f59 | 97 | u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */ |
83be5fac BJ |
98 | if (u.u_procp->p_textp) { |
99 | register int xrss = u.u_procp->p_textp->x_rssize; | |
100 | ||
101 | s += xrss; | |
f0da6d20 | 102 | u.u_ru.ru_ixrss += xrss; |
83be5fac | 103 | } |
f0da6d20 BJ |
104 | if (s > u.u_ru.ru_maxrss) |
105 | u.u_ru.ru_maxrss = s; | |
106 | if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > | |
107 | u.u_rlimit[RLIMIT_CPU].rlim_cur) { | |
39f2f769 | 108 | psignal(u.u_procp, SIGXCPU); |
f0da6d20 BJ |
109 | if (u.u_rlimit[RLIMIT_CPU].rlim_cur < |
110 | u.u_rlimit[RLIMIT_CPU].rlim_max) | |
111 | u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; | |
39f2f769 | 112 | } |
27b91f59 BJ |
113 | if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && |
114 | itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) | |
115 | psignal(u.u_procp, SIGPROF); | |
83be5fac | 116 | } |
27b91f59 | 117 | |
76b2a182 BJ |
118 | /* |
119 | * Charge the time out based on the mode the cpu is in. | |
120 | * Here again we fudge for the lack of proper interval timers | |
121 | * assuming that the current state has been around at least | |
122 | * one tick. | |
123 | */ | |
83be5fac | 124 | if (USERMODE(ps)) { |
76b2a182 BJ |
125 | /* |
126 | * CPU was in user state. Increment | |
127 | * user time counter, and process process-virtual time | |
128 | * interval timer. | |
129 | */ | |
130 | bumptime(&u.u_ru.ru_utime, tick); | |
27b91f59 BJ |
131 | if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && |
132 | itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) | |
133 | psignal(u.u_procp, SIGVTALRM); | |
f0da6d20 | 134 | if (u.u_procp->p_nice > NZERO) |
41888f16 BJ |
135 | cpstate = CP_NICE; |
136 | else | |
137 | cpstate = CP_USER; | |
83be5fac | 138 | } else { |
76b2a182 BJ |
139 | /* |
140 | * CPU was in system state. If profiling kernel | |
141 | * increment a counter. If no process is running | |
142 | * then this is a system tick if we were running | |
143 | * at a non-zero IPL (in a driver). If a process is running, | |
144 | * then we charge it with system time even if we were | |
145 | * at a non-zero IPL, since the system often runs | |
146 | * this way during processing of system calls. | |
147 | * This is approximate, but the lack of true interval | |
148 | * timers makes doing anything else difficult. | |
149 | */ | |
3484be37 BJ |
150 | #ifdef GPROF |
151 | int k = pc - s_lowpc; | |
152 | if (profiling < 2 && k < s_textsize) | |
153 | kcount[k / sizeof (*kcount)]++; | |
2752c877 | 154 | #endif |
41888f16 | 155 | cpstate = CP_SYS; |
ddb3ced5 | 156 | if (noproc) { |
460ab27f | 157 | if (BASEPRI(ps)) |
ddb3ced5 | 158 | cpstate = CP_IDLE; |
f0da6d20 | 159 | } else { |
76b2a182 | 160 | bumptime(&u.u_ru.ru_stime, tick); |
f0da6d20 | 161 | } |
83be5fac | 162 | } |
27b91f59 | 163 | |
76b2a182 BJ |
164 | /* |
165 | * We maintain statistics shown by user-level statistics | |
166 | * programs: the amount of time in each cpu state, and | |
167 | * the amount of time each of DK_NDRIVE ``drives'' is busy. | |
168 | */ | |
2d7d59e9 | 169 | cp_time[cpstate]++; |
f403d99f BJ |
170 | for (s = 0; s < DK_NDRIVE; s++) |
171 | if (dk_busy&(1<<s)) | |
172 | dk_time[s]++; | |
27b91f59 | 173 | |
76b2a182 BJ |
174 | /* |
175 | * We adjust the priority of the current process. | |
176 | * The priority of a process gets worse as it accumulates | |
177 | * CPU time. The cpu usage estimator (p_cpu) is increased here | |
178 | * and the formula for computing priorities (in kern_synch.c) | |
179 | * will compute a different value each time the p_cpu increases | |
180 | * by 4. The cpu usage estimator ramps up quite quickly when | |
181 | * the process is running (linearly), and decays away exponentially, | |
182 | * at a rate which is proportionally slower when the system is | |
183 | * busy. The basic principal is that the system will 90% forget | |
184 | * that a process used a lot of CPU time in 5*loadav seconds. | |
185 | * This causes the system to favor processes which haven't run | |
186 | * much recently, and to round-robin among other processes. | |
187 | */ | |
83be5fac | 188 | if (!noproc) { |
27b91f59 BJ |
189 | p = u.u_procp; |
190 | p->p_cpticks++; | |
191 | if (++p->p_cpu == 0) | |
192 | p->p_cpu--; | |
c53dce5d | 193 | #ifdef MUSH |
27b91f59 BJ |
194 | p->p_quota->q_cost += (p->p_nice > NZERO ? |
195 | (shconsts.sc_tic * ((2*NZERO)-p->p_nice)) / NZERO : | |
c53dce5d RE |
196 | shconsts.sc_tic) * (((int)avenrun[0]+2)/3); |
197 | #endif | |
76b2a182 | 198 | if ((p->p_cpu&3) == 0) { |
27b91f59 BJ |
199 | (void) setpri(p); |
200 | if (p->p_pri >= PUSER) | |
201 | p->p_pri = p->p_usrpri; | |
83be5fac BJ |
202 | } |
203 | } | |
76b2a182 BJ |
204 | |
205 | /* | |
206 | * Increment the time-of-day, and schedule | |
207 | * processing of the callouts at a very low cpu priority, | |
208 | * so we don't keep the relatively high clock interrupt | |
209 | * priority any longer than necessary. | |
210 | */ | |
211 | bumptime(&time, tick); | |
f403d99f BJ |
212 | setsoftclock(); |
213 | } | |
214 | ||
76b2a182 BJ |
215 | /* |
216 | * Software priority level clock interrupt. | |
217 | * Run periodic events from timeout queue. | |
218 | */ | |
260ea681 | 219 | /*ARGSUSED*/ |
b4e32d36 | 220 | #ifdef vax |
f403d99f | 221 | softclock(pc, ps) |
4512b9a4 | 222 | caddr_t pc; |
460ab27f | 223 | int ps; |
f403d99f | 224 | { |
460ab27f | 225 | #endif |
b4e32d36 | 226 | #ifdef sun |
460ab27f BJ |
227 | softclock(sirret, regs) |
228 | caddr_t sirreg; | |
229 | struct regs regs; | |
230 | { | |
231 | int ps = regs.r_sr; | |
232 | caddr_t pc = (caddr_t)regs.r_pc; | |
233 | #endif | |
f403d99f | 234 | |
27b91f59 | 235 | for (;;) { |
76b2a182 BJ |
236 | register struct callout *p1; |
237 | register caddr_t arg; | |
238 | register int (*func)(); | |
239 | register int a, s; | |
240 | ||
27b91f59 BJ |
241 | s = spl7(); |
242 | if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { | |
243 | splx(s); | |
244 | break; | |
f403d99f | 245 | } |
76b2a182 | 246 | arg = p1->c_arg; func = p1->c_func; a = p1->c_time; |
27b91f59 | 247 | calltodo.c_next = p1->c_next; |
27b91f59 BJ |
248 | p1->c_next = callfree; |
249 | callfree = p1; | |
4f083fd7 | 250 | splx(s); |
d01b68d6 | 251 | (*func)(arg, a); |
f403d99f | 252 | } |
83be5fac BJ |
253 | } |
254 | ||
255 | /* | |
27b91f59 | 256 | * Arrange that (*fun)(arg) is called in tim/hz seconds. |
83be5fac BJ |
257 | */ |
258 | timeout(fun, arg, tim) | |
4512b9a4 BJ |
259 | int (*fun)(); |
260 | caddr_t arg; | |
27b91f59 | 261 | int tim; |
83be5fac | 262 | { |
c4710996 | 263 | register struct callout *p1, *p2, *pnew; |
83be5fac BJ |
264 | register int t; |
265 | int s; | |
266 | ||
267 | t = tim; | |
83be5fac | 268 | s = spl7(); |
c4710996 BJ |
269 | pnew = callfree; |
270 | if (pnew == NULL) | |
271 | panic("timeout table overflow"); | |
272 | callfree = pnew->c_next; | |
273 | pnew->c_arg = arg; | |
274 | pnew->c_func = fun; | |
275 | for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) | |
276 | t -= p2->c_time; | |
277 | p1->c_next = pnew; | |
278 | pnew->c_next = p2; | |
279 | pnew->c_time = t; | |
280 | if (p2) | |
281 | p2->c_time -= t; | |
83be5fac BJ |
282 | splx(s); |
283 | } | |
1fa9ff62 SL |
284 | |
285 | /* | |
286 | * untimeout is called to remove a function timeout call | |
287 | * from the callout structure. | |
288 | */ | |
27b91f59 | 289 | untimeout(fun, arg) |
1fa9ff62 SL |
290 | int (*fun)(); |
291 | caddr_t arg; | |
292 | { | |
1fa9ff62 SL |
293 | register struct callout *p1, *p2; |
294 | register int s; | |
295 | ||
296 | s = spl7(); | |
297 | for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { | |
298 | if (p2->c_func == fun && p2->c_arg == arg) { | |
d01b68d6 | 299 | if (p2->c_next && p2->c_time > 0) |
1fa9ff62 SL |
300 | p2->c_next->c_time += p2->c_time; |
301 | p1->c_next = p2->c_next; | |
302 | p2->c_next = callfree; | |
303 | callfree = p2; | |
304 | break; | |
305 | } | |
306 | } | |
307 | splx(s); | |
308 | } | |
d01b68d6 | 309 | |
76b2a182 BJ |
310 | /* |
311 | * Compute number of hz until specified time. | |
312 | * Used to compute third argument to timeout() from an | |
313 | * absolute time. | |
314 | */ | |
d01b68d6 BJ |
315 | hzto(tv) |
316 | struct timeval *tv; | |
317 | { | |
76b2a182 BJ |
318 | register long ticks; |
319 | register long sec; | |
d01b68d6 BJ |
320 | int s = spl7(); |
321 | ||
76b2a182 BJ |
322 | /* |
323 | * If number of milliseconds will fit in 32 bit arithmetic, | |
324 | * then compute number of milliseconds to time and scale to | |
325 | * ticks. Otherwise just compute number of hz in time, rounding | |
326 | * times greater than representible to maximum value. | |
327 | * | |
328 | * Delta times less than 25 days can be computed ``exactly''. | |
329 | * Maximum value for any timeout in 10ms ticks is 250 days. | |
330 | */ | |
331 | sec = tv->tv_sec - time.tv_sec; | |
332 | if (sec <= 0x7fffffff / 1000 - 1000) | |
333 | ticks = ((tv->tv_sec - time.tv_sec) * 1000 + | |
334 | (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000); | |
335 | else if (sec <= 0x7fffffff / hz) | |
336 | ticks = sec * hz; | |
337 | else | |
338 | ticks = 0x7fffffff; | |
d01b68d6 BJ |
339 | splx(s); |
340 | return (ticks); | |
341 | } |