SLICC: Use pointers for directory entries
[gem5.git] / src / base / time.cc
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: Nathan Binkert
29 */
30
31 #include <iostream>
32 #include <sstream>
33
34 #include "base/time.hh"
35 #include "config/use_posix_clock.hh"
36 #include "sim/core.hh"
37 #include "sim/serialize.hh"
38
39 using namespace std;
40
41 void
42 Time::_set(bool monotonic)
43 {
44 #if USE_POSIX_CLOCK
45 ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
46 #else
47 timeval tv;
48 ::gettimeofday(&tv, NULL);
49 operator=(tv);
50 #endif
51 }
52
53 void
54 Time::setTick(Tick ticks)
55 {
56 uint64_t nsecs = ticks / SimClock::Int::ns;
57 set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC);
58 }
59
60 Tick
61 Time::getTick() const
62 {
63 return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns;
64 }
65
66 string
67 Time::date(const string &format) const
68 {
69 time_t sec = this->sec();
70 char buf[256];
71
72 if (format.empty()) {
73 #ifdef __SUNPRO_CC
74 ctime_r(&sec, buf, sizeof(buf));
75 #else
76 ctime_r(&sec, buf);
77 #endif
78 buf[24] = '\0';
79 return buf;
80 }
81
82 struct tm *tm = localtime(&sec);
83 strftime(buf, sizeof(buf), format.c_str(), tm);
84 return buf;
85 }
86
87 string
88 Time::time() const
89 {
90 double time = double(*this);
91 double secs = fmod(time, 60.0);
92 double all_mins = floor(time / 60.0);
93 double mins = fmod(all_mins, 60.0);
94 double hours = floor(all_mins / 60.0);
95
96 stringstream str;
97
98 if (hours > 0.0) {
99 if (hours < 10.0)
100 str << '0';
101 str << hours << ':';
102 }
103
104 if (mins > 0.0) {
105 if (mins < 10.0)
106 str << '0';
107 str << mins << ':';
108 }
109
110 if (secs < 10.0 && !str.str().empty())
111 str << '0';
112 str << secs;
113
114 return str.str();
115 }
116
117 void
118 Time::serialize(const std::string &base, ostream &os)
119 {
120 paramOut(os, base + ".sec", sec());
121 paramOut(os, base + ".nsec", nsec());
122 }
123
124 void
125 Time::unserialize(const std::string &base, Checkpoint *cp,
126 const string &section)
127 {
128 time_t secs;
129 time_t nsecs;
130 paramIn(cp, section, base + ".sec", secs);
131 paramIn(cp, section, base + ".nsec", nsecs);
132 sec(secs);
133 nsec(nsecs);
134 }
135
136 void
137 sleep(const Time &time)
138 {
139 timespec ts = time;
140
141 #if USE_POSIX_CLOCK
142 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
143 #else
144 nanosleep(&ts, NULL);
145 #endif
146 }