Skip to content

Commit

Permalink
close #34: added preemption
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Apr 4, 2016
1 parent a2c706b commit 26b8855
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Makevars
@@ -1 +1 @@
PKG_CXXFLAGS = -DBOOST_NO_LONG_LONG
PKG_CXXFLAGS = -DBOOST_NO_LONG_LONG -Wno-long-long
9 changes: 4 additions & 5 deletions src/entity.cpp
Expand Up @@ -32,23 +32,20 @@ inline void Arrival::run() {
inline void Arrival::activate() {
Process::activate();
busy_until = sim->now() + remaining;
Rcpp::Rcout << "activate: " << this->name << " " << sim->now() << " " << remaining << std::endl;
sim->schedule(remaining, this, activity ? activity->priority : 0);
remaining = 0;
}

inline void Arrival::deactivate() {
Process::deactivate();
remaining = busy_until - sim->now();
Rcpp::Rcout << "deactivate: " << this->name << " " << sim->now() << " " << remaining << std::endl;
}

inline void Arrival::leave(std::string name, double time) {
gen->notify_release(time, this, name);
}

void Arrival::reject(double time) {
Rcpp::Rcout << "reject: " << this->name << " " << sim->now() << std::endl;
gen->notify_end(time, this, false);
}

Expand Down Expand Up @@ -120,8 +117,10 @@ int Resource::release(Arrival* arrival, int amount) {
if (queue_ptr) {
RPQueue::iterator next = queue_ptr->begin();
if (room_in_server(next->amount, next->priority)) {
if (next->arrival->is_monitored())
next->arrival->set_activity(this->name, sim->now());
if (next->arrival->is_monitored()) {
double last = next->arrival->get_activity(this->name);
next->arrival->set_activity(this->name, sim->now() - last);
}
next->arrival->activate();
insert_in_server(next->arrived_at, next->arrival, next->amount,
next->priority, next->preemptible, next->restart);
Expand Down
24 changes: 12 additions & 12 deletions src/entity.h
Expand Up @@ -340,18 +340,11 @@ class Resource: public Entity {
}
queue_count += amount;
queue.emplace(time, arrival, amount, priority, preemptible, restart);
/////////
Rcpp::Rcout << "insert_in_queue: " << arrival->name << std::endl;
Rcpp::Rcout << "queue: " << queue.size() << " | ";
foreach_ (RPQueue::value_type& itr, queue) {
Rcpp::Rcout << itr.arrival->name << " ";
}
Rcpp::Rcout << std::endl;
}

virtual inline RPQueue* get_queue_ptr() {
if (queue_count) return &queue;
return NULL;
if (!queue_count) return NULL;
return &queue;
}
};

Expand Down Expand Up @@ -398,6 +391,10 @@ class PreemptiveResource: public Resource {
if (capacity > 0) while (server_count + amount > capacity) {
typename T::iterator first = server.begin();
first->arrival->deactivate();
if (first->arrival->is_monitored()) {
double last = first->arrival->get_activity(this->name);
first->arrival->set_activity(this->name, time - last);
}
preempted.insert((*first));
queue_count += first->amount;
server_count -= first->amount;
Expand All @@ -415,9 +412,12 @@ class PreemptiveResource: public Resource {
}

virtual inline RPQueue* get_queue_ptr() {
if (!preempted.empty()) return &preempted;
if (queue_count) return &queue;
return NULL;
if (!queue_count) return NULL;
if (queue.empty()) return &preempted;
if (preempted.empty()) return &queue;
if (preempted.begin()->priority > queue.begin()->priority)
return &preempted;
return &queue;
}
};

Expand Down
2 changes: 1 addition & 1 deletion working_dir/__working_file.R
Expand Up @@ -58,7 +58,7 @@ env <- simmer(verbose=F) %>%
add_generator("customer", mm1, function() rexp(1, 60), mon=F) %>%
run(10000)
})
# 24 seconds! (Simpy: 30 seconds)
# 26 seconds (Simpy: 30 seconds)

plot_resource_usage(env, "server")

Expand Down

0 comments on commit 26b8855

Please sign in to comment.