PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pkgs/libs/imagick/src/magick/timer.c

https://bitbucket.org/bosp/benchmarks-parsec
C | 395 lines | 137 code | 3 blank | 255 comment | 31 complexity | b7092a4b05c5f6101c14760e146dcbcd MD5 | raw file
Possible License(s): CC0-1.0, BSD-2-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, LGPL-2.0, GPL-2.0
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % TTTTT IIIII M M EEEEE RRRR %
  7. % T I MM MM E R R %
  8. % T I M M M EEE RRRR %
  9. % T I M M E R R %
  10. % T IIIII M M EEEEE R R %
  11. % %
  12. % %
  13. % ImageMagick Timing Methods %
  14. % %
  15. % Software Design %
  16. % John Cristy %
  17. % January 1993 %
  18. % %
  19. % %
  20. % Copyright 1999-2007 ImageMagick Studio LLC, a non-profit organization %
  21. % dedicated to making software imaging solutions freely available. %
  22. % %
  23. % You may not use this file except in compliance with the License. You may %
  24. % obtain a copy of the License at %
  25. % %
  26. % http://www.imagemagick.org/script/license.php %
  27. % %
  28. % Unless required by applicable law or agreed to in writing, software %
  29. % distributed under the License is distributed on an "AS IS" BASIS, %
  30. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
  31. % See the License for the specific language governing permissions and %
  32. % limitations under the License. %
  33. % %
  34. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35. %
  36. % Contributed by Bill Radcliffe and Bob Friesenhahn.
  37. %
  38. */
  39. /*
  40. Include declarations.
  41. */
  42. #include "magick/studio.h"
  43. #include "magick/log.h"
  44. #include "magick/memory_.h"
  45. #include "magick/timer.h"
  46. /*
  47. Define declarations.
  48. */
  49. #if defined(macintosh)
  50. #define CLK_TCK CLOCKS_PER_SEC
  51. #endif
  52. #if !defined(CLK_TCK)
  53. #define CLK_TCK sysconf(_SC_CLK_TCK)
  54. #endif
  55. /*
  56. Forward declarations.
  57. */
  58. static double
  59. UserTime(void);
  60. static void
  61. StopTimer(TimerInfo *);
  62. /*
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. % %
  65. % %
  66. % %
  67. % C o n t i n u e T i m e r %
  68. % %
  69. % %
  70. % %
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  72. %
  73. % ContinueTimer() resumes a stopped stopwatch. The stopwatch continues
  74. % counting from the last StartTimer() onwards.
  75. %
  76. % The format of the ContinueTimer method is:
  77. %
  78. % MagickBooleanType ContinueTimer(TimerInfo *time_info)
  79. %
  80. % A description of each parameter follows.
  81. %
  82. % o time_info: Time statistics structure.
  83. %
  84. */
  85. MagickExport MagickBooleanType ContinueTimer(TimerInfo *time_info)
  86. {
  87. assert(time_info != (TimerInfo *) NULL);
  88. assert(time_info->signature == MagickSignature);
  89. if (time_info->state == UndefinedTimerState)
  90. return(MagickFalse);
  91. if (time_info->state == StoppedTimerState)
  92. {
  93. time_info->user.total-=time_info->user.stop-time_info->user.start;
  94. time_info->elapsed.total-=
  95. time_info->elapsed.stop-time_info->elapsed.start;
  96. }
  97. time_info->state=RunningTimerState;
  98. return(MagickTrue);
  99. }
  100. /*
  101. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  102. % %
  103. % %
  104. % %
  105. + E l a p s e d T i m e %
  106. % %
  107. % %
  108. % %
  109. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  110. %
  111. % ElapsedTime() returns the elapsed time (in seconds) since the last call to
  112. % StartTimer().
  113. %
  114. % The format of the ElapsedTime method is:
  115. %
  116. % double ElapsedTime()
  117. %
  118. */
  119. static double ElapsedTime(void)
  120. {
  121. #if defined(HAVE_TIMES)
  122. struct tms
  123. timer;
  124. return((double) (times(&timer)/CLK_TCK));
  125. #else
  126. #if defined(__WINDOWS__)
  127. return(NTElapsedTime());
  128. #else
  129. return((double) clock()/CLK_TCK);
  130. #endif
  131. #endif
  132. }
  133. /*
  134. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  135. % %
  136. % %
  137. % %
  138. % G e t E l a p s e d T i m e %
  139. % %
  140. % %
  141. % %
  142. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  143. %
  144. % GetElapsedTime() returns the elapsed time (in seconds) passed between the
  145. % start and stop events. If the stopwatch is still running, it is stopped
  146. % first.
  147. %
  148. % The format of the GetElapsedTime method is:
  149. %
  150. % double GetElapsedTime(TimerInfo *time_info)
  151. %
  152. % A description of each parameter follows.
  153. %
  154. % o time_info: Timer statistics structure.
  155. %
  156. */
  157. MagickExport double GetElapsedTime(TimerInfo *time_info)
  158. {
  159. assert(time_info != (TimerInfo *) NULL);
  160. assert(time_info->signature == MagickSignature);
  161. if (time_info->state == UndefinedTimerState)
  162. return(0.0);
  163. if (time_info->state == RunningTimerState)
  164. StopTimer(time_info);
  165. return(time_info->elapsed.total);
  166. }
  167. /*
  168. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  169. % %
  170. % %
  171. % %
  172. % G e t T i m e r I n f o %
  173. % %
  174. % %
  175. % %
  176. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  177. %
  178. % GetTimerInfo() initializes the TimerInfo structure.
  179. %
  180. % The format of the GetTimerInfo method is:
  181. %
  182. % void GetTimerInfo(TimerInfo *time_info)
  183. %
  184. % A description of each parameter follows.
  185. %
  186. % o time_info: Timer statistics structure.
  187. %
  188. %
  189. */
  190. MagickExport void GetTimerInfo(TimerInfo *time_info)
  191. {
  192. /*
  193. Create a stopwatch and start it.
  194. */
  195. assert(time_info != (TimerInfo *) NULL);
  196. (void) ResetMagickMemory(time_info,0,sizeof(*time_info));
  197. time_info->state=UndefinedTimerState;
  198. time_info->signature=MagickSignature;
  199. StartTimer(time_info,MagickTrue);
  200. }
  201. /*
  202. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  203. % %
  204. % %
  205. % %
  206. % G e t U s e r T i m e %
  207. % %
  208. % %
  209. % %
  210. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  211. %
  212. % GetUserTime() returns the User time (user and system) by the operating
  213. % system (in seconds) between the start and stop events. If the stopwatch is
  214. % still running, it is stopped first.
  215. %
  216. % The format of the GetUserTime method is:
  217. %
  218. % double GetUserTime(TimerInfo *time_info)
  219. %
  220. % A description of each parameter follows.
  221. %
  222. % o time_info: Timer statistics structure.
  223. %
  224. */
  225. MagickExport double GetUserTime(TimerInfo *time_info)
  226. {
  227. assert(time_info != (TimerInfo *) NULL);
  228. assert(time_info->signature == MagickSignature);
  229. if (time_info->state == UndefinedTimerState)
  230. return(0.0);
  231. if (time_info->state == RunningTimerState)
  232. StopTimer(time_info);
  233. return(time_info->user.total);
  234. }
  235. /*
  236. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  237. % %
  238. % %
  239. % %
  240. % R e s e t T i m e r %
  241. % %
  242. % %
  243. % %
  244. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  245. %
  246. % ResetTimer() resets the stopwatch.
  247. %
  248. % The format of the ResetTimer method is:
  249. %
  250. % void ResetTimer(TimerInfo *time_info)
  251. %
  252. % A description of each parameter follows.
  253. %
  254. % o time_info: Timer statistics structure.
  255. %
  256. */
  257. MagickExport void ResetTimer(TimerInfo *time_info)
  258. {
  259. assert(time_info != (TimerInfo *) NULL);
  260. assert(time_info->signature == MagickSignature);
  261. StopTimer(time_info);
  262. time_info->elapsed.stop=0.0;
  263. time_info->user.stop=0.0;
  264. }
  265. /*
  266. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  267. % %
  268. % %
  269. % %
  270. + S t a r t T i m e r %
  271. % %
  272. % %
  273. % %
  274. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  275. %
  276. % StartTimer() starts the stopwatch.
  277. %
  278. % The format of the StartTimer method is:
  279. %
  280. % void StartTimer(TimerInfo *time_info,const MagickBooleanType reset)
  281. %
  282. % A description of each parameter follows.
  283. %
  284. % o time_info: Timer statistics structure.
  285. %
  286. % o reset: If reset is MagickTrue, then the stopwatch is reset prior to
  287. % starting. If reset is MagickFalse, then timing is continued without
  288. % resetting the stopwatch.
  289. %
  290. */
  291. MagickExport void StartTimer(TimerInfo *time_info,const MagickBooleanType reset)
  292. {
  293. assert(time_info != (TimerInfo *) NULL);
  294. assert(time_info->signature == MagickSignature);
  295. if (reset != MagickFalse)
  296. {
  297. /*
  298. Reset the stopwatch before starting it.
  299. */
  300. time_info->user.total=0.0;
  301. time_info->elapsed.total=0.0;
  302. }
  303. if (time_info->state != RunningTimerState)
  304. {
  305. time_info->elapsed.start=ElapsedTime();
  306. time_info->user.start=UserTime();
  307. }
  308. time_info->state=RunningTimerState;
  309. }
  310. /*
  311. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  312. % %
  313. % %
  314. % %
  315. + S t o p T i m e r %
  316. % %
  317. % %
  318. % %
  319. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  320. %
  321. % StopTimer() stops the stopwatch.
  322. %
  323. % The format of the StopTimer method is:
  324. %
  325. % void StopTimer(TimerInfo *time_info)
  326. %
  327. % A description of each parameter follows.
  328. %
  329. % o time_info: Timer statistics structure.
  330. %
  331. */
  332. static void StopTimer(TimerInfo *time_info)
  333. {
  334. assert(time_info != (TimerInfo *) NULL);
  335. assert(time_info->signature == MagickSignature);
  336. time_info->elapsed.stop=ElapsedTime();
  337. time_info->user.stop=UserTime();
  338. if (time_info->state == RunningTimerState)
  339. {
  340. time_info->user.total+=
  341. time_info->user.stop-time_info->user.start+MagickEpsilon;
  342. time_info->elapsed.total+=
  343. time_info->elapsed.stop-time_info->elapsed.start+MagickEpsilon;
  344. }
  345. time_info->state=StoppedTimerState;
  346. }
  347. /*
  348. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  349. % %
  350. % %
  351. % %
  352. + U s e r T i m e %
  353. % %
  354. % %
  355. % %
  356. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  357. %
  358. % UserTime() returns the total time the process has been scheduled (in
  359. % seconds) since the last call to StartTimer().
  360. %
  361. % The format of the UserTime method is:
  362. %
  363. % double UserTime()
  364. %
  365. */
  366. static double UserTime(void)
  367. {
  368. #if defined(HAVE_TIMES)
  369. struct tms
  370. timer;
  371. (void) times(&timer);
  372. return((double) (timer.tms_utime+timer.tms_stime)/CLK_TCK);
  373. #else
  374. #if defined(__WINDOWS__)
  375. return(NTUserTime());
  376. #else
  377. return((double) clock()/CLK_TCK);
  378. #endif
  379. #endif
  380. }