Instead of keeping track of the fraction of time that we're
[gem5.git] / sim / sim_time.cc
1 /*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <time.h>
32 #include <iostream>
33
34 #include "sim/sim_time.hh"
35
36 using namespace std;
37
38 namespace Time
39 {
40 struct _timeval
41 {
42 timeval tv;
43 };
44
45 double
46 convert(const timeval &tv)
47 {
48 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
49 }
50
51 Start::Start()
52 {
53 start = new _timeval;
54 ::gettimeofday(&start->tv, NULL);
55 }
56
57 Start::~Start()
58 {
59 delete start;
60 }
61
62 const timeval &
63 Start::get() const
64 {
65 return start->tv;
66 }
67
68 void
69 Start::reset()
70 {
71 ::gettimeofday(&start->tv, NULL);
72 }
73
74 double
75 Start::operator()() const
76 {
77 return convert(get());
78 }
79
80 Now::Now()
81 : now(0)
82 {
83 }
84
85 Now::~Now()
86 {
87 if (now)
88 delete now;
89 }
90
91 const timeval &
92 Now::get() const
93 {
94 if (!now)
95 now = new _timeval;
96
97 ::gettimeofday(&now->tv, NULL);
98 return now->tv;
99 }
100
101 double
102 Now::operator()() const
103 {
104 return convert(get());
105 }
106
107
108 Elapsed::Elapsed()
109 : elapsed(0)
110 {}
111
112 Elapsed::~Elapsed()
113 {
114 if (elapsed)
115 delete elapsed;
116 }
117
118 const timeval &
119 Elapsed::get() const
120 {
121 if (!elapsed)
122 elapsed = new _timeval;
123
124 timersub(&_now.get(), &_start.get(), &elapsed->tv);
125 return elapsed->tv;
126 }
127
128 void
129 Elapsed::reset()
130 {
131 _start.reset();
132 }
133
134 double
135 Elapsed::operator()() const
136 {
137 return convert(get());
138 }
139
140 Start start;
141 Now now;
142 Elapsed elapsed;
143
144 ostream &
145 operator<<(ostream &out, const Start &start)
146 {
147 out << ::ctime((const time_t *)&start.get().tv_sec);
148 return out;
149 }
150
151 ostream &
152 operator<<(ostream &out, const Now &now)
153 {
154 out << ::ctime((const time_t *)&now.get().tv_sec);
155 return out;
156 }
157
158 ostream &
159 operator<<(ostream &out, const Elapsed &elapsed)
160 {
161 out << ::ctime((const time_t *)&elapsed.get().tv_sec);
162 return out;
163 }
164 }