sc_time.cc revision 13335:299a16ef8e3c
1/* 2 * Copyright 2018 Google, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer; 8 * redistributions in binary form must reproduce the above copyright 9 * notice, this list of conditions and the following disclaimer in the 10 * documentation and/or other materials provided with the distribution; 11 * neither the name of the copyright holders nor the names of its 12 * contributors may be used to endorse or promote products derived from 13 * this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Authors: Gabe Black 28 */ 29 30#include <sstream> 31#include <vector> 32 33#include "base/types.hh" 34#include "python/pybind11/pybind.hh" 35#include "sim/core.hh" 36#include "systemc/core/python.hh" 37#include "systemc/core/time.hh" 38#include "systemc/ext/core/messages.hh" 39#include "systemc/ext/core/sc_main.hh" 40#include "systemc/ext/core/sc_time.hh" 41#include "systemc/ext/utils/sc_report_handler.hh" 42 43namespace sc_core 44{ 45 46namespace 47{ 48 49bool timeFixed = false; 50bool pythonReady = false; 51 52struct SetInfo 53{ 54 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) : 55 time(time), d(d), tu(tu) 56 {} 57 58 ::sc_core::sc_time *time; 59 double d; 60 ::sc_core::sc_time_unit tu; 61}; 62std::vector<SetInfo> toSet; 63 64void 65setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu) 66{ 67 double scale = sc_gem5::TimeUnitScale[tu] * SimClock::Float::s; 68 // Accellera claims there is a linux bug, and that these next two 69 // lines work around them. 70 volatile double tmp = d * scale + 0.5; 71 *time = sc_time::from_value(static_cast<uint64_t>(tmp)); 72} 73 74void 75fixTime() 76{ 77 auto ticks = pybind11::module::import("m5.ticks"); 78 auto fix_global_frequency = ticks.attr("fixGlobalFrequency"); 79 fix_global_frequency(); 80 81 for (auto &t: toSet) 82 setWork(t.time, t.d, t.tu); 83 toSet.clear(); 84} 85 86void 87attemptToFixTime() 88{ 89 // Only fix time once. 90 if (!timeFixed) { 91 timeFixed = true; 92 93 // If we've run, python is working and we haven't fixed time yet. 94 if (pythonReady) 95 fixTime(); 96 } 97} 98 99void 100setGlobalFrequency(Tick ticks_per_second) 101{ 102 auto ticks = pybind11::module::import("m5.ticks"); 103 auto set_global_frequency = ticks.attr("setGlobalFrequency"); 104 set_global_frequency(ticks_per_second); 105 fixTime(); 106} 107 108void 109set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) 110{ 111 if (d != 0) 112 attemptToFixTime(); 113 if (pythonReady) { 114 // Time should be working. Set up this sc_time. 115 setWork(time, d, tu); 116 } else { 117 // Time isn't set up yet. Defer setting up this sc_time. 118 toSet.emplace_back(time, d, tu); 119 } 120} 121 122class TimeSetter : public ::sc_gem5::PythonReadyFunc 123{ 124 public: 125 TimeSetter() : ::sc_gem5::PythonReadyFunc() {} 126 127 void 128 run() override 129 { 130 // Record that we've run and python/pybind should be usable. 131 pythonReady = true; 132 133 // If time is already fixed, let python know. 134 if (timeFixed) 135 fixTime(); 136 } 137} timeSetter; 138 139double defaultUnit = 1.0e-9; 140 141} // anonymous namespace 142 143sc_time::sc_time() : val(0) {} 144 145sc_time::sc_time(double d, sc_time_unit tu) 146{ 147 val = 0; 148 set(this, d, tu); 149} 150 151sc_time::sc_time(const sc_time &t) 152{ 153 val = t.val; 154} 155 156sc_time::sc_time(double d, const char *unit) 157{ 158 sc_time_unit tu; 159 for (tu = SC_FS; tu <= SC_SEC; tu = (sc_time_unit)(tu + 1)) { 160 if (strcmp(unit, sc_gem5::TimeUnitNames[tu]) == 0 || 161 strcmp(unit, sc_gem5::TimeUnitConstantNames[tu]) == 0) { 162 break; 163 } 164 } 165 166 if (tu > SC_SEC) { 167 SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_,"invalid unit given"); 168 val = 0; 169 return; 170 } 171 set(this, d, tu); 172} 173 174sc_time::sc_time(double d, bool scale) 175{ 176 double scaler = scale ? defaultUnit : SimClock::Float::Hz; 177 set(this, d * scaler, SC_SEC); 178} 179 180sc_time::sc_time(sc_dt::uint64 v, bool scale) 181{ 182 double scaler = scale ? defaultUnit : SimClock::Float::Hz; 183 set(this, static_cast<double>(v) * scaler, SC_SEC); 184} 185 186sc_time & 187sc_time::operator = (const sc_time &t) 188{ 189 val = t.val; 190 return *this; 191} 192 193sc_dt::uint64 194sc_time::value() const 195{ 196 return val; 197} 198 199double 200sc_time::to_double() const 201{ 202 return static_cast<double>(val); 203} 204double 205sc_time::to_seconds() const 206{ 207 return to_double() * SimClock::Float::Hz; 208} 209 210const std::string 211sc_time::to_string() const 212{ 213 std::ostringstream ss; 214 print(ss); 215 return ss.str(); 216} 217 218bool 219sc_time::operator == (const sc_time &t) const 220{ 221 return val == t.val; 222} 223 224bool 225sc_time::operator != (const sc_time &t) const 226{ 227 return val != t.val; 228} 229 230bool 231sc_time::operator < (const sc_time &t) const 232{ 233 return val < t.val; 234} 235 236bool 237sc_time::operator <= (const sc_time &t) const 238{ 239 return val <= t.val; 240} 241 242bool 243sc_time::operator > (const sc_time &t) const 244{ 245 return val > t.val; 246} 247 248bool 249sc_time::operator >= (const sc_time &t) const 250{ 251 return val >= t.val; 252} 253 254sc_time & 255sc_time::operator += (const sc_time &t) 256{ 257 val += t.val; 258 return *this; 259} 260 261sc_time & 262sc_time::operator -= (const sc_time &t) 263{ 264 val -= t.val; 265 return *this; 266} 267 268sc_time & 269sc_time::operator *= (double d) 270{ 271 val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5); 272 return *this; 273} 274 275sc_time & 276sc_time::operator /= (double d) 277{ 278 val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5); 279 return *this; 280} 281 282void 283sc_time::print(std::ostream &os) const 284{ 285 os << sc_time_tuple(*this).to_string(); 286} 287 288sc_time 289sc_time::from_value(sc_dt::uint64 u) 290{ 291 if (u) 292 attemptToFixTime(); 293 sc_time t; 294 t.val = u; 295 return t; 296} 297 298sc_time 299sc_time::from_seconds(double d) 300{ 301 sc_time t; 302 set(&t, d, SC_SEC); 303 return t; 304} 305 306sc_time 307sc_time::from_string(const char *str) 308{ 309 char *end = nullptr; 310 311 double d = str ? std::strtod(str, &end) : 0.0; 312 if (str == end || d < 0.0) { 313 SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_, "invalid value given"); 314 return SC_ZERO_TIME; 315 } 316 317 while (*end && std::isspace(*end)) 318 end++; 319 320 return sc_time(d, end); 321} 322 323const sc_time 324operator + (const sc_time &a, const sc_time &b) 325{ 326 return sc_time::from_value(a.value() + b.value()); 327} 328 329const sc_time 330operator - (const sc_time &a, const sc_time &b) 331{ 332 return sc_time::from_value(a.value() - b.value()); 333} 334 335const sc_time 336operator * (const sc_time &t, double d) 337{ 338 volatile double tmp = static_cast<double>(t.value()) * d + 0.5; 339 return sc_time::from_value(static_cast<int64_t>(tmp)); 340} 341 342const sc_time 343operator * (double d, const sc_time &t) 344{ 345 volatile double tmp = d * static_cast<double>(t.value()) + 0.5; 346 return sc_time::from_value(static_cast<int64_t>(tmp)); 347} 348 349const sc_time 350operator / (const sc_time &t, double d) 351{ 352 volatile double tmp = static_cast<double>(t.value()) / d + 0.5; 353 return sc_time::from_value(static_cast<int64_t>(tmp)); 354} 355 356double 357operator / (const sc_time &t1, const sc_time &t2) 358{ 359 return t1.to_double() / t2.to_double(); 360} 361 362std::ostream & 363operator << (std::ostream &os, const sc_time &t) 364{ 365 t.print(os); 366 return os; 367} 368 369const sc_time SC_ZERO_TIME; 370 371void 372sc_set_time_resolution(double d, sc_time_unit tu) 373{ 374 if (d <= 0.0) 375 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value not positive"); 376 377 double dummy; 378 if (modf(log10(d), &dummy) != 0.0) { 379 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, 380 "value not a power of ten"); 381 } 382 if (sc_is_running()) 383 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "simulation running"); 384 385 static bool specified = false; 386 if (specified) 387 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "already specified"); 388 389 // This won't detect the timescale being fixed outside of systemc, but 390 // it's at least some protection. 391 if (timeFixed) { 392 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, 393 "sc_time object(s) constructed"); 394 } 395 396 double seconds = d * sc_gem5::TimeUnitScale[tu]; 397 if (seconds < sc_gem5::TimeUnitScale[SC_FS]) 398 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value smaller than 1 fs"); 399 400 if (seconds > defaultUnit) { 401 SC_REPORT_WARNING(SC_ID_DEFAULT_TIME_UNIT_CHANGED_, ""); 402 defaultUnit = seconds; 403 } 404 405 // Get rid of fractional parts of d. 406 while (d < 1.0 && tu > SC_FS) { 407 d *= 1000; 408 tu = (sc_time_unit)(tu - 1); 409 } 410 411 Tick ticks_per_second = 412 sc_gem5::TimeUnitFrequency[tu] / static_cast<Tick>(d); 413 setGlobalFrequency(ticks_per_second); 414 specified = true; 415} 416 417sc_time 418sc_get_time_resolution() 419{ 420 return sc_time::from_value(1); 421} 422 423const sc_time & 424sc_max_time() 425{ 426 static const sc_time MaxScTime = sc_time::from_value(MaxTick); 427 return MaxScTime; 428} 429 430void 431sc_set_default_time_unit(double d, sc_time_unit tu) 432{ 433 if (d < 0.0) 434 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "value not positive"); 435 436 double dummy; 437 if (modf(log10(d), &dummy) != 0.0) { 438 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, 439 "value not a power of ten"); 440 } 441 if (sc_is_running()) 442 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "simulation running"); 443 444 static bool specified = false; 445 if (specified) { 446 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "already specified"); 447 } 448 // This won't detect the timescale being fixed outside of systemc, but 449 // it's at least some protection. 450 if (timeFixed) { 451 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, 452 "sc_time object(s) constructed"); 453 } 454 455 // Normalize d to seconds. 456 defaultUnit = d * sc_gem5::TimeUnitScale[tu]; 457 specified = true; 458 459 double resolution = SimClock::Float::Hz; 460 if (resolution == 0.0) 461 resolution = sc_gem5::TimeUnitScale[SC_PS]; 462 if (defaultUnit < resolution) { 463 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, 464 "value smaller than time resolution"); 465 } 466} 467 468sc_time 469sc_get_default_time_unit() 470{ 471 return sc_time(defaultUnit, SC_SEC); 472} 473 474sc_time_tuple::sc_time_tuple(const sc_time &t) : 475 _value(), _unit(SC_SEC), _set(true) 476{ 477 if (!t.value()) 478 return; 479 480 Tick frequency = SimClock::Frequency; 481 482 // Shrink the frequency by scaling down the time period, ie converting 483 // it from cycles per second to cycles per millisecond, etc. 484 while (_unit > 1 && (frequency % 1000 == 0)) { 485 _unit = (sc_time_unit)((int)_unit - 1); 486 frequency /= 1000; 487 } 488 489 // Convert the frequency into a period. 490 Tick period; 491 if (frequency > 1) { 492 _unit = (sc_time_unit)((int)_unit - 1); 493 period = 1000 / frequency; 494 } else { 495 period = frequency; 496 } 497 498 // Scale our integer value by the period. 499 _value = t.value() * period; 500 501 // Shrink the scaled time value by increasing the size of the units 502 // it's measured by, avoiding fractional parts. 503 while (_unit < SC_SEC && (_value % 1000) == 0) { 504 _unit = (sc_time_unit)((int)_unit + 1); 505 _value /= 1000; 506 } 507} 508 509bool 510sc_time_tuple::has_value() const 511{ 512 return _set; 513} 514 515sc_dt::uint64 sc_time_tuple::value() const { return _value; } 516 517const char * 518sc_time_tuple::unit_symbol() const 519{ 520 return sc_gem5::TimeUnitNames[_unit]; 521} 522 523double sc_time_tuple::to_double() const { return static_cast<double>(_value); } 524 525std::string 526sc_time_tuple::to_string() const 527{ 528 std::ostringstream ss; 529 ss << _value << ' ' << unit_symbol(); 530 return ss.str(); 531} 532 533} // namespace sc_core 534