kern_clock.c revision 1.22 1 1.22 cgd /* $NetBSD: kern_clock.c,v 1.22 1995/03/03 01:24:03 cgd Exp $ */
2 1.19 cgd
3 1.19 cgd /*-
4 1.19 cgd * Copyright (c) 1982, 1986, 1991, 1993
5 1.19 cgd * The Regents of the University of California. All rights reserved.
6 1.19 cgd * (c) UNIX System Laboratories, Inc.
7 1.19 cgd * All or some portions of this file are derived from material licensed
8 1.19 cgd * to the University of California by American Telephone and Telegraph
9 1.19 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.19 cgd * the permission of UNIX System Laboratories, Inc.
11 1.19 cgd *
12 1.19 cgd * Redistribution and use in source and binary forms, with or without
13 1.19 cgd * modification, are permitted provided that the following conditions
14 1.19 cgd * are met:
15 1.19 cgd * 1. Redistributions of source code must retain the above copyright
16 1.19 cgd * notice, this list of conditions and the following disclaimer.
17 1.19 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.19 cgd * notice, this list of conditions and the following disclaimer in the
19 1.19 cgd * documentation and/or other materials provided with the distribution.
20 1.19 cgd * 3. All advertising materials mentioning features or use of this software
21 1.19 cgd * must display the following acknowledgement:
22 1.19 cgd * This product includes software developed by the University of
23 1.19 cgd * California, Berkeley and its contributors.
24 1.19 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.19 cgd * may be used to endorse or promote products derived from this software
26 1.19 cgd * without specific prior written permission.
27 1.19 cgd *
28 1.19 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.19 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.19 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.19 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.19 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.19 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.19 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.19 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.19 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.19 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.19 cgd * SUCH DAMAGE.
39 1.19 cgd *
40 1.19 cgd * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
41 1.19 cgd */
42 1.19 cgd
43 1.19 cgd #include <sys/param.h>
44 1.19 cgd #include <sys/systm.h>
45 1.19 cgd #include <sys/dkstat.h>
46 1.19 cgd #include <sys/callout.h>
47 1.19 cgd #include <sys/kernel.h>
48 1.19 cgd #include <sys/proc.h>
49 1.19 cgd #include <sys/resourcevar.h>
50 1.19 cgd
51 1.19 cgd #include <machine/cpu.h>
52 1.19 cgd
53 1.19 cgd #ifdef GPROF
54 1.19 cgd #include <sys/gmon.h>
55 1.19 cgd #endif
56 1.19 cgd
57 1.19 cgd /*
58 1.19 cgd * Clock handling routines.
59 1.19 cgd *
60 1.19 cgd * This code is written to operate with two timers that run independently of
61 1.19 cgd * each other. The main clock, running hz times per second, is used to keep
62 1.19 cgd * track of real time. The second timer handles kernel and user profiling,
63 1.19 cgd * and does resource use estimation. If the second timer is programmable,
64 1.19 cgd * it is randomized to avoid aliasing between the two clocks. For example,
65 1.19 cgd * the randomization prevents an adversary from always giving up the cpu
66 1.19 cgd * just before its quantum expires. Otherwise, it would never accumulate
67 1.19 cgd * cpu ticks. The mean frequency of the second timer is stathz.
68 1.19 cgd *
69 1.19 cgd * If no second timer exists, stathz will be zero; in this case we drive
70 1.19 cgd * profiling and statistics off the main clock. This WILL NOT be accurate;
71 1.19 cgd * do not do it unless absolutely necessary.
72 1.19 cgd *
73 1.19 cgd * The statistics clock may (or may not) be run at a higher rate while
74 1.19 cgd * profiling. This profile clock runs at profhz. We require that profhz
75 1.19 cgd * be an integral multiple of stathz.
76 1.19 cgd *
77 1.19 cgd * If the statistics clock is running fast, it must be divided by the ratio
78 1.19 cgd * profhz/stathz for statistics. (For profiling, every tick counts.)
79 1.19 cgd */
80 1.19 cgd
81 1.19 cgd /*
82 1.19 cgd * TODO:
83 1.19 cgd * allocate more timeout table slots when table overflows.
84 1.19 cgd */
85 1.19 cgd
86 1.19 cgd /*
87 1.19 cgd * Bump a timeval by a small number of usec's.
88 1.19 cgd */
89 1.19 cgd #define BUMPTIME(t, usec) { \
90 1.19 cgd register volatile struct timeval *tp = (t); \
91 1.19 cgd register long us; \
92 1.19 cgd \
93 1.19 cgd tp->tv_usec = us = tp->tv_usec + (usec); \
94 1.19 cgd if (us >= 1000000) { \
95 1.19 cgd tp->tv_usec = us - 1000000; \
96 1.19 cgd tp->tv_sec++; \
97 1.19 cgd } \
98 1.19 cgd }
99 1.19 cgd
100 1.19 cgd int stathz;
101 1.19 cgd int profhz;
102 1.19 cgd int profprocs;
103 1.19 cgd int ticks;
104 1.22 cgd static int psdiv, pscnt; /* prof => stat divider */
105 1.22 cgd int psratio; /* ratio: prof / stat */
106 1.22 cgd int tickfix, tickfixinterval; /* used if tick not really integral */
107 1.22 cgd static int tickfixcnt; /* number of ticks since last fix */
108 1.19 cgd
109 1.19 cgd volatile struct timeval time;
110 1.19 cgd volatile struct timeval mono_time;
111 1.19 cgd
112 1.19 cgd /*
113 1.19 cgd * Initialize clock frequencies and start both clocks running.
114 1.19 cgd */
115 1.19 cgd void
116 1.19 cgd initclocks()
117 1.19 cgd {
118 1.19 cgd register int i;
119 1.19 cgd
120 1.19 cgd /*
121 1.19 cgd * Set divisors to 1 (normal case) and let the machine-specific
122 1.19 cgd * code do its bit.
123 1.19 cgd */
124 1.19 cgd psdiv = pscnt = 1;
125 1.19 cgd cpu_initclocks();
126 1.19 cgd
127 1.19 cgd /*
128 1.19 cgd * Compute profhz/stathz, and fix profhz if needed.
129 1.19 cgd */
130 1.19 cgd i = stathz ? stathz : hz;
131 1.19 cgd if (profhz == 0)
132 1.19 cgd profhz = i;
133 1.19 cgd psratio = profhz / i;
134 1.19 cgd }
135 1.19 cgd
136 1.19 cgd /*
137 1.19 cgd * The real-time timer, interrupting hz times per second.
138 1.19 cgd */
139 1.19 cgd void
140 1.19 cgd hardclock(frame)
141 1.19 cgd register struct clockframe *frame;
142 1.19 cgd {
143 1.19 cgd register struct callout *p1;
144 1.19 cgd register struct proc *p;
145 1.19 cgd register int delta, needsoft;
146 1.19 cgd extern int tickdelta;
147 1.19 cgd extern long timedelta;
148 1.19 cgd
149 1.19 cgd /*
150 1.19 cgd * Update real-time timeout queue.
151 1.19 cgd * At front of queue are some number of events which are ``due''.
152 1.19 cgd * The time to these is <= 0 and if negative represents the
153 1.19 cgd * number of ticks which have passed since it was supposed to happen.
154 1.19 cgd * The rest of the q elements (times > 0) are events yet to happen,
155 1.19 cgd * where the time for each is given as a delta from the previous.
156 1.19 cgd * Decrementing just the first of these serves to decrement the time
157 1.19 cgd * to all events.
158 1.19 cgd */
159 1.19 cgd needsoft = 0;
160 1.19 cgd for (p1 = calltodo.c_next; p1 != NULL; p1 = p1->c_next) {
161 1.19 cgd if (--p1->c_time > 0)
162 1.19 cgd break;
163 1.19 cgd needsoft = 1;
164 1.19 cgd if (p1->c_time == 0)
165 1.19 cgd break;
166 1.19 cgd }
167 1.19 cgd
168 1.19 cgd p = curproc;
169 1.19 cgd if (p) {
170 1.19 cgd register struct pstats *pstats;
171 1.19 cgd
172 1.19 cgd /*
173 1.19 cgd * Run current process's virtual and profile time, as needed.
174 1.19 cgd */
175 1.19 cgd pstats = p->p_stats;
176 1.19 cgd if (CLKF_USERMODE(frame) &&
177 1.19 cgd timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
178 1.19 cgd itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
179 1.19 cgd psignal(p, SIGVTALRM);
180 1.19 cgd if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
181 1.19 cgd itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
182 1.19 cgd psignal(p, SIGPROF);
183 1.19 cgd }
184 1.19 cgd
185 1.19 cgd /*
186 1.19 cgd * If no separate statistics clock is available, run it from here.
187 1.19 cgd */
188 1.19 cgd if (stathz == 0)
189 1.19 cgd statclock(frame);
190 1.19 cgd
191 1.19 cgd /*
192 1.22 cgd * Increment the time-of-day. The increment is normally just
193 1.22 cgd * ``tick''. If the machine is one which has a clock frequency
194 1.22 cgd * such that ``hz'' would not divide the second evenly into
195 1.22 cgd * milliseconds, a periodic adjustment must be applied. Finally,
196 1.22 cgd * if we are still adjusting the time (see adjtime()),
197 1.22 cgd * ``tickdelta'' may also be added in.
198 1.19 cgd */
199 1.19 cgd ticks++;
200 1.22 cgd delta = tick;
201 1.22 cgd if (tickfix) {
202 1.22 cgd tickfixcnt++;
203 1.22 cgd if (tickfixcnt > tickfixinterval) {
204 1.22 cgd delta += tickfix;
205 1.22 cgd tickfixcnt = 0;
206 1.22 cgd }
207 1.22 cgd }
208 1.22 cgd if (timedelta != 0) {
209 1.19 cgd delta = tick + tickdelta;
210 1.19 cgd timedelta -= tickdelta;
211 1.19 cgd }
212 1.19 cgd BUMPTIME(&time, delta);
213 1.19 cgd BUMPTIME(&mono_time, delta);
214 1.19 cgd
215 1.19 cgd /*
216 1.19 cgd * Process callouts at a very low cpu priority, so we don't keep the
217 1.19 cgd * relatively high clock interrupt priority any longer than necessary.
218 1.19 cgd */
219 1.19 cgd if (needsoft) {
220 1.19 cgd if (CLKF_BASEPRI(frame)) {
221 1.19 cgd /*
222 1.19 cgd * Save the overhead of a software interrupt;
223 1.19 cgd * it will happen as soon as we return, so do it now.
224 1.19 cgd */
225 1.19 cgd (void)splsoftclock();
226 1.19 cgd softclock();
227 1.19 cgd } else
228 1.19 cgd setsoftclock();
229 1.19 cgd }
230 1.19 cgd }
231 1.19 cgd
232 1.19 cgd /*
233 1.19 cgd * Software (low priority) clock interrupt.
234 1.19 cgd * Run periodic events from timeout queue.
235 1.19 cgd */
236 1.19 cgd /*ARGSUSED*/
237 1.19 cgd void
238 1.19 cgd softclock()
239 1.19 cgd {
240 1.19 cgd register struct callout *c;
241 1.19 cgd register void *arg;
242 1.19 cgd register void (*func) __P((void *));
243 1.19 cgd register int s;
244 1.19 cgd
245 1.19 cgd s = splhigh();
246 1.19 cgd while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
247 1.19 cgd func = c->c_func;
248 1.19 cgd arg = c->c_arg;
249 1.19 cgd calltodo.c_next = c->c_next;
250 1.19 cgd c->c_next = callfree;
251 1.19 cgd callfree = c;
252 1.19 cgd splx(s);
253 1.19 cgd (*func)(arg);
254 1.19 cgd (void) splhigh();
255 1.19 cgd }
256 1.19 cgd splx(s);
257 1.19 cgd }
258 1.19 cgd
259 1.19 cgd /*
260 1.19 cgd * timeout --
261 1.19 cgd * Execute a function after a specified length of time.
262 1.19 cgd *
263 1.19 cgd * untimeout --
264 1.19 cgd * Cancel previous timeout function call.
265 1.19 cgd *
266 1.19 cgd * See AT&T BCI Driver Reference Manual for specification. This
267 1.19 cgd * implementation differs from that one in that no identification
268 1.19 cgd * value is returned from timeout, rather, the original arguments
269 1.19 cgd * to timeout are used to identify entries for untimeout.
270 1.19 cgd */
271 1.19 cgd void
272 1.19 cgd timeout(ftn, arg, ticks)
273 1.19 cgd void (*ftn) __P((void *));
274 1.19 cgd void *arg;
275 1.19 cgd register int ticks;
276 1.19 cgd {
277 1.19 cgd register struct callout *new, *p, *t;
278 1.19 cgd register int s;
279 1.19 cgd
280 1.19 cgd if (ticks <= 0)
281 1.19 cgd ticks = 1;
282 1.19 cgd
283 1.19 cgd /* Lock out the clock. */
284 1.19 cgd s = splhigh();
285 1.19 cgd
286 1.19 cgd /* Fill in the next free callout structure. */
287 1.19 cgd if (callfree == NULL)
288 1.19 cgd panic("timeout table full");
289 1.19 cgd new = callfree;
290 1.19 cgd callfree = new->c_next;
291 1.19 cgd new->c_arg = arg;
292 1.19 cgd new->c_func = ftn;
293 1.19 cgd
294 1.19 cgd /*
295 1.19 cgd * The time for each event is stored as a difference from the time
296 1.19 cgd * of the previous event on the queue. Walk the queue, correcting
297 1.19 cgd * the ticks argument for queue entries passed. Correct the ticks
298 1.19 cgd * value for the queue entry immediately after the insertion point
299 1.19 cgd * as well. Watch out for negative c_time values; these represent
300 1.19 cgd * overdue events.
301 1.19 cgd */
302 1.19 cgd for (p = &calltodo;
303 1.19 cgd (t = p->c_next) != NULL && ticks > t->c_time; p = t)
304 1.19 cgd if (t->c_time > 0)
305 1.19 cgd ticks -= t->c_time;
306 1.19 cgd new->c_time = ticks;
307 1.19 cgd if (t != NULL)
308 1.19 cgd t->c_time -= ticks;
309 1.19 cgd
310 1.19 cgd /* Insert the new entry into the queue. */
311 1.19 cgd p->c_next = new;
312 1.19 cgd new->c_next = t;
313 1.19 cgd splx(s);
314 1.19 cgd }
315 1.19 cgd
316 1.19 cgd void
317 1.19 cgd untimeout(ftn, arg)
318 1.19 cgd void (*ftn) __P((void *));
319 1.19 cgd void *arg;
320 1.19 cgd {
321 1.19 cgd register struct callout *p, *t;
322 1.19 cgd register int s;
323 1.19 cgd
324 1.19 cgd s = splhigh();
325 1.19 cgd for (p = &calltodo; (t = p->c_next) != NULL; p = t)
326 1.19 cgd if (t->c_func == ftn && t->c_arg == arg) {
327 1.19 cgd /* Increment next entry's tick count. */
328 1.19 cgd if (t->c_next && t->c_time > 0)
329 1.19 cgd t->c_next->c_time += t->c_time;
330 1.19 cgd
331 1.19 cgd /* Move entry from callout queue to callfree queue. */
332 1.19 cgd p->c_next = t->c_next;
333 1.19 cgd t->c_next = callfree;
334 1.19 cgd callfree = t;
335 1.19 cgd break;
336 1.19 cgd }
337 1.19 cgd splx(s);
338 1.19 cgd }
339 1.19 cgd
340 1.19 cgd /*
341 1.19 cgd * Compute number of hz until specified time. Used to
342 1.19 cgd * compute third argument to timeout() from an absolute time.
343 1.19 cgd */
344 1.19 cgd int
345 1.19 cgd hzto(tv)
346 1.19 cgd struct timeval *tv;
347 1.19 cgd {
348 1.19 cgd register long ticks, sec;
349 1.19 cgd int s;
350 1.19 cgd
351 1.19 cgd /*
352 1.22 cgd * If number of microseconds will fit in 32 bit arithmetic,
353 1.22 cgd * then compute number of microseconds to time and scale to
354 1.19 cgd * ticks. Otherwise just compute number of hz in time, rounding
355 1.22 cgd * times greater than representible to maximum value. (We must
356 1.22 cgd * compute in microseconds, because hz can be greater than 1000,
357 1.22 cgd * and thus tick can be less than one millisecond).
358 1.19 cgd *
359 1.22 cgd * Delta times less than 14 hours can be computed ``exactly''.
360 1.22 cgd * (Note that if hz would yeild a non-integral number of us per
361 1.22 cgd * tick, i.e. tickfix is nonzero, timouts can be a tick longer
362 1.22 cgd * than they should be.) Maximum value for any timeout in 10ms
363 1.22 cgd * ticks is 250 days.
364 1.19 cgd */
365 1.19 cgd s = splhigh();
366 1.19 cgd sec = tv->tv_sec - time.tv_sec;
367 1.22 cgd if (sec <= 0x7fffffff / 1000000 - 1)
368 1.22 cgd ticks = ((tv->tv_sec - time.tv_sec) * 1000000 +
369 1.22 cgd (tv->tv_usec - time.tv_usec)) / tick;
370 1.19 cgd else if (sec <= 0x7fffffff / hz)
371 1.19 cgd ticks = sec * hz;
372 1.19 cgd else
373 1.19 cgd ticks = 0x7fffffff;
374 1.19 cgd splx(s);
375 1.19 cgd return (ticks);
376 1.19 cgd }
377 1.19 cgd
378 1.19 cgd /*
379 1.19 cgd * Start profiling on a process.
380 1.19 cgd *
381 1.19 cgd * Kernel profiling passes proc0 which never exits and hence
382 1.19 cgd * keeps the profile clock running constantly.
383 1.19 cgd */
384 1.19 cgd void
385 1.19 cgd startprofclock(p)
386 1.19 cgd register struct proc *p;
387 1.19 cgd {
388 1.19 cgd int s;
389 1.19 cgd
390 1.19 cgd if ((p->p_flag & P_PROFIL) == 0) {
391 1.19 cgd p->p_flag |= P_PROFIL;
392 1.19 cgd if (++profprocs == 1 && stathz != 0) {
393 1.19 cgd s = splstatclock();
394 1.19 cgd psdiv = pscnt = psratio;
395 1.19 cgd setstatclockrate(profhz);
396 1.19 cgd splx(s);
397 1.19 cgd }
398 1.19 cgd }
399 1.19 cgd }
400 1.19 cgd
401 1.19 cgd /*
402 1.19 cgd * Stop profiling on a process.
403 1.19 cgd */
404 1.19 cgd void
405 1.19 cgd stopprofclock(p)
406 1.19 cgd register struct proc *p;
407 1.19 cgd {
408 1.19 cgd int s;
409 1.19 cgd
410 1.19 cgd if (p->p_flag & P_PROFIL) {
411 1.19 cgd p->p_flag &= ~P_PROFIL;
412 1.19 cgd if (--profprocs == 0 && stathz != 0) {
413 1.19 cgd s = splstatclock();
414 1.19 cgd psdiv = pscnt = 1;
415 1.19 cgd setstatclockrate(stathz);
416 1.19 cgd splx(s);
417 1.19 cgd }
418 1.19 cgd }
419 1.19 cgd }
420 1.19 cgd
421 1.19 cgd int dk_ndrive = DK_NDRIVE;
422 1.19 cgd
423 1.19 cgd /*
424 1.19 cgd * Statistics clock. Grab profile sample, and if divider reaches 0,
425 1.19 cgd * do process and kernel statistics.
426 1.19 cgd */
427 1.19 cgd void
428 1.19 cgd statclock(frame)
429 1.19 cgd register struct clockframe *frame;
430 1.19 cgd {
431 1.19 cgd #ifdef GPROF
432 1.19 cgd register struct gmonparam *g;
433 1.19 cgd #endif
434 1.19 cgd register struct proc *p;
435 1.19 cgd register int i;
436 1.19 cgd
437 1.19 cgd if (CLKF_USERMODE(frame)) {
438 1.19 cgd p = curproc;
439 1.19 cgd if (p->p_flag & P_PROFIL)
440 1.19 cgd addupc_intr(p, CLKF_PC(frame), 1);
441 1.19 cgd if (--pscnt > 0)
442 1.19 cgd return;
443 1.19 cgd /*
444 1.19 cgd * Came from user mode; CPU was in user state.
445 1.19 cgd * If this process is being profiled record the tick.
446 1.19 cgd */
447 1.19 cgd p->p_uticks++;
448 1.19 cgd if (p->p_nice > NZERO)
449 1.19 cgd cp_time[CP_NICE]++;
450 1.19 cgd else
451 1.19 cgd cp_time[CP_USER]++;
452 1.19 cgd } else {
453 1.19 cgd #ifdef GPROF
454 1.19 cgd /*
455 1.19 cgd * Kernel statistics are just like addupc_intr, only easier.
456 1.19 cgd */
457 1.19 cgd g = &_gmonparam;
458 1.19 cgd if (g->state == GMON_PROF_ON) {
459 1.19 cgd i = CLKF_PC(frame) - g->lowpc;
460 1.19 cgd if (i < g->textsize) {
461 1.19 cgd i /= HISTFRACTION * sizeof(*g->kcount);
462 1.19 cgd g->kcount[i]++;
463 1.19 cgd }
464 1.19 cgd }
465 1.19 cgd #endif
466 1.19 cgd if (--pscnt > 0)
467 1.19 cgd return;
468 1.19 cgd /*
469 1.19 cgd * Came from kernel mode, so we were:
470 1.19 cgd * - handling an interrupt,
471 1.19 cgd * - doing syscall or trap work on behalf of the current
472 1.19 cgd * user process, or
473 1.19 cgd * - spinning in the idle loop.
474 1.19 cgd * Whichever it is, charge the time as appropriate.
475 1.19 cgd * Note that we charge interrupts to the current process,
476 1.19 cgd * regardless of whether they are ``for'' that process,
477 1.19 cgd * so that we know how much of its real time was spent
478 1.19 cgd * in ``non-process'' (i.e., interrupt) work.
479 1.19 cgd */
480 1.19 cgd p = curproc;
481 1.19 cgd if (CLKF_INTR(frame)) {
482 1.19 cgd if (p != NULL)
483 1.19 cgd p->p_iticks++;
484 1.19 cgd cp_time[CP_INTR]++;
485 1.19 cgd } else if (p != NULL) {
486 1.19 cgd p->p_sticks++;
487 1.19 cgd cp_time[CP_SYS]++;
488 1.19 cgd } else
489 1.19 cgd cp_time[CP_IDLE]++;
490 1.19 cgd }
491 1.19 cgd pscnt = psdiv;
492 1.19 cgd
493 1.19 cgd /*
494 1.19 cgd * We maintain statistics shown by user-level statistics
495 1.19 cgd * programs: the amount of time in each cpu state, and
496 1.19 cgd * the amount of time each of DK_NDRIVE ``drives'' is busy.
497 1.19 cgd *
498 1.19 cgd * XXX should either run linked list of drives, or (better)
499 1.19 cgd * grab timestamps in the start & done code.
500 1.19 cgd */
501 1.19 cgd for (i = 0; i < DK_NDRIVE; i++)
502 1.19 cgd if (dk_busy & (1 << i))
503 1.19 cgd dk_time[i]++;
504 1.19 cgd
505 1.19 cgd /*
506 1.19 cgd * We adjust the priority of the current process. The priority of
507 1.19 cgd * a process gets worse as it accumulates CPU time. The cpu usage
508 1.19 cgd * estimator (p_estcpu) is increased here. The formula for computing
509 1.19 cgd * priorities (in kern_synch.c) will compute a different value each
510 1.19 cgd * time p_estcpu increases by 4. The cpu usage estimator ramps up
511 1.19 cgd * quite quickly when the process is running (linearly), and decays
512 1.19 cgd * away exponentially, at a rate which is proportionally slower when
513 1.19 cgd * the system is busy. The basic principal is that the system will
514 1.19 cgd * 90% forget that the process used a lot of CPU time in 5 * loadav
515 1.19 cgd * seconds. This causes the system to favor processes which haven't
516 1.19 cgd * run much recently, and to round-robin among other processes.
517 1.19 cgd */
518 1.19 cgd if (p != NULL) {
519 1.19 cgd p->p_cpticks++;
520 1.19 cgd if (++p->p_estcpu == 0)
521 1.19 cgd p->p_estcpu--;
522 1.19 cgd if ((p->p_estcpu & 3) == 0) {
523 1.19 cgd resetpriority(p);
524 1.19 cgd if (p->p_priority >= PUSER)
525 1.19 cgd p->p_priority = p->p_usrpri;
526 1.19 cgd }
527 1.19 cgd }
528 1.19 cgd }
529 1.19 cgd
530 1.19 cgd /*
531 1.19 cgd * Return information about system clocks.
532 1.19 cgd */
533 1.19 cgd sysctl_clockrate(where, sizep)
534 1.19 cgd register char *where;
535 1.19 cgd size_t *sizep;
536 1.19 cgd {
537 1.19 cgd struct clockinfo clkinfo;
538 1.19 cgd
539 1.19 cgd /*
540 1.19 cgd * Construct clockinfo structure.
541 1.19 cgd */
542 1.20 mycroft clkinfo.tick = tick;
543 1.20 mycroft clkinfo.tickadj = tickadj;
544 1.19 cgd clkinfo.hz = hz;
545 1.19 cgd clkinfo.profhz = profhz;
546 1.19 cgd clkinfo.stathz = stathz ? stathz : hz;
547 1.19 cgd return (sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo)));
548 1.19 cgd }
549 1.19 cgd
550 1.19 cgd #ifdef DDB
551 1.21 mycroft #include <machine/db_machdep.h>
552 1.21 mycroft
553 1.19 cgd #include <ddb/db_access.h>
554 1.19 cgd #include <ddb/db_sym.h>
555 1.19 cgd
556 1.19 cgd void db_show_callout(long addr, int haddr, int count, char *modif)
557 1.19 cgd {
558 1.19 cgd register struct callout *p1;
559 1.19 cgd register int cum;
560 1.19 cgd register int s;
561 1.19 cgd db_expr_t offset;
562 1.19 cgd char *name;
563 1.19 cgd
564 1.19 cgd db_printf(" cum ticks arg func\n");
565 1.19 cgd s = splhigh();
566 1.19 cgd for (cum = 0, p1 = calltodo.c_next; p1; p1 = p1->c_next) {
567 1.19 cgd register int t = p1->c_time;
568 1.19 cgd
569 1.19 cgd if (t > 0)
570 1.19 cgd cum += t;
571 1.19 cgd
572 1.21 mycroft db_find_sym_and_offset((db_addr_t)p1->c_func, &name, &offset);
573 1.19 cgd if (name == NULL)
574 1.19 cgd name = "?";
575 1.19 cgd
576 1.19 cgd db_printf("%9d %9d %8x %s (%x)\n",
577 1.19 cgd cum, t, p1->c_arg, name, p1->c_func);
578 1.19 cgd }
579 1.19 cgd splx(s);
580 1.19 cgd }
581 1.19 cgd #endif
582