da_time.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "da_time.h"
  17. uint32_t curr_sec_ms; /* millisecond of current second (0..999) */
  18. uint32_t ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */
  19. uint64_t now_ms; /* internal date in milliseconds (may wrap) */
  20. uint64_t now_us; /* internal date in us (may wrap) */
  21. uint32_t samp_time; /* total elapsed time over current sample */
  22. uint32_t idle_time; /* total idle time over current sample */
  23. uint32_t idle_pct; /* idle to total ratio over last sample (percent) */
  24. struct timeval now; /* internal date is a monotonic function of real clock */
  25. struct timeval date; /* the real current date */
  26. struct timeval start_date; /* the process's start date */
  27. struct timeval before_poll; /* system date before calling poll() */
  28. struct timeval after_poll; /* system date after leaving poll() */
  29. /*
  30. * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
  31. */
  32. REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
  33. {
  34. tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
  35. tv->tv_sec = from->tv_sec + (ms / 1000);
  36. while (tv->tv_usec >= 1000000) {
  37. tv->tv_usec -= 1000000;
  38. tv->tv_sec++;
  39. }
  40. return tv;
  41. }
  42. /*
  43. * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
  44. * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
  45. */
  46. REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
  47. {
  48. return __tv_ms_cmp(tv1, tv2);
  49. }
  50. /*
  51. * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
  52. * assuming that TV_ETERNITY is greater than everything.
  53. */
  54. REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
  55. {
  56. return __tv_ms_cmp2(tv1, tv2);
  57. }
  58. /*
  59. * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
  60. * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
  61. * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
  62. * occurrences of (tv_ms_cmp2(tv,now) <= 0).
  63. */
  64. REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
  65. {
  66. return __tv_ms_le2(tv1, tv2);
  67. }
  68. /*
  69. * returns the remaining time between tv1=now and event=tv2
  70. * if tv2 is passed, 0 is returned.
  71. * Must not be used when either argument is eternity.
  72. */
  73. REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
  74. {
  75. return __tv_ms_remain(tv1, tv2);
  76. }
  77. /*
  78. * returns the remaining time between tv1=now and event=tv2
  79. * if tv2 is passed, 0 is returned.
  80. * Returns TIME_ETERNITY if tv2 is eternity.
  81. */
  82. REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
  83. {
  84. if (tv_iseternity(tv2))
  85. return TIME_ETERNITY;
  86. return __tv_ms_remain(tv1, tv2);
  87. }
  88. /*
  89. * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
  90. * Must not be used when either argument is eternity.
  91. */
  92. REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
  93. {
  94. return __tv_ms_elapsed(tv1, tv2);
  95. }
  96. /*
  97. * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
  98. */
  99. REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
  100. {
  101. return __tv_add(tv, from, inc);
  102. }
  103. /*
  104. * If <inc> is set, then add it to <from> and set the result to <tv>, then
  105. * return 1, otherwise return 0. It is meant to be used in if conditions.
  106. */
  107. REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
  108. {
  109. return __tv_add_ifset(tv, from, inc);
  110. }
  111. /*
  112. * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
  113. * 0 is returned. The result is stored into tv.
  114. */
  115. REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
  116. {
  117. return __tv_remain(tv1, tv2, tv);
  118. }
  119. /*
  120. * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
  121. * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
  122. * eternity.
  123. */
  124. REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
  125. {
  126. return __tv_remain2(tv1, tv2, tv);
  127. }
  128. /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
  129. REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
  130. {
  131. return __tv_isle(tv1, tv2);
  132. }
  133. /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
  134. REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
  135. {
  136. return __tv_isgt(tv1, tv2);
  137. }
  138. /* tv_udpate_date: sets <date> to system time, and sets <now> to something as
  139. * close as possible to real time, following a monotonic function. The main
  140. * principle consists in detecting backwards and forwards time jumps and adjust
  141. * an offset to correct them. This function should be called once after each
  142. * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
  143. * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
  144. * value means that we have not expired the timeout). Calling it with (-1,*)
  145. * sets both <date> and <now> to current date, and calling it with (0,1) simply
  146. * updates the values.
  147. */
  148. REGPRM2 void tv_update_date(int max_wait, int interrupted)
  149. {
  150. static struct timeval tv_offset; /* warning: signed offset! */
  151. struct timeval adjusted, deadline;
  152. gettimeofday(&date, NULL);
  153. if (unlikely(max_wait < 0)) {
  154. tv_zero(&tv_offset);
  155. adjusted = date;
  156. after_poll = date;
  157. samp_time = idle_time = 0;
  158. idle_pct = 100;
  159. goto to_ms;
  160. }
  161. __tv_add(&adjusted, &date, &tv_offset);
  162. if (unlikely(__tv_islt(&adjusted, &now))) {
  163. goto fixup; /* jump in the past */
  164. }
  165. /* OK we did not jump backwards, let's see if we have jumped too far
  166. * forwards. The poll value was in <max_wait>, we accept that plus
  167. * MAX_DELAY_MS to cover additional time.
  168. */
  169. _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
  170. if (likely(__tv_islt(&adjusted, &deadline)))
  171. goto to_ms; /* OK time is within expected range */
  172. fixup:
  173. /* Large jump. If the poll was interrupted, we consider that the date
  174. * has not changed (immediate wake-up), otherwise we add the poll
  175. * time-out to the previous date. The new offset is recomputed.
  176. */
  177. _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait);
  178. tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec;
  179. tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec;
  180. if (tv_offset.tv_usec < 0) {
  181. tv_offset.tv_usec += 1000000;
  182. tv_offset.tv_sec--;
  183. }
  184. to_ms:
  185. now = adjusted;
  186. curr_sec_ms = now.tv_usec / 1000; /* ms of current second */
  187. /* For frequency counters, we'll need to know the ratio of the previous
  188. * value to add to current value depending on the current millisecond.
  189. * The principle is that during the first millisecond, we use 999/1000
  190. * of the past value and that during the last millisecond we use 0/1000
  191. * of the past value. In summary, we only use the past value during the
  192. * first 999 ms of a second, and the last ms is used to complete the
  193. * current measure. The value is scaled to (2^32-1) so that a simple
  194. * multiply followed by a shift gives us the final value.
  195. */
  196. ms_left_scaled = (999U - curr_sec_ms) * 4294967U;
  197. now_ms = now.tv_sec * 1000 + curr_sec_ms;
  198. now_us = now.tv_sec * 1000000+now.tv_usec;
  199. return;
  200. }
  201. /*
  202. * Local variables:
  203. * c-indent-level: 8
  204. * c-basic-offset: 8
  205. * End:
  206. */