scc  2022.4.0
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 
46 using namespace sc_core;
47 using namespace scc;
48 
49 tracer::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)
50 : tracer_base(nm)
51 , cci_broker(cci::cci_get_broker())
52 , txdb(nullptr)
53 , lwtr_db(nullptr)
54 , owned{sig_type != NONE} {
55  if(sig_type == ENABLE)
56  sig_type = static_cast<file_type>(sig_trace_type.get_value());
57  if(sig_type != NONE) {
58  switch(sig_type) {
59  default:
60  trf = sc_create_vcd_trace_file(name.c_str());
61  break;
62  case PULL_VCD:
63  trf = scc::create_vcd_pull_trace_file(name.c_str());
64  break;
65  case PUSH_VCD:
66  trf = scc::create_vcd_push_trace_file(name.c_str());
67  break;
68  case FST:
69  trf = scc::create_fst_trace_file(name.c_str());
70  break;
71  }
72  }
73  if(trf)
74  trf->set_time_unit(1, SC_PS);
75  init_tx_db(tx_type == ENABLE ? static_cast<file_type>(tx_trace_type.get_value()) : tx_type, std::move(name));
76 }
77 
78 tracer::tracer(std::string const&& name, file_type tx_type, sc_core::sc_trace_file* tf, sc_core::sc_object* top,
79  sc_core::sc_module_name const& nm)
80 : tracer_base(nm)
81 , cci_broker(cci::cci_get_broker())
82 , txdb(nullptr)
83 , lwtr_db(nullptr)
84 , owned{false} {
85  trf = tf;
86  init_tx_db(tx_type == ENABLE ? static_cast<file_type>(tx_trace_type.get_value()) : tx_type, std::move(name));
87 }
88 
89 tracer::~tracer() {
90  delete txdb;
91  delete lwtr_db;
92  if(trf && owned)
93  scc_close_vcd_trace_file(trf);
94 }
95 
96 void tracer::init_tx_db(file_type type, std::string const&& name) {
97  if(type != NONE) {
98  std::stringstream ss;
99  ss << name;
100  switch(type) {
101  default:
102  SCVNS scv_tr_text_init();
103  ss << ".txlog";
104  break;
105  case COMPRESSED: {
106  auto* val = getenv("SCC_SCV_TR_COMPRESSION_LEVEL");
107  auto level = val ? atoi(val) : std::numeric_limits<unsigned>::max();
108  switch(level) {
109  case 0:
110  SCVNS scv_tr_plain_init();
111  break;
112  case 2:
113  SCVNS scv_tr_compressed_init();
114  break;
115  default:
116  SCVNS scv_tr_lz4_init();
117  break;
118  }
119  ss << ".txlog";
120  } break;
121 #ifdef WITH_SQLITE
122  case SQLITE:
123  SCVNS scv_tr_sqlite_init();
124  ss << ".txdb";
125  break;
126 #endif
127  case FTR:
128  SCVNS scv_tr_cbor_init(false);
129  break;
130  case CFTR:
131  SCVNS scv_tr_cbor_init(true);
132  break;
133  case LWFTR:
134  lwtr::tx_ftr_init(false);
135  break;
136  case LWCFTR:
137  lwtr::tx_ftr_init(true);
138  break;
139  case CUSTOM:
140  SCVNS scv_tr_mtc_init();
141  ss << ".txlog";
142  break;
143  }
144  if(type == LWFTR || type == LWCFTR) {
145  lwtr_db = new lwtr::tx_db(name.c_str());
146  } else {
147  txdb = new SCVNS scv_tr_db(ss.str().c_str());
148  }
149  if(trf) {
150  trf->write_comment(std::string("TXREC: ") + ss.str());
151  }
152  }
153 }
154 
155 void tracer::end_of_elaboration() {
156  if(trf) {
157  if(top) {
158  descend(top, trf);
159  } else {
160  for(auto o : sc_get_top_level_objects())
161  descend(o, default_trace_enable);
162  }
163  }
164 }
165 
166 void tracer::end_of_simulation() {
167  if(close_db_in_eos.get_value()) {
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 }
base class for automatic tracer
Definition: tracer_base.h:78
bool default_trace_enable
the default for tracing if no attribute is configured
Definition: tracer_base.h:139
cci::cci_param< bool > close_db_in_eos
Definition: tracer.h:88
SCC SystemC utilities.
sc_core::sc_trace_file * create_fst_trace_file(const char *name, std::function< bool()> enable)
create FST file which uses pull mechanism
Definition: fst_trace.cpp:490
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...
Definition: scv_tr_mtc.cpp:327
void scv_tr_sqlite_init()
initializes the infrastructure to use a SQLite based transaction recording database