Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed src/sim/core/.gitkeep
Empty file.
13 changes: 13 additions & 0 deletions src/sim/core/base_entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <string>

class BaseEntity {
public:
explicit BaseEntity(std::string id_) : entity_id(std::move(id_)) {}
virtual ~BaseEntity() = default;

const std::string& id() const { return entity_id; }

private:
std::string entity_id;
};
49 changes: 49 additions & 0 deletions src/sim/core/event_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//event_queue.cpp defines different priority/calender/ladder (for now just pq) queues
//at runtime EventQueue reference may point to any of them (depending on configuration)
#include "event_queue.h"
#include "../events/event.h"
#include <queue>
#include <vector>

using std::unique_ptr;
using std::priority_queue;
using std::vector;
using std::move;

namespace {

struct EventCompare {
bool operator()(
const unique_ptr<Event>& a,
const unique_ptr<Event>& b
) const {
return a->time > b->time;
}
};

class PriorityEventQueue : public EventQueue {
private:
priority_queue<
unique_ptr<Event>,
vector<unique_ptr<Event>>,
EventCompare
> pq;

public:
void push(unique_ptr<Event> e) override {
pq.push(move(e));
}

unique_ptr<Event> pop() override {
auto e = move(pq.top());
pq.pop();
return e;
}

bool empty() const override {
return pq.empty();
}
};

}

14 changes: 14 additions & 0 deletions src/sim/core/event_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//event_queue.h defines the abstract event queue API which will be used by event scheduler
#pragma once
#include <memory>

class Event;

class EventQueue {
public:
virtual ~EventQueue() = default;

virtual void push(std::unique_ptr<Event> e) = 0;
virtual std::unique_ptr<Event> pop() = 0;
virtual bool empty() const = 0;
};
18 changes: 18 additions & 0 deletions src/sim/core/scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <memory>

class Event;
class EventQueue;

class EventScheduler {
private:
EventQueue& queue; //private as components only schedule the events using scheduler, so they should not be able to inspect it

public:
explicit EventScheduler(EventQueue& q) : queue(q) {}

void schedule(std::unique_ptr<Event> e) {
queue.push(std::move(e));
}

};
5 changes: 5 additions & 0 deletions src/sim/core/sim_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//this is just for defining alias for uint64_t everywhere
#pragma once
#include <cstdint>

using SimTime = uint64_t;
27 changes: 27 additions & 0 deletions src/sim/core/simulator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "simulator.h"
#include "event_queue.h"
#include "../events/event.h"

Simulator::Simulator(
EventQueue& q,
const Context& ctx,
State& st
)
: queue(q),
scheduler(q),
context(ctx),
state(st) {}

void Simulator::run() {
while (!queue.empty()) {
auto event = queue.pop();

current_time = event->time;

event->execute(context, state, scheduler);
}
}

SimTime Simulator::now() const {
return current_time;
}
33 changes: 33 additions & 0 deletions src/sim/core/simulator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "sim_types.h"
#include "scheduler.h"
#include "event_loop.h"

#include "../entities/entity_context.h"
#include "../entities/entity_state.h"

using Context = SimulationContext;
using State = SimulationState;

class EventQueue;

class Simulator final : public EventLoop {
private:
SimTime current_time = 0;

EventQueue& queue;
EventScheduler scheduler;

const Context& context;
State& state;

public:
Simulator(
EventQueue& q,
const Context& ctx,
State& st
);

void run() override;
SimTime now() const;
};
25 changes: 25 additions & 0 deletions src/sim/entities/database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "../core/base_entity.h"

class DatabaseEntity final : public BaseEntity {
public:
// ---- context ----
const int capacity;
const double latency_mean;
const double failure_prob;

// ---- state ----
bool is_down = false;
int active_connections = 0;

DatabaseEntity(
std::string id,
int capacity,
double latency_mean,
double failure_prob
)
: BaseEntity(std::move(id)),
capacity(capacity),
latency_mean(latency_mean),
failure_prob(failure_prob) {}
};
29 changes: 29 additions & 0 deletions src/sim/entities/networklink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include "../core/base_entity.h"
#include <string>

class NetworkLinkEntity final : public BaseEntity {
public:
// ---- context ----
const std::string from;
const std::string to;
const double latency_mean;
const double failure_prob;

// ---- state ----
bool is_down = false;
int in_flight = 0;

NetworkLinkEntity(
std::string id,
std::string from,
std::string to,
double latency_mean,
double failure_prob
)
: BaseEntity(std::move(id)),
from(std::move(from)),
to(std::move(to)),
latency_mean(latency_mean),
failure_prob(failure_prob) {}
};
26 changes: 26 additions & 0 deletions src/sim/entities/service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "../core/base_entity.h"

class ServiceEntity final : public BaseEntity {
public:
// ---- context (immutable) ----
const int capacity;
const double latency_mean;
const double failure_prob;

// ---- state (mutable) ----
bool is_down = false;
int active_requests = 0;
int queued_requests = 0;

ServiceEntity(
std::string id,
int capacity,
double latency_mean,
double failure_prob
)
: BaseEntity(std::move(id)),
capacity(capacity),
latency_mean(latency_mean),
failure_prob(failure_prob) {}
};
62 changes: 62 additions & 0 deletions src/sim/factory/factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "entity_factory.h"
#include <stdexcept>

using std::make_unique;
using std::runtime_error;
using std::vector;

// ---------------- Public ----------------

void EntityFactory::build(
const vector<IRNode>& ir,
Simulation& simulation
) {
create_entities(ir, simulation);
}

// ---------------- Private ----------------

void EntityFactory::create_entities(
const vector<IRNode>& ir,
Simulation& simulation
) {
for (const auto& node : ir) {

switch (node.type) {

case IRType::SERVICE:
simulation.entities[node.id] =
make_unique<ServiceEntity>(
node.id,
node.capacity,
node.latency_mean,
node.failure_prob
);
break;

case IRType::DATABASE:
simulation.entities[node.id] =
make_unique<DatabaseEntity>(
node.id,
node.capacity,
node.latency_mean,
node.failure_prob
);
break;

case IRType::NETWORK_LINK:
simulation.entities[node.id] =
make_unique<NetworkLinkEntity>(
node.id,
node.from,
node.to,
node.latency_mean,
node.failure_prob
);
break;

default:
throw runtime_error("Unknown IRType");
}
}
}
53 changes: 53 additions & 0 deletions src/sim/factory/factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>

// Entities
#include "../entities/service.h"
#include "../entities/database.h"
#include "../entities/network_link.h"

// IR (will be changed after UI -> frontend is finalised)

enum class IRType {
SERVICE,
DATABASE,
NETWORK_LINK
};

struct IRNode {
std::string id;
IRType type;

// For network links
std::string from;
std::string to;

// Common parameters
int capacity;
double latency_mean;
double failure_prob;
};

// ------------- Simulation Registry -------------

struct Simulation {
std::unordered_map<std::string, std::unique_ptr<BaseEntity>> entities;
};

// Factory
class EntityFactory {
public:
void build(
const std::vector<IRNode>& ir,
Simulation& simulation
);

private:
void create_entities(
const std::vector<IRNode>& ir,
Simulation& simulation
);
};