2
0

cputime.h 7.4 KB

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