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

/opensource.apple.com/source/gcc_40/gcc_40-4061/libiberty/getruntime.c

#
C | 141 lines | 100 code | 15 blank | 26 comment | 0 complexity | 146f55695c36c8695010c9cf501e59be MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, MPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, IPL-1.0, ISC, AGPL-1.0, AGPL-3.0, JSON, Apache-2.0, 0BSD
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>getruntime.c</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">getruntime.c&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. <span class="enscript-comment">/* Return time used so far, in microseconds.
  24. Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
  25. This file is part of the libiberty library.
  26. Libiberty is free software; you can redistribute it and/or
  27. modify it under the terms of the GNU Library General Public
  28. License as published by the Free Software Foundation; either
  29. version 2 of the License, or (at your option) any later version.
  30. Libiberty is distributed in the hope that it will be useful,
  31. but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. Library General Public License for more details.
  34. You should have received a copy of the GNU Library General Public
  35. License along with libiberty; see the file COPYING.LIB. If
  36. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  37. Boston, MA 02111-1307, USA. */</span>
  38. #<span class="enscript-reference">include</span> <span class="enscript-string">&quot;config.h&quot;</span>
  39. #<span class="enscript-reference">include</span> <span class="enscript-string">&quot;ansidecl.h&quot;</span>
  40. #<span class="enscript-reference">include</span> <span class="enscript-string">&quot;libiberty.h&quot;</span>
  41. <span class="enscript-comment">/* On some systems (such as WindISS), you must include &lt;sys/types.h&gt;
  42. to get the definition of &quot;time_t&quot; before you include &lt;time.h&gt;. */</span>
  43. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/types.h&gt;</span>
  44. <span class="enscript-comment">/* There are several ways to get elapsed execution time; unfortunately no
  45. single way is available for all host systems, nor are there reliable
  46. ways to find out which way is correct for a given host. */</span>
  47. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">TIME_WITH_SYS_TIME</span>
  48. # <span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/time.h&gt;</span>
  49. # <span class="enscript-reference">include</span> <span class="enscript-string">&lt;time.h&gt;</span>
  50. #<span class="enscript-reference">else</span>
  51. # <span class="enscript-reference">if</span> <span class="enscript-variable-name">HAVE_SYS_TIME_H</span>
  52. # <span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/time.h&gt;</span>
  53. # <span class="enscript-reference">else</span>
  54. # <span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">HAVE_TIME_H</span>
  55. # <span class="enscript-reference">include</span> <span class="enscript-string">&lt;time.h&gt;</span>
  56. # <span class="enscript-reference">endif</span>
  57. # <span class="enscript-reference">endif</span>
  58. #<span class="enscript-reference">endif</span>
  59. #<span class="enscript-reference">if</span> <span class="enscript-variable-name">defined</span> (<span class="enscript-variable-name">HAVE_GETRUSAGE</span>) &amp;&amp; <span class="enscript-variable-name">defined</span> (<span class="enscript-variable-name">HAVE_SYS_RESOURCE_H</span>)
  60. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/resource.h&gt;</span>
  61. #<span class="enscript-reference">endif</span>
  62. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">HAVE_TIMES</span>
  63. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">HAVE_SYS_PARAM_H</span>
  64. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/param.h&gt;</span>
  65. #<span class="enscript-reference">endif</span>
  66. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/times.h&gt;</span>
  67. #<span class="enscript-reference">endif</span>
  68. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">HAVE_UNISTD_H</span>
  69. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;unistd.h&gt;</span>
  70. #<span class="enscript-reference">endif</span>
  71. <span class="enscript-comment">/* This is a fallback; if wrong, it will likely make obviously wrong
  72. results. */</span>
  73. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">CLOCKS_PER_SEC</span>
  74. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">CLOCKS_PER_SEC</span> 1
  75. #<span class="enscript-reference">endif</span>
  76. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">_SC_CLK_TCK</span>
  77. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">GNU_HZ</span> sysconf(_SC_CLK_TCK)
  78. #<span class="enscript-reference">else</span>
  79. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">HZ</span>
  80. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">GNU_HZ</span> HZ
  81. #<span class="enscript-reference">else</span>
  82. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">CLOCKS_PER_SEC</span>
  83. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">GNU_HZ</span> CLOCKS_PER_SEC
  84. #<span class="enscript-reference">endif</span>
  85. #<span class="enscript-reference">endif</span>
  86. #<span class="enscript-reference">endif</span>
  87. <span class="enscript-comment">/*
  88. @deftypefn Replacement long get_run_time (void)
  89. Returns the time used so far, in microseconds. If possible, this is
  90. the time used by this process, else it is the elapsed time since the
  91. process started.
  92. @end deftypefn
  93. */</span>
  94. <span class="enscript-type">long</span>
  95. <span class="enscript-function-name">get_run_time</span> ()
  96. {
  97. #<span class="enscript-reference">if</span> <span class="enscript-variable-name">defined</span> (<span class="enscript-variable-name">HAVE_GETRUSAGE</span>) &amp;&amp; <span class="enscript-variable-name">defined</span> (<span class="enscript-variable-name">HAVE_SYS_RESOURCE_H</span>)
  98. <span class="enscript-type">struct</span> rusage rusage;
  99. getrusage (0, &amp;rusage);
  100. <span class="enscript-keyword">return</span> (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  101. + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  102. #<span class="enscript-reference">else</span> <span class="enscript-comment">/* ! HAVE_GETRUSAGE */</span>
  103. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">HAVE_TIMES</span>
  104. <span class="enscript-type">struct</span> tms tms;
  105. times (&amp;tms);
  106. <span class="enscript-keyword">return</span> (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
  107. #<span class="enscript-reference">else</span> <span class="enscript-comment">/* ! HAVE_TIMES */</span>
  108. <span class="enscript-comment">/* Fall back on clock and hope it's correctly implemented. */</span>
  109. <span class="enscript-type">const</span> <span class="enscript-type">long</span> clocks_per_sec = CLOCKS_PER_SEC;
  110. <span class="enscript-keyword">if</span> (clocks_per_sec &lt;= 1000000)
  111. <span class="enscript-keyword">return</span> clock () * (1000000 / clocks_per_sec);
  112. <span class="enscript-keyword">else</span>
  113. <span class="enscript-keyword">return</span> clock () / clocks_per_sec;
  114. #<span class="enscript-reference">endif</span> <span class="enscript-comment">/* HAVE_TIMES */</span>
  115. #<span class="enscript-reference">endif</span> <span class="enscript-comment">/* HAVE_GETRUSAGE */</span>
  116. }
  117. </pre>
  118. <hr />
  119. </body></html>