time.cc revision 12981
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
43using namespace std;
44
45void
46Time::_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
57void
58Time::setTick(Tick ticks)
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
66Tick
67Time::getTick() const
68{
69    return sec() * SimClock::Frequency +
70        static_cast<uint64_t>(nsec() * SimClock::Float::ns);
71}
72
73string
74Time::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
94string
95Time::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
124void
125Time::serialize(const std::string &base, CheckpointOut &cp) const
126{
127    paramOut(cp, base + ".sec", sec());
128    paramOut(cp, base + ".nsec", nsec());
129}
130
131void
132Time::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
142void
143sleep(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
154time_t
155mkutctime(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
186