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