sc_time.cc revision 13040
112837Sgabeblack@google.com/* 212837Sgabeblack@google.com * Copyright 2018 Google, Inc. 312837Sgabeblack@google.com * 412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312837Sgabeblack@google.com * this software without specific prior written permission. 1412837Sgabeblack@google.com * 1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612837Sgabeblack@google.com * 2712837Sgabeblack@google.com * Authors: Gabe Black 2812837Sgabeblack@google.com */ 2912837Sgabeblack@google.com 3013039Sgabeblack@google.com#include <vector> 3113039Sgabeblack@google.com 3212837Sgabeblack@google.com#include "base/logging.hh" 3312989Sgabeblack@google.com#include "base/types.hh" 3412986Sgabeblack@google.com#include "python/pybind11/pybind.hh" 3513039Sgabeblack@google.com#include "systemc/core/python.hh" 3612837Sgabeblack@google.com#include "systemc/ext/core/sc_time.hh" 3712837Sgabeblack@google.com 3812837Sgabeblack@google.comnamespace sc_core 3912837Sgabeblack@google.com{ 4012837Sgabeblack@google.com 4112983Sgabeblack@google.comnamespace 4212837Sgabeblack@google.com{ 4312983Sgabeblack@google.com 4412983Sgabeblack@google.comconst char *TimeUnitNames[] = { 4512983Sgabeblack@google.com [SC_FS] = "fs", 4612983Sgabeblack@google.com [SC_PS] = "ps", 4712983Sgabeblack@google.com [SC_NS] = "ns", 4812983Sgabeblack@google.com [SC_US] = "us", 4912983Sgabeblack@google.com [SC_MS] = "ms", 5012983Sgabeblack@google.com [SC_SEC] = "s" 5112983Sgabeblack@google.com}; 5212983Sgabeblack@google.com 5312983Sgabeblack@google.comdouble TimeUnitScale[] = { 5412983Sgabeblack@google.com [SC_FS] = 1.0e-15, 5512983Sgabeblack@google.com [SC_PS] = 1.0e-12, 5612983Sgabeblack@google.com [SC_NS] = 1.0e-9, 5712983Sgabeblack@google.com [SC_US] = 1.0e-6, 5812983Sgabeblack@google.com [SC_MS] = 1.0e-3, 5912983Sgabeblack@google.com [SC_SEC] = 1.0 6012983Sgabeblack@google.com}; 6112983Sgabeblack@google.com 6213039Sgabeblack@google.combool timeFixed = false; 6313039Sgabeblack@google.combool pythonReady = false; 6413039Sgabeblack@google.com 6513039Sgabeblack@google.comstruct SetInfo 6613039Sgabeblack@google.com{ 6713039Sgabeblack@google.com SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) : 6813039Sgabeblack@google.com time(time), d(d), tu(tu) 6913039Sgabeblack@google.com {} 7013039Sgabeblack@google.com 7113039Sgabeblack@google.com ::sc_core::sc_time *time; 7213039Sgabeblack@google.com double d; 7313039Sgabeblack@google.com ::sc_core::sc_time_unit tu; 7413039Sgabeblack@google.com}; 7513039Sgabeblack@google.comstd::vector<SetInfo> toSet; 7613039Sgabeblack@google.com 7712986Sgabeblack@google.comvoid 7813039Sgabeblack@google.comsetWork(sc_time *time, double d, ::sc_core::sc_time_unit tu) 7912986Sgabeblack@google.com{ 8013039Sgabeblack@google.com //XXX Assuming the time resolution is 1ps. 8113039Sgabeblack@google.com double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS]; 8213039Sgabeblack@google.com // Accellera claims there is a linux bug, and that these next two 8313039Sgabeblack@google.com // lines work around them. 8413039Sgabeblack@google.com volatile double tmp = d * scale + 0.5; 8513039Sgabeblack@google.com *time = sc_time::from_value(static_cast<uint64_t>(tmp)); 8613039Sgabeblack@google.com} 8712986Sgabeblack@google.com 8813039Sgabeblack@google.comvoid 8913039Sgabeblack@google.comfixTime() 9013039Sgabeblack@google.com{ 9112986Sgabeblack@google.com auto ticks = pybind11::module::import("m5.ticks"); 9212986Sgabeblack@google.com auto fix_global_frequency = ticks.attr("fixGlobalFrequency"); 9312986Sgabeblack@google.com fix_global_frequency(); 9413039Sgabeblack@google.com 9513039Sgabeblack@google.com for (auto &t: toSet) 9613039Sgabeblack@google.com setWork(t.time, t.d, t.tu); 9713039Sgabeblack@google.com toSet.clear(); 9812986Sgabeblack@google.com} 9912986Sgabeblack@google.com 10013039Sgabeblack@google.comvoid 10113039Sgabeblack@google.comset(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) 10213039Sgabeblack@google.com{ 10313039Sgabeblack@google.com // Only fix time once. 10413039Sgabeblack@google.com if (!timeFixed) { 10513039Sgabeblack@google.com timeFixed = true; 10613039Sgabeblack@google.com 10713039Sgabeblack@google.com // If we've run, python is working and we haven't fixed time yet. 10813039Sgabeblack@google.com if (pythonReady) 10913039Sgabeblack@google.com fixTime(); 11013039Sgabeblack@google.com } 11113039Sgabeblack@google.com if (pythonReady) { 11213039Sgabeblack@google.com // Time should be working. Set up this sc_time. 11313039Sgabeblack@google.com setWork(time, d, tu); 11413039Sgabeblack@google.com } else { 11513039Sgabeblack@google.com // Time isn't set up yet. Defer setting up this sc_time. 11613039Sgabeblack@google.com toSet.emplace_back(time, d, tu); 11713039Sgabeblack@google.com } 11813039Sgabeblack@google.com} 11913039Sgabeblack@google.com 12013039Sgabeblack@google.comclass TimeSetter : public ::sc_gem5::PythonReadyFunc 12113039Sgabeblack@google.com{ 12213039Sgabeblack@google.com public: 12313039Sgabeblack@google.com TimeSetter() : ::sc_gem5::PythonReadyFunc() {} 12413039Sgabeblack@google.com 12513039Sgabeblack@google.com void 12613039Sgabeblack@google.com run() override 12713039Sgabeblack@google.com { 12813039Sgabeblack@google.com // Record that we've run and python/pybind should be usable. 12913039Sgabeblack@google.com pythonReady = true; 13013039Sgabeblack@google.com 13113039Sgabeblack@google.com // If time is already fixed, let python know. 13213039Sgabeblack@google.com if (timeFixed) 13313039Sgabeblack@google.com fixTime(); 13413039Sgabeblack@google.com } 13513039Sgabeblack@google.com} timeSetter; 13613039Sgabeblack@google.com 13712983Sgabeblack@google.com} // anonymous namespace 13812983Sgabeblack@google.com 13912983Sgabeblack@google.comsc_time::sc_time() : val(0) {} 14012983Sgabeblack@google.com 14112983Sgabeblack@google.comsc_time::sc_time(double d, sc_time_unit tu) 14212983Sgabeblack@google.com{ 14312983Sgabeblack@google.com val = 0; 14413039Sgabeblack@google.com if (d != 0) 14513039Sgabeblack@google.com set(this, d, tu); 14612837Sgabeblack@google.com} 14712837Sgabeblack@google.com 14812983Sgabeblack@google.comsc_time::sc_time(const sc_time &t) 14912837Sgabeblack@google.com{ 15012983Sgabeblack@google.com val = t.val; 15112837Sgabeblack@google.com} 15212837Sgabeblack@google.com 15312925Sgabeblack@google.comsc_time::sc_time(double, bool) 15412925Sgabeblack@google.com{ 15512925Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 15612925Sgabeblack@google.com} 15712925Sgabeblack@google.com 15812925Sgabeblack@google.comsc_time::sc_time(sc_dt::uint64, bool) 15912925Sgabeblack@google.com{ 16012925Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 16112925Sgabeblack@google.com} 16212925Sgabeblack@google.com 16312837Sgabeblack@google.comsc_time & 16412983Sgabeblack@google.comsc_time::operator = (const sc_time &t) 16512837Sgabeblack@google.com{ 16612983Sgabeblack@google.com val = t.val; 16712837Sgabeblack@google.com return *this; 16812837Sgabeblack@google.com} 16912837Sgabeblack@google.com 17012837Sgabeblack@google.comsc_dt::uint64 17112837Sgabeblack@google.comsc_time::value() const 17212837Sgabeblack@google.com{ 17312983Sgabeblack@google.com return val; 17412837Sgabeblack@google.com} 17512837Sgabeblack@google.com 17612837Sgabeblack@google.comdouble 17712837Sgabeblack@google.comsc_time::to_double() const 17812837Sgabeblack@google.com{ 17913040Sgabeblack@google.com return static_cast<double>(val); 18012837Sgabeblack@google.com} 18112837Sgabeblack@google.comdouble 18212837Sgabeblack@google.comsc_time::to_seconds() const 18312837Sgabeblack@google.com{ 18412837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 18512837Sgabeblack@google.com return 0.0; 18612837Sgabeblack@google.com} 18712837Sgabeblack@google.com 18812837Sgabeblack@google.comconst std::string 18912837Sgabeblack@google.comsc_time::to_string() const 19012837Sgabeblack@google.com{ 19112837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 19212837Sgabeblack@google.com return ""; 19312837Sgabeblack@google.com} 19412837Sgabeblack@google.com 19512837Sgabeblack@google.combool 19612983Sgabeblack@google.comsc_time::operator == (const sc_time &t) const 19712837Sgabeblack@google.com{ 19812983Sgabeblack@google.com return val == t.val; 19912837Sgabeblack@google.com} 20012837Sgabeblack@google.com 20112837Sgabeblack@google.combool 20212983Sgabeblack@google.comsc_time::operator != (const sc_time &t) const 20312837Sgabeblack@google.com{ 20412983Sgabeblack@google.com return val != t.val; 20512837Sgabeblack@google.com} 20612837Sgabeblack@google.com 20712837Sgabeblack@google.combool 20812983Sgabeblack@google.comsc_time::operator < (const sc_time &t) const 20912837Sgabeblack@google.com{ 21012983Sgabeblack@google.com return val < t.val; 21112837Sgabeblack@google.com} 21212837Sgabeblack@google.com 21312837Sgabeblack@google.combool 21412983Sgabeblack@google.comsc_time::operator <= (const sc_time &t) const 21512837Sgabeblack@google.com{ 21612983Sgabeblack@google.com return val <= t.val; 21712837Sgabeblack@google.com} 21812837Sgabeblack@google.com 21912837Sgabeblack@google.combool 22012983Sgabeblack@google.comsc_time::operator > (const sc_time &t) const 22112837Sgabeblack@google.com{ 22212983Sgabeblack@google.com return val > t.val; 22312837Sgabeblack@google.com} 22412837Sgabeblack@google.com 22512837Sgabeblack@google.combool 22612983Sgabeblack@google.comsc_time::operator >= (const sc_time &t) const 22712837Sgabeblack@google.com{ 22812983Sgabeblack@google.com return val >= t.val; 22912837Sgabeblack@google.com} 23012837Sgabeblack@google.com 23112837Sgabeblack@google.comsc_time & 23212983Sgabeblack@google.comsc_time::operator += (const sc_time &t) 23312837Sgabeblack@google.com{ 23412983Sgabeblack@google.com val += t.val; 23512837Sgabeblack@google.com return *this; 23612837Sgabeblack@google.com} 23712837Sgabeblack@google.com 23812837Sgabeblack@google.comsc_time & 23912983Sgabeblack@google.comsc_time::operator -= (const sc_time &t) 24012837Sgabeblack@google.com{ 24112983Sgabeblack@google.com val -= t.val; 24212837Sgabeblack@google.com return *this; 24312837Sgabeblack@google.com} 24412837Sgabeblack@google.com 24512837Sgabeblack@google.comsc_time & 24613040Sgabeblack@google.comsc_time::operator *= (double d) 24712837Sgabeblack@google.com{ 24813040Sgabeblack@google.com val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5); 24912837Sgabeblack@google.com return *this; 25012837Sgabeblack@google.com} 25112837Sgabeblack@google.com 25212837Sgabeblack@google.comsc_time & 25313040Sgabeblack@google.comsc_time::operator /= (double d) 25412837Sgabeblack@google.com{ 25513040Sgabeblack@google.com val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5); 25612837Sgabeblack@google.com return *this; 25712837Sgabeblack@google.com} 25812837Sgabeblack@google.com 25912837Sgabeblack@google.comvoid 26012983Sgabeblack@google.comsc_time::print(std::ostream &os) const 26112837Sgabeblack@google.com{ 26212983Sgabeblack@google.com if (val == 0) { 26312983Sgabeblack@google.com os << "0 s"; 26412983Sgabeblack@google.com } else { 26512983Sgabeblack@google.com //XXX Assuming the time resolution is 1ps. 26612983Sgabeblack@google.com sc_time_unit tu = SC_PS; 26712983Sgabeblack@google.com uint64_t scaled = val; 26812983Sgabeblack@google.com while (tu < SC_SEC && (scaled % 1000) == 0) { 26912983Sgabeblack@google.com tu = (sc_time_unit)((int)tu + 1); 27012983Sgabeblack@google.com scaled /= 1000; 27112983Sgabeblack@google.com } 27212983Sgabeblack@google.com 27312983Sgabeblack@google.com os << scaled << ' ' << TimeUnitNames[tu]; 27412983Sgabeblack@google.com } 27512837Sgabeblack@google.com} 27612837Sgabeblack@google.com 27712923Sgabeblack@google.comsc_time 27812983Sgabeblack@google.comsc_time::from_value(sc_dt::uint64 u) 27912923Sgabeblack@google.com{ 28012983Sgabeblack@google.com sc_time t; 28112983Sgabeblack@google.com t.val = u; 28212983Sgabeblack@google.com return t; 28312923Sgabeblack@google.com} 28412923Sgabeblack@google.com 28512923Sgabeblack@google.comsc_time 28612923Sgabeblack@google.comsc_time::from_seconds(double) 28712923Sgabeblack@google.com{ 28812923Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 28912923Sgabeblack@google.com return sc_time(); 29012923Sgabeblack@google.com} 29112923Sgabeblack@google.com 29212923Sgabeblack@google.comsc_time 29312923Sgabeblack@google.comsc_time::from_string(const char *str) 29412923Sgabeblack@google.com{ 29512923Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 29612923Sgabeblack@google.com return sc_time(); 29712923Sgabeblack@google.com} 29812923Sgabeblack@google.com 29912837Sgabeblack@google.comconst sc_time 30012989Sgabeblack@google.comoperator + (const sc_time &a, const sc_time &b) 30112837Sgabeblack@google.com{ 30212989Sgabeblack@google.com return sc_time::from_value(a.value() + b.value()); 30312837Sgabeblack@google.com} 30412837Sgabeblack@google.com 30512837Sgabeblack@google.comconst sc_time 30612989Sgabeblack@google.comoperator - (const sc_time &a, const sc_time &b) 30712837Sgabeblack@google.com{ 30812989Sgabeblack@google.com return sc_time::from_value(a.value() - b.value()); 30912837Sgabeblack@google.com} 31012837Sgabeblack@google.com 31112837Sgabeblack@google.comconst sc_time 31213040Sgabeblack@google.comoperator * (const sc_time &t, double d) 31312837Sgabeblack@google.com{ 31413040Sgabeblack@google.com volatile double tmp = static_cast<double>(t.value()) * d + 0.5; 31513040Sgabeblack@google.com return sc_time::from_value(static_cast<int64_t>(tmp)); 31612837Sgabeblack@google.com} 31712837Sgabeblack@google.com 31812837Sgabeblack@google.comconst sc_time 31913040Sgabeblack@google.comoperator * (double d, const sc_time &t) 32012837Sgabeblack@google.com{ 32113040Sgabeblack@google.com volatile double tmp = d * static_cast<double>(t.value()) + 0.5; 32213040Sgabeblack@google.com return sc_time::from_value(static_cast<int64_t>(tmp)); 32312837Sgabeblack@google.com} 32412837Sgabeblack@google.com 32512837Sgabeblack@google.comconst sc_time 32613040Sgabeblack@google.comoperator / (const sc_time &t, double d) 32712837Sgabeblack@google.com{ 32813040Sgabeblack@google.com volatile double tmp = static_cast<double>(t.value()) / d + 0.5; 32913040Sgabeblack@google.com return sc_time::from_value(static_cast<int64_t>(tmp)); 33012837Sgabeblack@google.com} 33112837Sgabeblack@google.com 33212837Sgabeblack@google.comdouble 33313040Sgabeblack@google.comoperator / (const sc_time &t1, const sc_time &t2) 33412837Sgabeblack@google.com{ 33513040Sgabeblack@google.com return t1.to_double() / t2.to_double(); 33612837Sgabeblack@google.com} 33712837Sgabeblack@google.com 33812837Sgabeblack@google.comstd::ostream & 33912983Sgabeblack@google.comoperator << (std::ostream &os, const sc_time &t) 34012837Sgabeblack@google.com{ 34112983Sgabeblack@google.com t.print(os); 34212837Sgabeblack@google.com return os; 34312837Sgabeblack@google.com} 34412837Sgabeblack@google.com 34512837Sgabeblack@google.comconst sc_time SC_ZERO_TIME; 34612837Sgabeblack@google.com 34712837Sgabeblack@google.comvoid 34812837Sgabeblack@google.comsc_set_time_resolution(double, sc_time_unit) 34912837Sgabeblack@google.com{ 35012837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 35112837Sgabeblack@google.com} 35212837Sgabeblack@google.com 35312837Sgabeblack@google.comsc_time 35412837Sgabeblack@google.comsc_get_time_resolution() 35512837Sgabeblack@google.com{ 35612837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 35712837Sgabeblack@google.com return sc_time(); 35812837Sgabeblack@google.com} 35912837Sgabeblack@google.com 36012837Sgabeblack@google.comconst sc_time & 36112837Sgabeblack@google.comsc_max_time() 36212837Sgabeblack@google.com{ 36312989Sgabeblack@google.com static const sc_time MaxScTime = sc_time::from_value(MaxTick); 36412989Sgabeblack@google.com return MaxScTime; 36512837Sgabeblack@google.com} 36612837Sgabeblack@google.com 36712916Sgabeblack@google.comvoid 36812916Sgabeblack@google.comsc_set_default_time_unit(double, sc_time_unit) 36912916Sgabeblack@google.com{ 37012916Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 37112916Sgabeblack@google.com} 37212916Sgabeblack@google.com 37312916Sgabeblack@google.comsc_time 37412916Sgabeblack@google.comsc_get_default_time_unit() 37512916Sgabeblack@google.com{ 37612916Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 37712916Sgabeblack@google.com return *(sc_time *)nullptr; 37812916Sgabeblack@google.com} 37912916Sgabeblack@google.com 38012927Sgabeblack@google.comsc_time_tuple::sc_time_tuple(const sc_time &) 38112927Sgabeblack@google.com{ 38212927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 38312927Sgabeblack@google.com} 38412927Sgabeblack@google.com 38512927Sgabeblack@google.combool 38612927Sgabeblack@google.comsc_time_tuple::has_value() const 38712927Sgabeblack@google.com{ 38812927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 38912927Sgabeblack@google.com return false; 39012927Sgabeblack@google.com} 39112927Sgabeblack@google.com 39212927Sgabeblack@google.comsc_dt::uint64 39312927Sgabeblack@google.comsc_time_tuple::value() const 39412927Sgabeblack@google.com{ 39512927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 39612927Sgabeblack@google.com return 0; 39712927Sgabeblack@google.com} 39812927Sgabeblack@google.com 39912927Sgabeblack@google.comconst char * 40012927Sgabeblack@google.comsc_time_tuple::unit_symbol() const 40112927Sgabeblack@google.com{ 40212927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 40312927Sgabeblack@google.com return ""; 40412927Sgabeblack@google.com} 40512927Sgabeblack@google.com 40612927Sgabeblack@google.comdouble 40712927Sgabeblack@google.comsc_time_tuple::to_double() const 40812927Sgabeblack@google.com{ 40912927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 41012927Sgabeblack@google.com return 0.0; 41112927Sgabeblack@google.com} 41212927Sgabeblack@google.com 41312927Sgabeblack@google.comstd::string 41412927Sgabeblack@google.comsc_time_tuple::to_string() const 41512927Sgabeblack@google.com{ 41612927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 41712927Sgabeblack@google.com return ""; 41812927Sgabeblack@google.com} 41912927Sgabeblack@google.com 42012837Sgabeblack@google.com} // namespace sc_core 421