time.hh (5543:3af77710f397) time.hh (7840:ed75cee5c793)
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;

--- 15 unchanged lines hidden (view full) ---

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 * Authors: Steve Reinhardt
29 * Nathan Binkert
30 */
31
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;

--- 15 unchanged lines hidden (view full) ---

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 * Authors: Steve Reinhardt
29 * Nathan Binkert
30 */
31
32#ifndef __SIM_TIME_HH__
33#define __SIM_TIME_HH__
32#ifndef __BASE_TIME_HH__
33#define __BASE_TIME_HH__
34
35#include <sys/time.h>
36
34
35#include <sys/time.h>
36
37#include <inttypes.h>
38
39#include <cmath>
40#include <cstring>
41#include <ctime>
37#include <iosfwd>
38#include <string>
39
42#include <iosfwd>
43#include <string>
44
40struct _timeval;
41
42class Time
43{
44 protected:
45class Time
46{
47 protected:
45 mutable _timeval *time;
48 timespec _time;
46
49
50 /**
51 * Internal time set function
52 */
53 void _set(bool monotonic);
54
47 public:
55 public:
48 explicit Time(bool set_now = false);
49 Time(const timeval &val);
50 Time(const Time &val);
51 ~Time();
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;
52
59
53 void set();
54 const timeval &get() const;
55 void set(const timeval &val);
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); }
56
67
57 double operator()() const;
58 std::string date(std::string format = "") const;
68 /**
69 * Accessors for getting and setting the current clock
70 */
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; }
59
75
60 public:
61 static const Time start;
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
81 /**
82 * Clear the time
83 */
84 void clear() { memset(&_time, 0, sizeof(_time)); }
85
86 /**
87 * Use this to set time for the purposes of time measurement (use
88 * a monotonic clock if it is available
89 */
90 void setTimer() { _set(true); }
91
92 /**
93 * Use this to set the time to the actual current time
94 */
95 void setWallclock() { _set(false); }
96
97 /**
98 * Set the current time
99 */
100 void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
101
102 const Time &
103 operator=(const Time &other)
104 {
105 sec(other.sec());
106 nsec(other.nsec());
107 return *this;
108 }
109
110 const Time &
111 operator=(double new_time)
112 {
113 double seconds = floor(new_time);
114 sec((time_t)seconds);
115 nsec((long)((seconds - new_time) * 1e9));
116 return *this;
117 }
118
119 const Time &
120 operator=(const timeval &tv)
121 {
122 sec(tv.tv_sec);
123 nsec(tv.tv_usec * 1000);
124 return *this;
125 }
126
127 const Time &
128 operator=(const timespec &ts)
129 {
130 sec(ts.tv_sec);
131 nsec(ts.tv_nsec);
132 return *this;
133 }
134
135 /**
136 * Get the time in floating point seconds
137 */
138 operator double() const
139 {
140 return (double)sec() + ((double)nsec()) * 1e-9;
141 }
142
143 /**
144 * operators for time conversion
145 */
146 operator timespec() const { return _time; }
147 operator timeval() const
148 {
149 timeval tv;
150 tv.tv_sec = sec();
151 tv.tv_usec = usec();
152 return tv;
153 }
154
155 const Time &
156 operator+=(const Time &other)
157 {
158
159 _time.tv_sec += other.sec();
160 _time.tv_nsec += other.nsec();
161 if (_time.tv_nsec > NSEC_PER_SEC) {
162 _time.tv_sec++;
163 _time.tv_nsec -= NSEC_PER_SEC;
164 }
165
166 return *this;
167 }
168
169 const Time &
170 operator-=(const Time &other)
171 {
172 _time.tv_sec -= other.sec();
173 _time.tv_nsec -= other.nsec();
174 if (_time.tv_nsec < 0) {
175 _time.tv_sec--;
176 _time.tv_nsec += NSEC_PER_SEC;
177 }
178
179 return *this;
180 }
181
182 std::string date(const std::string &format = "") const;
183 std::string time() const;
62};
63
184};
185
64Time operator-(const Time &l, const Time &r);
186void sleep(const Time &time);
65
187
66std::ostream &operator<<(std::ostream &out, const Time &time);
188inline bool
189operator==(const Time &l, const Time &r)
190{
191 return l.sec() == r.sec() && l.nsec() == r.nsec();
192}
67
193
194inline bool
195operator!=(const Time &l, const Time &r)
196{
197 return l.sec() != r.sec() || l.nsec() != r.nsec();
198}
68
199
69/*
70 * Copyright (c) 1982, 1986, 1993
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 *
97 * @(#)time.h 8.2 (Berkeley) 7/10/94
98 */
200inline bool
201operator<(const Time &l, const Time &r)
202{
203 return (l.sec() < r.sec()) ||
204 (l.sec() == r.sec() && l.nsec() < r.nsec());
205}
99
206
100#if defined(__sun)
101#define timersub(tvp, uvp, vvp) \
102 do { \
103 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
104 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
105 if ((vvp)->tv_usec < 0) { \
106 (vvp)->tv_sec--; \
107 (vvp)->tv_usec += 1000000; \
108 } \
109 } while (0)
110#endif
207inline bool
208operator<=(const Time &l, const Time &r)
209{
210 return (l.sec() < r.sec()) ||
211 (l.sec() == r.sec() && l.nsec() <= r.nsec());
212}
111
213
112#endif // __SIM_TIME_HH__
214inline bool
215operator>(const Time &l, const Time &r)
216{
217 return (l.sec() > r.sec()) ||
218 (l.sec() == r.sec() && l.nsec() > r.nsec());
219}
220
221inline bool
222operator>=(const Time &l, const Time &r)
223{
224 return (l.sec() > r.sec()) ||
225 (l.sec() == r.sec() && l.nsec() >= r.nsec());
226}
227
228inline Time
229operator+(const Time &l, const Time &r)
230{
231 Time time(l);
232 time += r;
233 return time;
234}
235
236inline Time
237operator-(const Time &l, const Time &r)
238{
239 Time time(l);
240 time -= r;
241 return time;
242}
243
244inline std::ostream &
245operator<<(std::ostream &out, const Time &time)
246{
247 out << time.date();
248 return out;
249}
250
251#endif // __BASE_TIME_HH__