gem5  v22.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 namespace gem5
42 {
43 
44 void
45 Time::_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 
56 void
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 
65 Tick
67 {
68  return sec() * sim_clock::Frequency +
69  static_cast<uint64_t>(nsec() * sim_clock::as_float::ns);
70 }
71 
72 std::string
73 Time::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 
93 std::string
94 Time::time() const
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 
123 void
124 Time::serialize(const std::string &base, CheckpointOut &cp) const
125 {
126  paramOut(cp, base + ".sec", sec());
127  paramOut(cp, base + ".nsec", nsec());
128 }
129 
130 void
131 Time::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 
141 void
142 sleep(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 
153 time_t
154 mkutctime(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
void set(time_t _sec, long _nsec)
Set the current time.
Definition: time.hh:103
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
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
const Time & operator=(const Time &other)
Definition: time.hh:118
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:190
Bitfield< 8 > tz
Definition: misc_types.hh:207
Bitfield< 31, 29 > format
Definition: misc_types.hh:653
Bitfield< 55, 52 > ts
Definition: misc_types.hh:93
Bitfield< 32 > tm
Definition: misc.hh:112
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
double ns
nanosecond
Definition: core.cc:56
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:48
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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 Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1