numam/storage/io_gen.cc
2022-03-20 22:17:26 +08:00

57 lines
1.9 KiB
C++

#include <sys/endian.h>
#include <random>
#include "nm.h"
#include "storage/io_gen.hh"
io_generator::io_generator(
unsigned long req_size,
unsigned long capacity,
unsigned int read_pct,
io_generator_address_mode addr_mode) : cur_offset(0),
capacity(capacity),
req_size(req_size),
read_pct(read_pct),
addr_mode(addr_mode),
rng(rd()),
dist(std::uniform_int_distribution<int>(0, 99)),
addr_rng(addr_rd()),
addr_dist(std::uniform_int_distribution<uint64_t>(0, capacity - 1))
{
rng.seed(nm_get_uptime_ns());
addr_rng.seed(nm_get_uptime_ns());
}
/* returns 0 on success */
int io_generator::issue(struct io_generator_ctx *ctx, char * buf)
{
ctx->size = req_size;
// determine next IO offset
if (addr_mode == IOGEN_ADDR_MONOTONIC_INCREASING) {
if (cur_offset + req_size > capacity) {
cur_offset = 0;
}
ctx->offset = cur_offset;
cur_offset = cur_offset + req_size;
} else {
ctx->offset = (addr_dist(addr_rng) / req_size) * req_size;
if (ctx->offset + req_size > capacity) {
ctx->offset -= req_size;
}
}
// determine next IO data
int op_rng = dist(rng);
if (op_rng < (int)read_pct) {
ctx->op = IOGEN_READ;
} else {
ctx->op = IOGEN_WRITE;
int data = dist(rng);
memset(buf, data, req_size);
}
return 0;
}