Skip to content

Commit

Permalink
RW actions atribute moved
Browse files Browse the repository at this point in the history
  • Loading branch information
janjurca committed Nov 22, 2023
1 parent b3877d3 commit 19ab348
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 50 deletions.
28 changes: 16 additions & 12 deletions include/filestorm/actions/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@

class FileActionAttributes {
private:
bool m_time_based;
std::chrono::milliseconds m_interval;
unsigned int m_block_size;
unsigned int m_file_size;
std::string m_file_path;
const bool m_time_based;
const unsigned int m_block_size;
const unsigned int m_file_size;
const std::string m_file_path;

public:
FileActionAttributes(bool time_based, std::chrono::milliseconds interval, unsigned int block_size,
unsigned int file_size, std::string file_path)
FileActionAttributes(bool time_based, unsigned int block_size, unsigned int file_size,
std::string file_path)
: m_time_based(time_based),
m_interval(interval),
m_block_size(block_size),
m_file_size(file_size),
m_file_path(file_path) {}
bool is_time_based() { return m_time_based; }
std::chrono::milliseconds get_interval() { return m_interval; }
unsigned int get_block_size() { return m_block_size; }
unsigned int get_file_size() { return m_file_size; }
bool is_time_based() const { return m_time_based; }
unsigned int get_block_size() const { return m_block_size; }
unsigned int get_file_size() const { return m_file_size; }
inline std::string get_file_path() const { return m_file_path; }
};

class ActionMonitor;
Expand Down Expand Up @@ -59,6 +57,8 @@ std::chrono::nanoseconds VirtualMeasuredAction::exec() {
work();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
last_duration = duration;
return duration;
}

std::chrono::nanoseconds VirtualMeasuredAction::getLastDuration() {
Expand Down Expand Up @@ -141,4 +141,8 @@ class VirtualMonitoredAction {
std::lock_guard<std::mutex> lock(m_monitoredDataMutex);
m_monitoredData[name].push_back(value);
}

std::chrono::milliseconds get_interval() { return m_interval; }
std::map<std::string, std::vector<float>> get_monitored_data() { return m_monitoredData; }
std::chrono::nanoseconds get_monitor_started_at() { return m_monitor_started_at; }
};
61 changes: 28 additions & 33 deletions include/filestorm/actions/rw_actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@
#include <algorithm>
#include <chrono>
#include <fstream>
#include <memory> // for std::unique_ptr
#include <random>
#include <string>
#include <vector>

// ABOUT DIRECT IO https://github.com/facebook/rocksdb/wiki/Direct-IO

class ReadAction : public VirtualMonitoredAction, public FileActionAttributes {
private:
std::string m_file_path;
size_t m_block_size;
long long m_read_bytes = 0;

public:
ReadAction(std::chrono::milliseconds monitoring_interval,
std::function<void(VirtualMonitoredAction*)> on_log,
FileActionAttributes file_strategy)
: m_file_path(file_path),
m_block_size(block_size),
VirtualMonitoredAction(monitoring_interval, on_log),
FileActionAttributes(file_strategy) {}
FileActionAttributes file_attributes)
: VirtualMonitoredAction(monitoring_interval, on_log),
FileActionAttributes(file_attributes) {}

