From aabc3a9dbcbc62468b2e57b7f6d3c3b26c7a1526 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 10 Jul 2020 15:19:38 +0800 Subject: [PATCH] [io_generator] Add random map to avoid generating repeated addresses --- base/io_generator.cc | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/base/io_generator.cc b/base/io_generator.cc index 4d6ebb0..5455740 100644 --- a/base/io_generator.cc +++ b/base/io_generator.cc @@ -1,4 +1,5 @@ #include "base/io_generator.h" +#include "persistent-data/run_set.h" #include #include #include @@ -69,17 +70,37 @@ namespace { sector_t block_size) : block_begin_(offset / block_size), nr_blocks_(size / block_size), - block_size_(block_size) { + block_size_(block_size), + nr_generated_(0) { } sector_t next_offset() { - return ((std::rand() % nr_blocks_) + block_begin_) * block_size_; + uint64_t blocknr; + + bool found = true; + while (found) { + blocknr = (std::rand() % nr_blocks_) + block_begin_; + found = io_map_.member(blocknr); + } + io_map_.add(blocknr); + ++nr_generated_; + + // wrap-around + if (nr_generated_ == nr_blocks_) { + io_map_.clear(); + nr_generated_ = 0; + } + + return blocknr * block_size_; } private: uint64_t block_begin_; uint64_t nr_blocks_; unsigned block_size_; + + base::run_set io_map_; + uint64_t nr_generated_; }; //--------------------------------