/gnu/egcs/libf2c/libU77/etime_.c

https://github.com/avsm/src · C · 160 lines · 120 code · 13 blank · 27 comment · 20 complexity · ce7bdf35aebde79bf4f02d99067f0c6d MD5 · raw file

  1. /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  2. This file is part of GNU Fortran libU77 library.
  3. This library is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. GNU Fortran 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 GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with GNU Fortran; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #if HAVE_UNISTD_H
  19. # include <unistd.h>
  20. #endif
  21. #include <sys/types.h>
  22. #if HAVE_SYS_TIMES_H
  23. # include <sys/times.h>
  24. #endif
  25. #if HAVE_SYS_PARAM_H
  26. # include <sys/param.h>
  27. #endif
  28. #if HAVE_GETRUSAGE
  29. # include <sys/time.h>
  30. # include <sys/resource.h>
  31. #endif
  32. #if defined (_WIN32)
  33. # include <windows.h>
  34. # undef min
  35. # undef max
  36. #endif
  37. #include <errno.h> /* for ENOSYS */
  38. #include "f2c.h"
  39. /* For dtime, etime we store the clock tick parameter (clk_tck) the
  40. first time either of them is invoked rather than each time. This
  41. approach probably speeds up each invocation by avoiding a system
  42. call each time, but means that the overhead of the first call is
  43. different to all others. */
  44. static long clk_tck = 0;
  45. #ifdef KR_headers
  46. double G77_etime_0 (tarray)
  47. real tarray[2];
  48. #else
  49. double G77_etime_0 (real tarray[2])
  50. #endif
  51. {
  52. #if defined (_WIN32)
  53. static int win32_platform = -1;
  54. double usertime, systime;
  55. if (win32_platform == -1)
  56. {
  57. OSVERSIONINFO osv;
  58. osv.dwOSVersionInfoSize = sizeof (osv);
  59. GetVersionEx (&osv);
  60. win32_platform = osv.dwPlatformId;
  61. }
  62. /* non-NT platforms don't have a clue as to how long a process has
  63. been running, so simply return the uptime. Bad judgement call? */
  64. if (win32_platform != VER_PLATFORM_WIN32_NT)
  65. {
  66. static unsigned long long clock_freq;
  67. static unsigned long long old_count;
  68. unsigned long long count;
  69. LARGE_INTEGER counter_val;
  70. if (clock_freq == 0)
  71. {
  72. LARGE_INTEGER freq;
  73. if (! QueryPerformanceFrequency (&freq))
  74. {
  75. errno = ENOSYS;
  76. return 0.0;
  77. }
  78. else
  79. {
  80. clock_freq = ((unsigned long long) freq.HighPart << 32)
  81. + ((unsigned) freq.LowPart);
  82. if (! QueryPerformanceCounter (&counter_val))
  83. return -1.0;
  84. old_count = ((unsigned long long) counter_val.HighPart << 32)
  85. + (unsigned) counter_val.LowPart;
  86. }
  87. }
  88. if (! QueryPerformanceCounter (&counter_val))
  89. return -1.0;
  90. count = ((unsigned long long) counter_val.HighPart << 32)
  91. + (unsigned) counter_val.LowPart;
  92. tarray[0] = usertime = (double) (count - old_count) / clock_freq;
  93. tarray[1] = systime = 0.0;
  94. }
  95. else
  96. {
  97. FILETIME creation_time, exit_time, kernel_time, user_time;
  98. unsigned long long utime, stime;
  99. GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
  100. &kernel_time, &user_time);
  101. utime = ((unsigned long long) user_time.dwHighDateTime << 32)
  102. + (unsigned) user_time.dwLowDateTime;
  103. stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
  104. + (unsigned) kernel_time.dwLowDateTime;
  105. tarray[0] = usertime = utime / 1.0e7;
  106. tarray[1] = systime = stime / 1.0e7;
  107. }
  108. return usertime + systime;
  109. #elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
  110. /* The getrusage version is only the default for convenience. */
  111. #ifdef HAVE_GETRUSAGE
  112. struct rusage rbuff;
  113. if (getrusage (RUSAGE_SELF, &rbuff) != 0)
  114. abort ();
  115. tarray[0] = ((float) (rbuff.ru_utime).tv_sec +
  116. (float) (rbuff.ru_utime).tv_usec/1000000.0);
  117. tarray[1] = ((float) (rbuff.ru_stime).tv_sec +
  118. (float) (rbuff.ru_stime).tv_usec/1000000.0);
  119. #else /* HAVE_GETRUSAGE */
  120. struct tms buffer;
  121. /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
  122. fixme: does using _POSIX_VERSION help? */
  123. # if defined _SC_CLK_TCK && defined _POSIX_VERSION
  124. if (! clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
  125. # elif defined CLOCKS_PER_SECOND
  126. if (! clk_tck) clk_tck = CLOCKS_PER_SECOND;
  127. # elif defined CLK_TCK
  128. if (! clk_tck) clk_tck = CLK_TCK;
  129. # elif defined HZ
  130. if (! clk_tck) clk_tck = HZ;
  131. # elif defined HAVE_GETRUSAGE
  132. # else
  133. #error Dont know clock tick length
  134. # endif
  135. if (times(&buffer) == (clock_t)-1) return -1.0;
  136. tarray[0] = (float) buffer.tms_utime / (float)clk_tck;
  137. tarray[1] = (float) buffer.tms_stime / (float)clk_tck;
  138. #endif /* HAVE_GETRUSAGE */
  139. return (tarray[0]+tarray[1]);
  140. #else /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
  141. errno = ENOSYS;
  142. return 0.0;
  143. #endif /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
  144. }