/storage/ndb/test/crund/martins_little_helpers/src/utils/hrt_utils.c

https://github.com/alibaba/AliSQL · C · 228 lines · 183 code · 20 blank · 25 comment · 22 complexity · 4a8222af3ec926da939026f011c2ef0a MD5 · raw file

  1. /*
  2. Copyright 2010 Sun Microsystems, Inc.
  3. All rights reserved. Use is subject to license terms.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. /*
  16. * hrt_utils.c
  17. *
  18. */
  19. #include <hrt_utils.h>
  20. #include <assert.h>
  21. /*
  22. * High-Resolution Time Utilities -- Implementation
  23. */
  24. int
  25. hrt_rtnow(hrt_rtstamp* x)
  26. {
  27. int ret;
  28. #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  29. # if (defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK+0 >= 0))
  30. # define REAL_CLOCK_TYPE CLOCK_MONOTONIC
  31. # else
  32. # define REAL_CLOCK_TYPE CLOCK_REALTIME
  33. # endif
  34. ret = clock_gettime(REAL_CLOCK_TYPE, &x->time);
  35. #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
  36. ret = gettimeofday(&x->time, NULL);
  37. #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
  38. {
  39. clock_t r = times(NULL);
  40. if (r == -1) {
  41. ret = r;
  42. } else {
  43. ret = 0;
  44. x->time = r;
  45. }
  46. }
  47. #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
  48. {
  49. time_t r = time(NULL);
  50. if (r == -1) {
  51. ret = r;
  52. } else {
  53. ret = 0;
  54. x->time = r;
  55. }
  56. }
  57. #endif
  58. return ret;
  59. }
  60. int
  61. hrt_ctnow(hrt_ctstamp* x)
  62. {
  63. int ret;
  64. #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  65. ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &x->time);
  66. #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
  67. ret = getrusage(RUSAGE_SELF, &x->time);
  68. #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
  69. {
  70. clock_t r = times(&x->time);
  71. ret = (r == -1 ? r : 0);
  72. }
  73. #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
  74. {
  75. clock_t r = clock();
  76. if (r == -1) {
  77. ret = r;
  78. } else {
  79. ret = 0;
  80. x->time = r;
  81. }
  82. }
  83. #endif
  84. return ret;
  85. }
  86. int
  87. hrt_tnow(hrt_tstamp* x)
  88. {
  89. int ret;
  90. ret = hrt_rtnow(&x->rtstamp);
  91. if (ret != 0)
  92. return ret;
  93. ret = hrt_ctnow(&x->ctstamp);
  94. return ret;
  95. }
  96. #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME \
  97. || HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  98. static inline double
  99. timespec_diff(const struct timespec *y, const struct timespec *x)
  100. {
  101. return (((y->tv_sec - x->tv_sec) * 1000000.0)
  102. + ((y->tv_nsec - x->tv_nsec) / 1000.0));
  103. }
  104. #endif
  105. #if (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY \
  106. || HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
  107. static inline double
  108. timeval_diff(const struct timeval *y, const struct timeval *x)
  109. {
  110. return (((y->tv_sec - x->tv_sec) * 1000000.0)
  111. + (y->tv_usec - x->tv_usec));
  112. }
  113. #endif
  114. #if (HRT_REALTIME_METHOD==HRT_USE_TIMES \
  115. || HRT_CPUTIME_METHOD==HRT_USE_TIMES \
  116. || HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
  117. static inline double
  118. clock_t_diff(clock_t y, clock_t x)
  119. {
  120. /* ignored: loosing precision if clock_t is specified as long double */
  121. return (double)(y - x);
  122. }
  123. #endif
  124. double
  125. hrt_rtmicros(const hrt_rtstamp* y, const hrt_rtstamp* x)
  126. {
  127. #if (HRT_REALTIME_METHOD==HRT_USE_TIMES)
  128. static long clock_ticks = 0;
  129. if (clock_ticks == 0) {
  130. clock_ticks = sysconf(_SC_CLK_TCK);
  131. assert (clock_ticks > 0);
  132. }
  133. #endif
  134. #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  135. return timespec_diff(&y->time, &x->time);
  136. #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
  137. return timeval_diff(&y->time, &x->time);
  138. #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
  139. return ((clock_t_diff(y->time, x->time) * 1000000.0) / clock_ticks);
  140. #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
  141. return (difftime(y->time, x->time) * 1000000.0);
  142. #endif
  143. }
  144. double
  145. hrt_ctmicros(const hrt_ctstamp* y, const hrt_ctstamp* x)
  146. {
  147. #if (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
  148. static long clock_ticks = 0;
  149. if (clock_ticks == 0) {
  150. clock_ticks = sysconf(_SC_CLK_TCK);
  151. assert (clock_ticks > 0);
  152. }
  153. #endif
  154. #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  155. return timespec_diff(&y->time, &x->time);
  156. #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
  157. return (timeval_diff(&y->time.ru_utime, &x->time.ru_utime)
  158. + timeval_diff(&y->time.ru_stime, &x->time.ru_stime));
  159. #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
  160. return (((clock_t_diff(y->time.tms_utime, x->time.tms_utime)
  161. + clock_t_diff(y->time.tms_stime, x->time.tms_stime))
  162. * 1000000.0) / clock_ticks);
  163. #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
  164. return ((clock_t_diff(y->time, x->time) * 1000000.0) / CLOCKS_PER_SEC);
  165. #endif
  166. }
  167. void
  168. hrt_rtnull(hrt_rtstamp* x)
  169. {
  170. #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  171. x->time.tv_sec = 0;
  172. x->time.tv_nsec = 0;
  173. #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
  174. x->time.tv_sec = 0;
  175. x->time.tv_usec = 0;
  176. #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
  177. x->time = 0;
  178. #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
  179. x->time = 0;
  180. #endif
  181. }
  182. void
  183. hrt_ctnull(hrt_ctstamp* x)
  184. {
  185. #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
  186. x->time.tv_sec = 0;
  187. x->time.tv_nsec = 0;
  188. #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
  189. x->time.ru_utime.tv_sec = 0;
  190. x->time.ru_utime.tv_usec = 0;
  191. x->time.ru_stime.tv_sec = 0;
  192. x->time.ru_stime.tv_usec = 0;
  193. #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
  194. x->time.tms_utime = 0;
  195. x->time.tms_stime = 0;
  196. #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
  197. x->time = 0;
  198. #endif
  199. }
  200. void
  201. hrt_tnull(hrt_tstamp* x)
  202. {
  203. hrt_rtnull(&x->rtstamp);
  204. hrt_ctnull(&x->ctstamp);
  205. }