* Authors: Nathan Binkert
*/
-#include <sys/types.h>
-#include <sys/time.h>
-#include <time.h>
#include <iostream>
-#include <string>
+#include <sstream>
#include "base/time.hh"
+#include "config/use_posix_clock.hh"
+#include "sim/core.hh"
+#include "sim/serialize.hh"
using namespace std;
-struct _timeval
-{
- timeval tv;
-};
-
-double
-convert(const timeval &tv)
-{
- return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
-}
-
-Time::Time(bool set_now)
-{
- time = new _timeval;
- if (set_now)
- set();
-}
-
-Time::Time(const timeval &val)
-{
- time = new _timeval;
- set(val);
-}
-
-Time::Time(const Time &val)
-{
- time = new _timeval;
- set(val.get());
-}
-
-Time::~Time()
-{
- delete time;
-}
-
-const timeval &
-Time::get() const
-{
- return time->tv;
-}
-
void
-Time::set()
+Time::_set(bool monotonic)
{
- ::gettimeofday(&time->tv, NULL);
+#if USE_POSIX_CLOCK
+ ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
+#else
+ timeval tv;
+ ::gettimeofday(&tv, NULL);
+ operator=(tv);
+#endif
}
void
-Time::set(const timeval &tv)
+Time::setTick(Tick ticks)
{
- memcpy(&time->tv, &tv, sizeof(timeval));
+ uint64_t nsecs = ticks / SimClock::Int::ns;
+ set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC);
}
-double
-Time::operator()() const
+Tick
+Time::getTick() const
{
- return convert(get());
+ return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns;
}
string
-Time::date(string format) const
+Time::date(const string &format) const
{
- const timeval &tv = get();
- time_t sec = tv.tv_sec;
+ time_t sec = this->sec();
char buf[256];
if (format.empty()) {
#ifdef __SUNPRO_CC
- ctime_r(&sec, buf, 256);
+ ctime_r(&sec, buf, sizeof(buf));
#else
ctime_r(&sec, buf);
#endif
return buf;
}
-ostream &
-operator<<(ostream &out, const Time &start)
+string
+Time::time() const
{
- out << start.date();
- return out;
+ double time = double(*this);
+ double secs = fmod(time, 60.0);
+ double all_mins = floor(time / 60.0);
+ double mins = fmod(all_mins, 60.0);
+ double hours = floor(all_mins / 60.0);
+
+ stringstream str;
+
+ if (hours > 0.0) {
+ if (hours < 10.0)
+ str << '0';
+ str << hours << ':';
+ }
+
+ if (mins > 0.0) {
+ if (mins < 10.0)
+ str << '0';
+ str << mins << ':';
+ }
+
+ if (secs < 10.0 && !str.str().empty())
+ str << '0';
+ str << secs;
+
+ return str.str();
}
-Time
-operator-(const Time &l, const Time &r)
+void
+Time::serialize(const std::string &base, ostream &os)
{
- timeval tv;
- timersub(&l.get(), &r.get(), &tv);
- return tv;
+ paramOut(os, base + ".sec", sec());
+ paramOut(os, base + ".nsec", nsec());
}
-const Time Time::start(true);
+void
+Time::unserialize(const std::string &base, Checkpoint *cp,
+ const string §ion)
+{
+ time_t secs;
+ time_t nsecs;
+ paramIn(cp, section, base + ".sec", secs);
+ paramIn(cp, section, base + ".nsec", nsecs);
+ sec(secs);
+ nsec(nsecs);
+}
+
+void
+sleep(const Time &time)
+{
+ timespec ts = time;
+
+#if USE_POSIX_CLOCK
+ clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
+#else
+ nanosleep(&ts, NULL);
+#endif
+}