gem5  v20.1.0.0
time.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003-2005 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 #ifndef __BASE_TIME_HH__
30 #define __BASE_TIME_HH__
31 
32 #include <sys/time.h>
33 #include <inttypes.h>
34 
35 #include <cmath>
36 #include <cstring>
37 #include <ctime>
38 #include <iosfwd>
39 #include <iostream>
40 #include <string>
41 
42 #include "base/types.hh"
43 #include "sim/serialize.hh"
44 
45 class Time
46 {
47  protected:
48  timespec _time;
49 
53  void _set(bool monotonic);
54 
55  public:
56  static const long NSEC_PER_SEC = 1000 * 1000 * 1000;
57  static const long NSEC_PER_MSEC = 1000 * 1000;
58  static const long NSEC_PER_USEC = 1000;
59 
60  public:
61  explicit Time() { clear(); }
62  explicit Time(double sec) { operator=(sec); }
63  Time(const Time &val) : _time(val._time) { }
64  Time(uint64_t sec, uint64_t nsec) { set(sec, nsec); }
65  Time(const timeval &tv) { operator=(tv); }
66  Time(const timespec &ts) { operator=(ts); }
67 
71  time_t sec() const { return _time.tv_sec; }
72  long msec() const { return _time.tv_nsec / NSEC_PER_MSEC; }
73  long usec() const { return _time.tv_nsec / NSEC_PER_USEC; }
74  long nsec() const { return _time.tv_nsec; }
75 
76  void sec(time_t sec) { _time.tv_sec = sec; }
77  void msec(long msec) { _time.tv_nsec = msec * NSEC_PER_MSEC; }
78  void usec(long usec) { _time.tv_nsec = usec * NSEC_PER_USEC; }
79  void nsec(long nsec) { _time.tv_nsec = nsec; }
80 
84  void clear() { memset(&_time, 0, sizeof(_time)); }
85 
90  void setTimer() { _set(true); }
91 
95  void setWallclock() { _set(false); }
96 
100  void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
101 
106  void setTick(Tick ticks);
107 
112  Tick getTick() const;
113 
114  const Time &
115  operator=(const Time &other)
116  {
117  sec(other.sec());
118  nsec(other.nsec());
119  return *this;
120  }
121 
122  const Time &
123  operator=(double new_time)
124  {
125  double seconds = floor(new_time);
126  sec((time_t)seconds);
127  nsec((long)((seconds - new_time) * 1e9));
128  return *this;
129  }
130 
131  const Time &
132  operator=(const timeval &tv)
133  {
134  sec(tv.tv_sec);
135  nsec(tv.tv_usec * 1000);
136  return *this;
137  }
138 
139  const Time &
140  operator=(const timespec &ts)
141  {
142  sec(ts.tv_sec);
143  nsec(ts.tv_nsec);
144  return *this;
145  }
146 
150  operator double() const
151  {
152  return (double)sec() + ((double)nsec()) * 1e-9;
153  }
154 
158  operator timespec() const { return _time; }
159  operator timeval() const
160  {
161  timeval tv;
162  tv.tv_sec = sec();
163  tv.tv_usec = usec();
164  return tv;
165  }
166 
167  const Time &
168  operator+=(const Time &other)
169  {
170 
171  _time.tv_sec += other.sec();
172  _time.tv_nsec += other.nsec();
173  if (_time.tv_nsec > NSEC_PER_SEC) {
174  _time.tv_sec++;
175  _time.tv_nsec -= NSEC_PER_SEC;
176  }
177 
178  return *this;
179  }
180 
181  const Time &
182  operator-=(const Time &other)
183  {
184  _time.tv_sec -= other.sec();
185  _time.tv_nsec -= other.nsec();
186  if (_time.tv_nsec < 0) {
187  _time.tv_sec--;
188  _time.tv_nsec += NSEC_PER_SEC;
189  }
190 
191  return *this;
192  }
193 
194  std::string date(const std::string &format = "") const;
195  std::string time() const;
196 
197  void serialize(const std::string &base, CheckpointOut &cp) const;
198  void unserialize(const std::string &base, CheckpointIn &cp);
199 };
200 
201 void sleep(const Time &time);
202 
203 inline bool
204 operator==(const Time &l, const Time &r)
205 {
206  return l.sec() == r.sec() && l.nsec() == r.nsec();
207 }
208 
209 inline bool
210 operator!=(const Time &l, const Time &r)
211 {
212  return l.sec() != r.sec() || l.nsec() != r.nsec();
213 }
214 
215 inline bool
216 operator<(const Time &l, const Time &r)
217 {
218  return (l.sec() < r.sec()) ||
219  (l.sec() == r.sec() && l.nsec() < r.nsec());
220 }
221 
222 inline bool
223 operator<=(const Time &l, const Time &r)
224 {
225  return (l.sec() < r.sec()) ||
226  (l.sec() == r.sec() && l.nsec() <= r.nsec());
227 }
228 
229 inline bool
230 operator>(const Time &l, const Time &r)
231 {
232  return (l.sec() > r.sec()) ||
233  (l.sec() == r.sec() && l.nsec() > r.nsec());
234 }
235 
236 inline bool
237 operator>=(const Time &l, const Time &r)
238 {
239  return (l.sec() > r.sec()) ||
240  (l.sec() == r.sec() && l.nsec() >= r.nsec());
241 }
242 
243 inline Time
244 operator+(const Time &l, const Time &r)
245 {
246  Time time(l);
247  time += r;
248  return time;
249 }
250 
251 inline Time
252 operator-(const Time &l, const Time &r)
253 {
254  Time time(l);
255  time -= r;
256  return time;
257 }
258 
259 inline std::ostream &
260 operator<<(std::ostream &out, const Time &time)
261 {
262  out << time.date();
263  return out;
264 }
265 
266 time_t mkutctime(struct tm *time);
267 
268 #endif // __BASE_TIME_HH__
Time::set
void set(time_t _sec, long _nsec)
Set the current time.
Definition: time.hh:100
Time::Time
Time(const timeval &tv)
Definition: time.hh:65
Time::operator-=
const Time & operator-=(const Time &other)
Definition: time.hh:182
Time::Time
Time(const Time &val)
Definition: time.hh:63
Time::setTick
void setTick(Tick ticks)
Set the current time from a value measured in Ticks.
Definition: time.cc:56
Time::Time
Time(const timespec &ts)
Definition: time.hh:66
serialize.hh
Time::operator=
const Time & operator=(double new_time)
Definition: time.hh:123
Time::operator=
const Time & operator=(const timespec &ts)
Definition: time.hh:140
operator-
Time operator-(const Time &l, const Time &r)
Definition: time.hh:252
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
operator>=
bool operator>=(const Time &l, const Time &r)
Definition: time.hh:237
operator<
bool operator<(const Time &l, const Time &r)
Definition: time.hh:216
Time::date
std::string date(const std::string &format="") const
Definition: time.cc:72
sleep
void sleep(const Time &time)
Definition: time.cc:141
operator<<
std::ostream & operator<<(std::ostream &out, const Time &time)
Definition: time.hh:260
Time::_set
void _set(bool monotonic)
Internal time set function.
Definition: time.cc:44
operator!=
bool operator!=(const Time &l, const Time &r)
Definition: time.hh:210
Time::msec
long msec() const
Definition: time.hh:72
ArmISA::ts
Bitfield< 55, 52 > ts
Definition: miscregs_types.hh:89
cp
Definition: cprintf.cc:40
Time::_time
timespec _time
Definition: time.hh:48
operator==
bool operator==(const Time &l, const Time &r)
Definition: time.hh:204
Time::unserialize
void unserialize(const std::string &base, CheckpointIn &cp)
Definition: time.cc:130
Time::time
std::string time() const
Definition: time.cc:93
Time::msec
void msec(long msec)
Definition: time.hh:77
Time::operator+=
const Time & operator+=(const Time &other)
Definition: time.hh:168
Time::setWallclock
void setWallclock()
Use this to set the time to the actual current time.
Definition: time.hh:95
MipsISA::r
r
Definition: pra_constants.hh:95
Time::Time
Time(double sec)
Definition: time.hh:62
Time::nsec
long nsec() const
Definition: time.hh:74
Time::Time
Time(uint64_t sec, uint64_t nsec)
Definition: time.hh:64
Time::nsec
void nsec(long nsec)
Definition: time.hh:79
operator>
bool operator>(const Time &l, const Time &r)
Definition: time.hh:230
mkutctime
time_t mkutctime(struct tm *time)
Definition: time.cc:153
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
Time::sec
void sec(time_t sec)
Definition: time.hh:76
Time::NSEC_PER_MSEC
static const long NSEC_PER_MSEC
Definition: time.hh:57
Time::usec
long usec() const
Definition: time.hh:73
Time::getTick
Tick getTick() const
Get the current time from a value measured in Ticks.
Definition: time.cc:65
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
Time::serialize
void serialize(const std::string &base, CheckpointOut &cp) const
Definition: time.cc:123
Time::operator=
const Time & operator=(const timeval &tv)
Definition: time.hh:132
Time::setTimer
void setTimer()
Use this to set time for the purposes of time measurement (use a monotonic clock if it is available.
Definition: time.hh:90
operator<=
bool operator<=(const Time &l, const Time &r)
Definition: time.hh:223
Time::NSEC_PER_USEC
static const long NSEC_PER_USEC
Definition: time.hh:58
types.hh
Time
Definition: time.hh:45
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
Time::Time
Time()
Definition: time.hh:61
operator+
Time operator+(const Time &l, const Time &r)
Definition: time.hh:244
ArmISA::format
Bitfield< 31, 29 > format
Definition: miscregs_types.hh:640
Time::NSEC_PER_SEC
static const long NSEC_PER_SEC
Definition: time.hh:56
CheckpointIn
Definition: serialize.hh:67
MipsISA::l
Bitfield< 5 > l
Definition: pra_constants.hh:320
Time::clear
void clear()
Clear the time.
Definition: time.hh:84
Time::usec
void usec(long usec)
Definition: time.hh:78
Time::operator=
const Time & operator=(const Time &other)
Definition: time.hh:115
Time::sec
time_t sec() const
Accessors for getting and setting the current clock.
Definition: time.hh:71

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17