cputime.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef CPUTIME_H
  2. #define CPUTIME_H
  3. /*
  4. * Figure out what clock to use. Each possibility can be specifically
  5. * enabled or disabled by predefining USE_XXX to 1 or 0. For some,
  6. * the code attempts to detect availability automatically. If the
  7. * Symbols HAVE_XXX are defined, they are used. If not, they are
  8. * set to reasonable default assumptions while further conditions
  9. * are checked. The choices, and the ways they are auto-detected are:
  10. * - gethrvtime(), if HAVE_GETHRVTIME is set to 1.
  11. * - clock_gettime(CLOCK_VIRTUAL,...), if CLOCK_VIRTUAL is defined in <time.h>
  12. * - getrusage(RUSAGE_SELF,...), if RUSAGE_SELF is defined in <sys/resource.h>
  13. * - clock(), if CLOCKS_PER_SEC or CLK_TCK are defined in <time.h>
  14. * - time(), unless specifically disabled.
  15. *
  16. * The symbol CLOCK_AVAIL is given a value of 1 if a clock is found.
  17. * The following are then available:
  18. * timetype (typedef): the type needed to hold a clock value.
  19. * gettime(t) (macro): A function that gets passed a timetype *.
  20. * subtime(d,s) (macro): Sets d -= s, essentially.
  21. * msec(t) (macro): Given a timetype, return the number of milliseconds
  22. * in it, as an unsigned integer between 0 and 999.
  23. * sec(t) (macro): Given a timetype, return the number of seconds in it,
  24. * as an unsigned long integer.
  25. */
  26. /* We expect that our caller has already #included "bnconfig.h" if possible. */
  27. #ifndef unix
  28. #define unix 0
  29. #endif
  30. #ifndef __unix
  31. #define __unix 0
  32. #endif
  33. #ifndef __unix__
  34. #define __unix__ 0
  35. #endif
  36. #ifdef UNIX
  37. /* Nothing */
  38. #elif unix
  39. #define UNIX 1
  40. #elif __unix
  41. #define UNIX 1
  42. #elif __unix__
  43. #define UNIX 1
  44. #endif
  45. #ifndef UNIX
  46. #define UNIX 0
  47. #endif
  48. #ifndef TIME_WITH_SYS_TIME
  49. #define TIME_WITH_SYS_TIME 1 /* Assume true if not told */
  50. #endif
  51. #ifndef HAVE_SYS_TIME_H
  52. #define HAVE_SYS_TIME_H 0 /* Assume true if not told */
  53. #endif
  54. /*
  55. * Include <time.h> unless that would prevent us from later including
  56. * <sys/time.h>, in which case include *that* immediately.
  57. */
  58. #if TIME_WITH_SYS_TIME
  59. #include <time.h>
  60. #elif HAVE_SYS_TIME_H
  61. #include <sys/time.h>
  62. #else
  63. #include <time.h>
  64. #endif
  65. #ifndef USE_GETHRVTIME
  66. #ifdef HAVE_GETHRVTIME
  67. #define USE_GETHRVTIME HAVE_GETHRVTIME
  68. #else
  69. #define USE_GETHRVTIME 0
  70. #endif
  71. #endif
  72. #if USE_GETHRVTIME
  73. #define CLOCK_AVAIL 1
  74. typedef hrtime_t timetype;
  75. #define gettime(t) *(t) = gethrvtime()
  76. #define subtime(d,s) d -= s
  77. #define msec(t) (unsigned)((t/1000000)%1000)
  78. #define sec(t) (unsigned long)(t/1000000000)
  79. #else
  80. #ifndef USE_CLOCK_GETTIME
  81. #define USE_CLOCK_GETTIME 0
  82. #ifndef HAVE_CLOCK_GETTIME
  83. #define HAVE_CLOCK_GETTIME 1 /* Assume the CLOCK_VIRTUAL test will catch */
  84. #endif
  85. /*
  86. * It turns out to be non-ANSI to use the apparently simpler construct
  87. * "#define USE_CLOCK_GETTIME defined(CLOCK_VIRTUAL)", since
  88. * "If the token defined is generated as a result of this replacement
  89. * process or use of the defined unary operator does not match one
  90. * of the two specified forms prior ro macro replacement, the behaviour
  91. * is undefined." (ANSI/ISO 9899-1990 section 6.8.1)
  92. * In practice, it breaks the DEC Alpha compiler.
  93. */
  94. #if HAVE_CLOCK_GETTIME
  95. #ifdef CLOCK_VIRTUAL
  96. #ifdef USE_CLOCK_GETTIME
  97. #undef USE_CLOCK_GETTIME
  98. #endif
  99. #define USE_CLOCK_GETTIME 1
  100. #endif
  101. #endif
  102. #endif
  103. #if USE_CLOCK_GETTIME
  104. #define CLOCK_AVAIL 1
  105. typedef struct timespec timetype;
  106. #define gettime(t) clock_gettime(CLOCK_VIRTUAL, t)
  107. #define subtime(d,s) \
  108. d.tv_sec -= s.tv_sec + (d.tv_nsec >= s.tv_nsec ? \
  109. (d.tv_nsec -= s.tv_nsec, 0) : \
  110. (d.tv_nsec += 1000000000-s.tv_nsec, 1))
  111. #define msec(t) (unsigned)(t.tv_nsec/1000000)
  112. #define sec(t) (unsigned long)(t.tv_sec)
  113. #else
  114. #if UNIX
  115. #ifndef HAVE_GETRUSAGE
  116. #define HAVE_GETRUSAGE 1
  117. #endif
  118. #endif /* UNIX */
  119. #define USE_GETRUSAGE 0
  120. /* AJR */
  121. /*#define HAVE_GETRUSAGE 0*/
  122. #include <sys/time.h>
  123. #if HAVE_GETRUSAGE
  124. #if TIME_WITH_SYS_TIME
  125. #ifndef HAVE_SYS_TIME_H
  126. #include <sys/time.h>
  127. #elif HAVE_SYS_TIME_H
  128. #include <sys/time.h>
  129. #endif
  130. #endif /* TIME_WITH_SYS_TIME */
  131. #include <sys/resource.h>
  132. #ifdef RUSAGE_SELF
  133. #undef USE_GETRUSAGE
  134. #define USE_GETRUSAGE 1
  135. #endif
  136. #endif /* HAVE_GETRUSAGE */
  137. #if USE_GETRUSAGE
  138. #define CLOCK_AVAIL 1
  139. typedef struct rusage timetype;
  140. #define gettime(t) getrusage(RUSAGE_SELF, t);
  141. #define subtime(d, s) \
  142. d.ru_utime.tv_sec -= s.ru_utime.tv_sec + \
  143. (d.ru_utime.tv_usec >= s.ru_utime.tv_usec ? \
  144. (d.ru_utime.tv_usec -= s.ru_utime.tv_usec, 0) : \
  145. (d.ru_utime.tv_usec += 1000000-s.ru_utime.tv_usec, 1))
  146. #define msec(t) (unsigned)(t.ru_utime.tv_usec/1000)
  147. #define sec(t) (unsigned long)(t.ru_utime.tv_sec)
  148. #else
  149. #ifndef HAVE_CLOCK
  150. #define HAVE_CLOCK 1
  151. #endif
  152. #define USE_CLOCK 0
  153. #define USE_TIME 0
  154. #if HAVE_CLOCK
  155. #ifndef CLOCKS_PER_SEC
  156. #ifdef CLK_TCK
  157. #define CLOCKS_PER_SEC CLK_TCK
  158. #endif
  159. #endif /* !defined(CLOCKS_PER_SEC) */
  160. #ifndef USE_CLOCK
  161. #ifdef CLOCKS_PER_SEC
  162. #define USE_CLOCK 1
  163. #endif
  164. #endif /* !defined(USE_CLOCK) */
  165. #endif /* HAVE_CLOCK */
  166. #if USE_CLOCK
  167. #define CLOCK_AVAIL 1
  168. typedef clock_t timetype;
  169. #define gettime(t) *(t) = clock()
  170. #define subtime(d, s) d -= s
  171. /*
  172. * I don't like having to do floating point math. CLOCKS_PER_SEC is
  173. * almost always an integer, and the most common non-integral case is
  174. * the MS-DOS wierdness of 18.2. We have to be a bit careful with the
  175. * casts, because ANSI C doesn't provide % with non-integral operands,
  176. * but just to be extra annoying, some implementations define it as an
  177. * integral-valued float. (E.g. Borland C++ 4.5 with 1000.0)
  178. */
  179. /* AJR #if ((unsigned)CLOCKS_PER_SEC == CLOCKS_PER_SEC)*/
  180. #if 1
  181. /* Integer CLOCKS_PER_SEC */
  182. #define sec(t) (unsigned long)(t/CLOCKS_PER_SEC)
  183. #define msec(t) (unsigned)(t % (unsigned)CLOCKS_PER_SEC * 1000 / \
  184. (unsigned)CLOCKS_PER_SEC)
  185. /* AJR #elif (CLOCKS_PER_SEC == 18.2)*/
  186. #elif 0
  187. /* MS-DOS-ism */
  188. #define sec(t) (unsigned long)(t*5 / 91)
  189. #define msec(t) (unsigned)(t*5 % 91 * 1000 / 91)
  190. #else /* We are forced to muck with floating point.... */
  191. #include <math.h> /* For floor() */
  192. #define sec(t) (unsigned long)(t/CLOCKS_PER_SEC)
  193. #define msec(t) (unsigned)((t - sec(t)*CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC)
  194. #endif
  195. #else
  196. #ifndef HAVE_TIME
  197. #define HAVE_TIME 1
  198. #endif
  199. #if HAVE_TIME
  200. #ifndef USE_TIME
  201. #define USE_TIME 1
  202. #endif
  203. #endif
  204. #if USE_TIME
  205. #define CLOCK_AVAIL 1
  206. typedef time_t timetype;
  207. #define gettime(t) time(t)
  208. #define subtime(d, s) d -= s
  209. #define msec(t) (unsigned)0
  210. #define sec(t) (unsigned long)t
  211. #endif /* USE_TIME */
  212. #endif /* USE_CLOCK */
  213. #endif /* USE_GETRUSAGE */
  214. #endif /* USE_CLOCK_GETTIME */
  215. #endif /* USE_GETHRVTIME */
  216. #endif /*CPUTIME_H*/