linux-bk/include/linux/time.h
<<
>>
Prefs
   1#ifndef _LINUX_TIME_H
   2#define _LINUX_TIME_H
   3
   4#include <asm/param.h>
   5#include <linux/types.h>
   6
   7#ifndef _STRUCT_TIMESPEC
   8#define _STRUCT_TIMESPEC
   9struct timespec {
  10        time_t  tv_sec;         /* seconds */
  11        long    tv_nsec;        /* nanoseconds */
  12};
  13#endif /* _STRUCT_TIMESPEC */
  14
  15struct timeval {
  16        time_t          tv_sec;         /* seconds */
  17        suseconds_t     tv_usec;        /* microseconds */
  18};
  19
  20struct timezone {
  21        int     tz_minuteswest; /* minutes west of Greenwich */
  22        int     tz_dsttime;     /* type of dst correction */
  23};
  24
  25#ifdef __KERNEL__
  26
  27#include <linux/spinlock.h>
  28#include <linux/seqlock.h>
  29#include <linux/timex.h>
  30#include <asm/div64.h>
  31#ifndef div_long_long_rem
  32
  33#define div_long_long_rem(dividend,divisor,remainder) ({ \
  34                       u64 result = dividend;           \
  35                       *remainder = do_div(result,divisor); \
  36                       result; })
  37
  38#endif
  39
  40/*
  41 * Have the 32 bit jiffies value wrap 5 minutes after boot
  42 * so jiffies wrap bugs show up earlier.
  43 */
  44#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
  45
  46/*
  47 * Change timeval to jiffies, trying to avoid the
  48 * most obvious overflows..
  49 *
  50 * And some not so obvious.
  51 *
  52 * Note that we don't want to return MAX_LONG, because
  53 * for various timeout reasons we often end up having
  54 * to wait "jiffies+1" in order to guarantee that we wait
  55 * at _least_ "jiffies" - so "jiffies+1" had better still
  56 * be positive.
  57 */
  58#define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
  59
  60/* Parameters used to convert the timespec values */
  61#ifndef USEC_PER_SEC
  62#define USEC_PER_SEC (1000000L)
  63#endif
  64
  65#ifndef NSEC_PER_SEC
  66#define NSEC_PER_SEC (1000000000L)
  67#endif
  68
  69#ifndef NSEC_PER_USEC
  70#define NSEC_PER_USEC (1000L)
  71#endif
  72
  73/*
  74 * We want to do realistic conversions of time so we need to use the same
  75 * values the update wall clock code uses as the jiffies size.  This value
  76 * is: TICK_NSEC (which is defined in timex.h).  This
  77 * is a constant and is in nanoseconds.  We will used scaled math
  78 * with a set of scales defined here as SEC_JIFFIE_SC,  USEC_JIFFIE_SC and
  79 * NSEC_JIFFIE_SC.  Note that these defines contain nothing but
  80 * constants and so are computed at compile time.  SHIFT_HZ (computed in
  81 * timex.h) adjusts the scaling for different HZ values.
  82
  83 * Scaled math???  What is that?
  84 *
  85 * Scaled math is a way to do integer math on values that would,
  86 * otherwise, either overflow, underflow, or cause undesired div
  87 * instructions to appear in the execution path.  In short, we "scale"
  88 * up the operands so they take more bits (more precision, less
  89 * underflow), do the desired operation and then "scale" the result back
  90 * by the same amount.  If we do the scaling by shifting we avoid the
  91 * costly mpy and the dastardly div instructions.
  92
  93 * Suppose, for example, we want to convert from seconds to jiffies
  94 * where jiffies is defined in nanoseconds as NSEC_PER_JIFFIE.  The
  95 * simple math is: jiff = (sec * NSEC_PER_SEC) / NSEC_PER_JIFFIE; We
  96 * observe that (NSEC_PER_SEC / NSEC_PER_JIFFIE) is a constant which we
  97 * might calculate at compile time, however, the result will only have
  98 * about 3-4 bits of precision (less for smaller values of HZ).
  99 *
 100 * So, we scale as follows:
 101 * jiff = (sec) * (NSEC_PER_SEC / NSEC_PER_JIFFIE);
 102 * jiff = ((sec) * ((NSEC_PER_SEC * SCALE)/ NSEC_PER_JIFFIE)) / SCALE;
 103 * Then we make SCALE a power of two so:
 104 * jiff = ((sec) * ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) >> SCALE;
 105 * Now we define:
 106 * #define SEC_CONV = ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE))
 107 * jiff = (sec * SEC_CONV) >> SCALE;
 108 *
 109 * Often the math we use will expand beyond 32-bits so we tell C how to
 110 * do this and pass the 64-bit result of the mpy through the ">> SCALE"
 111 * which should take the result back to 32-bits.  We want this expansion
 112 * to capture as much precision as possible.  At the same time we don't
 113 * want to overflow so we pick the SCALE to avoid this.  In this file,
 114 * that means using a different scale for each range of HZ values (as
 115 * defined in timex.h).
 116 *
 117 * For those who want to know, gcc will give a 64-bit result from a "*"
 118 * operator if the result is a long long AND at least one of the
 119 * operands is cast to long long (usually just prior to the "*" so as
 120 * not to confuse it into thinking it really has a 64-bit operand,
 121 * which, buy the way, it can do, but it take more code and at least 2
 122 * mpys).
 123
 124 * We also need to be aware that one second in nanoseconds is only a
 125 * couple of bits away from overflowing a 32-bit word, so we MUST use
 126 * 64-bits to get the full range time in nanoseconds.
 127
 128 */
 129
 130/*
 131 * Here are the scales we will use.  One for seconds, nanoseconds and
 132 * microseconds.
 133 *
 134 * Within the limits of cpp we do a rough cut at the SEC_JIFFIE_SC and
 135 * check if the sign bit is set.  If not, we bump the shift count by 1.
 136 * (Gets an extra bit of precision where we can use it.)
 137 * We know it is set for HZ = 1024 and HZ = 100 not for 1000.
 138 * Haven't tested others.
 139
 140 * Limits of cpp (for #if expressions) only long (no long long), but
 141 * then we only need the most signicant bit.
 142 */
 143
 144#define SEC_JIFFIE_SC (31 - SHIFT_HZ)
 145#if !((((NSEC_PER_SEC << 2) / TICK_NSEC) << (SEC_JIFFIE_SC - 2)) & 0x80000000)
 146#undef SEC_JIFFIE_SC
 147#define SEC_JIFFIE_SC (32 - SHIFT_HZ)
 148#endif
 149#define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29)
 150#define USEC_JIFFIE_SC (SEC_JIFFIE_SC + 19)
 151#define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC) +\
 152                                TICK_NSEC -1) / (u64)TICK_NSEC))
 153
 154#define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\
 155                                        TICK_NSEC -1) / (u64)TICK_NSEC))
 156#define USEC_CONVERSION  \
 157                    ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC) +\
 158                                        TICK_NSEC -1) / (u64)TICK_NSEC))
 159/*
 160 * USEC_ROUND is used in the timeval to jiffie conversion.  See there
 161 * for more details.  It is the scaled resolution rounding value.  Note
 162 * that it is a 64-bit value.  Since, when it is applied, we are already
 163 * in jiffies (albit scaled), it is nothing but the bits we will shift
 164 * off.
 165 */
 166#define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1)
 167/*
 168 * The maximum jiffie value is (MAX_INT >> 1).  Here we translate that
 169 * into seconds.  The 64-bit case will overflow if we are not careful,
 170 * so use the messy SH_DIV macro to do it.  Still all constants.
 171 */
 172#if BITS_PER_LONG < 64
 173# define MAX_SEC_IN_JIFFIES \
 174        (long)((u64)((u64)MAX_JIFFY_OFFSET * TICK_NSEC) / NSEC_PER_SEC)
 175#else   /* take care of overflow on 64 bits machines */
 176# define MAX_SEC_IN_JIFFIES \
 177        (SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1)
 178
 179#endif
 180
 181/*
 182 * Convert jiffies to milliseconds and back.
 183 *
 184 * Avoid unnecessary multiplications/divisions in the
 185 * two most common HZ cases:
 186 */
 187static inline unsigned int jiffies_to_msecs(const unsigned long j)
 188{
 189#if HZ <= 1000 && !(1000 % HZ)
 190        return (1000 / HZ) * j;
 191#elif HZ > 1000 && !(HZ % 1000)
 192        return (j + (HZ / 1000) - 1)/(HZ / 1000);
 193#else
 194        return (j * 1000) / HZ;
 195#endif
 196}
 197static inline unsigned long msecs_to_jiffies(const unsigned int m)
 198{
 199#if HZ <= 1000 && !(1000 % HZ)
 200        return (m + (1000 / HZ) - 1) / (1000 / HZ);
 201#elif HZ > 1000 && !(HZ % 1000)
 202        return m * (HZ / 1000);
 203#else
 204        return (m * HZ + 999) / 1000;
 205#endif
 206}
 207
 208/*
 209 * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
 210 * that a remainder subtract here would not do the right thing as the
 211 * resolution values don't fall on second boundries.  I.e. the line:
 212 * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
 213 *
 214 * Rather, we just shift the bits off the right.
 215 *
 216 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
 217 * value to a scaled second value.
 218 */
 219static __inline__ unsigned long
 220timespec_to_jiffies(const struct timespec *value)
 221{
 222        unsigned long sec = value->tv_sec;
 223        long nsec = value->tv_nsec + TICK_NSEC - 1;
 224
 225        if (sec >= MAX_SEC_IN_JIFFIES){
 226                sec = MAX_SEC_IN_JIFFIES;
 227                nsec = 0;
 228        }
 229        return (((u64)sec * SEC_CONVERSION) +
 230                (((u64)nsec * NSEC_CONVERSION) >>
 231                 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
 232
 233}
 234
 235static __inline__ void
 236jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
 237{
 238        /*
 239         * Convert jiffies to nanoseconds and separate with
 240         * one divide.
 241         */
 242        u64 nsec = (u64)jiffies * TICK_NSEC; 
 243        value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec);
 244}
 245
 246/* Same for "timeval"
 247 *
 248 * Well, almost.  The problem here is that the real system resolution is
 249 * in nanoseconds and the value being converted is in micro seconds.
 250 * Also for some machines (those that use HZ = 1024, in-particular),
 251 * there is a LARGE error in the tick size in microseconds.
 252
 253 * The solution we use is to do the rounding AFTER we convert the
 254 * microsecond part.  Thus the USEC_ROUND, the bits to be shifted off.
 255 * Instruction wise, this should cost only an additional add with carry
 256 * instruction above the way it was done above.
 257 */
 258static __inline__ unsigned long
 259timeval_to_jiffies(const struct timeval *value)
 260{
 261        unsigned long sec = value->tv_sec;
 262        long usec = value->tv_usec;
 263
 264        if (sec >= MAX_SEC_IN_JIFFIES){
 265                sec = MAX_SEC_IN_JIFFIES;
 266                usec = 0;
 267        }
 268        return (((u64)sec * SEC_CONVERSION) +
 269                (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
 270                 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
 271}
 272
 273static __inline__ void
 274jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
 275{
 276        /*
 277         * Convert jiffies to nanoseconds and separate with
 278         * one divide.
 279         */
 280        u64 nsec = (u64)jiffies * TICK_NSEC; 
 281        value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_usec);
 282        value->tv_usec /= NSEC_PER_USEC;
 283}
 284
 285static __inline__ int timespec_equal(struct timespec *a, struct timespec *b) 
 286{ 
 287        return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
 288} 
 289
 290/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
 291 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
 292 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
 293 *
 294 * [For the Julian calendar (which was used in Russia before 1917,
 295 * Britain & colonies before 1752, anywhere else before 1582,
 296 * and is still in use by some communities) leave out the
 297 * -year/100+year/400 terms, and add 10.]
 298 *
 299 * This algorithm was first published by Gauss (I think).
 300 *
 301 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
 302 * machines were long is 32-bit! (However, as time_t is signed, we
 303 * will already get problems at other places on 2038-01-19 03:14:08)
 304 */
 305static inline unsigned long
 306mktime (unsigned int year, unsigned int mon,
 307        unsigned int day, unsigned int hour,
 308        unsigned int min, unsigned int sec)
 309{
 310        if (0 >= (int) (mon -= 2)) {    /* 1..12 -> 11,12,1..10 */
 311                mon += 12;              /* Puts Feb last since it has leap day */
 312                year -= 1;
 313        }
 314
 315        return (((
 316                (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
 317                        year*365 - 719499
 318            )*24 + hour /* now have hours */
 319          )*60 + min /* now have minutes */
 320        )*60 + sec; /* finally seconds */
 321}
 322
 323extern struct timespec xtime;
 324extern struct timespec wall_to_monotonic;
 325extern seqlock_t xtime_lock;
 326
 327static inline unsigned long get_seconds(void)
 328{ 
 329        return xtime.tv_sec;
 330}
 331
 332struct timespec current_kernel_time(void);
 333
 334#define CURRENT_TIME (current_kernel_time())
 335
 336#endif /* __KERNEL__ */
 337
 338#define NFDBITS                 __NFDBITS
 339
 340#ifdef __KERNEL__
 341extern void do_gettimeofday(struct timeval *tv);
 342extern int do_settimeofday(struct timespec *tv);
 343extern int do_sys_settimeofday(struct timespec *tv, struct timezone *tz);
 344extern void clock_was_set(void); // call when ever the clock is set
 345extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
 346extern long do_nanosleep(struct timespec *t);
 347extern long do_utimes(char __user * filename, struct timeval * times);
 348struct itimerval;
 349extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);
 350extern int do_getitimer(int which, struct itimerval *value);
 351
 352static inline void
 353set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
 354{
 355        while (nsec > NSEC_PER_SEC) {
 356                nsec -= NSEC_PER_SEC;
 357                ++sec;
 358        }
 359        while (nsec < 0) {
 360                nsec += NSEC_PER_SEC;
 361                --sec;
 362        }
 363        ts->tv_sec = sec;
 364        ts->tv_nsec = nsec;
 365}
 366#endif
 367
 368#define FD_SETSIZE              __FD_SETSIZE
 369#define FD_SET(fd,fdsetp)       __FD_SET(fd,fdsetp)
 370#define FD_CLR(fd,fdsetp)       __FD_CLR(fd,fdsetp)
 371#define FD_ISSET(fd,fdsetp)     __FD_ISSET(fd,fdsetp)
 372#define FD_ZERO(fdsetp)         __FD_ZERO(fdsetp)
 373
 374/*
 375 * Names of the interval timers, and structure
 376 * defining a timer setting.
 377 */
 378#define ITIMER_REAL     0
 379#define ITIMER_VIRTUAL  1
 380#define ITIMER_PROF     2
 381
 382struct  itimerspec {
 383        struct  timespec it_interval;    /* timer period */
 384        struct  timespec it_value;       /* timer expiration */
 385};
 386
 387struct  itimerval {
 388        struct  timeval it_interval;    /* timer interval */
 389        struct  timeval it_value;       /* current value */
 390};
 391
 392
 393/*
 394 * The IDs of the various system clocks (for POSIX.1b interval timers).
 395 */
 396#define CLOCK_REALTIME            0
 397#define CLOCK_MONOTONIC   1
 398#define CLOCK_PROCESS_CPUTIME_ID 2
 399#define CLOCK_THREAD_CPUTIME_ID  3
 400#define CLOCK_REALTIME_HR        4
 401#define CLOCK_MONOTONIC_HR        5
 402
 403#define MAX_CLOCKS 6
 404#define CLOCKS_MASK  (CLOCK_REALTIME | CLOCK_MONOTONIC | \
 405                     CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR)
 406#define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR)
 407
 408/*
 409 * The various flags for setting POSIX.1b interval timers.
 410 */
 411
 412#define TIMER_ABSTIME 0x01
 413
 414
 415#endif
 416
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.