time.hh revision 10905:a6ca6831e775
12632Sstever@eecs.umich.edu/*
22632Sstever@eecs.umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
32632Sstever@eecs.umich.edu * All rights reserved.
42632Sstever@eecs.umich.edu *
52632Sstever@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
62632Sstever@eecs.umich.edu * modification, are permitted provided that the following conditions are
72632Sstever@eecs.umich.edu * met: redistributions of source code must retain the above copyright
86498Snate@binkert.org * notice, this list of conditions and the following disclaimer;
94479Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104479Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
112632Sstever@eecs.umich.edu * documentation and/or other materials provided with the distribution;
122632Sstever@eecs.umich.edu * neither the name of the copyright holders nor the names of its
132632Sstever@eecs.umich.edu * contributors may be used to endorse or promote products derived from
142632Sstever@eecs.umich.edu * this software without specific prior written permission.
152632Sstever@eecs.umich.edu *
162632Sstever@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172632Sstever@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182632Sstever@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192632Sstever@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202632Sstever@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212632Sstever@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222632Sstever@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232632Sstever@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246498Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252632Sstever@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262632Sstever@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272632Sstever@eecs.umich.edu *
282632Sstever@eecs.umich.edu * Authors: Steve Reinhardt
292632Sstever@eecs.umich.edu *          Nathan Binkert
302632Sstever@eecs.umich.edu */
312632Sstever@eecs.umich.edu
322632Sstever@eecs.umich.edu#ifndef __BASE_TIME_HH__
332632Sstever@eecs.umich.edu#define __BASE_TIME_HH__
346498Snate@binkert.org
352632Sstever@eecs.umich.edu#include <sys/time.h>
362632Sstever@eecs.umich.edu#include <inttypes.h>
372632Sstever@eecs.umich.edu
382632Sstever@eecs.umich.edu#include <cmath>
392632Sstever@eecs.umich.edu#include <cstring>
402632Sstever@eecs.umich.edu#include <ctime>
412632Sstever@eecs.umich.edu#include <iosfwd>
422632Sstever@eecs.umich.edu#include <iostream>
432632Sstever@eecs.umich.edu#include <string>
442632Sstever@eecs.umich.edu
452632Sstever@eecs.umich.edu#include "base/types.hh"
462632Sstever@eecs.umich.edu#include "sim/serialize.hh"
472632Sstever@eecs.umich.edu
482632Sstever@eecs.umich.educlass Time
492632Sstever@eecs.umich.edu{
502632Sstever@eecs.umich.edu  protected:
512632Sstever@eecs.umich.edu    timespec _time;
522632Sstever@eecs.umich.edu
536498Snate@binkert.org    /**
542632Sstever@eecs.umich.edu     * Internal time set function
552632Sstever@eecs.umich.edu     */
562632Sstever@eecs.umich.edu    void _set(bool monotonic);
576498Snate@binkert.org
582632Sstever@eecs.umich.edu  public:
592632Sstever@eecs.umich.edu    static const long NSEC_PER_SEC  = 1000 * 1000 * 1000;
602632Sstever@eecs.umich.edu    static const long NSEC_PER_MSEC = 1000 * 1000;
612632Sstever@eecs.umich.edu    static const long NSEC_PER_USEC = 1000;
622632Sstever@eecs.umich.edu
632632Sstever@eecs.umich.edu  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
71    /**
72     * Accessors for getting and setting the current clock
73     */
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
84    /**
85     * Clear the time
86     */
87    void clear() { memset(&_time, 0, sizeof(_time)); }
88
89    /**
90     * Use this to set time for the purposes of time measurement (use
91     * a monotonic clock if it is available
92     */
93    void setTimer() { _set(true); }
94
95    /**
96     * Use this to set the time to the actual current time
97     */
98    void setWallclock() { _set(false); }
99
100    /**
101     * Set the current time
102     */
103    void set(time_t _sec, long _nsec) { sec(_sec); nsec(_nsec); }
104
105    /**
106     * Set the current time from a value measured in Ticks
107     * @param ticks Number of ticks to convert into a time.
108     */
109    void setTick(Tick ticks);
110
111    /**
112     * Get the current time from a value measured in Ticks
113     * @return Time value measured in Ticks.
114     */
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
150    /**
151     * Get the time in floating point seconds
152     */
153    operator double() const
154    {
155        return (double)sec() + ((double)nsec()) * 1e-9;
156    }
157
158    /**
159     * operators for time conversion
160     */
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
204void sleep(const Time &time);
205
206inline bool
207operator==(const Time &l, const Time &r)
208{
209    return l.sec() == r.sec() && l.nsec() == r.nsec();
210}
211
212inline bool
213operator!=(const Time &l, const Time &r)
214{
215    return l.sec() != r.sec() || l.nsec() != r.nsec();
216}
217
218inline bool
219operator<(const Time &l, const Time &r)
220{
221    return (l.sec() < r.sec()) ||
222        (l.sec() == r.sec() && l.nsec() < r.nsec());
223}
224
225inline bool
226operator<=(const Time &l, const Time &r)
227{
228    return (l.sec() < r.sec()) ||
229        (l.sec() == r.sec() && l.nsec() <= r.nsec());
230}
231
232inline bool
233operator>(const Time &l, const Time &r)
234{
235    return (l.sec() > r.sec()) ||
236        (l.sec() == r.sec() && l.nsec() > r.nsec());
237}
238
239inline bool
240operator>=(const Time &l, const Time &r)
241{
242    return (l.sec() > r.sec()) ||
243        (l.sec() == r.sec() && l.nsec() >= r.nsec());
244}
245
246inline Time
247operator+(const Time &l, const Time &r)
248{
249    Time time(l);
250    time += r;
251    return time;
252}
253
254inline Time
255operator-(const Time &l, const Time &r)
256{
257    Time time(l);
258    time -= r;
259    return time;
260}
261
262inline std::ostream &
263operator<<(std::ostream &out, const Time &time)
264{
265    out << time.date();
266    return out;
267}
268
269time_t mkutctime(struct tm *time);
270
271#endif // __BASE_TIME_HH__
272