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

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6