gem5  v21.1.0.2
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 namespace gem5
46 {
47 
48 class Time
49 {
50  protected:
51  timespec _time;
52 
56  void _set(bool monotonic);
57 
58  public:
59  static const long NSEC_PER_SEC = 1000 * 1000 * 1000;
60  static const long NSEC_PER_MSEC = 1000 * 1000;
61  static const long NSEC_PER_USEC = 1000;
62 
63  public:
64  explicit Time() { clear(); }
65  explicit Time(double sec) { operator=(sec); }
66  Time(const Time &val) : _time(val._time) { }
67  Time(uint64_t sec, uint64_t nsec) { set(sec, nsec); }
68  Time(const timeval &tv) { operator=(tv); }
69  Time(const timespec &ts) { operator=(ts); }
70 
74  time_t sec() const { return _time.tv_sec; }
75  long msec() const { return _time.tv_nsec / NSEC_PER_MSEC; }
76  long usec() const { return _time.tv_nsec / NSEC_PER_USEC; }
77  long nsec() const { return _time.tv_nsec; }
78 
79  void sec(time_t sec) { _time.tv_sec = sec; }
80  void msec(long msec) { _time.tv_nsec = msec * NSEC_PER_MSEC; }
81  void usec(long usec) { _time.tv_nsec = usec * NSEC_PER_USEC; }
82  void nsec(long nsec) { _time.tv_nsec = nsec; }
83 
87  void clear() { memset(&_time, 0, sizeof(_time)); }
88 
93  void setTimer() { _set(true); }
94 
98  void setWallclock() { _set(false); }
99 
103  void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
104 
109  void setTick(Tick ticks);
110 
115  Tick getTick() const;
116 
117  const Time &
118  operator=(const Time &other)
119  {
120  sec(other.sec());
121  nsec(other.nsec());
122  return *this;
123  }
124 
125  const Time &
126  operator=(double new_time)
127  {
128  double seconds = floor(new_time);
129  sec((time_t)seconds);
130  nsec((long)((seconds - new_time) * 1e9));
131  return *this;
132  }
133 
134  const Time &
135  operator=(const timeval &tv)
136  {
137  sec(tv.tv_sec);
138  nsec(tv.tv_usec * 1000);
139  return *this;
140  }
141 
142  const Time &
143  operator=(const timespec &ts)
144  {
145  sec(ts.tv_sec);
146  nsec(ts.tv_nsec);
147  return *this;
148  }
149 
153  operator double() const
154  {
155  return (double)sec() + ((double)nsec()) * 1e-9;
156  }
157 
161  operator timespec() const { return _time; }
162  operator timeval() const
163  {
164  timeval tv;
165  tv.tv_sec = sec();
166  tv.tv_usec = usec();
167  return tv;
168  }
169 
170  const Time &
171  operator+=(const Time &other)
172  {
173 
174  _time.tv_sec += other.sec();
175  _time.tv_nsec += other.nsec();
176  if (_time.tv_nsec > NSEC_PER_SEC) {
177  _time.tv_sec++;
178  _time.tv_nsec -= NSEC_PER_SEC;
179  }
180 
181  return *this;
182  }
183 
184  const Time &
185  operator-=(const Time &other)
186  {
187  _time.tv_sec -= other.sec();
188  _time.tv_nsec -= other.nsec();
189  if (_time.tv_nsec < 0) {
190  _time.tv_sec--;
191  _time.tv_nsec += NSEC_PER_SEC;
192  }
193 
194  return *this;
195  }
196 
197  std::string date(const std::string &format = "") const;
198  std::string time() const;
199 
200  void serialize(const std::string &base, CheckpointOut &cp) const;
201  void unserialize(const std::string &base, CheckpointIn &cp);
202 };
203 
204 void sleep(const Time &time);
205 
206 inline bool
207 operator==(const Time &l, const Time &r)
208 {
209  return l.sec() == r.sec() && l.nsec() == r.nsec();
210 }
211 
212 inline bool
213 operator!=(const Time &l, const Time &r)
214 {
215  return l.sec() != r.sec() || l.nsec() != r.nsec();
216 }
217 
218 inline bool
219 operator<(const Time &l, const Time &r)
220 {
221  return (l.sec() < r.sec()) ||
222  (l.sec() == r.sec() && l.nsec() < r.nsec());
223 }
224 
225 inline bool
226 operator<=(const Time &l, const Time &r)
227 {
228  return (l.sec() < r.sec()) ||
229  (l.sec() == r.sec() && l.nsec() <= r.nsec());
230 }
231 
232 inline bool
233 operator>(const Time &l, const Time &r)
234 {
235  return (l.sec() > r.sec()) ||
236  (l.sec() == r.sec() && l.nsec() > r.nsec());
237 }
238 
239 inline bool
240 operator>=(const Time &l, const Time &r)
241 {
242  return (l.sec() > r.sec()) ||
243  (l.sec() == r.sec() && l.nsec() >= r.nsec());
244 }
245 
246 inline Time
247 operator+(const Time &l, const Time &r)
248 {
249  Time time(l);
250  time += r;
251  return time;
252 }
253 
254 inline Time
255 operator-(const Time &l, const Time &r)
256 {
257  Time time(l);
258  time -= r;
259  return time;
260 }
261 
262 inline std::ostream &
263 operator<<(std::ostream &out, const Time &time)
264 {
265  out << time.date();
266  return out;
267 }
268 
269 time_t mkutctime(struct tm *time);
270 
271 } // namespace gem5
272 
273 #endif // __BASE_TIME_HH__
gem5::operator<<
std::ostream & operator<<(std::ostream &os, const ArmSemihosting::InPlaceArg &ipa)
Definition: semihosting.cc:1045
gem5::operator!=
bool operator!=(const RefCountingPtr< T > &l, const RefCountingPtr< T > &r)
Check for inequality of two reference counting pointers.
Definition: refcnt.hh:294
gem5::operator>=
bool operator>=(const Time &l, const Time &r)
Definition: time.hh:240
gem5::ArmISA::format
Bitfield< 31, 29 > format
Definition: misc_types.hh:646
gem5::Time::sec
void sec(time_t sec)
Definition: time.hh:79
gem5::Time::Time
Time()
Definition: time.hh:64
serialize.hh
gem5::Time::Time
Time(const Time &val)
Definition: time.hh:66
gem5::Time::msec
long msec() const
Definition: time.hh:75
gem5::sleep
void sleep(const Time &time)
Definition: time.cc:142
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:64
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
gem5::Time::sec
time_t sec() const
Accessors for getting and setting the current clock.
Definition: time.hh:74
gem5::X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
gem5::Time::getTick
Tick getTick() const
Get the current time from a value measured in Ticks.
Definition: time.cc:66
gem5::Time::_set
void _set(bool monotonic)
Internal time set function.
Definition: time.cc:45
gem5::Time::msec
void msec(long msec)
Definition: time.hh:80
gem5::Time::setWallclock
void setWallclock()
Use this to set the time to the actual current time.
Definition: time.hh:98
gem5::operator<=
bool operator<=(const Time &l, const Time &r)
Definition: time.hh:226
gem5::Time::Time
Time(const timespec &ts)
Definition: time.hh:69
gem5::Time::operator-=
const Time & operator-=(const Time &other)
Definition: time.hh:185
gem5::Time::date
std::string date(const std::string &format="") const
Definition: time.cc:73
gem5::Time::_time
timespec _time
Definition: time.hh:51
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::Time::nsec
long nsec() const
Definition: time.hh:77
gem5::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:93
gem5::operator==
bool operator==(const Pixel &lhs, const Pixel &rhs)
Definition: pixel.hh:73
gem5::Time::serialize
void serialize(const std::string &base, CheckpointOut &cp) const
Definition: time.cc:124
gem5::Time::operator=
const Time & operator=(const Time &other)
Definition: time.hh:118
gem5::operator+
Time operator+(const Time &l, const Time &r)
Definition: time.hh:247
gem5::Time::setTick
void setTick(Tick ticks)
Set the current time from a value measured in Ticks.
Definition: time.cc:57
gem5::MipsISA::l
Bitfield< 5 > l
Definition: pra_constants.hh:323
gem5::Time
Definition: time.hh:48
gem5::Time::set
void set(time_t _sec, long _nsec)
Set the current time.
Definition: time.hh:103
gem5::Time::operator=
const Time & operator=(const timespec &ts)
Definition: time.hh:143
gem5::Time::operator=
const Time & operator=(double new_time)
Definition: time.hh:126
gem5::Time::NSEC_PER_USEC
static const long NSEC_PER_USEC
Definition: time.hh:61
gem5::Time::usec
void usec(long usec)
Definition: time.hh:81
gem5::PowerISA::tm
Bitfield< 32 > tm
Definition: misc.hh:107
gem5::Time::Time
Time(double sec)
Definition: time.hh:65
gem5::operator>
bool operator>(const Time &l, const Time &r)
Definition: time.hh:233
gem5::mkutctime
time_t mkutctime(struct tm *time)
Definition: time.cc:154
gem5::operator-
Time operator-(const Time &l, const Time &r)
Definition: time.hh:255
gem5::Time::Time
Time(uint64_t sec, uint64_t nsec)
Definition: time.hh:67
gem5::Time::usec
long usec() const
Definition: time.hh:76
types.hh
gem5::Time::operator=
const Time & operator=(const timeval &tv)
Definition: time.hh:135
gem5::operator<
bool operator<(const Time &l, const Time &r)
Definition: time.hh:219
gem5::ArmISA::ts
Bitfield< 55, 52 > ts
Definition: misc_types.hh:92
gem5::MipsISA::r
r
Definition: pra_constants.hh:98
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::Time::NSEC_PER_SEC
static const long NSEC_PER_SEC
Definition: time.hh:59
gem5::Time::NSEC_PER_MSEC
static const long NSEC_PER_MSEC
Definition: time.hh:60
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::Time::Time
Time(const timeval &tv)
Definition: time.hh:68
gem5::Time::time
std::string time() const
Definition: time.cc:94
gem5::Time::clear
void clear()
Clear the time.
Definition: time.hh:87
gem5::Time::nsec
void nsec(long nsec)
Definition: time.hh:82
gem5::Time::operator+=
const Time & operator+=(const Time &other)
Definition: time.hh:171
gem5::Time::unserialize
void unserialize(const std::string &base, CheckpointIn &cp)
Definition: time.cc:131

Generated on Tue Sep 21 2021 12:25:00 for gem5 by doxygen 1.8.17