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