]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/timer.cc
2009-12-17 Rafael Avila de Espindola <espindola@google.com>
[thirdparty/binutils-gdb.git] / gold / timer.cc
1 // timer.cc -- helper class for time accounting
2
3 // Copyright 2009 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 #include <sys/times.h>
26
27 #include "libiberty.h"
28
29 #include "timer.h"
30
31 namespace gold
32 {
33
34 // Class Timer
35
36 Timer::Timer()
37 {
38 this->start_time_.wall = 0;
39 this->start_time_.user = 0;
40 this->start_time_.sys = 0;
41 }
42
43 // Start couting the time.
44 void
45 Timer::start ()
46 {
47 this->get_time(&this->start_time_);
48 }
49
50 #if HAVE_SYSCONF && defined _SC_CLK_TCK
51 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
52 #else
53 # ifdef CLK_TCK
54 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
55 # else
56 # ifdef HZ
57 # define TICKS_PER_SECOND HZ /* traditional UNIX */
58 # else
59 # define TICKS_PER_SECOND 100 /* often the correct value */
60 # endif
61 # endif
62 #endif
63
64 // times returns statistics in clock_t units. This variable will hold the
65 // conversion factor to seconds. We use a variable that is initialize once
66 // because sysconf can be slow.
67 static long ticks_per_sec;
68 class Timer_init
69 {
70 public:
71 Timer_init()
72 {
73 ticks_per_sec = TICKS_PER_SECOND;
74 }
75 };
76 Timer_init timer_init;
77
78 // Write the current time infortamion.
79 void
80 Timer::get_time (TimeStats *now)
81 {
82 #ifdef HAVE_TIMES
83 tms t;
84 now->wall = (times(&t) * 1000) / ticks_per_sec;
85 now->user = (t.tms_utime * 1000) / ticks_per_sec;
86 now->sys = (t.tms_stime * 1000) / ticks_per_sec;
87 #else
88 now->wall = get_run_time() / 1000;
89 now->user = 0;
90 now->sys = 0;
91 #endif
92 }
93
94 // Return the stats since start was called.
95 Timer::TimeStats
96 Timer::get_elapsed_time ()
97 {
98 TimeStats now;
99 this->get_time(&now);
100 TimeStats delta;
101 delta.wall = now.wall - this->start_time_.wall;
102 delta.user = now.user - this->start_time_.user;
103 delta.sys = now.sys - this->start_time_.sys;
104 return delta;
105 }
106
107 }