The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_time.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1993
    3  *      The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)kern_time.c 8.1 (Berkeley) 6/10/93
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/5.4/sys/kern/kern_time.c 145335 2005-04-20 19:11:07Z cvs2svn $");
   34 
   35 #include "opt_mac.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/lock.h>
   40 #include <sys/mutex.h>
   41 #include <sys/sysproto.h>
   42 #include <sys/resourcevar.h>
   43 #include <sys/signalvar.h>
   44 #include <sys/kernel.h>
   45 #include <sys/mac.h>
   46 #include <sys/syscallsubr.h>
   47 #include <sys/sysent.h>
   48 #include <sys/proc.h>
   49 #include <sys/time.h>
   50 #include <sys/timetc.h>
   51 #include <sys/vnode.h>
   52 
   53 #include <vm/vm.h>
   54 #include <vm/vm_extern.h>
   55 
   56 int tz_minuteswest;
   57 int tz_dsttime;
   58 
   59 /*
   60  * Time of day and interval timer support.
   61  *
   62  * These routines provide the kernel entry points to get and set
   63  * the time-of-day and per-process interval timers.  Subroutines
   64  * here provide support for adding and subtracting timeval structures
   65  * and decrementing interval timers, optionally reloading the interval
   66  * timers when they expire.
   67  */
   68 
   69 static int      settime(struct thread *, struct timeval *);
   70 static void     timevalfix(struct timeval *);
   71 static void     no_lease_updatetime(int);
   72 
   73 static void 
   74 no_lease_updatetime(deltat)
   75         int deltat;
   76 {
   77 }
   78 
   79 void (*lease_updatetime)(int)  = no_lease_updatetime;
   80 
   81 static int
   82 settime(struct thread *td, struct timeval *tv)
   83 {
   84         struct timeval delta, tv1, tv2;
   85         static struct timeval maxtime, laststep;
   86         struct timespec ts;
   87         int s;
   88 
   89         s = splclock();
   90         microtime(&tv1);
   91         delta = *tv;
   92         timevalsub(&delta, &tv1);
   93 
   94         /*
   95          * If the system is secure, we do not allow the time to be 
   96          * set to a value earlier than 1 second less than the highest
   97          * time we have yet seen. The worst a miscreant can do in
   98          * this circumstance is "freeze" time. He couldn't go
   99          * back to the past.
  100          *
  101          * We similarly do not allow the clock to be stepped more
  102          * than one second, nor more than once per second. This allows
  103          * a miscreant to make the clock march double-time, but no worse.
  104          */
  105         if (securelevel_gt(td->td_ucred, 1) != 0) {
  106                 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
  107                         /*
  108                          * Update maxtime to latest time we've seen.
  109                          */
  110                         if (tv1.tv_sec > maxtime.tv_sec)
  111                                 maxtime = tv1;
  112                         tv2 = *tv;
  113                         timevalsub(&tv2, &maxtime);
  114                         if (tv2.tv_sec < -1) {
  115                                 tv->tv_sec = maxtime.tv_sec - 1;
  116                                 printf("Time adjustment clamped to -1 second\n");
  117                         }
  118                 } else {
  119                         if (tv1.tv_sec == laststep.tv_sec) {
  120                                 splx(s);
  121                                 return (EPERM);
  122                         }
  123                         if (delta.tv_sec > 1) {
  124                                 tv->tv_sec = tv1.tv_sec + 1;
  125                                 printf("Time adjustment clamped to +1 second\n");
  126                         }
  127                         laststep = *tv;
  128                 }
  129         }
  130 
  131         ts.tv_sec = tv->tv_sec;
  132         ts.tv_nsec = tv->tv_usec * 1000;
  133         mtx_lock(&Giant);
  134         tc_setclock(&ts);
  135         (void) splsoftclock();
  136         lease_updatetime(delta.tv_sec);
  137         splx(s);
  138         resettodr();
  139         mtx_unlock(&Giant);
  140         return (0);
  141 }
  142 
  143 #ifndef _SYS_SYSPROTO_H_
  144 struct clock_gettime_args {
  145         clockid_t clock_id;
  146         struct  timespec *tp;
  147 };
  148 #endif
  149 
  150 /*
  151  * MPSAFE
  152  */
  153 /* ARGSUSED */
  154 int
  155 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
  156 {
  157         struct timespec ats;
  158         struct timeval sys, user;
  159 
  160         switch (uap->clock_id) {
  161         case CLOCK_REALTIME:
  162                 nanotime(&ats);
  163                 break;
  164         case CLOCK_VIRTUAL:
  165                 mtx_lock_spin(&sched_lock);
  166                 calcru(td->td_proc, &user, &sys, NULL);
  167                 mtx_unlock_spin(&sched_lock);
  168                 TIMEVAL_TO_TIMESPEC(&user, &ats);
  169                 break;
  170         case CLOCK_PROF:
  171                 mtx_lock_spin(&sched_lock);
  172                 calcru(td->td_proc, &user, &sys, NULL);
  173                 mtx_unlock_spin(&sched_lock);
  174                 timevaladd(&user, &sys);
  175                 TIMEVAL_TO_TIMESPEC(&user, &ats);
  176                 break;
  177         case CLOCK_MONOTONIC:
  178                 nanouptime(&ats);
  179                 break;
  180         default:
  181                 return (EINVAL);
  182         }
  183         return (copyout(&ats, uap->tp, sizeof(ats)));
  184 }
  185 
  186 #ifndef _SYS_SYSPROTO_H_
  187 struct clock_settime_args {
  188         clockid_t clock_id;
  189         const struct    timespec *tp;
  190 };
  191 #endif
  192 
  193 /*
  194  * MPSAFE
  195  */
  196 /* ARGSUSED */
  197 int
  198 clock_settime(struct thread *td, struct clock_settime_args *uap)
  199 {
  200         struct timeval atv;
  201         struct timespec ats;
  202         int error;
  203 
  204 #ifdef MAC
  205         error = mac_check_system_settime(td->td_ucred);
  206         if (error)
  207                 return (error);
  208 #endif
  209         if ((error = suser(td)) != 0)
  210                 return (error);
  211         if (uap->clock_id != CLOCK_REALTIME)
  212                 return (EINVAL);
  213         if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
  214                 return (error);
  215         if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
  216                 return (EINVAL);
  217         /* XXX Don't convert nsec->usec and back */
  218         TIMESPEC_TO_TIMEVAL(&atv, &ats);
  219         error = settime(td, &atv);
  220         return (error);
  221 }
  222 
  223 #ifndef _SYS_SYSPROTO_H_
  224 struct clock_getres_args {
  225         clockid_t clock_id;
  226         struct  timespec *tp;
  227 };
  228 #endif
  229 
  230 int
  231 clock_getres(struct thread *td, struct clock_getres_args *uap)
  232 {
  233         struct timespec ts;
  234 
  235         ts.tv_sec = 0;
  236         switch (uap->clock_id) {
  237         case CLOCK_REALTIME:
  238         case CLOCK_MONOTONIC:
  239                 /*
  240                  * Round up the result of the division cheaply by adding 1.
  241                  * Rounding up is especially important if rounding down
  242                  * would give 0.  Perfect rounding is unimportant.
  243                  */
  244                 ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
  245                 break;
  246         case CLOCK_VIRTUAL:
  247         case CLOCK_PROF:
  248                 /* Accurately round up here because we can do so cheaply. */
  249                 ts.tv_nsec = (1000000000 + hz - 1) / hz;
  250                 break;
  251         default:
  252                 return (EINVAL);
  253         }
  254         if (uap->tp == NULL)
  255                 return (0);
  256         return (copyout(&ts, uap->tp, sizeof(ts)));
  257 }
  258 
  259 static int nanowait;
  260 
  261 int
  262 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
  263 {
  264         struct timespec ts, ts2, ts3;
  265         struct timeval tv;
  266         int error;
  267 
  268         if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
  269                 return (EINVAL);
  270         if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
  271                 return (0);
  272         getnanouptime(&ts);
  273         timespecadd(&ts, rqt);
  274         TIMESPEC_TO_TIMEVAL(&tv, rqt);
  275         for (;;) {
  276                 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
  277                     tvtohz(&tv));
  278                 getnanouptime(&ts2);
  279                 if (error != EWOULDBLOCK) {
  280                         if (error == ERESTART)
  281                                 error = EINTR;
  282                         if (rmt != NULL) {
  283                                 timespecsub(&ts, &ts2);
  284                                 if (ts.tv_sec < 0)
  285                                         timespecclear(&ts);
  286                                 *rmt = ts;
  287                         }
  288                         return (error);
  289                 }
  290                 if (timespeccmp(&ts2, &ts, >=))
  291                         return (0);
  292                 ts3 = ts;
  293                 timespecsub(&ts3, &ts2);
  294                 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
  295         }
  296 }
  297 
  298 #ifndef _SYS_SYSPROTO_H_
  299 struct nanosleep_args {
  300         struct  timespec *rqtp;
  301         struct  timespec *rmtp;
  302 };
  303 #endif
  304 
  305 /* 
  306  * MPSAFE
  307  */
  308 /* ARGSUSED */
  309 int
  310 nanosleep(struct thread *td, struct nanosleep_args *uap)
  311 {
  312         struct timespec rmt, rqt;
  313         int error;
  314 
  315         error = copyin(uap->rqtp, &rqt, sizeof(rqt));
  316         if (error)
  317                 return (error);
  318 
  319         if (uap->rmtp &&
  320             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
  321                         return (EFAULT);
  322         error = kern_nanosleep(td, &rqt, &rmt);
  323         if (error && uap->rmtp) {
  324                 int error2;
  325 
  326                 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
  327                 if (error2)
  328                         error = error2;
  329         }
  330         return (error);
  331 }
  332 
  333 #ifndef _SYS_SYSPROTO_H_
  334 struct gettimeofday_args {
  335         struct  timeval *tp;
  336         struct  timezone *tzp;
  337 };
  338 #endif
  339 /*
  340  * MPSAFE
  341  */
  342 /* ARGSUSED */
  343 int
  344 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
  345 {
  346         struct timeval atv;
  347         struct timezone rtz;
  348         int error = 0;
  349 
  350         if (uap->tp) {
  351                 microtime(&atv);
  352                 error = copyout(&atv, uap->tp, sizeof (atv));
  353         }
  354         if (error == 0 && uap->tzp != NULL) {
  355                 rtz.tz_minuteswest = tz_minuteswest;
  356                 rtz.tz_dsttime = tz_dsttime;
  357                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
  358         }
  359         return (error);
  360 }
  361 
  362 #ifndef _SYS_SYSPROTO_H_
  363 struct settimeofday_args {
  364         struct  timeval *tv;
  365         struct  timezone *tzp;
  366 };
  367 #endif
  368 /*
  369  * MPSAFE
  370  */
  371 /* ARGSUSED */
  372 int
  373 settimeofday(struct thread *td, struct settimeofday_args *uap)
  374 {
  375         struct timeval atv;
  376         struct timezone atz;
  377         int error = 0;
  378 
  379 #ifdef MAC
  380         error = mac_check_system_settime(td->td_ucred);
  381         if (error)
  382                 return (error);
  383 #endif
  384         if ((error = suser(td)))
  385                 return (error);
  386         /* Verify all parameters before changing time. */
  387         if (uap->tv) {
  388                 if ((error = copyin(uap->tv, &atv, sizeof(atv))))
  389                         return (error);
  390                 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
  391                         return (EINVAL);
  392         }
  393         if (uap->tzp &&
  394             (error = copyin(uap->tzp, &atz, sizeof(atz))))
  395                 return (error);
  396         
  397         if (uap->tv && (error = settime(td, &atv)))
  398                 return (error);
  399         if (uap->tzp) {
  400                 tz_minuteswest = atz.tz_minuteswest;
  401                 tz_dsttime = atz.tz_dsttime;
  402         }
  403         return (error);
  404 }
  405 /*
  406  * Get value of an interval timer.  The process virtual and
  407  * profiling virtual time timers are kept in the p_stats area, since
  408  * they can be swapped out.  These are kept internally in the
  409  * way they are specified externally: in time until they expire.
  410  *
  411  * The real time interval timer is kept in the process table slot
  412  * for the process, and its value (it_value) is kept as an
  413  * absolute time rather than as a delta, so that it is easy to keep
  414  * periodic real-time signals from drifting.
  415  *
  416  * Virtual time timers are processed in the hardclock() routine of
  417  * kern_clock.c.  The real time timer is processed by a timeout
  418  * routine, called from the softclock() routine.  Since a callout
  419  * may be delayed in real time due to interrupt processing in the system,
  420  * it is possible for the real time timeout routine (realitexpire, given below),
  421  * to be delayed in real time past when it is supposed to occur.  It
  422  * does not suffice, therefore, to reload the real timer .it_value from the
  423  * real time timers .it_interval.  Rather, we compute the next time in
  424  * absolute time the timer should go off.
  425  */
  426 #ifndef _SYS_SYSPROTO_H_
  427 struct getitimer_args {
  428         u_int   which;
  429         struct  itimerval *itv;
  430 };
  431 #endif
  432 /*
  433  * MPSAFE
  434  */
  435 int
  436 getitimer(struct thread *td, struct getitimer_args *uap)
  437 {
  438         struct proc *p = td->td_proc;
  439         struct timeval ctv;
  440         struct itimerval aitv;
  441 
  442         if (uap->which > ITIMER_PROF)
  443                 return (EINVAL);
  444 
  445         if (uap->which == ITIMER_REAL) {
  446                 /*
  447                  * Convert from absolute to relative time in .it_value
  448                  * part of real time timer.  If time for real time timer
  449                  * has passed return 0, else return difference between
  450                  * current time and time for the timer to go off.
  451                  */
  452                 PROC_LOCK(p);
  453                 aitv = p->p_realtimer;
  454                 PROC_UNLOCK(p);
  455                 if (timevalisset(&aitv.it_value)) {
  456                         getmicrouptime(&ctv);
  457                         if (timevalcmp(&aitv.it_value, &ctv, <))
  458                                 timevalclear(&aitv.it_value);
  459                         else
  460                                 timevalsub(&aitv.it_value, &ctv);
  461                 }
  462         } else {
  463                 mtx_lock_spin(&sched_lock);
  464                 aitv = p->p_stats->p_timer[uap->which];
  465                 mtx_unlock_spin(&sched_lock);
  466         }
  467         return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
  468 }
  469 
  470 #ifndef _SYS_SYSPROTO_H_
  471 struct setitimer_args {
  472         u_int   which;
  473         struct  itimerval *itv, *oitv;
  474 };
  475 #endif
  476 /*
  477  * MPSAFE
  478  */
  479 int
  480 setitimer(struct thread *td, struct setitimer_args *uap)
  481 {
  482         struct proc *p = td->td_proc;
  483         struct itimerval aitv, oitv;
  484         struct timeval ctv;
  485         int error;
  486 
  487         if (uap->itv == NULL) {
  488                 uap->itv = uap->oitv;
  489                 return (getitimer(td, (struct getitimer_args *)uap));
  490         }
  491 
  492         if (uap->which > ITIMER_PROF)
  493                 return (EINVAL);
  494         if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
  495                 return (error);
  496         if (itimerfix(&aitv.it_value))
  497                 return (EINVAL);
  498         if (!timevalisset(&aitv.it_value))
  499                 timevalclear(&aitv.it_interval);
  500         else if (itimerfix(&aitv.it_interval))
  501                 return (EINVAL);
  502 
  503         if (uap->which == ITIMER_REAL) {
  504                 PROC_LOCK(p);
  505                 if (timevalisset(&p->p_realtimer.it_value))
  506                         callout_stop(&p->p_itcallout);
  507                 getmicrouptime(&ctv);
  508                 if (timevalisset(&aitv.it_value)) {
  509                         callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
  510                             realitexpire, p);
  511                         timevaladd(&aitv.it_value, &ctv);
  512                 }
  513                 oitv = p->p_realtimer;
  514                 p->p_realtimer = aitv;
  515                 PROC_UNLOCK(p);
  516                 if (timevalisset(&oitv.it_value)) {
  517                         if (timevalcmp(&oitv.it_value, &ctv, <))
  518                                 timevalclear(&oitv.it_value);
  519                         else
  520                                 timevalsub(&oitv.it_value, &ctv);
  521                 }
  522         } else {
  523                 mtx_lock_spin(&sched_lock);
  524                 oitv = p->p_stats->p_timer[uap->which];
  525                 p->p_stats->p_timer[uap->which] = aitv;
  526                 mtx_unlock_spin(&sched_lock);
  527         }
  528         if (uap->oitv == NULL)
  529                 return (0);
  530         return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
  531 }
  532 
  533 /*
  534  * Real interval timer expired:
  535  * send process whose timer expired an alarm signal.
  536  * If time is not set up to reload, then just return.
  537  * Else compute next time timer should go off which is > current time.
  538  * This is where delay in processing this timeout causes multiple
  539  * SIGALRM calls to be compressed into one.
  540  * tvtohz() always adds 1 to allow for the time until the next clock
  541  * interrupt being strictly less than 1 clock tick, but we don't want
  542  * that here since we want to appear to be in sync with the clock
  543  * interrupt even when we're delayed.
  544  */
  545 void
  546 realitexpire(void *arg)
  547 {
  548         struct proc *p;
  549         struct timeval ctv, ntv;
  550 
  551         p = (struct proc *)arg;
  552         PROC_LOCK(p);
  553         psignal(p, SIGALRM);
  554         if (!timevalisset(&p->p_realtimer.it_interval)) {
  555                 timevalclear(&p->p_realtimer.it_value);
  556                 if (p->p_flag & P_WEXIT)
  557                         wakeup(&p->p_itcallout);
  558                 PROC_UNLOCK(p);
  559                 return;
  560         }
  561         for (;;) {
  562                 timevaladd(&p->p_realtimer.it_value,
  563                     &p->p_realtimer.it_interval);
  564                 getmicrouptime(&ctv);
  565                 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
  566                         ntv = p->p_realtimer.it_value;
  567                         timevalsub(&ntv, &ctv);
  568                         callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
  569                             realitexpire, p);
  570                         PROC_UNLOCK(p);
  571                         return;
  572                 }
  573         }
  574         /*NOTREACHED*/
  575 }
  576 
  577 /*
  578  * Check that a proposed value to load into the .it_value or
  579  * .it_interval part of an interval timer is acceptable, and
  580  * fix it to have at least minimal value (i.e. if it is less
  581  * than the resolution of the clock, round it up.)
  582  */
  583 int
  584 itimerfix(struct timeval *tv)
  585 {
  586 
  587         if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
  588             tv->tv_usec < 0 || tv->tv_usec >= 1000000)
  589                 return (EINVAL);
  590         if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
  591                 tv->tv_usec = tick;
  592         return (0);
  593 }
  594 
  595 /*
  596  * Decrement an interval timer by a specified number
  597  * of microseconds, which must be less than a second,
  598  * i.e. < 1000000.  If the timer expires, then reload
  599  * it.  In this case, carry over (usec - old value) to
  600  * reduce the value reloaded into the timer so that
  601  * the timer does not drift.  This routine assumes
  602  * that it is called in a context where the timers
  603  * on which it is operating cannot change in value.
  604  */
  605 int
  606 itimerdecr(struct itimerval *itp, int usec)
  607 {
  608 
  609         if (itp->it_value.tv_usec < usec) {
  610                 if (itp->it_value.tv_sec == 0) {
  611                         /* expired, and already in next interval */
  612                         usec -= itp->it_value.tv_usec;
  613                         goto expire;
  614                 }
  615                 itp->it_value.tv_usec += 1000000;
  616                 itp->it_value.tv_sec--;
  617         }
  618         itp->it_value.tv_usec -= usec;
  619         usec = 0;
  620         if (timevalisset(&itp->it_value))
  621                 return (1);
  622         /* expired, exactly at end of interval */
  623 expire:
  624         if (timevalisset(&itp->it_interval)) {
  625                 itp->it_value = itp->it_interval;
  626                 itp->it_value.tv_usec -= usec;
  627                 if (itp->it_value.tv_usec < 0) {
  628                         itp->it_value.tv_usec += 1000000;
  629                         itp->it_value.tv_sec--;
  630                 }
  631         } else
  632                 itp->it_value.tv_usec = 0;              /* sec is already 0 */
  633         return (0);
  634 }
  635 
  636 /*
  637  * Add and subtract routines for timevals.
  638  * N.B.: subtract routine doesn't deal with
  639  * results which are before the beginning,
  640  * it just gets very confused in this case.
  641  * Caveat emptor.
  642  */
  643 void
  644 timevaladd(struct timeval *t1, const struct timeval *t2)
  645 {
  646 
  647         t1->tv_sec += t2->tv_sec;
  648         t1->tv_usec += t2->tv_usec;
  649         timevalfix(t1);
  650 }
  651 
  652 void
  653 timevalsub(struct timeval *t1, const struct timeval *t2)
  654 {
  655 
  656         t1->tv_sec -= t2->tv_sec;
  657         t1->tv_usec -= t2->tv_usec;
  658         timevalfix(t1);
  659 }
  660 
  661 static void
  662 timevalfix(struct timeval *t1)
  663 {
  664 
  665         if (t1->tv_usec < 0) {
  666                 t1->tv_sec--;
  667                 t1->tv_usec += 1000000;
  668         }
  669         if (t1->tv_usec >= 1000000) {
  670                 t1->tv_sec++;
  671                 t1->tv_usec -= 1000000;
  672         }
  673 }
  674 
  675 /*
  676  * ratecheck(): simple time-based rate-limit checking.
  677  */
  678 int
  679 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
  680 {
  681         struct timeval tv, delta;
  682         int rv = 0;
  683 
  684         getmicrouptime(&tv);            /* NB: 10ms precision */
  685         delta = tv;
  686         timevalsub(&delta, lasttime);
  687 
  688         /*
  689          * check for 0,0 is so that the message will be seen at least once,
  690          * even if interval is huge.
  691          */
  692         if (timevalcmp(&delta, mininterval, >=) ||
  693             (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
  694                 *lasttime = tv;
  695                 rv = 1;
  696         }
  697 
  698         return (rv);
  699 }
  700 
  701 /*
  702  * ppsratecheck(): packets (or events) per second limitation.
  703  *
  704  * Return 0 if the limit is to be enforced (e.g. the caller
  705  * should drop a packet because of the rate limitation).
  706  *
  707  * maxpps of 0 always causes zero to be returned.  maxpps of -1
  708  * always causes 1 to be returned; this effectively defeats rate
  709  * limiting.
  710  *
  711  * Note that we maintain the struct timeval for compatibility
  712  * with other bsd systems.  We reuse the storage and just monitor
  713  * clock ticks for minimal overhead.  
  714  */
  715 int
  716 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
  717 {
  718         int now;
  719 
  720         /*
  721          * Reset the last time and counter if this is the first call
  722          * or more than a second has passed since the last update of
  723          * lasttime.
  724          */
  725         now = ticks;
  726         if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
  727                 lasttime->tv_sec = now;
  728                 *curpps = 1;
  729                 return (maxpps != 0);
  730         } else {
  731                 (*curpps)++;            /* NB: ignore potential overflow */
  732                 return (maxpps < 0 || *curpps < maxpps);
  733         }
  734 }

Cache object: 674e62dfa51b3883cab27b26e382bc12


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.