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