Skip to content

Commit

Permalink
psm, fixed filetree ready to mkdir/file
Browse files Browse the repository at this point in the history
  • Loading branch information
janjurca committed Jan 19, 2024
1 parent 6912c4d commit 3f2d978
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 134 deletions.
25 changes: 23 additions & 2 deletions include/filestorm/filetree.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <filestorm/utils.h>

#include <atomic>
#include <iostream>
#include <map>
Expand All @@ -13,21 +15,37 @@ class FileTree {
static std::atomic<int> directory_count;
static std::atomic<int> file_count;

static std::atomic<int> directory_id;
static std::atomic<int> file_id;

unsigned int _max_depth;

struct Node {
std::string name;
Type type;
Node* parent;
long size;
std::map<std::string, std::unique_ptr<Node>> children;
std::map<std::string, std::unique_ptr<Node>> folders;
std::map<std::string, std::unique_ptr<Node>> files;

Node(const std::string& n, Type t, Node* p, long size = 0) : name(n), type(t), parent(p), size(size) {}
std::string path(bool include_root = false) const {
if (parent == nullptr) {
if (include_root) {
return name;
} else {
return "";
}
}
return parent->path(include_root) + "/" + name;
}
};

private:
std::unique_ptr<Node> root;

public:
FileTree(const std::string& rootName);
FileTree(const std::string& rootName, unsigned int max_depth = 0);
Node* addDirectory(Node* parent, const std::string& dirName);
void remove(Node* node);
FileTree::Node* addFile(Node* parent, const std::string& fileName, long size = 0);
Expand All @@ -45,6 +63,9 @@ class FileTree {
int getDirectoryCount() const { return directory_count; }
int getFileCount() const { return file_count; }

std::string newDirectoryPath();
std::string newFilePath();

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

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

#include <map>
Expand Down Expand Up @@ -73,6 +74,23 @@ class BasicScenario : public Scenario {

class AgingScenario : public Scenario {
protected:
enum States {
S,
CREATE,
ALTER,
DELETE,
CREATE_FILE,
CREATE_DIR,
ALTER_SMALLER,
ALTER_BIGGER,
ALTER_METADATA,
DELETE_FILE,
DELETE_DIR,
END,
};

void compute_probabilities(std::map<std::string, double>& probabilities, FileTree& tree);

public:
AgingScenario();
~AgingScenario();
Expand Down
2 changes: 2 additions & 0 deletions include/filestorm/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
#include <vector>

std::vector<std::string> split(const std::string& str, char delimiter);
std::string strip(const std::string& str);
std::string strip(const std::string& str, const char stripChar = ' ');
46 changes: 19 additions & 27 deletions include/filestorm/utils/psm.h
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
#pragma once

#include <spdlog/spdlog.h>

#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;
int _from;
int _to;

std::function<double()> _probability_function;
std::string _probability_key;

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

const State& from() const { return _from; }
const State& to() const { return _to; }
double probability() const { return _probability_function(); }
int from() const { return _from; }
int to() const { return _to; }
std::string probability_key() const { return _probability_key; }
};

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

void performTransition() {
void performTransition(std::map<std::string, double>& probabilities) {
// Choose a transition based on probabilities
double randomValue = static_cast<double>(rand()) / RAND_MAX;
double randomValue = ((double)rand() / (RAND_MAX));
double cumulativeProbability = 0.0;

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

cumulativeProbability += transition.second.probability();
cumulativeProbability += probabilities[transition.second.probability_key()];
// spdlog::debug("Cumulative probability: {}", cumulativeProbability);
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;
return;
}
}

throw std::runtime_error("No transitions from current state");
}

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

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

double CAF(double x) { return sqrt(1 - (x * x)); }

int main() {
double capacity = 1024;
double free = 1024;
while (free > 0) {
std::cout << "capacity: " << capacity << " free: " << free << " pC= " << CAF((float(capacity - free) / float(capacity))) << std::endl;
free -= 1;
}
return 0;
}
Loading

0 comments on commit 3f2d978

Please sign in to comment.