gem5 v24.0.0.0
Loading...
Searching...
No Matches
time.cc
Go to the documentation of this file.
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
29#include "base/time.hh"
30
31#include <cstdlib>
32#include <ctime>
33#include <iostream>
34#include <sstream>
35
36#include "base/logging.hh"
37#include "config/use_posix_clock.hh"
38#include "sim/core.hh"
39#include "sim/serialize.hh"
40
41namespace gem5
42{
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
58{
59 uint64_t secs = ticks / sim_clock::Frequency;
60 ticks -= secs * sim_clock::Frequency;
61 uint64_t nsecs = static_cast<uint64_t>(ticks * sim_clock::as_float::GHz);
62 set(secs, nsecs);
63}
64
65Tick
67{
68 return sec() * sim_clock::Frequency +
69 static_cast<uint64_t>(nsec() * sim_clock::as_float::ns);
70}
71
72std::string
73Time::date(const std::string &format) const
74{
75 time_t sec = this->sec();
76 char buf[256];
77
78 if (format.empty()) {
79#ifdef __SUNPRO_CC
80 ctime_r(&sec, buf, sizeof(buf));
81#else
82 ctime_r(&sec, buf);
83#endif
84 buf[24] = '\0';
85 return buf;
86 }
87
88 struct tm *tm = localtime(&sec);
89 strftime(buf, sizeof(buf), format.c_str(), tm);
90 return buf;
91}
92
93std::string
95{
96 double time = double(*this);
97 double secs = fmod(time, 60.0);
98 double all_mins = floor(time / 60.0);
99 double mins = fmod(all_mins, 60.0);
100 double hours = floor(all_mins / 60.0);
101
102 std::stringstream str;
103
104 if (hours > 0.0) {
105 if (hours < 10.0)
106 str << '0';
107 str << hours << ':';
108 }
109
110 if (mins > 0.0) {
111 if (mins < 10.0)
112 str << '0';
113 str << mins << ':';
114 }
115
116 if (secs < 10.0 && !str.str().empty())
117 str << '0';
118 str << secs;
119
120 return str.str();
121}
122
123void
124Time::serialize(const std::string &base, CheckpointOut &cp) const
125{
126 paramOut(cp, base + ".sec", sec());
127 paramOut(cp, base + ".nsec", nsec());
128}
129
130void
131Time::unserialize(const std::string &base, CheckpointIn &cp)
132{
133 time_t secs;
134 time_t nsecs;
135 paramIn(cp, base + ".sec", secs);
136 paramIn(cp, base + ".nsec", nsecs);
137 sec(secs);
138 nsec(nsecs);
139}
140
141void
142sleep(const Time &time)
143{
144 timespec ts = time;
145
146#if USE_POSIX_CLOCK
147 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
148#else
149 nanosleep(&ts, NULL);
150#endif
151}
152
153time_t
154mkutctime(struct tm *time)
155{
156 // get the current timezone
157 char *tz = getenv("TZ");
158
159 // copy the string as the pointer gets invalidated when updating
160 // the environment
161 if (tz) {
162 tz = strdup(tz);
163 if (!tz) {
164 fatal("Failed to reserve memory for UTC time conversion\n");
165 }
166 }
167
168 // change to UTC and get the time
169 setenv("TZ", "", 1);
170 tzset();
171 time_t ret = mktime(time);
172
173 // restore the timezone again
174 if (tz) {
175 setenv("TZ", tz, 1);
176 free(tz);
177 } else {
178 unsetenv("TZ");
179 }
180 tzset();
181
182 return ret;
183}
184
185} // namespace gem5
Tick getTick() const
Get the current time from a value measured in Ticks.
Definition time.cc:66
void serialize(const std::string &base, CheckpointOut &cp) const
Definition time.cc:124
time_t sec() const
Accessors for getting and setting the current clock.
Definition time.hh:74
std::string date(const std::string &format="") const
Definition time.cc:73
timespec _time
Definition time.hh:51
std::string time() const
Definition time.cc:94
long nsec() const
Definition time.hh:77
void unserialize(const std::string &base, CheckpointIn &cp)
Definition time.cc:131
const Time & operator=(const Time &other)
Definition time.hh:118
void _set(bool monotonic)
Internal time set function.
Definition time.cc:45
void setTick(Tick ticks)
Set the current time from a value measured in Ticks.
Definition time.cc:57
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:200
Bitfield< 8 > tz
Bitfield< 31, 29 > format
Bitfield< 12, 11 > set
Bitfield< 55, 52 > ts
Bitfield< 32 > tm
Definition misc.hh:112
Bitfield< 51, 12 > base
Definition pagetable.hh:141
double ns
nanosecond
Definition core.cc:54
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition core.cc:47
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::ostream CheckpointOut
Definition serialize.hh:66
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition types.cc:40
time_t mkutctime(struct tm *time)
Definition time.cc:154
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition types.cc:72
uint64_t Tick
Tick count type.
Definition types.hh:58
void sleep(const Time &time)
Definition time.cc:142

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0