time.cc revision 2
1/* 2 * Copyright (c) 2003 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 29#include <sys/types.h> 30#include <sys/time.h> 31#include <time.h> 32#include <iostream> 33 34#include "sim_time.hh" 35 36using namespace std; 37 38namespace Time 39{ 40 struct _timeval 41 { 42 timeval tv; 43 }; 44 45 double 46 convert(const timeval &tv) 47 { 48 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; 49 } 50 51 Start::Start() 52 { 53 start = new _timeval; 54 ::gettimeofday(&start->tv, NULL); 55 } 56 57 Start::~Start() 58 { 59 delete start; 60 } 61 62 const timeval & 63 Start::get() const 64 { 65 return start->tv; 66 } 67 68 double 69 Start::operator()() const 70 { 71 return convert(get()); 72 } 73 74 Now::Now() 75 : now(0) 76 { 77 } 78 79 Now::~Now() 80 { 81 if (now) 82 delete now; 83 } 84 85 const timeval & 86 Now::get() const 87 { 88 if (!now) 89 now = new _timeval; 90 91 ::gettimeofday(&now->tv, NULL); 92 return now->tv; 93 } 94 95 double 96 Now::operator()() const 97 { 98 return convert(get()); 99 } 100 101 102 Elapsed::Elapsed() 103 : elapsed(0) 104 {} 105 106 Elapsed::~Elapsed() 107 { 108 if (elapsed) 109 delete elapsed; 110 } 111 112 const timeval & 113 Elapsed::get() const 114 { 115 if (!elapsed) 116 elapsed = new _timeval; 117 118 timersub(&now.get(), &start.get(), &elapsed->tv); 119 return elapsed->tv; 120 } 121 122 double 123 Elapsed::operator()() const 124 { 125 return convert(get()); 126 } 127 128 Start start; 129 Now now; 130 Elapsed elapsed; 131 132 ostream & 133 operator<<(ostream &out, const Start &start) 134 { 135 out << ::ctime(&start.get().tv_sec); 136 return out; 137 } 138 139 ostream & 140 operator<<(ostream &out, const Now &now) 141 { 142 out << ::ctime(&now.get().tv_sec); 143 return out; 144 } 145 146 ostream & 147 operator<<(ostream &out, const Elapsed &elapsed) 148 { 149 out << ::ctime(&elapsed.get().tv_sec); 150 return out; 151 } 152} 153