PageRenderTime 31ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/osprey/libu/util/timef.c

https://bitbucket.org/osunix/open64
C | 233 lines | 111 code | 28 blank | 94 comment | 11 complexity | 13a62f30f20d85913ec3298cf914bb00 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, GPL-3.0
  1. /*
  2. * Copyright 2002, 2003, 2004 PathScale, Inc. All Rights Reserved.
  3. */
  4. /*
  5. Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved.
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of version 2.1 of the GNU Lesser General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it would be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. Further, this software is distributed without any warranty that it is
  13. free of the rightful claim of any third person regarding infringement
  14. or the like. Any license provided herein, whether implied or
  15. otherwise, applies only to this software file. Patent licenses, if
  16. any, provided herein do not apply to combinations of this program with
  17. other software, or any other product whatsoever.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this program; if not, write the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307,
  21. USA.
  22. Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky,
  23. Mountain View, CA 94043, or:
  24. http://www.sgi.com
  25. For further information regarding this notice, see:
  26. http://oss.sgi.com/projects/GenInfo/NoticeExplan
  27. */
  28. #pragma ident "@(#) libu/util/timef.c 92.1 07/07/99 13:18:33"
  29. #include <fortran.h>
  30. #include <unistd.h>
  31. #include <sys/time.h>
  32. /* In Linux <sys/time.h> this is ifdef'ed out */
  33. #if defined(KEY) && ! defined(BUILD_OS_DARWIN)
  34. struct timezone
  35. {
  36. int tz_minuteswest; /* Minutes west of GMT. */
  37. int tz_dsttime; /* Nonzero if DST is ever in effect. */
  38. };
  39. #endif
  40. #ifndef _WORD32
  41. typedef long int64;
  42. #else
  43. typedef long long int64;
  44. #endif
  45. #ifdef _UNICOS
  46. double _sec_per_clock; /* Seconds per clock tick */
  47. #define MSECPERCLK ( _sec_per_clock * 1000.0 )
  48. #elif ! defined(__mips)
  49. #define MSECPERCLK ( (double).001 ) /* gettimeofday has 1000000 ticks/sec */
  50. #endif
  51. /*
  52. * TIMEF returns the elapsed wall-clock time in floating-point
  53. * milliseconds since the first call to TIMEF.
  54. *
  55. * NOTE (YMP systems):
  56. *
  57. * Because the real-time clock value may exceed 48 bits, the
  58. * floating-point conversion done here often loses some precision.
  59. * The greatest possible error in the value returned by TIMEF is
  60. * N seconds, where N is approximated by:
  61. *
  62. * N = (machine clock period time in nanoseconds) * (1.4E-6)
  63. *
  64. * Called from Fortran:
  65. *
  66. * REAL TIMEF
  67. *
  68. * x = TIMEF();
  69. * or CALL TIMEF(x) UNICOS systems only
  70. */
  71. #ifndef __mips
  72. _f_real
  73. #ifdef _UNICOS
  74. TIMEF(_f_real *time)
  75. #else
  76. timef_(void)
  77. #endif
  78. {
  79. static int64 initial_rt = -1; /* Clock value from initial call */
  80. int64 rt, rtdif;
  81. double retval;
  82. #ifdef _UNICOS
  83. if (_sec_per_clock == 0.0)
  84. _sec_per_clock = 1.0 / (double) sysconf(_SC_CLK_TCK);
  85. rt = _rtc();
  86. #else
  87. {
  88. struct timeval buf;
  89. struct timezone buf2;
  90. (void) gettimeofday (&buf, &buf2);
  91. rt = (long long)buf.tv_sec * 1000000LL + buf.tv_usec;
  92. }
  93. #endif
  94. if (initial_rt < 0) {
  95. initial_rt = rt;
  96. rtdif = 0;
  97. /*
  98. * force rtdif to 0 to prevent anomalies due to possible
  99. * race conditions between 2 or more tasks calling TIMEF
  100. * concurrently on the initial call.
  101. */
  102. }
  103. else
  104. rtdif = rt - initial_rt;
  105. /*
  106. * On pre-7.0 UNICOS CX/CEA systems and on all CRAY-2 systems the
  107. * real-time hardware clock is set to 0 on reboot. If a restarted
  108. * process had called TIMEF before the system was brought down
  109. * and then after reboot, a negative difference in the real-time
  110. * clock value would be observed. To minimize the impact of
  111. * wrong timings being returned, we return 0 when this situation is
  112. * detected.
  113. */
  114. if (rtdif < 0) {
  115. initial_rt = rt;
  116. rtdif = 0;
  117. }
  118. retval = (double) rtdif * MSECPERCLK;
  119. #ifdef _UNICOS
  120. if (_numargs() > 0)
  121. *time = (_f_real) retval;
  122. #endif
  123. return( (_f_real) retval);
  124. }
  125. #ifdef KEY /* Bug 12813 */
  126. _f_real8
  127. _Timef(void)
  128. {
  129. static int64 initial_rt = -1; /* Clock value from initial call */
  130. int64 rt, rtdif;
  131. double retval;
  132. {
  133. struct timeval buf;
  134. struct timezone buf2;
  135. (void) gettimeofday (&buf, &buf2);
  136. rt = (long long)buf.tv_sec * 1000000LL + buf.tv_usec;
  137. }
  138. if (initial_rt < 0) {
  139. initial_rt = rt;
  140. rtdif = 0;
  141. /*
  142. * force rtdif to 0 to prevent anomalies due to possible
  143. * race conditions between 2 or more tasks calling TIMEF
  144. * concurrently on the initial call.
  145. */
  146. }
  147. else
  148. rtdif = rt - initial_rt;
  149. /*
  150. * On pre-7.0 UNICOS CX/CEA systems and on all CRAY-2 systems the
  151. * real-time hardware clock is set to 0 on reboot. If a restarted
  152. * process had called TIMEF before the system was brought down
  153. * and then after reboot, a negative difference in the real-time
  154. * clock value would be observed. To minimize the impact of
  155. * wrong timings being returned, we return 0 when this situation is
  156. * detected.
  157. */
  158. if (rtdif < 0) {
  159. initial_rt = rt;
  160. rtdif = 0;
  161. }
  162. return (_f_real8) rtdif * MSECPERCLK;
  163. }
  164. #endif /* KEY Bug 12813 */
  165. #endif /* ! __mips */
  166. /*
  167. * MIPS version is different from Sparc version.
  168. */
  169. #ifdef __mips
  170. extern double _nowrap_cycles_per_sec;
  171. _f_real8
  172. timef_(void)
  173. {
  174. static int64 initial_rt = -1; /* Clock value from initial call */
  175. int64 rt, rtdif;
  176. static double msec_per_cycle; /* reciprocal so we can multiply */
  177. if (msec_per_cycle == 0.0) {
  178. _init_hw_clock();
  179. msec_per_cycle = 1000.0 / _nowrap_cycles_per_sec;
  180. }
  181. rt = _sysclock_nowrap();
  182. if (initial_rt < 0) {
  183. initial_rt = rt;
  184. rtdif = 0;
  185. /*
  186. * force rtdif to 0 to prevent anomalies due to possible
  187. * race conditions between 2 or more tasks calling TIMEF
  188. * concurrently on the initial call.
  189. */
  190. }
  191. else
  192. rtdif = rt - initial_rt;
  193. return (_f_real8)rtdif * msec_per_cycle;
  194. }
  195. #endif /* __mips */