113245Sgabeblack@google.com/*
213245Sgabeblack@google.com * Copyright 2018 Google, Inc.
313245Sgabeblack@google.com *
413245Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513245Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613245Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713245Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813245Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913245Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013245Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113245Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213245Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313245Sgabeblack@google.com * this software without specific prior written permission.
1413245Sgabeblack@google.com *
1513245Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613245Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713245Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813245Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913245Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013245Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113245Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213245Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313245Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413245Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513245Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613245Sgabeblack@google.com *
2713245Sgabeblack@google.com * Authors: Gabe Black
2813245Sgabeblack@google.com */
2913245Sgabeblack@google.com
3013245Sgabeblack@google.com#include "systemc/utils/tracefile.hh"
3113245Sgabeblack@google.com
3213245Sgabeblack@google.com#include <ctime>
3313245Sgabeblack@google.com#include <iomanip>
3413245Sgabeblack@google.com
3513245Sgabeblack@google.com#include "base/output.hh"
3613245Sgabeblack@google.com#include "sim/core.hh"
3713253Sgabeblack@google.com#include "systemc/core/time.hh"
3813245Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
3913256Sgabeblack@google.com#include "systemc/ext/core/sc_time.hh"
4013245Sgabeblack@google.com#include "systemc/ext/utils/functions.hh"
4113245Sgabeblack@google.com
4213245Sgabeblack@google.comnamespace sc_gem5
4313245Sgabeblack@google.com{
4413245Sgabeblack@google.com
4513245Sgabeblack@google.comTraceFile::TraceFile(const std::string &name) :
4613245Sgabeblack@google.com    _os(simout.create(name, true, true)), timeUnitTicks(0),
4713256Sgabeblack@google.com    timeUnitValue(0.0), timeUnitUnit(::sc_core::SC_PS), _traceDeltas(false)
4813245Sgabeblack@google.com{}
4913245Sgabeblack@google.com
5013245Sgabeblack@google.comTraceFile::~TraceFile()
5113245Sgabeblack@google.com{
5213245Sgabeblack@google.com    simout.close(_os);
5313245Sgabeblack@google.com}
5413245Sgabeblack@google.com
5513245Sgabeblack@google.comstd::ostream &TraceFile::stream() { return *_os->stream(); }
5613245Sgabeblack@google.com
5713245Sgabeblack@google.comvoid
5813245Sgabeblack@google.comTraceFile::set_time_unit(double d, ::sc_core::sc_time_unit tu)
5913245Sgabeblack@google.com{
6013245Sgabeblack@google.com    timeUnitValue = d;
6113245Sgabeblack@google.com    timeUnitUnit = tu;
6213253Sgabeblack@google.com
6313253Sgabeblack@google.com    double secs = d * TimeUnitScale[tu];
6413253Sgabeblack@google.com    for (tu = ::sc_core::SC_SEC; tu > ::sc_core::SC_FS;
6513253Sgabeblack@google.com            tu = (::sc_core::sc_time_unit)(tu - 1)) {
6613253Sgabeblack@google.com        if (TimeUnitScale[tu] <= secs)
6713253Sgabeblack@google.com            break;
6813253Sgabeblack@google.com    }
6913253Sgabeblack@google.com
7013253Sgabeblack@google.com    uint64_t i = static_cast<uint64_t>(secs / TimeUnitScale[tu]);
7113253Sgabeblack@google.com    std::ostringstream ss;
7213253Sgabeblack@google.com    ss << i << " " << TimeUnitNames[tu] << " (" << _os->name() << ")";
7313253Sgabeblack@google.com    SC_REPORT_INFO("(I703) tracing timescale unit set", ss.str().c_str());
7413245Sgabeblack@google.com}
7513245Sgabeblack@google.com
7613245Sgabeblack@google.comvoid
7713245Sgabeblack@google.comTraceFile::finalizeTime()
7813245Sgabeblack@google.com{
7913256Sgabeblack@google.com    ::sc_core::sc_time time;
8013256Sgabeblack@google.com    if (timeUnitValue == 0.0) {
8113256Sgabeblack@google.com        // The time scale was never set. Use the global time resolution.
8213256Sgabeblack@google.com        time = ::sc_core::sc_get_time_resolution();
8313256Sgabeblack@google.com    } else {
8413256Sgabeblack@google.com        time = ::sc_core::sc_time(timeUnitValue, timeUnitUnit);
8513256Sgabeblack@google.com    }
8613256Sgabeblack@google.com    timeUnitTicks = time.value();
8713245Sgabeblack@google.com}
8813245Sgabeblack@google.com
8913245Sgabeblack@google.com} // namespace sc_gem5
90