Optimize SAIF writes (#5916)

This commit is contained in:
Wilson Snyder 2025-04-05 15:09:18 -04:00
parent 2bbb24eb38
commit e12b971ba6
7 changed files with 32 additions and 7 deletions

View File

@ -319,6 +319,7 @@ void VerilatedSaif::close() VL_MT_SAFE_EXCLUDES(m_mutex) {
finalizeSaifFileContents();
clearCurrentlyCollectedData();
writeBuffered(true);
::close(m_filep);
m_isOpen = false;
@ -437,9 +438,25 @@ void VerilatedSaif::clearCurrentlyCollectedData() {
m_activityAccumulators.clear();
}
void VerilatedSaif::printStr(const char* str) { ::write(m_filep, str, strlen(str)); }
void VerilatedSaif::printStr(const char* str) {
m_buffer.append(str);
writeBuffered(false);
}
void VerilatedSaif::printStr(const std::string& str) { ::write(m_filep, str.c_str(), str.size()); }
void VerilatedSaif::printStr(const std::string& str) {
m_buffer.append(str);
writeBuffered(false);
}
void VerilatedSaif::writeBuffered(bool force) {
if (VL_UNLIKELY(m_buffer.size() >= WRITE_BUFFER_SIZE || force)) {
if (VL_UNLIKELY(!m_buffer.empty())) {
::write(m_filep, m_buffer.data(), m_buffer.size());
m_buffer = "";
m_buffer.reserve(WRITE_BUFFER_SIZE * 2);
}
}
}
//=============================================================================
// Definitions
@ -454,7 +471,7 @@ void VerilatedSaif::incrementIndent() { m_indent += 1; }
void VerilatedSaif::decrementIndent() { m_indent -= 1; }
void VerilatedSaif::printIndent() {
for (int i = 0; i < m_indent; ++i) printStr(" ");
printStr(std::string(m_indent, ' ')); // Must use () constructor
}
void VerilatedSaif::pushPrefix(const std::string& name, VerilatedTracePrefixType type) {

View File

@ -52,8 +52,10 @@ private:
int m_filep = 0; // File we're writing to
bool m_isOpen = false; // True indicates open file
std::string m_filename; // Filename we're writing to (if open)
std::string m_buffer; // Write data buffer
int m_indent = 0; // Indentation size in spaces
static constexpr size_t WRITE_BUFFER_SIZE = 256 * 1024; // Bytes between write calls
// Currently active scope
VerilatedSaifActivityScope* m_currentScope = nullptr;
@ -92,6 +94,7 @@ private:
void printStr(const char* str);
void printStr(const std::string& str);
void writeBuffered(bool force);
void clearCurrentlyCollectedData();

View File

@ -2382,6 +2382,7 @@ class VlTest:
out = test.run_capture(cmd, check=True)
if out != '':
print(out)
self.copy_if_golden(fn1, fn2)
self.error("SAIF files don't match!")
def _vcd_read(self, filename: str) -> str:

View File

@ -17,6 +17,7 @@ test.compile(verilator_flags2=['--cc --trace-saif --trace-structs'])
test.execute()
test.saif_identical(test.trace_filename, test.golden_filename)
# saif_identical is very slow, so require exact match
test.files_identical(test.trace_filename, test.golden_filename)
test.passes()

View File

@ -17,6 +17,7 @@ test.compile(verilator_flags2=['--cc --trace-saif --trace-structs', '-CFLAGS -DV
test.execute()
test.saif_identical(test.trace_filename, test.golden_filename)
# saif_identical is very slow, so require exact match
test.files_identical(test.trace_filename, test.golden_filename)
test.passes()

View File

@ -17,6 +17,7 @@ test.compile(verilator_flags2=['--cc --trace-saif --trace-threads 1 --trace-stru
test.execute()
test.saif_identical(test.trace_filename, test.golden_filename)
# saif_identical is very slow, so require exact match
test.files_identical(test.trace_filename, test.golden_filename)
test.passes()

View File

@ -17,6 +17,7 @@ test.compile(verilator_flags2=['--cc --trace-saif --trace-threads 2 --trace-stru
test.execute()
test.saif_identical(test.trace_filename, test.golden_filename)
# saif_identical is very slow, so require exact match
test.files_identical(test.trace_filename, test.golden_filename)
test.passes()