time.hh revision 7870:7cb62588fcdc
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 <iostream>
44#include <string>
45
46#include "base/types.hh"
47
48class Checkpoint;
49
50class Time
51{
52  protected:
53    timespec _time;
54
55    /**
56     * Internal time set function
57     */
58    void _set(bool monotonic);
59
60  public:
61    static const long NSEC_PER_SEC  = 1000 * 1000 * 1000;
62    static const long NSEC_PER_MSEC = 1000 * 1000;
63    static const long NSEC_PER_USEC = 1000;
64
65  public:
66    explicit Time() { clear(); }
67    explicit Time(double sec) { operator=(sec); }
68    Time(const Time &val) : _time(val._time) { }
69    Time(uint64_t sec, uint64_t nsec) { set(sec, nsec); }
70    Time(const timeval &tv) { operator=(tv); }
71    Time(const timespec &ts) { operator=(ts); }
72
73    /**
74     * Accessors for getting and setting the current clock
75     */
76    time_t sec() const { return _time.tv_sec; }
77    long msec() const { return _time.tv_nsec / NSEC_PER_MSEC; }
78    long usec() const { return _time.tv_nsec / NSEC_PER_USEC; }
79    long nsec() const { return _time.tv_nsec; }
80
81    void sec(time_t sec) { _time.tv_sec = sec; }
82    void msec(long msec) { _time.tv_nsec = msec * NSEC_PER_MSEC; }
83    void usec(long usec) { _time.tv_nsec = usec * NSEC_PER_USEC; }
84    void nsec(long nsec) { _time.tv_nsec = nsec; }
85
86    /**
87     * Clear the time
88     */
89    void clear() { memset(&_time, 0, sizeof(_time)); }
90
91    /**
92     * Use this to set time for the purposes of time measurement (use
93     * a monotonic clock if it is available
94     */
95    void setTimer() { _set(true); }
96
97    /**
98     * Use this to set the time to the actual current time
99     */
100    void setWallclock() { _set(false); }
101
102    /**
103     * Set the current time
104     */
105    void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
106
107    /**
108     * Set the current time from a value measured in Ticks
109     * @param ticks Number of ticks to convert into a time.
110     */
111    void setTick(Tick ticks);
112
113    /**
114     * Get the current time from a value measured in Ticks
115     * @return Time value measured in Ticks.
116     */
117    Tick getTick() const;
118
119    const Time &
120    operator=(const Time &other)
121    {
122        sec(other.sec());
123        nsec(other.nsec());
124        return *this;
125    }
126
127    const Time &
128    operator=(double new_time)
129    {
130        double seconds = floor(new_time);
131        sec((time_t)seconds);
132        nsec((long)((seconds - new_time) * 1e9));
133        return *this;
134    }
135
136    const Time &
137    operator=(const timeval &tv)
138    {
139        sec(tv.tv_sec);
140        nsec(tv.tv_usec * 1000);
141        return *this;
142    }
143
144    const Time &
145    operator=(const timespec &ts)
146    {
147        sec(ts.tv_sec);
148        nsec(ts.tv_nsec);
149        return *this;
150    }
151
152    /**
153     * Get the time in floating point seconds
154     */
155    operator double() const
156    {
157        return (double)sec() + ((double)nsec()) * 1e-9;
158    }
159
160    /**
161     * operators for time conversion
162     */
163    operator timespec() const { return _time; }
164    operator timeval() const
165    {
166        timeval tv;
167        tv.tv_sec = sec();
168        tv.tv_usec = usec();
169        return tv;
170    }
171
172    const Time &
173    operator+=(const Time &other)
174    {
175
176        _time.tv_sec += other.sec();
177        _time.tv_nsec += other.nsec();
178        if (_time.tv_nsec > NSEC_PER_SEC) {
179            _time.tv_sec++;
180            _time.tv_nsec -= NSEC_PER_SEC;
181        }
182
183        return *this;
184    }
185
186    const Time &
187    operator-=(const Time &other)
188    {
189        _time.tv_sec -= other.sec();
190        _time.tv_nsec -= other.nsec();
191        if (_time.tv_nsec < 0) {
192            _time.tv_sec--;
193            _time.tv_nsec += NSEC_PER_SEC;
194        }
195
196        return *this;
197    }
198
199    std::string date(const std::string &format = "") const;
200    std::string time() const;
201
202    void serialize(const std::string &base, std::ostream &os);
203    void unserialize(const std::string &base, Checkpoint *cp,
204                     const std::string &section);
205};
206
207void sleep(const Time &time);
208
209inline bool
210operator==(const Time &l, const Time &r)
211{
212    return l.sec() == r.sec() && l.nsec() == r.nsec();
213}
214
215inline bool
216operator!=(const Time &l, const Time &r)
217{
218    return 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 bool
229operator<=(const Time &l, const Time &r)
230{
231    return (l.sec() < r.sec()) ||
232        (l.sec() == r.sec() && l.nsec() <= r.nsec());
233}
234
235inline bool
236operator>(const Time &l, const Time &r)
237{
238    return (l.sec() > r.sec()) ||
239        (l.sec() == r.sec() && l.nsec() > r.nsec());
240}
241
242inline bool
243operator>=(const Time &l, const Time &r)
244{
245    return (l.sec() > r.sec()) ||
246        (l.sec() == r.sec() && l.nsec() >= r.nsec());
247}
248
249inline Time
250operator+(const Time &l, const Time &r)
251{
252    Time time(l);
253    time += r;
254    return time;
255}
256
257inline Time
258operator-(const Time &l, const Time &r)
259{
260    Time time(l);
261    time -= r;
262    return time;
263}
264
265inline std::ostream &
266operator<<(std::ostream &out, const Time &time)
267{
268    out << time.date();
269    return out;
270}
271
272#endif // __BASE_TIME_HH__
273