time.cc revision 12334:e0ab29a34764
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 nsecs = ticks / SimClock::Int::ns;
61    set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC);
62}
63
64Tick
65Time::getTick() const
66{
67    return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns;
68}
69
70string
71Time::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
91string
92Time::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
121void
122Time::serialize(const std::string &base, CheckpointOut &cp) const
123{
124    paramOut(cp, base + ".sec", sec());
125    paramOut(cp, base + ".nsec", nsec());
126}
127
128void
129Time::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
139void
140sleep(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
151time_t
152mkutctime(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
183