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

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13