timer.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // timer.cc -- helper class for time accounting
  2. // Copyright (C) 2009-2022 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. #include <unistd.h>
  19. #ifdef HAVE_TIMES
  20. #include <sys/times.h>
  21. #endif
  22. #include "libiberty.h"
  23. #include "timer.h"
  24. namespace gold
  25. {
  26. // Class Timer
  27. Timer::Timer()
  28. {
  29. this->start_time_.wall = 0;
  30. this->start_time_.user = 0;
  31. this->start_time_.sys = 0;
  32. }
  33. // Start counting the time.
  34. void
  35. Timer::start()
  36. {
  37. this->get_time(&this->start_time_);
  38. }
  39. // Record the time used by pass N (0 <= N <= 2).
  40. void
  41. Timer::stamp(int n)
  42. {
  43. gold_assert(n >= 0 && n <= 2);
  44. TimeStats& thispass = this->pass_times_[n];
  45. this->get_time(&thispass);
  46. }
  47. #if HAVE_SYSCONF && defined _SC_CLK_TCK
  48. # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
  49. #else
  50. # ifdef CLK_TCK
  51. # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
  52. # else
  53. # ifdef HZ
  54. # define TICKS_PER_SECOND HZ /* traditional UNIX */
  55. # else
  56. # define TICKS_PER_SECOND 100 /* often the correct value */
  57. # endif
  58. # endif
  59. #endif
  60. // times returns statistics in clock_t units. This variable will hold the
  61. // conversion factor to seconds. We use a variable that is initialized once
  62. // because sysconf can be slow.
  63. static long ticks_per_sec;
  64. class Timer_init
  65. {
  66. public:
  67. Timer_init()
  68. {
  69. ticks_per_sec = TICKS_PER_SECOND;
  70. }
  71. };
  72. Timer_init timer_init;
  73. // Write the current time information.
  74. void
  75. Timer::get_time(TimeStats *now)
  76. {
  77. #ifdef HAVE_TIMES
  78. tms t;
  79. now->wall = (times(&t) * 1000) / ticks_per_sec;
  80. now->user = (t.tms_utime * 1000) / ticks_per_sec;
  81. now->sys = (t.tms_stime * 1000) / ticks_per_sec;
  82. #else
  83. now->wall = get_run_time() / 1000;
  84. now->user = 0;
  85. now->sys = 0;
  86. #endif
  87. }
  88. // Return the stats since start was called.
  89. Timer::TimeStats
  90. Timer::get_elapsed_time()
  91. {
  92. TimeStats now;
  93. this->get_time(&now);
  94. TimeStats delta;
  95. delta.wall = now.wall - this->start_time_.wall;
  96. delta.user = now.user - this->start_time_.user;
  97. delta.sys = now.sys - this->start_time_.sys;
  98. return delta;
  99. }
  100. // Return the stats for pass N (0 <= N <= 2).
  101. Timer::TimeStats
  102. Timer::get_pass_time(int n)
  103. {
  104. gold_assert(n >= 0 && n <= 2);
  105. TimeStats thispass = this->pass_times_[n];
  106. TimeStats& lastpass = n > 0 ? this->pass_times_[n-1] : this->start_time_;
  107. thispass.wall -= lastpass.wall;
  108. thispass.user -= lastpass.user;
  109. thispass.sys -= lastpass.sys;
  110. return thispass;
  111. }
  112. }