time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
  3. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  4. * Copyright (C) 2012-2014 Cisco Systems
  5. * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
  6. * Licensed under the GPL
  7. */
  8. #include <stddef.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <time.h>
  12. #include <sys/time.h>
  13. #include <kern_util.h>
  14. #include <os.h>
  15. #include <string.h>
  16. #include <timer-internal.h>
  17. static timer_t event_high_res_timer = 0;
  18. static inline long long timeval_to_ns(const struct timeval *tv)
  19. {
  20. return ((long long) tv->tv_sec * UM_NSEC_PER_SEC) +
  21. tv->tv_usec * UM_NSEC_PER_USEC;
  22. }
  23. static inline long long timespec_to_ns(const struct timespec *ts)
  24. {
  25. return ((long long) ts->tv_sec * UM_NSEC_PER_SEC) +
  26. ts->tv_nsec;
  27. }
  28. long long os_persistent_clock_emulation (void) {
  29. struct timespec realtime_tp;
  30. clock_gettime(CLOCK_REALTIME, &realtime_tp);
  31. return timespec_to_ns(&realtime_tp);
  32. }
  33. /**
  34. * os_timer_create() - create an new posix (interval) timer
  35. */
  36. int os_timer_create(void* timer) {
  37. timer_t* t = timer;
  38. if(t == NULL) {
  39. t = &event_high_res_timer;
  40. }
  41. if (timer_create(
  42. CLOCK_MONOTONIC,
  43. NULL,
  44. t) == -1) {
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. int os_timer_set_interval(void* timer, void* i)
  50. {
  51. struct itimerspec its;
  52. unsigned long long nsec;
  53. timer_t* t = timer;
  54. struct itimerspec* its_in = i;
  55. if(t == NULL) {
  56. t = &event_high_res_timer;
  57. }
  58. nsec = UM_NSEC_PER_SEC / UM_HZ;
  59. if(its_in != NULL) {
  60. its.it_value.tv_sec = its_in->it_value.tv_sec;
  61. its.it_value.tv_nsec = its_in->it_value.tv_nsec;
  62. } else {
  63. its.it_value.tv_sec = 0;
  64. its.it_value.tv_nsec = nsec;
  65. }
  66. its.it_interval.tv_sec = 0;
  67. its.it_interval.tv_nsec = nsec;
  68. if(timer_settime(*t, 0, &its, NULL) == -1) {
  69. return -errno;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * os_timer_remain() - returns the remaining nano seconds of the given interval
  75. * timer
  76. * Because this is the remaining time of an interval timer, which correspondends
  77. * to HZ, this value can never be bigger than one second. Just
  78. * the nanosecond part of the timer is returned.
  79. * The returned time is relative to the start time of the interval timer.
  80. * Return an negative value in an error case.
  81. */
  82. long os_timer_remain(void* timer)
  83. {
  84. struct itimerspec its;
  85. timer_t* t = timer;
  86. if(t == NULL) {
  87. t = &event_high_res_timer;
  88. }
  89. if(timer_gettime(t, &its) == -1) {
  90. return -errno;
  91. }
  92. return its.it_value.tv_nsec;
  93. }
  94. int os_timer_one_shot(int ticks)
  95. {
  96. struct itimerspec its;
  97. unsigned long long nsec;
  98. unsigned long sec;
  99. nsec = (ticks + 1);
  100. sec = nsec / UM_NSEC_PER_SEC;
  101. nsec = nsec % UM_NSEC_PER_SEC;
  102. its.it_value.tv_sec = nsec / UM_NSEC_PER_SEC;
  103. its.it_value.tv_nsec = nsec;
  104. its.it_interval.tv_sec = 0;
  105. its.it_interval.tv_nsec = 0; // we cheat here
  106. timer_settime(event_high_res_timer, 0, &its, NULL);
  107. return 0;
  108. }
  109. /**
  110. * os_timer_disable() - disable the posix (interval) timer
  111. * Returns the remaining interval timer time in nanoseconds
  112. */
  113. long long os_timer_disable(void)
  114. {
  115. struct itimerspec its;
  116. memset(&its, 0, sizeof(struct itimerspec));
  117. timer_settime(event_high_res_timer, 0, &its, &its);
  118. return its.it_value.tv_sec * UM_NSEC_PER_SEC + its.it_value.tv_nsec;
  119. }
  120. long long os_vnsecs(void)
  121. {
  122. struct timespec ts;
  123. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&ts);
  124. return timespec_to_ns(&ts);
  125. }
  126. long long os_nsecs(void)
  127. {
  128. struct timespec ts;
  129. clock_gettime(CLOCK_MONOTONIC,&ts);
  130. return timespec_to_ns(&ts);
  131. }
  132. /**
  133. * os_idle_sleep() - sleep for a given time of nsecs
  134. * @nsecs: nanoseconds to sleep
  135. */
  136. void os_idle_sleep(unsigned long long nsecs)
  137. {
  138. struct timespec ts;
  139. if (nsecs <= 0) {
  140. return;
  141. }
  142. ts = ((struct timespec) {
  143. .tv_sec = nsecs / UM_NSEC_PER_SEC,
  144. .tv_nsec = nsecs % UM_NSEC_PER_SEC
  145. });
  146. /*
  147. * Relay the signal if clock_nanosleep is interrupted.
  148. */
  149. if (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL)) {
  150. deliver_alarm();
  151. }
  152. }