time.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #ifndef _LINUX_TIME_H
  2. #define _LINUX_TIME_H
  3. #include <linux/types.h>
  4. #ifdef __KERNEL__
  5. # include <linux/bitops.h>
  6. # include <linux/cache.h>
  7. # include <linux/posix_types.h>
  8. # include <linux/seqlock.h>
  9. # include <linux/string.h>
  10. # include <linux/math64.h>
  11. #endif
  12. #ifndef _STRUCT_TIMESPEC
  13. #define _STRUCT_TIMESPEC
  14. struct timespec {
  15. __kernel_time_t tv_sec; /* seconds */
  16. long tv_nsec; /* nanoseconds */
  17. };
  18. #endif
  19. struct timeval {
  20. __kernel_time_t tv_sec; /* seconds */
  21. __kernel_suseconds_t tv_usec; /* microseconds */
  22. };
  23. struct timezone {
  24. int tz_minuteswest; /* minutes west of Greenwich */
  25. int tz_dsttime; /* type of dst correction */
  26. };
  27. #ifdef __KERNEL__
  28. extern struct timezone sys_tz;
  29. /* Parameters used to convert the timespec values: */
  30. #define MSEC_PER_SEC 1000L
  31. #define USEC_PER_MSEC 1000L
  32. #define NSEC_PER_USEC 1000L
  33. #define NSEC_PER_MSEC 1000000L
  34. #define USEC_PER_SEC 1000000L
  35. #define NSEC_PER_SEC 1000000000L
  36. #define FSEC_PER_SEC 1000000000000000LL
  37. #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
  38. static inline int timespec_equal(const struct timespec *a,
  39. const struct timespec *b)
  40. {
  41. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  42. }
  43. /*
  44. * lhs < rhs: return <0
  45. * lhs == rhs: return 0
  46. * lhs > rhs: return >0
  47. */
  48. static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
  49. {
  50. if (lhs->tv_sec < rhs->tv_sec)
  51. return -1;
  52. if (lhs->tv_sec > rhs->tv_sec)
  53. return 1;
  54. return lhs->tv_nsec - rhs->tv_nsec;
  55. }
  56. static inline int timeval_compare(const struct timeval *lhs, const struct timeval *rhs)
  57. {
  58. if (lhs->tv_sec < rhs->tv_sec)
  59. return -1;
  60. if (lhs->tv_sec > rhs->tv_sec)
  61. return 1;
  62. return lhs->tv_usec - rhs->tv_usec;
  63. }
  64. extern unsigned long mktime(const unsigned int year, const unsigned int mon,
  65. const unsigned int day, const unsigned int hour,
  66. const unsigned int min, const unsigned int sec);
  67. extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
  68. /*
  69. * timespec_add_safe assumes both values are positive and checks
  70. * for overflow. It will return TIME_T_MAX if the reutrn would be
  71. * smaller then either of the arguments.
  72. */
  73. extern struct timespec timespec_add_safe(const struct timespec lhs,
  74. const struct timespec rhs);
  75. static inline struct timespec timespec_add(struct timespec lhs,
  76. struct timespec rhs)
  77. {
  78. struct timespec ts_delta;
  79. set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
  80. lhs.tv_nsec + rhs.tv_nsec);
  81. return ts_delta;
  82. }
  83. /*
  84. * sub = lhs - rhs, in normalized form
  85. */
  86. static inline struct timespec timespec_sub(struct timespec lhs,
  87. struct timespec rhs)
  88. {
  89. struct timespec ts_delta;
  90. set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
  91. lhs.tv_nsec - rhs.tv_nsec);
  92. return ts_delta;
  93. }
  94. /*
  95. * Returns true if the timespec is norm, false if denorm:
  96. */
  97. #define timespec_valid(ts) \
  98. (((ts)->tv_sec >= 0) && (((unsigned long) (ts)->tv_nsec) < NSEC_PER_SEC))
  99. extern void read_persistent_clock(struct timespec *ts);
  100. extern void read_boot_clock(struct timespec *ts);
  101. extern int update_persistent_clock(struct timespec now);
  102. extern int no_sync_cmos_clock __read_mostly;
  103. void timekeeping_init(void);
  104. extern int timekeeping_suspended;
  105. unsigned long get_seconds(void);
  106. struct timespec current_kernel_time(void);
  107. struct timespec __current_kernel_time(void); /* does not take xtime_lock */
  108. struct timespec get_monotonic_coarse(void);
  109. void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
  110. struct timespec *wtom, struct timespec *sleep);
  111. void timekeeping_inject_sleeptime(struct timespec *delta);
  112. #define CURRENT_TIME (current_kernel_time())
  113. #define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
  114. /* Some architectures do not supply their own clocksource.
  115. * This is mainly the case in architectures that get their
  116. * inter-tick times by reading the counter on their interval
  117. * timer. Since these timers wrap every tick, they're not really
  118. * useful as clocksources. Wrapping them to act like one is possible
  119. * but not very efficient. So we provide a callout these arches
  120. * can implement for use with the jiffies clocksource to provide
  121. * finer then tick granular time.
  122. */
  123. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  124. extern u32 arch_gettimeoffset(void);
  125. #else
  126. static inline u32 arch_gettimeoffset(void) { return 0; }
  127. #endif
  128. extern void do_gettimeofday(struct timeval *tv);
  129. extern int do_settimeofday(const struct timespec *tv);
  130. extern int do_sys_settimeofday(const struct timespec *tv,
  131. const struct timezone *tz);
  132. #define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
  133. extern long do_utimes(int dfd, const char __user *filename, struct timespec *times, int flags);
  134. struct itimerval;
  135. extern int do_setitimer(int which, struct itimerval *value,
  136. struct itimerval *ovalue);
  137. extern unsigned int alarm_setitimer(unsigned int seconds);
  138. extern int do_getitimer(int which, struct itimerval *value);
  139. extern void getnstimeofday(struct timespec *tv);
  140. extern void getrawmonotonic(struct timespec *ts);
  141. extern void getnstime_raw_and_real(struct timespec *ts_raw,
  142. struct timespec *ts_real);
  143. extern void getboottime(struct timespec *ts);
  144. extern void monotonic_to_bootbased(struct timespec *ts);
  145. extern void get_monotonic_boottime(struct timespec *ts);
  146. extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
  147. extern int timekeeping_valid_for_hres(void);
  148. extern u64 timekeeping_max_deferment(void);
  149. extern void timekeeping_leap_insert(int leapsecond);
  150. extern int timekeeping_inject_offset(struct timespec *ts);
  151. struct tms;
  152. extern void do_sys_times(struct tms *);
  153. /*
  154. * Similar to the struct tm in userspace <time.h>, but it needs to be here so
  155. * that the kernel source is self contained.
  156. */
  157. struct tm {
  158. /*
  159. * the number of seconds after the minute, normally in the range
  160. * 0 to 59, but can be up to 60 to allow for leap seconds
  161. */
  162. int tm_sec;
  163. /* the number of minutes after the hour, in the range 0 to 59*/
  164. int tm_min;
  165. /* the number of hours past midnight, in the range 0 to 23 */
  166. int tm_hour;
  167. /* the day of the month, in the range 1 to 31 */
  168. int tm_mday;
  169. /* the number of months since January, in the range 0 to 11 */
  170. int tm_mon;
  171. /* the number of years since 1900 */
  172. long tm_year;
  173. /* the number of days since Sunday, in the range 0 to 6 */
  174. int tm_wday;
  175. /* the number of days since January 1, in the range 0 to 365 */
  176. int tm_yday;
  177. };
  178. void time_to_tm(time_t totalsecs, int offset, struct tm *result);
  179. /**
  180. * timespec_to_ns - Convert timespec to nanoseconds
  181. * @ts: pointer to the timespec variable to be converted
  182. *
  183. * Returns the scalar nanosecond representation of the timespec
  184. * parameter.
  185. */
  186. static inline s64 timespec_to_ns(const struct timespec *ts)
  187. {
  188. return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  189. }
  190. /**
  191. * timeval_to_ns - Convert timeval to nanoseconds
  192. * @ts: pointer to the timeval variable to be converted
  193. *
  194. * Returns the scalar nanosecond representation of the timeval
  195. * parameter.
  196. */
  197. static inline s64 timeval_to_ns(const struct timeval *tv)
  198. {
  199. return ((s64) tv->tv_sec * NSEC_PER_SEC) +
  200. tv->tv_usec * NSEC_PER_USEC;
  201. }
  202. /**
  203. * ns_to_timespec - Convert nanoseconds to timespec
  204. * @nsec: the nanoseconds value to be converted
  205. *
  206. * Returns the timespec representation of the nsec parameter.
  207. */
  208. extern struct timespec ns_to_timespec(const s64 nsec);
  209. /**
  210. * ns_to_timeval - Convert nanoseconds to timeval
  211. * @nsec: the nanoseconds value to be converted
  212. *
  213. * Returns the timeval representation of the nsec parameter.
  214. */
  215. extern struct timeval ns_to_timeval(const s64 nsec);
  216. /**
  217. * timespec_add_ns - Adds nanoseconds to a timespec
  218. * @a: pointer to timespec to be incremented
  219. * @ns: unsigned nanoseconds value to be added
  220. *
  221. * This must always be inlined because its used from the x86-64 vdso,
  222. * which cannot call other kernel functions.
  223. */
  224. static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
  225. {
  226. a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
  227. a->tv_nsec = ns;
  228. }
  229. static inline void __FD_SET(unsigned long __fd, __kernel_fd_set *__fdsetp)
  230. {
  231. __set_bit(__fd, __fdsetp->fds_bits);
  232. }
  233. static inline void __FD_CLR(unsigned long __fd, __kernel_fd_set *__fdsetp)
  234. {
  235. __clear_bit(__fd, __fdsetp->fds_bits);
  236. }
  237. static inline int __FD_ISSET(unsigned long __fd, const __kernel_fd_set *__fdsetp)
  238. {
  239. return test_bit(__fd, __fdsetp->fds_bits);
  240. }
  241. static inline void __FD_ZERO(__kernel_fd_set *__fdsetp)
  242. {
  243. memset(__fdsetp->fds_bits, 0, sizeof __fdsetp->fds_bits);
  244. }
  245. #endif /* __KERNEL__ */
  246. #define NFDBITS __NFDBITS
  247. #define FD_SETSIZE __FD_SETSIZE
  248. #define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp)
  249. #define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp)
  250. #define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp)
  251. #define FD_ZERO(fdsetp) __FD_ZERO(fdsetp)
  252. /*
  253. * Names of the interval timers, and structure
  254. * defining a timer setting:
  255. */
  256. #define ITIMER_REAL 0
  257. #define ITIMER_VIRTUAL 1
  258. #define ITIMER_PROF 2
  259. struct itimerspec {
  260. struct timespec it_interval; /* timer period */
  261. struct timespec it_value; /* timer expiration */
  262. };
  263. struct itimerval {
  264. struct timeval it_interval; /* timer interval */
  265. struct timeval it_value; /* current value */
  266. };
  267. /*
  268. * The IDs of the various system clocks (for POSIX.1b interval timers):
  269. */
  270. #define CLOCK_REALTIME 0
  271. #define CLOCK_MONOTONIC 1
  272. #define CLOCK_PROCESS_CPUTIME_ID 2
  273. #define CLOCK_THREAD_CPUTIME_ID 3
  274. #define CLOCK_MONOTONIC_RAW 4
  275. #define CLOCK_REALTIME_COARSE 5
  276. #define CLOCK_MONOTONIC_COARSE 6
  277. #define CLOCK_BOOTTIME 7
  278. #define CLOCK_REALTIME_ALARM 8
  279. #define CLOCK_BOOTTIME_ALARM 9
  280. /*
  281. * The IDs of various hardware clocks:
  282. */
  283. #define CLOCK_SGI_CYCLE 10
  284. #define MAX_CLOCKS 16
  285. #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC)
  286. #define CLOCKS_MONO CLOCK_MONOTONIC
  287. /*
  288. * The various flags for setting POSIX.1b interval timers:
  289. */
  290. #define TIMER_ABSTIME 0x01
  291. #endif