time.cc revision 7870
12SN/A/* 21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292SN/A */ 302SN/A 312SN/A#include <iostream> 327840Snate@binkert.org#include <sstream> 332SN/A 34400SN/A#include "base/time.hh" 357840Snate@binkert.org#include "config/use_posix_clock.hh" 367862Sgblack@eecs.umich.edu#include "sim/core.hh" 377870Sgblack@eecs.umich.edu#include "sim/serialize.hh" 382SN/A 392SN/Ausing namespace std; 402SN/A 417840Snate@binkert.orgvoid 427840Snate@binkert.orgTime::_set(bool monotonic) 432SN/A{ 447840Snate@binkert.org#if USE_POSIX_CLOCK 457840Snate@binkert.org ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time); 467840Snate@binkert.org#else 47400SN/A timeval tv; 487840Snate@binkert.org ::gettimeofday(&tv, NULL); 497840Snate@binkert.org operator=(tv); 507840Snate@binkert.org#endif 51400SN/A} 52400SN/A 537862Sgblack@eecs.umich.eduvoid 547862Sgblack@eecs.umich.eduTime::setTick(Tick ticks) 557862Sgblack@eecs.umich.edu{ 567862Sgblack@eecs.umich.edu uint64_t nsecs = ticks / SimClock::Int::ns; 577862Sgblack@eecs.umich.edu set(nsecs / NSEC_PER_SEC, nsecs % NSEC_PER_SEC); 587862Sgblack@eecs.umich.edu} 597862Sgblack@eecs.umich.edu 607862Sgblack@eecs.umich.eduTick 617862Sgblack@eecs.umich.eduTime::getTick() const 627862Sgblack@eecs.umich.edu{ 637862Sgblack@eecs.umich.edu return (nsec() + sec() * NSEC_PER_SEC) * SimClock::Int::ns; 647862Sgblack@eecs.umich.edu} 657862Sgblack@eecs.umich.edu 66400SN/Astring 677840Snate@binkert.orgTime::date(const string &format) const 68400SN/A{ 697840Snate@binkert.org time_t sec = this->sec(); 70400SN/A char buf[256]; 71400SN/A 72400SN/A if (format.empty()) { 733918Ssaidi@eecs.umich.edu#ifdef __SUNPRO_CC 747840Snate@binkert.org ctime_r(&sec, buf, sizeof(buf)); 753918Ssaidi@eecs.umich.edu#else 76400SN/A ctime_r(&sec, buf); 773918Ssaidi@eecs.umich.edu#endif 78400SN/A buf[24] = '\0'; 79400SN/A return buf; 802SN/A } 812SN/A 82400SN/A struct tm *tm = localtime(&sec); 83400SN/A strftime(buf, sizeof(buf), format.c_str(), tm); 84400SN/A return buf; 85400SN/A} 862SN/A 877840Snate@binkert.orgstring 887840Snate@binkert.orgTime::time() const 89400SN/A{ 907840Snate@binkert.org double time = double(*this); 917840Snate@binkert.org double secs = fmod(time, 60.0); 927840Snate@binkert.org double all_mins = floor(time / 60.0); 937840Snate@binkert.org double mins = fmod(all_mins, 60.0); 947840Snate@binkert.org double hours = floor(all_mins / 60.0); 957840Snate@binkert.org 967840Snate@binkert.org stringstream str; 977840Snate@binkert.org 987840Snate@binkert.org if (hours > 0.0) { 997840Snate@binkert.org if (hours < 10.0) 1007840Snate@binkert.org str << '0'; 1017840Snate@binkert.org str << hours << ':'; 1027840Snate@binkert.org } 1037840Snate@binkert.org 1047840Snate@binkert.org if (mins > 0.0) { 1057840Snate@binkert.org if (mins < 10.0) 1067840Snate@binkert.org str << '0'; 1077840Snate@binkert.org str << mins << ':'; 1087840Snate@binkert.org } 1097840Snate@binkert.org 1107840Snate@binkert.org if (secs < 10.0 && !str.str().empty()) 1117840Snate@binkert.org str << '0'; 1127840Snate@binkert.org str << secs; 1137840Snate@binkert.org 1147840Snate@binkert.org return str.str(); 115400SN/A} 1162SN/A 1177840Snate@binkert.orgvoid 1187870Sgblack@eecs.umich.eduTime::serialize(const std::string &base, ostream &os) 1197870Sgblack@eecs.umich.edu{ 1207870Sgblack@eecs.umich.edu paramOut(os, base + ".sec", sec()); 1217870Sgblack@eecs.umich.edu paramOut(os, base + ".nsec", nsec()); 1227870Sgblack@eecs.umich.edu} 1237870Sgblack@eecs.umich.edu 1247870Sgblack@eecs.umich.eduvoid 1257870Sgblack@eecs.umich.eduTime::unserialize(const std::string &base, Checkpoint *cp, 1267870Sgblack@eecs.umich.edu const string §ion) 1277870Sgblack@eecs.umich.edu{ 1287870Sgblack@eecs.umich.edu time_t secs; 1297870Sgblack@eecs.umich.edu time_t nsecs; 1307870Sgblack@eecs.umich.edu paramIn(cp, section, base + ".sec", secs); 1317870Sgblack@eecs.umich.edu paramIn(cp, section, base + ".nsec", nsecs); 1327870Sgblack@eecs.umich.edu sec(secs); 1337870Sgblack@eecs.umich.edu nsec(nsecs); 1347870Sgblack@eecs.umich.edu} 1357870Sgblack@eecs.umich.edu 1367870Sgblack@eecs.umich.eduvoid 1377840Snate@binkert.orgsleep(const Time &time) 138400SN/A{ 1397840Snate@binkert.org timespec ts = time; 1407840Snate@binkert.org 1417840Snate@binkert.org#if USE_POSIX_CLOCK 1427840Snate@binkert.org clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); 1437840Snate@binkert.org#else 1447840Snate@binkert.org nanosleep(&ts, NULL); 1457840Snate@binkert.org#endif 146400SN/A} 147