binutils/
[deliverable/binutils-gdb.git] / gold / timer.cc
CommitLineData
d675ff46
RÁE
1// timer.cc -- helper class for time accounting
2
bca1c3ae 3// Copyright 2009, 2010 Free Software Foundation, Inc.
d675ff46
RÁE
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
bca1c3ae 25#ifdef HAVE_TIMES
d675ff46 26#include <sys/times.h>
bca1c3ae 27#endif
d675ff46
RÁE
28
29#include "libiberty.h"
30
31#include "timer.h"
32
33namespace gold
34{
35
36// Class Timer
37
38Timer::Timer()
39{
40 this->start_time_.wall = 0;
41 this->start_time_.user = 0;
42 this->start_time_.sys = 0;
43}
44
9b547ce6 45// Start counting the time.
d675ff46 46void
ca09d69a 47Timer::start()
d675ff46
RÁE
48{
49 this->get_time(&this->start_time_);
50}
51
b490c0bb
CC
52// Record the time used by pass N (0 <= N <= 2).
53void
54Timer::stamp(int n)
55{
56 gold_assert(n >= 0 && n <= 2);
57 TimeStats& thispass = this->pass_times_[n];
58 this->get_time(&thispass);
59}
60
d675ff46
RÁE
61#if HAVE_SYSCONF && defined _SC_CLK_TCK
62# define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
63#else
64# ifdef CLK_TCK
65# define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
66# else
67# ifdef HZ
68# define TICKS_PER_SECOND HZ /* traditional UNIX */
69# else
70# define TICKS_PER_SECOND 100 /* often the correct value */
71# endif
72# endif
73#endif
74
9b547ce6
RW
75// times returns statistics in clock_t units. This variable will hold the
76// conversion factor to seconds. We use a variable that is initialized once
d675ff46
RÁE
77// because sysconf can be slow.
78static long ticks_per_sec;
79class Timer_init
80{
81 public:
82 Timer_init()
83 {
84 ticks_per_sec = TICKS_PER_SECOND;
85 }
86};
87Timer_init timer_init;
88
9b547ce6 89// Write the current time information.
d675ff46 90void
ca09d69a 91Timer::get_time(TimeStats *now)
d675ff46
RÁE
92{
93#ifdef HAVE_TIMES
94 tms t;
95 now->wall = (times(&t) * 1000) / ticks_per_sec;
96 now->user = (t.tms_utime * 1000) / ticks_per_sec;
97 now->sys = (t.tms_stime * 1000) / ticks_per_sec;
98#else
99 now->wall = get_run_time() / 1000;
100 now->user = 0;
101 now->sys = 0;
102#endif
103}
104
105// Return the stats since start was called.
106Timer::TimeStats
ca09d69a 107Timer::get_elapsed_time()
d675ff46
RÁE
108{
109 TimeStats now;
110 this->get_time(&now);
111 TimeStats delta;
112 delta.wall = now.wall - this->start_time_.wall;
113 delta.user = now.user - this->start_time_.user;
114 delta.sys = now.sys - this->start_time_.sys;
115 return delta;
116}
117
b490c0bb
CC
118// Return the stats for pass N (0 <= N <= 2).
119Timer::TimeStats
120Timer::get_pass_time(int n)
121{
122 gold_assert(n >= 0 && n <= 2);
123 TimeStats thispass = this->pass_times_[n];
124 TimeStats& lastpass = n > 0 ? this->pass_times_[n-1] : this->start_time_;
125 thispass.wall -= lastpass.wall;
126 thispass.user -= lastpass.user;
127 thispass.sys -= lastpass.sys;
128 return thispass;
129}
130
d675ff46 131}
This page took 0.139491 seconds and 4 git commands to generate.