PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mingw-w64-v2.0.999/binutils/src/gold/timer.cc

#
C++ | 131 lines | 86 code | 20 blank | 25 comment | 3 complexity | 019abe953dddc7173a3dc270bce2b58a MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, 0BSD, LGPL-2.0, GPL-3.0, AGPL-1.0, Unlicense, GPL-2.0, BSD-3-Clause
  1. // timer.cc -- helper class for time accounting
  2. // Copyright 2009, 2010 Free Software Foundation, Inc.
  3. // Written by Rafael Avila de Espindola <espindola@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #ifdef HAVE_TIMES
  19. #include <sys/times.h>
  20. #endif
  21. #include "libiberty.h"
  22. #include "timer.h"
  23. namespace gold
  24. {
  25. // Class Timer
  26. Timer::Timer()
  27. {
  28. this->start_time_.wall = 0;
  29. this->start_time_.user = 0;
  30. this->start_time_.sys = 0;
  31. }
  32. // Start counting the time.
  33. void
  34. Timer::start()
  35. {
  36. this->get_time(&this->start_time_);
  37. }
  38. // Record the time used by pass N (0 <= N <= 2).
  39. void
  40. Timer::stamp(int n)
  41. {
  42. gold_assert(n >= 0 && n <= 2);
  43. TimeStats& thispass = this->pass_times_[n];
  44. this->get_time(&thispass);
  45. }
  46. #if HAVE_SYSCONF && defined _SC_CLK_TCK
  47. # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
  48. #else
  49. # ifdef CLK_TCK
  50. # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
  51. # else
  52. # ifdef HZ
  53. # define TICKS_PER_SECOND HZ /* traditional UNIX */
  54. # else
  55. # define TICKS_PER_SECOND 100 /* often the correct value */
  56. # endif
  57. # endif
  58. #endif
  59. // times returns statistics in clock_t units. This variable will hold the
  60. // conversion factor to seconds. We use a variable that is initialized once
  61. // because sysconf can be slow.
  62. static long ticks_per_sec;
  63. class Timer_init
  64. {
  65. public:
  66. Timer_init()
  67. {
  68. ticks_per_sec = TICKS_PER_SECOND;
  69. }
  70. };
  71. Timer_init timer_init;
  72. // Write the current time information.
  73. void
  74. Timer::get_time(TimeStats *now)
  75. {
  76. #ifdef HAVE_TIMES
  77. tms t;
  78. now->wall = (times(&t) * 1000) / ticks_per_sec;
  79. now->user = (t.tms_utime * 1000) / ticks_per_sec;
  80. now->sys = (t.tms_stime * 1000) / ticks_per_sec;
  81. #else
  82. now->wall = get_run_time() / 1000;
  83. now->user = 0;
  84. now->sys = 0;
  85. #endif
  86. }
  87. // Return the stats since start was called.
  88. Timer::TimeStats
  89. Timer::get_elapsed_time()
  90. {
  91. TimeStats now;
  92. this->get_time(&now);
  93. TimeStats delta;
  94. delta.wall = now.wall - this->start_time_.wall;
  95. delta.user = now.user - this->start_time_.user;
  96. delta.sys = now.sys - this->start_time_.sys;
  97. return delta;
  98. }
  99. // Return the stats for pass N (0 <= N <= 2).
  100. Timer::TimeStats
  101. Timer::get_pass_time(int n)
  102. {
  103. gold_assert(n >= 0 && n <= 2);
  104. TimeStats thispass = this->pass_times_[n];
  105. TimeStats& lastpass = n > 0 ? this->pass_times_[n-1] : this->start_time_;
  106. thispass.wall -= lastpass.wall;
  107. thispass.user -= lastpass.user;
  108. thispass.sys -= lastpass.sys;
  109. return thispass;
  110. }
  111. }