changed \&ps to ps in man page
[unix-history] / sys / kern / kern_time.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_time.c 7.15 (Berkeley) 3/17/91
34 */
35
36#include "param.h"
37#include "resourcevar.h"
38#include "kernel.h"
39#include "proc.h"
40
41#include "machine/cpu.h"
42
43/*
44 * Time of day and interval timer support.
45 *
46 * These routines provide the kernel entry points to get and set
47 * the time-of-day and per-process interval timers. Subroutines
48 * here provide support for adding and subtracting timeval structures
49 * and decrementing interval timers, optionally reloading the interval
50 * timers when they expire.
51 */
52
3c7eb27c
DG
53struct gettimeofday_args {
54 struct timeval *tp;
55 struct timezone *tzp;
56};
57
15637ed4
RG
58/* ARGSUSED */
59gettimeofday(p, uap, retval)
60 struct proc *p;
3c7eb27c 61 register struct gettimeofday_args *uap;
15637ed4
RG
62 int *retval;
63{
64 struct timeval atv;
65 int error = 0;
66
67 if (uap->tp) {
68 microtime(&atv);
69 if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
70 sizeof (atv)))
71 return (error);
72 }
73 if (uap->tzp)
74 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
75 sizeof (tz));
76 return (error);
77}
78
3c7eb27c
DG
79struct settimeofday_args {
80 struct timeval *tv;
81 struct timezone *tzp;
82};
83
15637ed4
RG
84/* ARGSUSED */
85settimeofday(p, uap, retval)
86 struct proc *p;
3c7eb27c 87 struct settimeofday_args *uap;
15637ed4
RG
88 int *retval;
89{
90 struct timeval atv;
91 struct timezone atz;
92 int error, s;
93
94 if (error = suser(p->p_ucred, &p->p_acflag))
95 return (error);
96 if (uap->tv) {
97 if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
98 sizeof (struct timeval)))
99 return (error);
100 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
101 boottime.tv_sec += atv.tv_sec - time.tv_sec;
102 s = splhigh(); time = atv; splx(s);
103 resettodr();
104 }
105 if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
106 sizeof (atz))) == 0)
107 tz = atz;
108 return (error);
109}
110
111extern int tickadj; /* "standard" clock skew, us./tick */
112int tickdelta; /* current clock skew, us. per tick */
113long timedelta; /* unapplied time correction, us. */
114long bigadj = 1000000; /* use 10x skew above bigadj us. */
115
3c7eb27c
DG
116struct adjtime_args {
117 struct timeval *delta;
118 struct timeval *olddelta;
119};
120
15637ed4
RG
121/* ARGSUSED */
122adjtime(p, uap, retval)
123 struct proc *p;
3c7eb27c 124 register struct adjtime_args *uap;
15637ed4
RG
125 int *retval;
126{
127 struct timeval atv, oatv;
128 register long ndelta;
129 int s, error;
130
131 if (error = suser(p->p_ucred, &p->p_acflag))
132 return (error);
133 if (error =
134 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
135 return (error);
136 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
137 if (timedelta == 0)
138 if (ndelta > bigadj)
139 tickdelta = 10 * tickadj;
140 else
141 tickdelta = tickadj;
142 if (ndelta % tickdelta)
143 ndelta = ndelta / tickadj * tickadj;
144
145 s = splclock();
146 if (uap->olddelta) {
147 oatv.tv_sec = timedelta / 1000000;
148 oatv.tv_usec = timedelta % 1000000;
149 }
150 timedelta = ndelta;
151 splx(s);
152
153 if (uap->olddelta)
154 (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
155 sizeof (struct timeval));
156 return (0);
157}
158
159/*
160 * Get value of an interval timer. The process virtual and
161 * profiling virtual time timers are kept in the p_stats area, since
162 * they can be swapped out. These are kept internally in the
163 * way they are specified externally: in time until they expire.
164 *
165 * The real time interval timer is kept in the process table slot
166 * for the process, and its value (it_value) is kept as an
167 * absolute time rather than as a delta, so that it is easy to keep
168 * periodic real-time signals from drifting.
169 *
170 * Virtual time timers are processed in the hardclock() routine of
171 * kern_clock.c. The real time timer is processed by a timeout
172 * routine, called from the softclock() routine. Since a callout
173 * may be delayed in real time due to interrupt processing in the system,
174 * it is possible for the real time timeout routine (realitexpire, given below),
175 * to be delayed in real time past when it is supposed to occur. It
176 * does not suffice, therefore, to reload the real timer .it_value from the
177 * real time timers .it_interval. Rather, we compute the next time in
178 * absolute time the timer should go off.
179 */
3c7eb27c
DG
180
181struct getitimer_args {
182 u_int which;
183 struct itimerval *itv;
184};
185
15637ed4
RG
186/* ARGSUSED */
187getitimer(p, uap, retval)
188 struct proc *p;
3c7eb27c 189 register struct getitimer_args *uap;
15637ed4
RG
190 int *retval;
191{
192 struct itimerval aitv;
193 int s;
194
195 if (uap->which > ITIMER_PROF)
196 return (EINVAL);
197 s = splclock();
198 if (uap->which == ITIMER_REAL) {
199 /*
200 * Convert from absoulte to relative time in .it_value
201 * part of real time timer. If time for real time timer
202 * has passed return 0, else return difference between
203 * current time and time for the timer to go off.
204 */
205 aitv = p->p_realtimer;
206 if (timerisset(&aitv.it_value))
207 if (timercmp(&aitv.it_value, &time, <))
208 timerclear(&aitv.it_value);
209 else
210 timevalsub(&aitv.it_value, &time);
211 } else
212 aitv = p->p_stats->p_timer[uap->which];
213 splx(s);
214 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
215 sizeof (struct itimerval)));
216}
217
3c7eb27c
DG
218struct setitimer_args {
219 u_int which;
220 struct itimerval *itv, *oitv;
221};
222
15637ed4
RG
223/* ARGSUSED */
224setitimer(p, uap, retval)
225 struct proc *p;
3c7eb27c 226 register struct setitimer_args *uap;
15637ed4
RG
227 int *retval;
228{
229 struct itimerval aitv;
230 register struct itimerval *itvp;
231 int s, error;
232
233 if (uap->which > ITIMER_PROF)
234 return (EINVAL);
235 itvp = uap->itv;
236 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
237 sizeof(struct itimerval))))
238 return (error);
239 if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
240 return (error);
241 if (itvp == 0)
242 return (0);
243 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
244 return (EINVAL);
245 s = splclock();
246 if (uap->which == ITIMER_REAL) {
247 untimeout(realitexpire, (caddr_t)p);
248 if (timerisset(&aitv.it_value)) {
249 timevaladd(&aitv.it_value, &time);
250 timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
251 }
252 p->p_realtimer = aitv;
253 } else
254 p->p_stats->p_timer[uap->which] = aitv;
255 splx(s);
256 return (0);
257}
258
259/*
260 * Real interval timer expired:
261 * send process whose timer expired an alarm signal.
262 * If time is not set up to reload, then just return.
263 * Else compute next time timer should go off which is > current time.
264 * This is where delay in processing this timeout causes multiple
265 * SIGALRM calls to be compressed into one.
266 */
267realitexpire(p)
268 register struct proc *p;
269{
270 int s;
271
272 psignal(p, SIGALRM);
273 if (!timerisset(&p->p_realtimer.it_interval)) {
274 timerclear(&p->p_realtimer.it_value);
275 return;
276 }
277 for (;;) {
278 s = splclock();
279 timevaladd(&p->p_realtimer.it_value,
280 &p->p_realtimer.it_interval);
281 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
282 timeout(realitexpire, (caddr_t)p,
283 hzto(&p->p_realtimer.it_value));
284 splx(s);
285 return;
286 }
287 splx(s);
288 }
289}
290
291/*
292 * Check that a proposed value to load into the .it_value or
293 * .it_interval part of an interval timer is acceptable, and
294 * fix it to have at least minimal value (i.e. if it is less
295 * than the resolution of the clock, round it up.)
296 */
297itimerfix(tv)
298 struct timeval *tv;
299{
300
301 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
302 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
303 return (EINVAL);
304 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
305 tv->tv_usec = tick;
306 return (0);
307}
308
309/*
310 * Decrement an interval timer by a specified number
311 * of microseconds, which must be less than a second,
312 * i.e. < 1000000. If the timer expires, then reload
313 * it. In this case, carry over (usec - old value) to
314 * reducint the value reloaded into the timer so that
315 * the timer does not drift. This routine assumes
316 * that it is called in a context where the timers
317 * on which it is operating cannot change in value.
318 */
319itimerdecr(itp, usec)
320 register struct itimerval *itp;
321 int usec;
322{
323
324 if (itp->it_value.tv_usec < usec) {
325 if (itp->it_value.tv_sec == 0) {
326 /* expired, and already in next interval */
327 usec -= itp->it_value.tv_usec;
328 goto expire;
329 }
330 itp->it_value.tv_usec += 1000000;
331 itp->it_value.tv_sec--;
332 }
333 itp->it_value.tv_usec -= usec;
334 usec = 0;
335 if (timerisset(&itp->it_value))
336 return (1);
337 /* expired, exactly at end of interval */
338expire:
339 if (timerisset(&itp->it_interval)) {
340 itp->it_value = itp->it_interval;
341 itp->it_value.tv_usec -= usec;
342 if (itp->it_value.tv_usec < 0) {
343 itp->it_value.tv_usec += 1000000;
344 itp->it_value.tv_sec--;
345 }
346 } else
347 itp->it_value.tv_usec = 0; /* sec is already 0 */
348 return (0);
349}
350
351/*
352 * Add and subtract routines for timevals.
353 * N.B.: subtract routine doesn't deal with
354 * results which are before the beginning,
355 * it just gets very confused in this case.
356 * Caveat emptor.
357 */
358timevaladd(t1, t2)
359 struct timeval *t1, *t2;
360{
361
362 t1->tv_sec += t2->tv_sec;
363 t1->tv_usec += t2->tv_usec;
364 timevalfix(t1);
365}
366
367timevalsub(t1, t2)
368 struct timeval *t1, *t2;
369{
370
371 t1->tv_sec -= t2->tv_sec;
372 t1->tv_usec -= t2->tv_usec;
373 timevalfix(t1);
374}
375
376timevalfix(t1)
377 struct timeval *t1;
378{
379
380 if (t1->tv_usec < 0) {
381 t1->tv_sec--;
382 t1->tv_usec += 1000000;
383 }
384 if (t1->tv_usec >= 1000000) {
385 t1->tv_sec++;
386 t1->tv_usec -= 1000000;
387 }
388}