Skip to content

Commit

Permalink
implementation of probabilities computation
Browse files Browse the repository at this point in the history
  • Loading branch information
janjurca committed Jan 17, 2024
1 parent f6f9bf7 commit 0e3fec1
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 7 deletions.
3 changes: 3 additions & 0 deletions include/filestorm/filetree.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class FileTree {

Node* getRoot() const;

const int getDirectoryCount() const { return directory_count; }
const int getFileCount() const { return file_count; }

private:
void printRec(const Node* node, int depth) const;
};
Expand Down
17 changes: 14 additions & 3 deletions include/filestorm/scenarios/scenario.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <filestorm/actions/actions.h>
#include <filestorm/utils/psm.h>

#include <map>
#include <string>

class Parameter {
Expand All @@ -11,16 +13,22 @@ class Parameter {
std::string _description;
std::string _value;
bool _has_value;
std::function<std::string(std::string)> _on_set;

public:
Parameter(std::string short_name, std::string long_name, std::string description, std::string value, bool has_value = true)
: _short_name(short_name), _long_name(long_name), _description(description), _value(value), _has_value(has_value){};
Parameter(std::string short_name, std::string long_name, std::string description, std::string value, bool has_value = true, std::function<std::string(std::string)> on_set = nullptr)
: _short_name(short_name), _long_name(long_name), _description(description), _value(value), _has_value(has_value), _on_set(on_set){};
~Parameter(){};
std::string short_name() const { return _short_name; };
std::string long_name() const { return _long_name; };
std::string description() const { return _description; };
std::string value() const { return _value; };
std::string value(const std::string& value) { return _value = value; };
std::string value(const std::string& value) {
if (_on_set != nullptr) {
return _value = _on_set(value);
}
return _value = value;
};
bool has_value() const { return _has_value; };
int get_int() const { return std::stoi(_value); };
bool get_bool() const { return _value == "true" || _value == "True" || _value == "t" || _value == "T"; };
Expand Down Expand Up @@ -64,8 +72,11 @@ class BasicScenario : public Scenario {
};

class AgingScenario : public Scenario {
protected:
public:
AgingScenario();
~AgingScenario();
void run() override;

double CAF(double x) { return sqrt(1 - (x * x)); }
};
9 changes: 9 additions & 0 deletions include/filestorm/utils/fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <filestorm/data_sizes.h>

#include <filesystem>

namespace fs_utils {
std::filesystem::space_info get_fs_status(const std::filesystem::path& path) { return std::filesystem::space(path); }
} // namespace fs_utils
63 changes: 63 additions & 0 deletions include/filestorm/utils/psm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <cstdlib>
#include <iostream>
#include <vector>

class State {
public:
State(std::string name) : name(name) {}
State(const char* name) : name(name) {}
const std::string& getName() const { return name; }

private:
std::string name;
};

class Transition {
private:
State& _from;
State& _to;

std::function<double()> _probability_function;

public:
Transition(State& from, State& to, std::function<double()> probability_callback) : _from(from), _to(to), _probability_function(probability_callback) {}

const State& from() const { return _from; }
const State& to() const { return _to; }
double probability() const { return _probability_function(); }
};

class ProbabilisticStateMachine {
public:
ProbabilisticStateMachine(std::map<std::string, State>& states, std::map<std::string, Transition>& transitions, State& init) : _states(states), _transitions(transitions), _currentState(init) {}

void performTransition() {
// Choose a transition based on probabilities
double randomValue = static_cast<double>(rand()) / RAND_MAX;
double cumulativeProbability = 0.0;

for (const auto& transition : _transitions) {
if (transition.second.from().getName() != _currentState.getName()) {
continue;
}

cumulativeProbability += transition.second.probability();
if (randomValue <= cumulativeProbability) {
// Transition to the next state
_currentState = transition.second.to();
std::cout << "Performed transition from " << transition.second.from().getName() << " to " << transition.second.to().getName() << std::endl;
break;
}
}
throw std::runtime_error("No transitions from current state");
}

const State& getCurrentState() const { return _currentState; }

private:
State& _currentState;
std::map<std::string, State>& _states;
std::map<std::string, Transition>& _transitions;
};
34 changes: 34 additions & 0 deletions misc/fs-capacity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <chrono>
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;
using namespace std::chrono;

int main() {
try {
auto start = high_resolution_clock::now();

// Replace "C:" with the drive or path you want to check
fs::space_info spaceInfo = fs::space("/Users/jjurca/Nextcloud");

auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);

std::cout << "Available space: " << spaceInfo.available << " bytes\n";

// Convert capacity to gigabytes
double capacityGB = static_cast<double>(spaceInfo.capacity) / (1 << 30);
std::cout << "Total space: " << capacityGB << " GB\n";

// Convert free space to gigabytes
double freeGB = static_cast<double>(spaceInfo.free) / (1 << 30);
std::cout << "Free space: " << freeGB << " GB\n";

std::cout << "Time taken by function: " << duration.count() << " microseconds\n";
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}

return 0;
}
70 changes: 66 additions & 4 deletions source/scenarios/aging.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <filestorm/filetree.h>
#include <filestorm/scenarios/scenario.h>
#include <filestorm/utils/fs.h>
#include <filestorm/utils/progress_bar.h>

AgingScenario::AgingScenario() {
_name = "aging";
Expand All @@ -7,12 +10,71 @@ AgingScenario::AgingScenario() {
addParameter(Parameter("r", "depth", "Max directory depth", "5"));
addParameter(Parameter("n", "ndirs", "Max number of dirs per level", "false"));
addParameter(Parameter("f", "nfiles", "Max number of files per level", "false"));
addParameter(Parameter("m", "fs-capacity", "Max overall filesystem size", "full"));
addParameter(Parameter("m", "fs-capacity", "Max overall filesystem size", "full")); // TODO: add support for this
addParameter(Parameter("s", "fsize", "Max file size", "full"));
addParameter(Parameter("p", "sdist", "File size probabilistic distribution", "random"));
addParameter(Parameter(std::string(), "capacity-awareness", "File size probabilistic distribution", "random"));
addParameter(Parameter("p", "sdist", "File size probabilistic distribution", "1000"));
addParameter(Parameter("i", "iterations", "Iterations to run", "1000"));
}

AgingScenario::~AgingScenario() {}

void AgingScenario::run() {}
void AgingScenario::run() {
FileTree tree(getParameter("directory").get_string());

std::function<double()> pC = [&]() {
// CAF - Capacity Awareness Factor
auto fs_status = fs_utils::get_fs_status(getParameter("directory").get_string());
return CAF(fs_status.capacity / (fs_status.capacity - fs_status.free));
};
std::function<double()> pD = [&]() { return 0.1 * (1 / pC()); };
std::function<double()> pA = [&]() { return 1 - pC() - pD(); };
std::function<double()> pAM = []() { return 0.1; };
std::function<double()> pAB = pC;
std::function<double()> pAS = [&]() { return 1 - pAM() - pAB(); };
std::function<double()> pDD = []() { return 0.01; };
std::function<double()> pDF = [&]() { return 1 - pDD(); };
std::function<double()> pCF = [&]() { return (tree.getDirectoryCount() / getParameter("ndirs").get_int()); };
std::function<double()> pCD = [&]() { return 1 - pCF(); };
std::function<double()> p1 = [=]() { return 1.0; };

std::map<std::string, State> states;
states.emplace("S", State("S"));
states.emplace("CREATE", State("CREATE"));
states.emplace("ALTER", State("ALTER"));
states.emplace("DELETE", State("DELETE"));
states.emplace("CREATE_FILE", State("CREATE_FILE"));
states.emplace("CREATE_DIR", State("CREATE_DIR"));
states.emplace("ALTER_SMALLER", State("ALTER_SMALLER"));
states.emplace("ALTER_BIGGER", State("ALTER_BIGGER"));
states.emplace("DELETE_FILE", State("DELETE_FILE"));
states.emplace("DELETE_DIR", State("DELETE_DIR"));
states.emplace("END", State("END"));

std::map<std::string, Transition> transtions;
transtions.emplace("S->CREATE", Transition(states.at("S"), states.at("CREATE"), pC));
transtions.emplace("S->ALTER", Transition(states.at("S"), states.at("ALTER"), pA));
transtions.emplace("S->DELETE", Transition(states.at("S"), states.at("DELETE"), pD));
transtions.emplace("CREATE->CREATE_FILE", Transition(states.at("CREATE"), states.at("CREATE_FILE"), pCF));
transtions.emplace("CREATE->CREATE_DIR", Transition(states.at("CREATE"), states.at("CREATE_DIR"), pCD));
transtions.emplace("ALTER->ALTER_SMALLER", Transition(states.at("ALTER"), states.at("ALTER_SMALLER"), pAS));
transtions.emplace("ALTER->ALTER_BIGGER", Transition(states.at("ALTER"), states.at("ALTER_BIGGER"), pAB));
transtions.emplace("DELETE->DELETE_FILE", Transition(states.at("DELETE"), states.at("DELETE_FILE"), pDF));
transtions.emplace("DELETE->DELETE_DIR", Transition(states.at("DELETE"), states.at("DELETE_DIR"), pDD));
transtions.emplace("CREATE_FILE->END", Transition(states.at("CREATE_FILE"), states.at("END"), p1));
transtions.emplace("CREATE_DIR->END", Transition(states.at("CREATE_DIR"), states.at("END"), p1));
transtions.emplace("ALTER_SMALLER->END", Transition(states.at("ALTER_SMALLER"), states.at("END"), p1));
transtions.emplace("ALTER_BIGGER->END", Transition(states.at("ALTER_BIGGER"), states.at("END"), p1));
transtions.emplace("DELETE_FILE->END", Transition(states.at("DELETE_FILE"), states.at("END"), p1));
transtions.emplace("DELETE_DIR->END", Transition(states.at("DELETE_DIR"), states.at("END"), p1));
transtions.emplace("END->S", Transition(states.at("END"), states.at("S"), p1));

int iteration = 0;
progressbar bar(getParameter("iterations").get_int());

ProbabilisticStateMachine psm(states, transtions, states.at("S"));

while (iteration < getParameter("iterations").get_int()) {
bar.update();
iteration++;
}
}

0 comments on commit 0e3fec1

Please sign in to comment.