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

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17