Skip to content

Commit

Permalink
adds chunk 0 to SCV FTR
Browse files Browse the repository at this point in the history
  • Loading branch information
eyck committed Feb 22, 2023
1 parent 8fc1394 commit e0c3b9a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 37 deletions.
11 changes: 7 additions & 4 deletions examples/transaction_recording/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 3.12)
add_executable (transaction_recording
scv_tr_recording_example.cpp
)
add_executable (transaction_recording scv_tr_recording_example.cpp )
target_link_libraries (transaction_recording LINK_PUBLIC scc)

add_test(NAME tx_rec_test COMMAND transaction_recording)
add_executable (transaction_recording_c scv_tr_recording_example.cpp )
target_compile_definitions(transaction_recording_c PUBLIC COMPRESSED)
target_link_libraries (transaction_recording_c LINK_PUBLIC scc)

add_test(NAME tx_rec_test COMMAND transaction_recording)
add_test(NAME tx_rec_compressed_test COMMAND transaction_recording)
7 changes: 6 additions & 1 deletion examples/transaction_recording/scv_tr_recording_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,14 @@ inline void design::data_phase() {
int sc_main(int argc, char *argv[]) {
auto start = std::chrono::system_clock::now();
scc::init_logging(scc::LogConfig().logLevel(scc::log::DEBUG));
scv_tr_cbor_init(false);
scv_tr::scv_tr_text_init();
#ifdef COMPRESSED
scv_tr_cbor_init(true);
scv_tr_db db("my_db_c");
#else
scv_tr_cbor_init(false);
scv_tr_db db("my_db");
#endif
sc_trace_file *tf = sc_create_vcd_trace_file("my_db");
// create signals
sc_clock clk("clk", 20.0, SC_NS, 0.5, 0.0, SC_NS, true);
Expand Down
78 changes: 60 additions & 18 deletions src/sysc/scc/scv/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include <limits>
#include <unordered_map>
#include <lz4.h>
#include <ctime>

namespace cbor {
enum {
MAX_TXBUFFER_SIZE=1<<16,
MAX_REL_SIZE=1<<16,
INFO_CHUNK_ID=0,
DICT_CHUNK_ID=1,
DIR_CHUNK_ID=2,
TX_CHUNK_ID=3,
Expand Down Expand Up @@ -168,28 +170,56 @@ struct chunk_writer {
}

void write_chunk(uint64_t type, std::vector<uint8_t> const& data, uint64_t subtype = std::numeric_limits<uint64_t>::max()){
enc.write_tag(6+(type*2)+(COMPRESSED?1:0)); // unassigned tags
if(subtype < std::numeric_limits<uint64_t>::max()) {
enc.start_array(2+(COMPRESSED?1:0));
enc.write(subtype);
} else if(COMPRESSED)
enc.start_array(2);
if(COMPRESSED){
enc.write(data.size());
const int max_dst_size = LZ4_compressBound(data.size());
uint8_t* compressed_data = (uint8_t*)malloc(max_dst_size);
const int compressed_data_size = LZ4_compress_default(
reinterpret_cast<char const*>(data.data()),
reinterpret_cast<char*>(compressed_data),
data.size(), max_dst_size);
enc.write(compressed_data, compressed_data_size);
free(compressed_data);
if(COMPRESSED && type>INFO_CHUNK_ID) {
enc.write_tag(6+type*2+1); // unassigned tags
if(subtype < std::numeric_limits<uint64_t>::max()) {
enc.start_array(3);
enc.write(subtype);
} else
enc.start_array(2);
enc.write(data.size());
const int max_dst_size = LZ4_compressBound(data.size());
uint8_t* compressed_data = (uint8_t*)malloc(max_dst_size);
const int compressed_data_size = LZ4_compress_default(
reinterpret_cast<char const*>(data.data()),
reinterpret_cast<char*>(compressed_data),
data.size(), max_dst_size);
enc.write(compressed_data, compressed_data_size);
free(compressed_data);
} else {
enc.write(data.data(), data.size());
enc.write_tag(6+type*2); // unassigned tags
if(subtype < std::numeric_limits<uint64_t>::max()) {
enc.start_array(2);
enc.write(subtype);
}
enc.write(data.data(), data.size());
}
}
};

struct info {
encoder<memory_writer> enc;

inline void add_time_scale(uint64_t denominator, uint64_t numerator=1) {
enc.start_array(3);
enc.write(numerator);
enc.write(denominator);
enc.write_tag(1);
enc.write(time(nullptr));
}

template<bool COMPRESSED>
void flush(chunk_writer<COMPRESSED>& cw) {
if(enc.is_empty()) return;
cw.write_chunk(INFO_CHUNK_ID, enc.buffer);
enc.buffer.clear();
}

size_t size() {
return enc.buffer.size();
}
};

struct dictionary {
std::vector<std::string> out_dict{""};
std::unordered_map<std::string, size_t> lut;
Expand Down Expand Up @@ -353,6 +383,12 @@ struct tx_block {
* cbor array(*) of tagged chunks, chunks can be
* chunk type 0 (info)
* cbor tag(6)
* array(3);
* unsigned - numerator of timescale)
* unsigned - denominator of timescale)
* epoch time - creation time
* cbor tag(1)
* unsigned - timestamp
* chunk type 1 (dictionary)
* cbor tag(8) uncompressed
* bytes() - content
Expand Down Expand Up @@ -437,6 +473,7 @@ template<bool COMPRESSED=false>
struct chunked_writer {

chunk_writer<COMPRESSED> cw;
info inf;
dictionary dict;
directory dir{dict};
relations rel{dict};
Expand All @@ -459,7 +496,12 @@ struct chunked_writer {
for(auto e: free_pool_blocks) free(e);
}

inline void writeStream(uint64_t id, std::string const& name, std::string const& kind) {
inline void writeInfo(uint64_t denominator, uint64_t numerator=1) {
inf.add_time_scale(denominator, numerator);
inf.flush(cw);
}

inline void writeStream(uint64_t id, std::string const& name, std::string const& kind) {
dir.add_stream(id, name, kind);
if(id>=fiber_blocks.size()) fiber_blocks.resize(id+1);
fiber_blocks[id].reset(new tx_block(dict, id));
Expand Down
27 changes: 14 additions & 13 deletions src/sysc/scc/scv/scv_tr_ftr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ struct tx_db {
if((_scv_tr_db.get_name() != nullptr) && (strlen(_scv_tr_db.get_name()) != 0))
fName = _scv_tr_db.get_name();
try {
db = new chunked_writer<COMPRESSED>(fName+".txftr");
db = new chunked_writer<COMPRESSED>(fName+".ftr");
} catch(...) {
_scv_message::message(_scv_message::TRANSACTION_RECORDING_INTERNAL, "Can't open recording file");
}
if(!db->cw.enc.ofs.is_open()) {
delete db;
db=nullptr;
}
} else
db->writeInfo(sc_core::sc_time(1, sc_core::SC_SEC)/sc_core::sc_time(1, sc_core::SC_PS));
break;
case scv_tr_db::DELETE:
try {
Expand All @@ -76,7 +77,7 @@ struct tx_db {
}
// ----------------------------------------------------------------------------
static void streamCb(const scv_tr_stream& s, scv_tr_stream::callback_reason reason, void* data) {
if(reason == scv_tr_stream::CREATE) {
if(db && reason == scv_tr_stream::CREATE) {
try {
db->writeStream(s.get_id(), s.get_name(), s.get_stream_kind());
} catch(std::runtime_error& e) {
Expand All @@ -86,15 +87,15 @@ struct tx_db {
}
// ----------------------------------------------------------------------------
static inline void recordAttribute(uint64_t id, event_type event, const string& name, data_type type, const string& value) {
try {
db->writeAttribute(id, event, name, static_cast<cbor::data_type>(type), value);
} catch(std::runtime_error& e) {
_scv_message::message(_scv_message::TRANSACTION_RECORDING_INTERNAL, "Can't create attribute entry");
}
if(db) try {
db->writeAttribute(id, event, name, static_cast<cbor::data_type>(type), value);
} catch(std::runtime_error& e) {
_scv_message::message(_scv_message::TRANSACTION_RECORDING_INTERNAL, "Can't create attribute entry");
}
}
// ----------------------------------------------------------------------------
static inline void recordAttribute(uint64_t id, event_type event, const string& name, data_type type, char const * value) {
try {
if(db) try {
db->writeAttribute(id, event, name, static_cast<cbor::data_type>(type), value);
} catch(std::runtime_error& e) {
_scv_message::message(_scv_message::TRANSACTION_RECORDING_INTERNAL, "Can't create attribute entry");
Expand All @@ -103,15 +104,15 @@ struct tx_db {
// ----------------------------------------------------------------------------
template<typename T>
static inline void recordAttribute(uint64_t id, event_type event, const string& name, data_type type, T value) {
try {
if(db) try {
db->writeAttribute(id, event, name, static_cast<cbor::data_type>(type), value);
} catch(std::runtime_error& e) {
_scv_message::message(_scv_message::TRANSACTION_RECORDING_INTERNAL, "Can't create attribute entry");
}
}
// ----------------------------------------------------------------------------
static inline std::string get_name(const char* prefix, const scv_extensions_if* my_exts_p) {
string name{prefix};
static inline std::string get_name(const char* prefix, const scv_extensions_if* my_exts_p) {
string name{prefix};
if(!prefix || strlen(prefix) == 0) {
name = my_exts_p->get_name();
} else {
Expand Down Expand Up @@ -185,7 +186,7 @@ static inline std::string get_name(const char* prefix, const scv_extensions_if*
}
// ----------------------------------------------------------------------------
static void generatorCb(const scv_tr_generator_base& g, scv_tr_generator_base::callback_reason reason, void* data) {
if(reason == scv_tr_generator_base::CREATE && db) {
if(db && reason == scv_tr_generator_base::CREATE) {
try {
db->writeGenerator(g.get_id(), g.get_name(), g.get_scv_tr_stream().get_id());
} catch(std::runtime_error& e) {
Expand Down
2 changes: 1 addition & 1 deletion third_party/lwtr4sc

0 comments on commit e0c3b9a

Please sign in to comment.