Basic C++ file IO for trace logger
This commit is contained in:
@@ -8,95 +8,34 @@
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <unistd.h>
|
||||
#include "SimMemTraceLogger.h"
|
||||
#include "SimMemTrace.h"
|
||||
|
||||
// Global singleton instance
|
||||
static std::unique_ptr<MemTraceLogger> logger;
|
||||
static std::unique_ptr<MemTraceWriter> logger;
|
||||
|
||||
MemTraceLogger::MemTraceLogger(const std::string &filename) {
|
||||
MemTraceWriter::MemTraceWriter(const std::string &filename) {
|
||||
char cwd[4096];
|
||||
if (getcwd(cwd, sizeof(cwd))) {
|
||||
printf("MemTraceLogger: current working dir: %s\n", cwd);
|
||||
}
|
||||
|
||||
infile.open(filename);
|
||||
if (infile.fail()) {
|
||||
outfile = fopen(filename.c_str(), "w");
|
||||
if (!outfile) {
|
||||
fprintf(stderr, "failed to open file %s\n", filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
MemTraceLogger::~MemTraceLogger() {
|
||||
infile.close();
|
||||
printf("MemTraceLogger destroyed\n");
|
||||
MemTraceWriter::~MemTraceWriter() {
|
||||
fclose(outfile);
|
||||
printf("MemTraceWriter destroyed\n");
|
||||
}
|
||||
|
||||
#if 0
|
||||
// Parse trace file in its entirety and store it into internal structure.
|
||||
// TODO: might block for a long time when the trace gets big, check if need to
|
||||
// be broken down
|
||||
void MemTraceLogger::parse() {
|
||||
MemTraceLine line;
|
||||
void MemTraceWriter::write_trace_at(const MemTraceLine line) {
|
||||
printf("tick(): cycle=%ld\n", line.cycle);
|
||||
|
||||
printf("MemTraceLogger: started parsing\n");
|
||||
|
||||
while (infile >> line.cycle >> line.loadstore >> line.core_id >>
|
||||
line.lane_id >> std::hex >> line.address >> line.data >> std::dec >>
|
||||
line.data_size) {
|
||||
line.valid = true;
|
||||
trace.push_back(line);
|
||||
}
|
||||
read_pos = trace.cbegin();
|
||||
|
||||
printf("MemTraceLogger: finished parsing\n");
|
||||
fprintf(outfile, "cycle=%ld\n", line.cycle);
|
||||
}
|
||||
|
||||
// Try to read a memory request that might have happened at a given cycle, on a
|
||||
// given SIMD lane (= "thread"). In case no request happened at that point,
|
||||
// return an empty line with .valid = false.
|
||||
MemTraceLine MemTraceLogger::read_trace_at(const long cycle,
|
||||
const int lane_id) {
|
||||
MemTraceLine line;
|
||||
line.valid = false;
|
||||
|
||||
// printf("tick(): cycle=%ld\n", cycle);
|
||||
|
||||
if (finished()) {
|
||||
return line;
|
||||
}
|
||||
|
||||
line = *read_pos;
|
||||
// It should always be guaranteed that we consumed all of the past lines, and
|
||||
// the next line is in the future.
|
||||
if (line.cycle < cycle) {
|
||||
// fprintf(stderr, "line.cycle=%ld, cycle=%ld\n", line.cycle, cycle);
|
||||
assert(false && "some trace lines are left unread in the past");
|
||||
}
|
||||
|
||||
if (line.lane_id != lane_id) {
|
||||
line.valid = false;
|
||||
}
|
||||
if (line.cycle > cycle) {
|
||||
// We haven't reached the cycle mark specified in this line yet, so we don't
|
||||
// read it right now.
|
||||
return MemTraceLine{};
|
||||
} else if (line.cycle == cycle && line.lane_id == lane_id) {
|
||||
printf("fire! cycle=%ld, valid=%d, %s addr=%x \n", cycle, line.valid,
|
||||
line.loadstore, line.address);
|
||||
|
||||
// FIXME! Currently lane_id is assumed to be in round-robin order, e.g.
|
||||
// 0->1->2->3->0->..., both in the trace file and the order the caller calls
|
||||
// this function. If this is not true, we cannot simply monotonically
|
||||
// increment read_pos.
|
||||
|
||||
// Only advance pointer when cycle and threa_id both match
|
||||
// now increaseing sequence is fine (0, 1, 3), but unordered is not fine (0, 3, 1)
|
||||
++read_pos;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
#endif
|
||||
|
||||
extern "C" void memtracelogger_init(const char *filename) {
|
||||
#ifndef NO_VPI
|
||||
s_vpi_vlog_info info;
|
||||
@@ -117,7 +56,7 @@ extern "C" void memtracelogger_init(const char *filename) {
|
||||
|
||||
printf("memtrace_init: filename=[%s]\n", filename);
|
||||
|
||||
logger = std::make_unique<MemTraceLogger>(filename);
|
||||
logger = std::make_unique<MemTraceWriter>(filename);
|
||||
}
|
||||
|
||||
// TODO: accept core_id as well
|
||||
@@ -133,9 +72,22 @@ extern "C" void memtracelogger_log(unsigned char trace_log_valid,
|
||||
// trace_read_lane_id);
|
||||
*trace_log_ready = 1;
|
||||
|
||||
if (trace_log_valid) {
|
||||
printf("%s: [%lu] valid: address=%lx, tid=%u, size=%d\n", __func__,
|
||||
trace_log_cycle, trace_log_address, trace_log_lane_id,
|
||||
trace_log_size);
|
||||
if (!trace_log_valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s: [%lu] valid: address=%lx, tid=%u, size=%d\n", __func__,
|
||||
trace_log_cycle, trace_log_address, trace_log_lane_id,
|
||||
trace_log_size);
|
||||
|
||||
MemTraceLine line{.valid = (trace_log_valid == 1),
|
||||
.cycle = static_cast<long>(trace_log_cycle),
|
||||
.is_store = (trace_log_is_store == 1),
|
||||
.core_id = 0, // TODO support multicores
|
||||
.lane_id = trace_log_lane_id,
|
||||
.address = trace_log_address,
|
||||
.data = trace_log_data,
|
||||
.log_data_size = trace_log_size};
|
||||
|
||||
logger->write_trace_at(line);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user