2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
29 #include "base/time.hh"
36 #include "base/logging.hh"
37 #include "config/use_posix_clock.hh"
38 #include "sim/core.hh"
39 #include "sim/serialize.hh"
42 Time::_set(bool monotonic
)
45 ::clock_gettime(monotonic
? CLOCK_MONOTONIC
: CLOCK_REALTIME
, &_time
);
48 ::gettimeofday(&tv
, NULL
);
54 Time::setTick(Tick ticks
)
56 uint64_t secs
= ticks
/ SimClock::Frequency
;
57 ticks
-= secs
* SimClock::Frequency
;
58 uint64_t nsecs
= static_cast<uint64_t>(ticks
* SimClock::Float::GHz
);
65 return sec() * SimClock::Frequency
+
66 static_cast<uint64_t>(nsec() * SimClock::Float::ns
);
70 Time::date(const std::string
&format
) const
72 time_t sec
= this->sec();
77 ctime_r(&sec
, buf
, sizeof(buf
));
85 struct tm
*tm
= localtime(&sec
);
86 strftime(buf
, sizeof(buf
), format
.c_str(), tm
);
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);
99 std::stringstream str
;
113 if (secs
< 10.0 && !str
.str().empty())
121 Time::serialize(const std::string
&base
, CheckpointOut
&cp
) const
123 paramOut(cp
, base
+ ".sec", sec());
124 paramOut(cp
, base
+ ".nsec", nsec());
128 Time::unserialize(const std::string
&base
, CheckpointIn
&cp
)
132 paramIn(cp
, base
+ ".sec", secs
);
133 paramIn(cp
, base
+ ".nsec", nsecs
);
139 sleep(const Time
&time
)
144 clock_nanosleep(CLOCK_MONOTONIC
, 0, &ts
, NULL
);
146 nanosleep(&ts
, NULL
);
151 mkutctime(struct tm
*time
)
153 // get the current timezone
154 char *tz
= getenv("TZ");
156 // copy the string as the pointer gets invalidated when updating
161 fatal("Failed to reserve memory for UTC time conversion\n");
165 // change to UTC and get the time
168 time_t ret
= mktime(time
);
170 // restore the timezone again