void work() override {
std::ifstream file(m_file_path);
char line[m_block_size];
std::ifstream file(get_file_path());
// allocate array if bytes on heap c++ way
// char line[get_block_size()];
std::unique_ptr<char[]> line(new char[get_block_size()]);

// read file by blocks in size of block size
if (!file.is_open()) {
throw std::runtime_error("ReadAction::work: error opening file");
Expand All @@ -47,13 +48,13 @@ class ReadAction : public VirtualMonitoredAction, public FileActionAttributes {
file.clear();
file.seekg(0, std::ios::beg);
}
file.read(line, m_block_size);
m_read_bytes += m_block_size;
file.read(line.get(), get_block_size());
m_read_bytes += get_block_size();
now_time = std::chrono::high_resolution_clock::now();
}
} else {
while (file.read(line, m_block_size)) {
m_read_bytes += m_block_size;
while (file.read(line.get(), get_block_size())) {
m_read_bytes += get_block_size();
}
}
}
Expand All @@ -69,20 +70,14 @@ class ReadAction : public VirtualMonitoredAction, public FileActionAttributes {

class WriteAction : public VirtualMonitoredAction, public FileActionAttributes {
private:
std::string m_file_path;
size_t m_block_size;
size_t m_file_size;
long long m_written_bytes = 0;

public:
WriteAction(std::chrono::milliseconds monitoring_interval,
std::function<void(VirtualMonitoredAction*)> on_log,
FileActionAttributes file_strategy)
: m_file_path(file_path),
m_block_size(block_size),
m_file_size(file_size),
VirtualMonitoredAction(monitoring_interval, on_log),
FileActionAttributes(file_strategy) {}
FileActionAttributes file_attributes)
: VirtualMonitoredAction(monitoring_interval, on_log),
FileActionAttributes(file_attributes) {}

inline void generate_random_chunk(char* chunk, size_t size) {
static std::random_device rd;
Expand All @@ -94,21 +89,21 @@ class WriteAction : public VirtualMonitoredAction, public FileActionAttributes {

void work() override {
int fd;
char line[m_block_size];
std::unique_ptr<char[]> line(new char[get_block_size()]);

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
# error "Windows is not supported"
#elif __APPLE__
fd = open(m_file_path.c_str(), O_RDWR | O_CREAT, S_IRWXU);
fd = open(get_file_path().c_str(), O_RDWR | O_CREAT, S_IRWXU);
if (fcntl(fd, F_NOCACHE, 1) == -1 && fd != -1) {
close(fd);
throw std::runtime_error("WriteAction::work: error on fcntl F_NOCACHE");
}
#elif __linux__ || __unix__ || defined(_POSIX_VERSION)
fd = open(m_file_path.c_str(), O_DIRECT | O_RDWR | O_CREAT, S_IRWXU);
fd = open(get_file_path().c_str(), O_DIRECT | O_RDWR | O_CREAT, S_IRWXU);
#else
# warning "Unknown system"
fd = open(m_file_path.c_str(), O_DIRECT | O_RDWR | O_CREAT, S_IRWXU);
fd = open(get_file_path().c_str(), O_DIRECT | O_RDWR | O_CREAT, S_IRWXU);
#endif

if (fd == -1) {
Expand All @@ -121,16 +116,16 @@ class WriteAction : public VirtualMonitoredAction, public FileActionAttributes {

while (std::chrono::duration_cast<std::chrono::milliseconds>(now_time - start_time).count()
<= get_interval().count()) {
generate_random_chunk(line, m_block_size);
write(fd, line, m_block_size);
m_written_bytes += m_block_size;
generate_random_chunk(line.get(), get_block_size());
write(fd, line.get(), get_block_size());
m_written_bytes += get_block_size();
now_time = std::chrono::high_resolution_clock::now();
}
} else {
while (m_written_bytes < m_file_size) {
generate_random_chunk(line, m_block_size);
write(fd, line, m_block_size);
m_written_bytes += m_block_size;
while (m_written_bytes < get_file_size()) {
generate_random_chunk(line.get(), get_block_size());
write(fd, line.get(), get_block_size());
m_written_bytes += get_block_size();
}
}
close(fd);
Expand Down
12 changes: 7 additions & 5 deletions standalone/source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <filestorm/actions/actions.h>
#include <filestorm/actions/rw_actions.h>
#include <filestorm/version.h>

#include <cxxopts.hpp>
Expand All @@ -14,7 +16,7 @@ auto main(int argc, char** argv) -> int {
options.add_options()
("h,help", "Show help")
("v,version", "Print the current version number")
("d,directory", "Specify directory which should be tested in")
("f,file", "Specify file which should be tested")
("s,scenario",std::string("Define which testing mode you'd like to run. Supported: ") + config.get_supported_scenarios_as_string(), cxxopts::value<std::string>()->default_value(config.get_supported_scenarios().at(0).name()) )
;

Expand All @@ -30,14 +32,14 @@ auto main(int argc, char** argv) -> int {
return 0;
}

if (!result.count("directory")) {
if (!result.count("file")) {
std::cout << "Please specify directory to test in" << std::endl;
return 1;
}


WriteAction wa(std::chrono::milliseconds(100), [](VirtualMonitoredAction* action) {
std::cout << "WriteAction: " << action->get_monitored_data().at("write_bytes").at(0) << std::endl;
}, FileActionAttributes(true, 4096, 4096*1024, result["file"].as<std::string>()));



return 0;
}

0 comments on commit 19ab348

Please sign in to comment.