{
private:
ifstream input;
public:
trace_reader_t(string filename)
: input( filename.c_str())
{
}
/*
* Read and convert a line to a host_io_command
*
* Return true if success.
*
*/
bool read(host_io_command_t& command)
{
static uint32_t ln = 0;
typedef char_separator<char> CS;
CS delimeter(" ,\t");
string line;
if (!input.is_open())
{
INFO("trace reader error: the file was not open.");
return false;
}
bool ret = false;
while (std::getline(input, line))
{
tokenizer<CS> tok(line, delimeter);
vector<string> vec;
ln++;
vec.assign(tok.begin(), tok.end());
if (vec.size() < 3)
{
// discard this line
continue;
}
if (vec[0] == string("$read"))
{
command.type = IO_READ;
}
else if (vec[0] == string("$write"))
{
command.type = IO_WRITE;
}
else
{
// discard this line
continue;
}
command.id = ln;
command.lba = stoul(vec[1], 0, 0);
command.blk_number = stoul(vec[2], 0, 0);
ret = true;
break;
}
return ret;
}
~trace_reader_t()
{
input.close();
}
};
csv文件的内容是:
Version=2,,,,
Operation,Address,Size,StartTime,Duration
$read,10235,23,0,0
$read,12052,15,0,0
最终的结果是将 10235给command的lba,23改command的blk,而其中的id是记录这是第几条命令的。
host_io_command_t的结构是:
class host_io_command_t
{
public:
uint32_t id;
io_command_type type;
uint32_t lba; // logical block address
uint32_t blk_number; // how many blocks does this command operate?
// Assumes the block size is 4096 bytes.
// defines more fileds below
// 这个的结果是用户的信息回车,加上这些信息。
string& to_string(string& str)
{
str += "\n command: ";
switch (type)
{
case IO_READ: str += "IO Read"; break;
case IO_WRITE: str += "IO Write"; break;
case IO_TRIM: str += "IO Trim"; break;
case IO_FLUSH: str += "IO Flush"; break;
default: str += "<Unknown>"; break;
}
str += "\n id: " + std::to_string(id);
str += "\n lba: " + std::to_string(lba);
str += "\n len(in block): " + std::to_string(blk_number);
return str;
}
};
io_command_type 的结构是:
enum io_command_type
{
IO_READ,
IO_WRITE,
IO_TRIM,
IO_FLUSH
};
对外提供的接口函数,或者说是使用方法是:
host_io_command_t cmd;
file_reader_t trace("./input.csv");
trace.read(cmd)
到这里,就将input.csv文件中的一行数据读到cmd这个结构体里边去了。
while (trace.read(cmd))
可以将整个文件的内容读完。
这个读取文件的类用图形表示如下: