From 9991b196100873121d054ce95a11faae786138ef Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sat, 25 Apr 2020 18:29:27 +0100 Subject: [PATCH] Another attempt at flushing threaded VCD correctly. --- include/verilated_vcd_c.cpp | 14 +++++++++----- include/verilated_vcd_c.h | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/verilated_vcd_c.cpp b/include/verilated_vcd_c.cpp index 9f5befff3..f52c80882 100644 --- a/include/verilated_vcd_c.cpp +++ b/include/verilated_vcd_c.cpp @@ -274,6 +274,7 @@ void VerilatedVcd::closePrev() { // This function is on the flush() call path if (!isOpen()) return; + VerilatedTrace::flush(); bufferFlush(); m_isOpen = false; m_filep->close(); @@ -300,13 +301,16 @@ void VerilatedVcd::close() { printStr(" $end\n"); } closePrev(); - // closePrev() already called VerilatedTrace::flush() via - // bufferFlush(), and there were no opportunities to enqueue further - // buffers as this close() is running on the main thread the same as dump() - // so just shutting down the trace thread will suffice. + // closePrev() called VerilatedTrace::flush(), so we just + // need to shut down the tracing thread here. VerilatedTrace::close(); } +void VerilatedVcd::flush() { + VerilatedTrace::flush(); + bufferFlush(); +} + void VerilatedVcd::printStr(const char* str) { // Not fast... while (*str) { @@ -336,13 +340,13 @@ void VerilatedVcd::bufferResize(vluint64_t minsize) { } void VerilatedVcd::bufferFlush() VL_MT_UNSAFE_ONE { + // This function can be called from the trace thread // This function is on the flush() call path // We add output data to m_writep. // When it gets nearly full we dump it using this routine which calls write() // This is much faster than using buffered I/O m_assertOne.check(); if (VL_UNLIKELY(!isOpen())) return; - VerilatedTrace::flush(); char* wp = m_wrBufp; while (true) { ssize_t remaining = (m_writep - wp); diff --git a/include/verilated_vcd_c.h b/include/verilated_vcd_c.h index 0ee8084c8..e9838c15b 100644 --- a/include/verilated_vcd_c.h +++ b/include/verilated_vcd_c.h @@ -149,7 +149,7 @@ public: /// Close the file void close() VL_MT_UNSAFE_ONE; /// Flush any remaining data to this file - void flush() VL_MT_UNSAFE_ONE { bufferFlush(); } + void flush() VL_MT_UNSAFE_ONE; /// Is file open? bool isOpen() const { return m_isOpen; }