time.cc (10905:a6ca6831e775) time.cc (11793:ef606668d247)
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
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 "base/time.hh"
32
31#include <cstdlib>
32#include <ctime>
33#include <iostream>
34#include <sstream>
35
33#include <cstdlib>
34#include <ctime>
35#include <iostream>
36#include <sstream>
37
36#include "base/time.hh"
37#include "config/use_posix_clock.hh"
38#include "sim/core.hh"
39#include "sim/serialize.hh"
40
41using namespace std;
42
43void
44Time::_set(bool monotonic)
45{
46#if USE_POSIX_CLOCK
47 ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
48#else
49 timeval tv;
50 ::gettimeofday(&tv, NULL);
51 operator=(tv);
52#endif
53}
54
55void
56Time::setTick(Tick ticks)
57{
58 uint64_t nsecs = ticks / SimClock::Int::ns;
59 set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC);
60}
61
62Tick
63Time::getTick() const
64{
65 return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns;
66}
67
68string
69Time::date(const string &format) const
70{
71 time_t sec = this->sec();
72 char buf[256];
73
74 if (format.empty()) {
75#ifdef __SUNPRO_CC
76 ctime_r(&sec, buf, sizeof(buf));
77#else
78 ctime_r(&sec, buf);
79#endif
80 buf[24] = '\0';
81 return buf;
82 }
83
84 struct tm *tm = localtime(&sec);
85 strftime(buf, sizeof(buf), format.c_str(), tm);
86 return buf;
87}
88
89string
90Time::time() const
91{
92 double time = double(*this);
93 double secs = fmod(time, 60.0);
94 double all_mins = floor(time / 60.0);
95 double mins = fmod(all_mins, 60.0);
96 double hours = floor(all_mins / 60.0);
97
98 stringstream str;
99
100 if (hours > 0.0) {
101 if (hours < 10.0)
102 str << '0';
103 str << hours << ':';
104 }
105
106 if (mins > 0.0) {
107 if (mins < 10.0)
108 str << '0';
109 str << mins << ':';
110 }
111
112 if (secs < 10.0 && !str.str().empty())
113 str << '0';
114 str << secs;
115
116 return str.str();
117}
118
119void
120Time::serialize(const std::string &base, CheckpointOut &cp) const
121{
122 paramOut(cp, base + ".sec", sec());
123 paramOut(cp, base + ".nsec", nsec());
124}
125
126void
127Time::unserialize(const std::string &base, CheckpointIn &cp)
128{
129 time_t secs;
130 time_t nsecs;
131 paramIn(cp, base + ".sec", secs);
132 paramIn(cp, base + ".nsec", nsecs);
133 sec(secs);
134 nsec(nsecs);
135}
136
137void
138sleep(const Time &time)
139{
140 timespec ts = time;
141
142#if USE_POSIX_CLOCK
143 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
144#else
145 nanosleep(&ts, NULL);
146#endif
147}
148
149time_t
150mkutctime(struct tm *time)
151{
152 // get the current timezone
153 char *tz = getenv("TZ");
154
155 // copy the string as the pointer gets invalidated when updating
156 // the environment
157 if (tz) {
158 tz = strdup(tz);
159 if (!tz) {
160 fatal("Failed to reserve memory for UTC time conversion\n");
161 }
162 }
163
164 // change to UTC and get the time
165 setenv("TZ", "", 1);
166 tzset();
167 time_t ret = mktime(time);
168
169 // restore the timezone again
170 if (tz) {
171 setenv("TZ", tz, 1);
172 free(tz);
173 } else {
174 unsetenv("TZ");
175 }
176 tzset();
177
178 return ret;
179}
180
38#include "config/use_posix_clock.hh"
39#include "sim/core.hh"
40#include "sim/serialize.hh"
41
42using namespace std;
43
44void
45Time::_set(bool monotonic)
46{
47#if USE_POSIX_CLOCK
48 ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
49#else
50 timeval tv;
51 ::gettimeofday(&tv, NULL);
52 operator=(tv);
53#endif
54}
55
56void
57Time::setTick(Tick ticks)
58{
59 uint64_t nsecs = ticks / SimClock::Int::ns;
60 set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC);
61}
62
63Tick
64Time::getTick() const
65{
66 return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns;
67}
68
69string
70Time::date(const string &format) const
71{
72 time_t sec = this->sec();
73 char buf[256];
74
75 if (format.empty()) {
76#ifdef __SUNPRO_CC
77 ctime_r(&sec, buf, sizeof(buf));
78#else
79 ctime_r(&sec, buf);
80#endif
81 buf[24] = '\0';
82 return buf;
83 }
84
85 struct tm *tm = localtime(&sec);
86 strftime(buf, sizeof(buf), format.c_str(), tm);
87 return buf;
88}
89
90string
91Time::time() const
92{
93 double time = double(*this);
94 double secs = fmod(time, 60.0);
95 double all_mins = floor(time / 60.0);
96 double mins = fmod(all_mins, 60.0);
97 double hours = floor(all_mins / 60.0);
98
99 stringstream str;
100
101 if (hours > 0.0) {
102 if (hours < 10.0)
103 str << '0';
104 str << hours << ':';
105 }
106
107 if (mins > 0.0) {
108 if (mins < 10.0)
109 str << '0';
110 str << mins << ':';
111 }
112
113 if (secs < 10.0 && !str.str().empty())
114 str << '0';
115 str << secs;
116
117 return str.str();
118}
119
120void
121Time::serialize(const std::string &base, CheckpointOut &cp) const
122{
123 paramOut(cp, base + ".sec", sec());
124 paramOut(cp, base + ".nsec", nsec());
125}
126
127void
128Time::unserialize(const std::string &base, CheckpointIn &cp)
129{
130 time_t secs;
131 time_t nsecs;
132 paramIn(cp, base + ".sec", secs);
133 paramIn(cp, base + ".nsec", nsecs);
134 sec(secs);
135 nsec(nsecs);
136}
137
138void
139sleep(const Time &time)
140{
141 timespec ts = time;
142
143#if USE_POSIX_CLOCK
144 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
145#else
146 nanosleep(&ts, NULL);
147#endif
148}
149
150time_t
151mkutctime(struct tm *time)
152{
153 // get the current timezone
154 char *tz = getenv("TZ");
155
156 // copy the string as the pointer gets invalidated when updating
157 // the environment
158 if (tz) {
159 tz = strdup(tz);
160 if (!tz) {
161 fatal("Failed to reserve memory for UTC time conversion\n");
162 }
163 }
164
165 // change to UTC and get the time
166 setenv("TZ", "", 1);
167 tzset();
168 time_t ret = mktime(time);
169
170 // restore the timezone again
171 if (tz) {
172 setenv("TZ", tz, 1);
173 free(tz);
174 } else {
175 unsetenv("TZ");
176 }
177 tzset();
178
179 return ret;
180}
181