time.hh revision 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;
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 * Authors: Steve Reinhardt
29 *          Nathan Binkert
30 */
31
32#ifndef __BASE_TIME_HH__
33#define __BASE_TIME_HH__
34
35#include <sys/time.h>
36
37#include <inttypes.h>
38
39#include <cmath>
40#include <cstring>
41#include <ctime>
42#include <iosfwd>
43#include <string>
44
45class Time
46{
47  protected:
48    timespec _time;
49
50    /**
51     * Internal time set function
52     */
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
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; }
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
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;
184};
185
186void sleep(const Time &time);
187
188inline bool
189operator==(const Time &l, const Time &r)
190{
191    return l.sec() == r.sec() && l.nsec() == r.nsec();
192}
193
194inline bool
195operator!=(const Time &l, const Time &r)
196{
197    return l.sec() != r.sec() || l.nsec() != r.nsec();
198}
199
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}
206
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}
213
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__
252