Skip to content

Commit

Permalink
fix benchmark and adjust periodic dump for perf
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorbel1 committed Jan 5, 2023
1 parent bd4b711 commit c9b335b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
28 changes: 18 additions & 10 deletions benchmark/wave_dump_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:rohd/rohd.dart';

class _ModuleToDump extends Module {
_ModuleToDump(Logic d) {
static const _numExtraOutputs = 50;

_ModuleToDump(Logic d, Logic clk) {
d = addInput('d', d);

final q = addOutput('q');

final clk = SimpleClockGenerator(10).clk;

for (var i = 0; i < 100; i++) {
for (var i = 0; i < _numExtraOutputs; i++) {
addOutput('i$i') <= FlipFlop(clk, ~output('i$i')).q;
}

Expand All @@ -31,32 +31,40 @@ class _ModuleToDump extends Module {

class WaveDumpBenchmark extends AsyncBenchmarkBase {
late _ModuleToDump _mod;
late Logic _clk;

static const _maxSimTime = 1000;

static const _vcdTemporaryPath = 'tmp_test/wave_dump_benchmark.vcd';

WaveDumpBenchmark() : super('WaveDump');

@override
Future<void> setup() async {
Simulator.setMaxSimTime(1000);

_mod = _ModuleToDump(Logic());
await _mod.build();
Simulator.setMaxSimTime(_maxSimTime);
}

@override
Future<void> teardown() async {
File(_vcdTemporaryPath).deleteSync();
await Simulator.reset();
if (File(_vcdTemporaryPath).existsSync()) {
File(_vcdTemporaryPath).deleteSync();
}
}

@override
Future<void> run() async {
_clk = SimpleClockGenerator(10).clk;
_mod = _ModuleToDump(Logic(), _clk);
await _mod.build();

WaveDumper(_mod, outputPath: _vcdTemporaryPath);

await Simulator.run();

assert(Simulator.time == _maxSimTime, 'sim should run through end');

await Simulator.reset();
Simulator.setMaxSimTime(_maxSimTime);
}
}

Expand Down
32 changes: 23 additions & 9 deletions lib/src/wave_dumper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import 'package:rohd/src/utilities/uniquifier.dart';
/// Outputs to vcd format at [outputPath]. [module] must be built prior to
/// attaching the [WaveDumper].
///
/// The waves will only dump to the file once the simulation has completed.
/// The waves will only dump to the file periodically and then once the
/// simulation has completed.
class WaveDumper {
/// The [Module] being dumped.
final Module module;
Expand Down Expand Up @@ -87,15 +88,28 @@ class WaveDumper {
});
}

/// Number of characters in the buffer after which it will
/// write contents to the output file.
static const _fileBufferLimit = 100000;

/// Buffers [contents] to be written to the output file.
void _writeToFile(String contents) {
void _writeToBuffer(String contents) {
_fileBuffer.write(contents);

if (_fileBuffer.length > _fileBufferLimit) {
_writeToFile();
}
}

/// Terminates the waveform dumping, including closing the file.
Future<void> _terminate() async {
/// Writes all pending items in the [_fileBuffer] to the file.
void _writeToFile() {
_outFileSink.write(_fileBuffer.toString());
_fileBuffer.clear();
}

/// Terminates the waveform dumping, including closing the file.
Future<void> _terminate() async {
_writeToFile();
await _outFileSink.flush();
await _outFileSink.close();
}
Expand Down Expand Up @@ -142,7 +156,7 @@ class WaveDumper {
\$end
\$timescale $timescale \$end
''';
_writeToFile(header);
_writeToBuffer(header);
}

/// Writes the scope of the VCD, including signal and hierarchy declarations,
Expand All @@ -151,9 +165,9 @@ class WaveDumper {
var scopeString = _computeScopeString(module);
scopeString += '\$enddefinitions \$end\n';
scopeString += '\$dumpvars\n';
_writeToFile(scopeString);
_writeToBuffer(scopeString);
_signalToMarkerMap.keys.forEach(_writeSignalValueUpdate);
_writeToFile('\$end\n');
_writeToBuffer('\$end\n');
}

/// Generates the top of the scope string (signal and hierarchy definitions).
Expand Down Expand Up @@ -191,7 +205,7 @@ class WaveDumper {
/// Writes the current timestamp to the VCD.
void _captureTimestamp(int timestamp) {
final timestampString = '#$timestamp\n';
_writeToFile(timestampString);
_writeToBuffer(timestampString);

_changedLogicsThisTimestamp
..forEach(_writeSignalValueUpdate)
Expand All @@ -209,7 +223,7 @@ class WaveDumper {
: signal.value.toString(includeWidth: false);
final marker = _signalToMarkerMap[signal];
final updateString = '$updateValue$marker\n';
_writeToFile(updateString);
_writeToBuffer(updateString);
}
}

Expand Down

0 comments on commit c9b335b

Please sign in to comment.