1 /* $OpenBSD: kern_clock.c,v 1.124 2024/07/08 13:17:11 claudio 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. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/clockintr.h> 43 #include <sys/timeout.h> 44 #include <sys/kernel.h> 45 #include <sys/limits.h> 46 #include <sys/proc.h> 47 #include <sys/user.h> 48 #include <sys/resourcevar.h> 49 #include <sys/sysctl.h> 50 #include <sys/sched.h> 51 #include <sys/timetc.h> 52 53 /* 54 * Clock handling routines. 55 * 56 * This code is written to operate with two timers that run independently of 57 * each other. The main clock, running hz times per second, is used to keep 58 * track of real time. The second timer handles kernel and user profiling, 59 * and does resource use estimation. If the second timer is programmable, 60 * it is randomized to avoid aliasing between the two clocks. For example, 61 * the randomization prevents an adversary from always giving up the cpu 62 * just before its quantum expires. Otherwise, it would never accumulate 63 * cpu ticks. The mean frequency of the second timer is stathz. 64 * 65 * If no second timer exists, stathz will be zero; in this case we drive 66 * profiling and statistics off the main clock. This WILL NOT be accurate; 67 * do not do it unless absolutely necessary. 68 * 69 * The statistics clock may (or may not) be run at a higher rate while 70 * profiling. This profile clock runs at profhz. We require that profhz 71 * be an integral multiple of stathz. 72 * 73 * If the statistics clock is running fast, it must be divided by the ratio 74 * profhz/stathz for statistics. (For profiling, every tick counts.) 75 */ 76 77 int stathz; 78 int profhz; 79 int profprocs; 80 int ticks = INT_MAX - (15 * 60 * HZ); 81 82 /* Don't force early wrap around, triggers bug in inteldrm */ 83 volatile unsigned long jiffies; 84 85 uint64_t hardclock_period; /* [I] hardclock period (ns) */ 86 uint64_t statclock_avg; /* [I] average statclock period (ns) */ 87 uint64_t statclock_min; /* [I] minimum statclock period (ns) */ 88 uint32_t statclock_mask; /* [I] set of allowed offsets */ 89 int statclock_is_randomized; /* [I] fixed or pseudorandom period? */ 90 91 /* 92 * Initialize clock frequencies and start both clocks running. 93 */ 94 void 95 initclocks(void) 96 { 97 uint64_t half_avg; 98 uint32_t var; 99 100 /* 101 * Let the machine-specific code do its bit. 102 */ 103 cpu_initclocks(); 104 105 KASSERT(hz > 0 && hz <= 1000000000); 106 hardclock_period = 1000000000 / hz; 107 roundrobin_period = hardclock_period * 10; 108 109 KASSERT(stathz >= 1 && stathz <= 1000000000); 110 111 /* 112 * Compute the average statclock() period. Then find var, the 113 * largest 32-bit power of two such that var <= statclock_avg / 2. 114 */ 115 statclock_avg = 1000000000 / stathz; 116 half_avg = statclock_avg / 2; 117 for (var = 1U << 31; var > half_avg; var /= 2) 118 continue; 119 120 /* 121 * Set a lower bound for the range using statclock_avg and var. 122 * The mask for that range is just (var - 1). 123 */ 124 statclock_min = statclock_avg - (var / 2); 125 statclock_mask = var - 1; 126 127 KASSERT(profhz >= stathz && profhz <= 1000000000); 128 KASSERT(profhz % stathz == 0); 129 profclock_period = 1000000000 / profhz; 130 131 inittimecounter(); 132 133 /* Start dispatching clock interrupts on the primary CPU. */ 134 cpu_startclock(); 135 } 136 137 /* 138 * The real-time timer, interrupting hz times per second. 139 */ 140 void 141 hardclock(struct clockframe *frame) 142 { 143 tc_ticktock(); 144 ticks++; 145 jiffies++; 146 147 /* 148 * Update the timeout wheel. 149 */ 150 timeout_hardclock_update(); 151 } 152 153 /* 154 * Compute number of hz in the specified amount of time. 155 */ 156 int 157 tvtohz(const struct timeval *tv) 158 { 159 unsigned long nticks; 160 time_t sec; 161 long usec; 162 163 /* 164 * If the number of usecs in the whole seconds part of the time 165 * fits in a long, then the total number of usecs will 166 * fit in an unsigned long. Compute the total and convert it to 167 * ticks, rounding up and adding 1 to allow for the current tick 168 * to expire. Rounding also depends on unsigned long arithmetic 169 * to avoid overflow. 170 * 171 * Otherwise, if the number of ticks in the whole seconds part of 172 * the time fits in a long, then convert the parts to 173 * ticks separately and add, using similar rounding methods and 174 * overflow avoidance. This method would work in the previous 175 * case but it is slightly slower and assumes that hz is integral. 176 * 177 * Otherwise, round the time down to the maximum 178 * representable value. 179 * 180 * If ints have 32 bits, then the maximum value for any timeout in 181 * 10ms ticks is 248 days. 182 */ 183 sec = tv->tv_sec; 184 usec = tv->tv_usec; 185 if (sec < 0 || (sec == 0 && usec <= 0)) 186 nticks = 0; 187 else if (sec <= LONG_MAX / 1000000) 188 nticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) 189 / tick + 1; 190 else if (sec <= LONG_MAX / hz) 191 nticks = sec * hz 192 + ((unsigned long)usec + (tick - 1)) / tick + 1; 193 else 194 nticks = LONG_MAX; 195 if (nticks > INT_MAX) 196 nticks = INT_MAX; 197 return ((int)nticks); 198 } 199 200 int 201 tstohz(const struct timespec *ts) 202 { 203 struct timeval tv; 204 TIMESPEC_TO_TIMEVAL(&tv, ts); 205 206 /* Round up. */ 207 if ((ts->tv_nsec % 1000) != 0) { 208 tv.tv_usec += 1; 209 if (tv.tv_usec >= 1000000) { 210 tv.tv_usec -= 1000000; 211 tv.tv_sec += 1; 212 } 213 } 214 215 return (tvtohz(&tv)); 216 } 217 218 /* 219 * Start profiling on a process. 220 * 221 * Kernel profiling passes proc0 which never exits and hence 222 * keeps the profile clock running constantly. 223 */ 224 void 225 startprofclock(struct process *pr) 226 { 227 int s; 228 229 if ((pr->ps_flags & PS_PROFIL) == 0) { 230 atomic_setbits_int(&pr->ps_flags, PS_PROFIL); 231 if (++profprocs == 1) { 232 s = splstatclock(); 233 setstatclockrate(profhz); 234 splx(s); 235 } 236 } 237 } 238 239 /* 240 * Stop profiling on a process. 241 */ 242 void 243 stopprofclock(struct process *pr) 244 { 245 int s; 246 247 if (pr->ps_flags & PS_PROFIL) { 248 atomic_clearbits_int(&pr->ps_flags, PS_PROFIL); 249 if (--profprocs == 0) { 250 s = splstatclock(); 251 setstatclockrate(stathz); 252 splx(s); 253 } 254 } 255 } 256 257 /* 258 * Statistics clock. Grab profile sample, and if divider reaches 0, 259 * do process and kernel statistics. 260 */ 261 void 262 statclock(struct clockrequest *cr, void *cf, void *arg) 263 { 264 uint64_t count, i; 265 struct clockframe *frame = cf; 266 struct cpu_info *ci = curcpu(); 267 struct schedstate_percpu *spc = &ci->ci_schedstate; 268 struct proc *p = curproc; 269 struct process *pr; 270 271 if (statclock_is_randomized) { 272 count = clockrequest_advance_random(cr, statclock_min, 273 statclock_mask); 274 } else { 275 count = clockrequest_advance(cr, statclock_avg); 276 } 277 278 if (CLKF_USERMODE(frame)) { 279 pr = p->p_p; 280 /* 281 * Came from user mode; CPU was in user state. 282 * If this process is being profiled record the tick. 283 */ 284 tu_enter(&p->p_tu); 285 p->p_tu.tu_uticks += count; 286 tu_leave(&p->p_tu); 287 if (pr->ps_nice > NZERO) 288 spc->spc_cp_time[CP_NICE] += count; 289 else 290 spc->spc_cp_time[CP_USER] += count; 291 } else { 292 /* 293 * Came from kernel mode, so we were: 294 * - spinning on a lock 295 * - handling an interrupt, 296 * - doing syscall or trap work on behalf of the current 297 * user process, or 298 * - spinning in the idle loop. 299 * Whichever it is, charge the time as appropriate. 300 * Note that we charge interrupts to the current process, 301 * regardless of whether they are ``for'' that process, 302 * so that we know how much of its real time was spent 303 * in ``non-process'' (i.e., interrupt) work. 304 */ 305 if (CLKF_INTR(frame)) { 306 if (p != NULL) { 307 tu_enter(&p->p_tu); 308 p->p_tu.tu_iticks += count; 309 tu_leave(&p->p_tu); 310 } 311 spc->spc_cp_time[spc->spc_spinning ? 312 CP_SPIN : CP_INTR] += count; 313 } else if (p != NULL && p != spc->spc_idleproc) { 314 tu_enter(&p->p_tu); 315 p->p_tu.tu_sticks += count; 316 tu_leave(&p->p_tu); 317 spc->spc_cp_time[spc->spc_spinning ? 318 CP_SPIN : CP_SYS] += count; 319 } else 320 spc->spc_cp_time[spc->spc_spinning ? 321 CP_SPIN : CP_IDLE] += count; 322 } 323 324 if (p != NULL) { 325 p->p_cpticks += count; 326 /* 327 * schedclock() runs every fourth statclock(). 328 */ 329 for (i = 0; i < count; i++) { 330 if ((++spc->spc_schedticks & 3) == 0) 331 schedclock(p); 332 } 333 } 334 } 335 336 /* 337 * Return information about system clocks. 338 */ 339 int 340 sysctl_clockrate(char *where, size_t *sizep, void *newp) 341 { 342 struct clockinfo clkinfo; 343 344 /* 345 * Construct clockinfo structure. 346 */ 347 memset(&clkinfo, 0, sizeof clkinfo); 348 clkinfo.tick = tick; 349 clkinfo.hz = hz; 350 clkinfo.profhz = profhz; 351 clkinfo.stathz = stathz; 352 return (sysctl_rdstruct(where, sizep, newp, &clkinfo, sizeof(clkinfo))); 353 } 354