time.cc revision 7862:23758c97c227
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 <iostream>
32#include <sstream>
33
34#include "base/time.hh"
35#include "config/use_posix_clock.hh"
36#include "sim/core.hh"
37
38using namespace std;
39
40void
41Time::_set(bool monotonic)
42{
43#if USE_POSIX_CLOCK
44    ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
45#else
46    timeval tv;
47    ::gettimeofday(&tv, NULL);
48    operator=(tv);
49#endif
50}
51
52void
53Time::setTick(Tick ticks)
54{
55    uint64_t nsecs = ticks / SimClock::Int::ns;
56    set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC);
57}
58
59Tick
60Time::getTick() const
61{
62    return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns;
63}
64
65string
66Time::date(const string &format) const
67{
68    time_t sec = this->sec();
69    char buf[256];
70
71    if (format.empty()) {
72#ifdef __SUNPRO_CC
73        ctime_r(&sec, buf, sizeof(buf));
74#else
75        ctime_r(&sec, buf);
76#endif
77        buf[24] = '\0';
78        return buf;
79    }
80
81    struct tm *tm = localtime(&sec);
82    strftime(buf, sizeof(buf), format.c_str(), tm);
83    return buf;
84}
85
86string
87Time::time() const
88{
89    double time = double(*this);
90    double secs = fmod(time, 60.0);
91    double all_mins = floor(time / 60.0);
92    double mins = fmod(all_mins, 60.0);
93    double hours = floor(all_mins / 60.0);
94
95    stringstream str;
96
97    if (hours > 0.0) {
98        if (hours < 10.0)
99            str << '0';
100        str << hours << ':';
101    }
102
103    if (mins > 0.0) {
104        if (mins < 10.0)
105            str << '0';
106        str << mins << ':';
107    }
108
109    if (secs < 10.0 && !str.str().empty())
110        str << '0';
111    str << secs;
112
113    return str.str();
114}
115
116void
117sleep(const Time &time)
118{
119    timespec ts = time;
120
121#if USE_POSIX_CLOCK
122    clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
123#else
124    nanosleep(&ts, NULL);
125#endif
126}
127