sc_time.cc revision 13039
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{ 17912837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 18012837Sgabeblack@google.com return 0.0; 18112837Sgabeblack@google.com} 18212837Sgabeblack@google.comdouble 18312837Sgabeblack@google.comsc_time::to_seconds() const 18412837Sgabeblack@google.com{ 18512837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 18612837Sgabeblack@google.com return 0.0; 18712837Sgabeblack@google.com} 18812837Sgabeblack@google.com 18912837Sgabeblack@google.comconst std::string 19012837Sgabeblack@google.comsc_time::to_string() const 19112837Sgabeblack@google.com{ 19212837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 19312837Sgabeblack@google.com return ""; 19412837Sgabeblack@google.com} 19512837Sgabeblack@google.com 19612837Sgabeblack@google.combool 19712983Sgabeblack@google.comsc_time::operator == (const sc_time &t) const 19812837Sgabeblack@google.com{ 19912983Sgabeblack@google.com return val == t.val; 20012837Sgabeblack@google.com} 20112837Sgabeblack@google.com 20212837Sgabeblack@google.combool 20312983Sgabeblack@google.comsc_time::operator != (const sc_time &t) const 20412837Sgabeblack@google.com{ 20512983Sgabeblack@google.com return val != t.val; 20612837Sgabeblack@google.com} 20712837Sgabeblack@google.com 20812837Sgabeblack@google.combool 20912983Sgabeblack@google.comsc_time::operator < (const sc_time &t) const 21012837Sgabeblack@google.com{ 21112983Sgabeblack@google.com return val < t.val; 21212837Sgabeblack@google.com} 21312837Sgabeblack@google.com 21412837Sgabeblack@google.combool 21512983Sgabeblack@google.comsc_time::operator <= (const sc_time &t) const 21612837Sgabeblack@google.com{ 21712983Sgabeblack@google.com return val <= t.val; 21812837Sgabeblack@google.com} 21912837Sgabeblack@google.com 22012837Sgabeblack@google.combool 22112983Sgabeblack@google.comsc_time::operator > (const sc_time &t) const 22212837Sgabeblack@google.com{ 22312983Sgabeblack@google.com return val > t.val; 22412837Sgabeblack@google.com} 22512837Sgabeblack@google.com 22612837Sgabeblack@google.combool 22712983Sgabeblack@google.comsc_time::operator >= (const sc_time &t) const 22812837Sgabeblack@google.com{ 22912983Sgabeblack@google.com return val >= t.val; 23012837Sgabeblack@google.com} 23112837Sgabeblack@google.com 23212837Sgabeblack@google.comsc_time & 23312983Sgabeblack@google.comsc_time::operator += (const sc_time &t) 23412837Sgabeblack@google.com{ 23512983Sgabeblack@google.com val += t.val; 23612837Sgabeblack@google.com return *this; 23712837Sgabeblack@google.com} 23812837Sgabeblack@google.com 23912837Sgabeblack@google.comsc_time & 24012983Sgabeblack@google.comsc_time::operator -= (const sc_time &t) 24112837Sgabeblack@google.com{ 24212983Sgabeblack@google.com val -= t.val; 24312837Sgabeblack@google.com return *this; 24412837Sgabeblack@google.com} 24512837Sgabeblack@google.com 24612837Sgabeblack@google.comsc_time & 24712837Sgabeblack@google.comsc_time::operator *= (double) 24812837Sgabeblack@google.com{ 24912837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 25012837Sgabeblack@google.com return *this; 25112837Sgabeblack@google.com} 25212837Sgabeblack@google.com 25312837Sgabeblack@google.comsc_time & 25412837Sgabeblack@google.comsc_time::operator /= (double) 25512837Sgabeblack@google.com{ 25612837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 25712837Sgabeblack@google.com return *this; 25812837Sgabeblack@google.com} 25912837Sgabeblack@google.com 26012837Sgabeblack@google.comvoid 26112983Sgabeblack@google.comsc_time::print(std::ostream &os) const 26212837Sgabeblack@google.com{ 26312983Sgabeblack@google.com if (val == 0) { 26412983Sgabeblack@google.com os << "0 s"; 26512983Sgabeblack@google.com } else { 26612983Sgabeblack@google.com //XXX Assuming the time resolution is 1ps. 26712983Sgabeblack@google.com sc_time_unit tu = SC_PS; 26812983Sgabeblack@google.com uint64_t scaled = val; 26912983Sgabeblack@google.com while (tu < SC_SEC && (scaled % 1000) == 0) { 27012983Sgabeblack@google.com tu = (sc_time_unit)((int)tu + 1); 27112983Sgabeblack@google.com scaled /= 1000; 27212983Sgabeblack@google.com } 27312983Sgabeblack@google.com 27412983Sgabeblack@google.com os << scaled << ' ' << TimeUnitNames[tu]; 27512983Sgabeblack@google.com } 27612837Sgabeblack@google.com} 27712837Sgabeblack@google.com 27812923Sgabeblack@google.comsc_time 27912983Sgabeblack@google.comsc_time::from_value(sc_dt::uint64 u) 28012923Sgabeblack@google.com{ 28112983Sgabeblack@google.com sc_time t; 28212983Sgabeblack@google.com t.val = u; 28312983Sgabeblack@google.com return t; 28412923Sgabeblack@google.com} 28512923Sgabeblack@google.com 28612923Sgabeblack@google.comsc_time 28712923Sgabeblack@google.comsc_time::from_seconds(double) 28812923Sgabeblack@google.com{ 28912923Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 29012923Sgabeblack@google.com return sc_time(); 29112923Sgabeblack@google.com} 29212923Sgabeblack@google.com 29312923Sgabeblack@google.comsc_time 29412923Sgabeblack@google.comsc_time::from_string(const char *str) 29512923Sgabeblack@google.com{ 29612923Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 29712923Sgabeblack@google.com return sc_time(); 29812923Sgabeblack@google.com} 29912923Sgabeblack@google.com 30012837Sgabeblack@google.comconst sc_time 30112989Sgabeblack@google.comoperator + (const sc_time &a, const sc_time &b) 30212837Sgabeblack@google.com{ 30312989Sgabeblack@google.com return sc_time::from_value(a.value() + b.value()); 30412837Sgabeblack@google.com} 30512837Sgabeblack@google.com 30612837Sgabeblack@google.comconst sc_time 30712989Sgabeblack@google.comoperator - (const sc_time &a, const sc_time &b) 30812837Sgabeblack@google.com{ 30912989Sgabeblack@google.com return sc_time::from_value(a.value() - b.value()); 31012837Sgabeblack@google.com} 31112837Sgabeblack@google.com 31212837Sgabeblack@google.comconst sc_time 31312837Sgabeblack@google.comoperator * (const sc_time &, double) 31412837Sgabeblack@google.com{ 31512837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 31612837Sgabeblack@google.com return sc_time(); 31712837Sgabeblack@google.com} 31812837Sgabeblack@google.com 31912837Sgabeblack@google.comconst sc_time 32012837Sgabeblack@google.comoperator * (double, const sc_time &) 32112837Sgabeblack@google.com{ 32212837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 32312837Sgabeblack@google.com return sc_time(); 32412837Sgabeblack@google.com} 32512837Sgabeblack@google.com 32612837Sgabeblack@google.comconst sc_time 32712837Sgabeblack@google.comoperator / (const sc_time &, double) 32812837Sgabeblack@google.com{ 32912837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 33012837Sgabeblack@google.com return sc_time(); 33112837Sgabeblack@google.com} 33212837Sgabeblack@google.com 33312837Sgabeblack@google.comdouble 33412837Sgabeblack@google.comoperator / (const sc_time &, const sc_time &) 33512837Sgabeblack@google.com{ 33612837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 33712837Sgabeblack@google.com return 0.0; 33812837Sgabeblack@google.com} 33912837Sgabeblack@google.com 34012837Sgabeblack@google.comstd::ostream & 34112983Sgabeblack@google.comoperator << (std::ostream &os, const sc_time &t) 34212837Sgabeblack@google.com{ 34312983Sgabeblack@google.com t.print(os); 34412837Sgabeblack@google.com return os; 34512837Sgabeblack@google.com} 34612837Sgabeblack@google.com 34712837Sgabeblack@google.comconst sc_time SC_ZERO_TIME; 34812837Sgabeblack@google.com 34912837Sgabeblack@google.comvoid 35012837Sgabeblack@google.comsc_set_time_resolution(double, sc_time_unit) 35112837Sgabeblack@google.com{ 35212837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 35312837Sgabeblack@google.com} 35412837Sgabeblack@google.com 35512837Sgabeblack@google.comsc_time 35612837Sgabeblack@google.comsc_get_time_resolution() 35712837Sgabeblack@google.com{ 35812837Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 35912837Sgabeblack@google.com return sc_time(); 36012837Sgabeblack@google.com} 36112837Sgabeblack@google.com 36212837Sgabeblack@google.comconst sc_time & 36312837Sgabeblack@google.comsc_max_time() 36412837Sgabeblack@google.com{ 36512989Sgabeblack@google.com static const sc_time MaxScTime = sc_time::from_value(MaxTick); 36612989Sgabeblack@google.com return MaxScTime; 36712837Sgabeblack@google.com} 36812837Sgabeblack@google.com 36912916Sgabeblack@google.comvoid 37012916Sgabeblack@google.comsc_set_default_time_unit(double, sc_time_unit) 37112916Sgabeblack@google.com{ 37212916Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 37312916Sgabeblack@google.com} 37412916Sgabeblack@google.com 37512916Sgabeblack@google.comsc_time 37612916Sgabeblack@google.comsc_get_default_time_unit() 37712916Sgabeblack@google.com{ 37812916Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 37912916Sgabeblack@google.com return *(sc_time *)nullptr; 38012916Sgabeblack@google.com} 38112916Sgabeblack@google.com 38212927Sgabeblack@google.comsc_time_tuple::sc_time_tuple(const sc_time &) 38312927Sgabeblack@google.com{ 38412927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 38512927Sgabeblack@google.com} 38612927Sgabeblack@google.com 38712927Sgabeblack@google.combool 38812927Sgabeblack@google.comsc_time_tuple::has_value() const 38912927Sgabeblack@google.com{ 39012927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 39112927Sgabeblack@google.com return false; 39212927Sgabeblack@google.com} 39312927Sgabeblack@google.com 39412927Sgabeblack@google.comsc_dt::uint64 39512927Sgabeblack@google.comsc_time_tuple::value() const 39612927Sgabeblack@google.com{ 39712927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 39812927Sgabeblack@google.com return 0; 39912927Sgabeblack@google.com} 40012927Sgabeblack@google.com 40112927Sgabeblack@google.comconst char * 40212927Sgabeblack@google.comsc_time_tuple::unit_symbol() const 40312927Sgabeblack@google.com{ 40412927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 40512927Sgabeblack@google.com return ""; 40612927Sgabeblack@google.com} 40712927Sgabeblack@google.com 40812927Sgabeblack@google.comdouble 40912927Sgabeblack@google.comsc_time_tuple::to_double() const 41012927Sgabeblack@google.com{ 41112927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 41212927Sgabeblack@google.com return 0.0; 41312927Sgabeblack@google.com} 41412927Sgabeblack@google.com 41512927Sgabeblack@google.comstd::string 41612927Sgabeblack@google.comsc_time_tuple::to_string() const 41712927Sgabeblack@google.com{ 41812927Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 41912927Sgabeblack@google.com return ""; 42012927Sgabeblack@google.com} 42112927Sgabeblack@google.com 42212837Sgabeblack@google.com} // namespace sc_core 423