scc 2025.09
SystemC components library
tracer.cpp
1/*******************************************************************************
2 * Copyright 2017, 2018 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16/*
17 * tracer.cpp
18 *
19 * Created on: Nov 9, 2016
20 * Author: developer
21 */
22
23#include "tracer.h"
24#include "report.h"
25#include "sc_vcd_trace.h"
26#include "scv/scv_tr_db.h"
27#include "utilities.h"
28#include <scc/sc_vcd_trace.h>
29#include <scc/trace.h>
30#ifdef HAS_SCV
31#include <scv.h>
32#ifndef SCVNS
33#define SCVNS
34#endif
35#else
36#include <scv-tr.h>
37#ifndef SCVNS
38#define SCVNS ::scv_tr::
39#endif
40#endif
41#include <cstring>
42#include <iostream>
43#include <lwtr/lwtr.h>
44#include <sstream>
45
46using namespace sc_core;
47using namespace scc;
48
49static char const* const tx_trace_type_name = "scc_tracer.tx_trace_type";
50static char const* const sig_trace_type_name = "scc_tracer.sig_trace_type";
51static char const* const close_db_in_eos_name = "scc_tracer.close_db_in_eos";
52
53tracer::tracer(std::string const&& name, file_type tx_type, file_type sig_type, sc_core::sc_object* top, sc_core::sc_module_name const& nm)
54: tracer_base(nm)
55, txdb(nullptr)
56, lwtr_db(nullptr)
57, owned{sig_type != NONE} {
58 init_cci_handles();
59 if(sig_type == ENABLE)
60 sig_type = static_cast<file_type>(sig_trace_type_handle.get_cci_value().get<unsigned>());
61 if(sig_type != NONE) {
62 switch(sig_type) {
63 default:
64 trf = sc_create_vcd_trace_file(name.c_str());
65 break;
66 case PULL_VCD:
67 trf = scc::create_vcd_pull_trace_file(name.c_str());
68 break;
69 case PUSH_VCD:
70 trf = scc::create_vcd_push_trace_file(name.c_str());
71 break;
72 case FST:
73 trf = scc::create_fst_trace_file(name.c_str());
74 break;
75 }
76 }
77 if(trf)
78 trf->set_time_unit(1, SC_PS);
79 init_tx_db(tx_type == ENABLE ? static_cast<file_type>(tx_trace_type_handle.get_cci_value().get<unsigned>()) : tx_type, std::move(name));
80}
81
82tracer::tracer(std::string const&& name, file_type tx_type, sc_core::sc_trace_file* tf, sc_core::sc_object* top,
83 sc_core::sc_module_name const& nm)
84: tracer_base(nm)
85, txdb(nullptr)
86, lwtr_db(nullptr)
87, owned{false} {
88 init_cci_handles();
89 trf = tf;
90 init_tx_db(tx_type == ENABLE ? static_cast<file_type>(tx_trace_type_handle.get_cci_value().get<unsigned>()) : tx_type, std::move(name));
91}
92
94 delete txdb;
95 delete lwtr_db;
96 if(trf && owned)
97 scc_close_vcd_trace_file(trf);
98}
99
100void tracer::init_tx_db(file_type type, std::string const&& name) {
101 if(type != NONE) {
102 std::stringstream ss;
103 ss << name;
104 switch(type) {
105 default:
106 SCVNS scv_tr_text_init();
107 ss << ".txlog";
108 break;
109 case COMPRESSED: {
110 auto* val = getenv("SCC_SCV_TR_COMPRESSION_LEVEL");
111 auto level = val ? atoi(val) : std::numeric_limits<unsigned>::max();
112 switch(level) {
113 case 0:
114 SCVNS scv_tr_plain_init();
115 break;
116 case 2:
117 SCVNS scv_tr_compressed_init();
118 break;
119 default:
120 SCVNS scv_tr_lz4_init();
121 break;
122 }
123 ss << ".txlog";
124 } break;
125#ifdef WITH_SQLITE
126 case SQLITE:
127 SCVNS scv_tr_sqlite_init();
128 ss << ".txdb";
129 break;
130#endif
131 case FTR:
132 SCVNS scv_tr_ftr_init(false);
133 break;
134 case CFTR:
135 SCVNS scv_tr_ftr_init(true);
136 break;
137 case LWFTR:
138 lwtr::tx_ftr_init(false);
139 break;
140 case LWCFTR:
141 lwtr::tx_ftr_init(true);
142 break;
143 case CUSTOM:
144 SCVNS scv_tr_mtc_init();
145 ss << ".txlog";
146 break;
147 }
148 if(type == LWFTR || type == LWCFTR) {
149 lwtr_db = new lwtr::tx_db(name.c_str());
150 } else {
151 txdb = new SCVNS scv_tr_db(ss.str().c_str());
152 }
153 if(trf) {
154 trf->write_comment(std::string("TXREC: ") + ss.str());
155 }
156 }
157}
158
159void tracer::end_of_elaboration() {
160 if(trf) {
161 for(auto o : sc_get_top_level_objects())
162 descend(o, default_trace_enable_handle.get_cci_value().get<bool>());
163 }
164}
165
166void tracer::end_of_simulation() {
167 if(close_db_in_eos_handle.get_cci_value().get<bool>()) {
168 delete txdb;
169 txdb = nullptr;
170 delete lwtr_db;
171 lwtr_db = nullptr;
172 if(trf && owned) {
173 scc_close_vcd_trace_file(trf);
174 trf = nullptr;
175 }
176 }
177}
178
179void tracer::init_cci_handles() {
180 tx_trace_type_handle = cci_broker.get_param_handle(tx_trace_type_name);
181 if(!tx_trace_type_handle.is_valid()) {
182 tx_trace_type = scc::make_unique<cci::cci_param<unsigned>>(
183 tx_trace_type_name, CFTR, "Type of TX trace file used for recording. See also scc::tracer::file_type", cci::CCI_ABSOLUTE_NAME);
184 tx_trace_type_handle = cci_broker.get_param_handle(tx_trace_type_name);
185 }
186 sig_trace_type_handle = cci_broker.get_param_handle(sig_trace_type_name);
187 if(!sig_trace_type_handle.is_valid()) {
188 sig_trace_type = scc::make_unique<cci::cci_param<unsigned>>(
189 sig_trace_type_name, FST, "Type of signal trace file used for recording. See also scc::tracer::wave_type",
190 cci::CCI_ABSOLUTE_NAME);
191 sig_trace_type_handle = cci_broker.get_param_handle(sig_trace_type_name);
192 }
193 close_db_in_eos_handle = cci_broker.get_param_handle(close_db_in_eos_name);
194 if(!close_db_in_eos_handle.is_valid()) {
195
196 close_db_in_eos = scc::make_unique<cci::cci_param<bool>>(
197 close_db_in_eos_name, false, "Close the waveform/transaction tracing databases during end_of_simulation",
198 cci::CCI_ABSOLUTE_NAME);
199 close_db_in_eos_handle = cci_broker.get_param_handle(close_db_in_eos_name);
200 }
201}
cci::cci_param_handle tx_trace_type_handle
Definition tracer.h:79
virtual ~tracer() override
the destructor
Definition tracer.cpp:93
cci::cci_param_handle close_db_in_eos_handle
Definition tracer.h:89
cci::cci_param_handle sig_trace_type_handle
Definition tracer.h:84
SCC TLM utilities.
sc_core::sc_trace_file * create_fst_trace_file(const char *name, std::function< bool()> enable)
create FST file which uses pull mechanism
sc_core::sc_trace_file * create_vcd_pull_trace_file(const char *name, std::function< bool()> enable=std::function< bool()>())
create VCD file which uses pull mechanism
sc_core::sc_trace_file * create_vcd_push_trace_file(const char *name, std::function< bool()> enable=std::function< bool()>())
create VCD file which uses push mechanism
void scv_tr_mtc_init()
initializes the infrastructure to use a compressed text based transaction recording database with a m...
void scv_tr_sqlite_init()
initializes the infrastructure to use a SQLite based transaction recording database
base class for automatic tracer
Definition tracer_base.h:80
cci::cci_param_handle default_trace_enable_handle
Definition tracer_base.h:84
cci::cci_broker_handle cci_broker