1 /* $OpenBSD: kern_clock.c,v 1.39 2002/07/06 19:14:20 nordin Exp $ */ 2 /* $NetBSD: kern_clock.c,v 1.34 1996/06/09 04:51:03 briggs Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/dkstat.h> 47 #include <sys/timeout.h> 48 #include <sys/kernel.h> 49 #include <sys/limits.h> 50 #include <sys/proc.h> 51 #include <sys/resourcevar.h> 52 #include <sys/signalvar.h> 53 #include <uvm/uvm_extern.h> 54 #include <sys/sysctl.h> 55 #include <sys/sched.h> 56 57 #include <machine/cpu.h> 58 59 #ifdef GPROF 60 #include <sys/gmon.h> 61 #endif 62 63 /* 64 * Clock handling routines. 65 * 66 * This code is written to operate with two timers that run independently of 67 * each other. The main clock, running hz times per second, is used to keep 68 * track of real time. The second timer handles kernel and user profiling, 69 * and does resource use estimation. If the second timer is programmable, 70 * it is randomized to avoid aliasing between the two clocks. For example, 71 * the randomization prevents an adversary from always giving up the cpu 72 * just before its quantum expires. Otherwise, it would never accumulate 73 * cpu ticks. The mean frequency of the second timer is stathz. 74 * 75 * If no second timer exists, stathz will be zero; in this case we drive 76 * profiling and statistics off the main clock. This WILL NOT be accurate; 77 * do not do it unless absolutely necessary. 78 * 79 * The statistics clock may (or may not) be run at a higher rate while 80 * profiling. This profile clock runs at profhz. We require that profhz 81 * be an integral multiple of stathz. 82 * 83 * If the statistics clock is running fast, it must be divided by the ratio 84 * profhz/stathz for statistics. (For profiling, every tick counts.) 85 */ 86 87 /* 88 * Bump a timeval by a small number of usec's. 89 */ 90 #define BUMPTIME(t, usec) { \ 91 register volatile struct timeval *tp = (t); \ 92 register long us; \ 93 \ 94 tp->tv_usec = us = tp->tv_usec + (usec); \ 95 if (us >= 1000000) { \ 96 tp->tv_usec = us - 1000000; \ 97 tp->tv_sec++; \ 98 } \ 99 } 100 101 int stathz; 102 int schedhz; 103 int profhz; 104 int profprocs; 105 int ticks; 106 static int psdiv, pscnt; /* prof => stat divider */ 107 int psratio; /* ratio: prof / stat */ 108 int tickfix, tickfixinterval; /* used if tick not really integral */ 109 static int tickfixcnt; /* accumulated fractional error */ 110 111 long cp_time[CPUSTATES]; 112 113 volatile struct timeval time; 114 volatile struct timeval mono_time; 115 116 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 117 void *softclock_si; 118 void generic_softclock(void *); 119 120 void 121 generic_softclock(void *ignore) 122 { 123 /* 124 * XXX - dont' commit, just a dummy wrapper until we learn everyone 125 * deal with a changed proto for softclock(). 126 */ 127 softclock(); 128 } 129 #endif 130 131 /* 132 * Initialize clock frequencies and start both clocks running. 133 */ 134 void 135 initclocks() 136 { 137 int i; 138 139 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 140 softclock_si = softintr_establish(IPL_SOFTCLOCK, generic_softclock, NULL); 141 if (softclock_si == NULL) 142 panic("initclocks: unable to register softclock intr"); 143 #endif 144 145 /* 146 * Set divisors to 1 (normal case) and let the machine-specific 147 * code do its bit. 148 */ 149 psdiv = pscnt = 1; 150 cpu_initclocks(); 151 152 /* 153 * Compute profhz/stathz, and fix profhz if needed. 154 */ 155 i = stathz ? stathz : hz; 156 if (profhz == 0) 157 profhz = i; 158 psratio = profhz / i; 159 } 160 161 /* 162 * The real-time timer, interrupting hz times per second. 163 */ 164 void 165 hardclock(frame) 166 register struct clockframe *frame; 167 { 168 register struct proc *p; 169 register int delta; 170 extern int tickdelta; 171 extern long timedelta; 172 173 p = curproc; 174 if (p) { 175 register struct pstats *pstats; 176 177 /* 178 * Run current process's virtual and profile time, as needed. 179 */ 180 pstats = p->p_stats; 181 if (CLKF_USERMODE(frame) && 182 timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) && 183 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 184 psignal(p, SIGVTALRM); 185 if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) && 186 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 187 psignal(p, SIGPROF); 188 } 189 190 /* 191 * If no separate statistics clock is available, run it from here. 192 */ 193 if (stathz == 0) 194 statclock(frame); 195 196 /* 197 * Increment the time-of-day. The increment is normally just 198 * ``tick''. If the machine is one which has a clock frequency 199 * such that ``hz'' would not divide the second evenly into 200 * milliseconds, a periodic adjustment must be applied. Finally, 201 * if we are still adjusting the time (see adjtime()), 202 * ``tickdelta'' may also be added in. 203 */ 204 ticks++; 205 delta = tick; 206 207 if (tickfix) { 208 tickfixcnt += tickfix; 209 if (tickfixcnt >= tickfixinterval) { 210 delta++; 211 tickfixcnt -= tickfixinterval; 212 } 213 } 214 /* Imprecise 4bsd adjtime() handling */ 215 if (timedelta != 0) { 216 delta += tickdelta; 217 timedelta -= tickdelta; 218 } 219 220 #ifdef notyet 221 microset(); 222 #endif 223 224 BUMPTIME(&time, delta); 225 BUMPTIME(&mono_time, delta); 226 227 /* 228 * Update real-time timeout queue. 229 * Process callouts at a very low cpu priority, so we don't keep the 230 * relatively high clock interrupt priority any longer than necessary. 231 */ 232 if (timeout_hardclock_update()) { 233 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 234 softintr_schedule(softclock_si); 235 #else 236 setsoftclock(); 237 #endif 238 } 239 } 240 241 /* 242 * Compute number of hz until specified time. Used to 243 * compute the second argument to timeout_add() from an absolute time. 244 */ 245 int 246 hzto(tv) 247 struct timeval *tv; 248 { 249 unsigned long ticks; 250 long sec, usec; 251 int s; 252 253 /* 254 * If the number of usecs in the whole seconds part of the time 255 * difference fits in a long, then the total number of usecs will 256 * fit in an unsigned long. Compute the total and convert it to 257 * ticks, rounding up and adding 1 to allow for the current tick 258 * to expire. Rounding also depends on unsigned long arithmetic 259 * to avoid overflow. 260 * 261 * Otherwise, if the number of ticks in the whole seconds part of 262 * the time difference fits in a long, then convert the parts to 263 * ticks separately and add, using similar rounding methods and 264 * overflow avoidance. This method would work in the previous 265 * case but it is slightly slower and assumes that hz is integral. 266 * 267 * Otherwise, round the time difference down to the maximum 268 * representable value. 269 * 270 * If ints have 32 bits, then the maximum value for any timeout in 271 * 10ms ticks is 248 days. 272 */ 273 s = splhigh(); 274 sec = tv->tv_sec - time.tv_sec; 275 usec = tv->tv_usec - time.tv_usec; 276 splx(s); 277 if (usec < 0) { 278 sec--; 279 usec += 1000000; 280 } 281 if (sec < 0 || (sec == 0 && usec <= 0)) { 282 ticks = 0; 283 } else if (sec <= LONG_MAX / 1000000) 284 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) 285 / tick + 1; 286 else if (sec <= LONG_MAX / hz) 287 ticks = sec * hz 288 + ((unsigned long)usec + (tick - 1)) / tick + 1; 289 else 290 ticks = LONG_MAX; 291 if (ticks > INT_MAX) 292 ticks = INT_MAX; 293 return ((int)ticks); 294 } 295 296 /* 297 * Compute number of hz in the specified amount of time. 298 */ 299 int 300 tvtohz(struct timeval *tv) 301 { 302 unsigned long ticks; 303 long sec, usec; 304 305 /* 306 * If the number of usecs in the whole seconds part of the time 307 * fits in a long, then the total number of usecs will 308 * fit in an unsigned long. Compute the total and convert it to 309 * ticks, rounding up and adding 1 to allow for the current tick 310 * to expire. Rounding also depends on unsigned long arithmetic 311 * to avoid overflow. 312 * 313 * Otherwise, if the number of ticks in the whole seconds part of 314 * the time fits in a long, then convert the parts to 315 * ticks separately and add, using similar rounding methods and 316 * overflow avoidance. This method would work in the previous 317 * case but it is slightly slower and assumes that hz is integral. 318 * 319 * Otherwise, round the time down to the maximum 320 * representable value. 321 * 322 * If ints have 32 bits, then the maximum value for any timeout in 323 * 10ms ticks is 248 days. 324 */ 325 sec = tv->tv_sec; 326 usec = tv->tv_usec; 327 if (sec < 0 || (sec == 0 && usec <= 0)) 328 ticks = 0; 329 else if (sec <= LONG_MAX / 1000000) 330 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) 331 / tick + 1; 332 else if (sec <= LONG_MAX / hz) 333 ticks = sec * hz 334 + ((unsigned long)usec + (tick - 1)) / tick + 1; 335 else 336 ticks = LONG_MAX; 337 if (ticks > INT_MAX) 338 ticks = INT_MAX; 339 return ((int)ticks); 340 } 341 342 /* 343 * Start profiling on a process. 344 * 345 * Kernel profiling passes proc0 which never exits and hence 346 * keeps the profile clock running constantly. 347 */ 348 void 349 startprofclock(p) 350 register struct proc *p; 351 { 352 int s; 353 354 if ((p->p_flag & P_PROFIL) == 0) { 355 p->p_flag |= P_PROFIL; 356 if (++profprocs == 1 && stathz != 0) { 357 s = splstatclock(); 358 psdiv = pscnt = psratio; 359 setstatclockrate(profhz); 360 splx(s); 361 } 362 } 363 } 364 365 /* 366 * Stop profiling on a process. 367 */ 368 void 369 stopprofclock(p) 370 register struct proc *p; 371 { 372 int s; 373 374 if (p->p_flag & P_PROFIL) { 375 p->p_flag &= ~P_PROFIL; 376 if (--profprocs == 0 && stathz != 0) { 377 s = splstatclock(); 378 psdiv = pscnt = 1; 379 setstatclockrate(stathz); 380 splx(s); 381 } 382 } 383 } 384 385 /* 386 * Statistics clock. Grab profile sample, and if divider reaches 0, 387 * do process and kernel statistics. 388 */ 389 void 390 statclock(frame) 391 register struct clockframe *frame; 392 { 393 #ifdef GPROF 394 register struct gmonparam *g; 395 register int i; 396 #endif 397 static int schedclk; 398 register struct proc *p; 399 400 if (CLKF_USERMODE(frame)) { 401 p = curproc; 402 if (p->p_flag & P_PROFIL) 403 addupc_intr(p, CLKF_PC(frame)); 404 if (--pscnt > 0) 405 return; 406 /* 407 * Came from user mode; CPU was in user state. 408 * If this process is being profiled record the tick. 409 */ 410 p->p_uticks++; 411 if (p->p_nice > NZERO) 412 cp_time[CP_NICE]++; 413 else 414 cp_time[CP_USER]++; 415 } else { 416 #ifdef GPROF 417 /* 418 * Kernel statistics are just like addupc_intr, only easier. 419 */ 420 g = &_gmonparam; 421 if (g->state == GMON_PROF_ON) { 422 i = CLKF_PC(frame) - g->lowpc; 423 if (i < g->textsize) { 424 i /= HISTFRACTION * sizeof(*g->kcount); 425 g->kcount[i]++; 426 } 427 } 428 #endif 429 if (--pscnt > 0) 430 return; 431 /* 432 * Came from kernel mode, so we were: 433 * - handling an interrupt, 434 * - doing syscall or trap work on behalf of the current 435 * user process, or 436 * - spinning in the idle loop. 437 * Whichever it is, charge the time as appropriate. 438 * Note that we charge interrupts to the current process, 439 * regardless of whether they are ``for'' that process, 440 * so that we know how much of its real time was spent 441 * in ``non-process'' (i.e., interrupt) work. 442 */ 443 p = curproc; 444 if (CLKF_INTR(frame)) { 445 if (p != NULL) 446 p->p_iticks++; 447 cp_time[CP_INTR]++; 448 } else if (p != NULL) { 449 p->p_sticks++; 450 cp_time[CP_SYS]++; 451 } else 452 cp_time[CP_IDLE]++; 453 } 454 pscnt = psdiv; 455 456 if (p != NULL) { 457 p->p_cpticks++; 458 /* 459 * If no schedclock is provided, call it here at ~~12-25 Hz; 460 * ~~16 Hz is best 461 */ 462 if (schedhz == 0) 463 if ((++schedclk & 3) == 0) 464 schedclock(p); 465 } 466 } 467 468 /* 469 * Return information about system clocks. 470 */ 471 int 472 sysctl_clockrate(where, sizep) 473 register char *where; 474 size_t *sizep; 475 { 476 struct clockinfo clkinfo; 477 478 /* 479 * Construct clockinfo structure. 480 */ 481 clkinfo.tick = tick; 482 clkinfo.tickadj = tickadj; 483 clkinfo.hz = hz; 484 clkinfo.profhz = profhz; 485 clkinfo.stathz = stathz ? stathz : hz; 486 return (sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo))); 487 } 488