Skip to content

Commit

Permalink
less strings in logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Nov 11, 2023
1 parent 0edd6ef commit 6d794bc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
35 changes: 24 additions & 11 deletions symmetri/petri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ std::vector<int8_t> createPriorityLookup(
Petri::Petri(const Net &_net, const PriorityTable &_priority,
const Marking &_initial_tokens, const Marking &_final_marking,
const std::string &_case_id, std::shared_ptr<TaskSystem> stp)
: event_log({}),
: event_log_small({}),
state(Color::Scheduled),
case_id(_case_id),
thread_id_(std::nullopt),
reducer_queue(
std::make_shared<moodycamel::BlockingConcurrentQueue<Reducer>>(128)),
pool(stp) {
event_log.reserve(1000);
event_log_small.reserve(1000);
tokens.reserve(100);
scheduled_callbacks.reserve(10);

std::tie(net.transition, net.place, net.color, net.store) = convert(_net);
std::tie(net.input_n, net.output_n) =
populateIoLookups(_net, net.place, net.color);
Expand All @@ -117,11 +120,12 @@ std::vector<AugmentedToken> Petri::toTokens(

void Petri::fireSynchronous(const size_t t) {
const auto &task = net.store[t];
const auto &transition = net.transition[t];
const auto &lookup_t = net.output_n[t];
event_log.push_back({case_id, transition, Color::Started, Clock::now()});
const auto now = Clock::now();
event_log_small.push_back({t, Color::Started, now});
auto result = ::fire(task);
event_log.push_back({case_id, transition, result, Clock::now()});
event_log_small.push_back({t, result, now});

switch (result) {
case Color::Scheduled:
case Color::Started:
Expand All @@ -141,9 +145,9 @@ void Petri::fireSynchronous(const size_t t) {

void Petri::fireAsynchronous(const size_t t) {
const auto &task = net.store[t];
const auto &transition = net.transition[t];
scheduled_callbacks.push_back(t);
event_log.push_back({case_id, transition, Color::Scheduled, Clock::now()});
event_log_small.push_back({t, Color::Scheduled, Clock::now()});

pool->push([=] {
reducer_queue->enqueue(scheduleCallback(t, task, reducer_queue));
});
Expand Down Expand Up @@ -230,15 +234,24 @@ Marking Petri::getMarking() const {
return marking;
}

Eventlog Petri::getLog() const {
Eventlog eventlog = event_log;
Eventlog Petri::getLogInternal() const {
Eventlog eventlog;
eventlog.reserve(event_log_small.size());
for (const auto &[t_i, result, time] : event_log_small) {
eventlog.push_back({case_id, net.transition[t_i], result, time});
}

// get event log from parent nets:
for (size_t pt_idx : scheduled_callbacks) {
auto sub_el = ::getLog(net.store[pt_idx]);
for (const auto &cb : net.store) {
Eventlog sub_el = getLog(cb);
if (!sub_el.empty()) {
eventlog.insert(eventlog.end(), sub_el.begin(), sub_el.end());
}
}

std::sort(eventlog.begin(), eventlog.end(),
[](const auto &a, const auto &b) { return a.stamp < b.stamp; });

return eventlog;
}

Expand Down
17 changes: 15 additions & 2 deletions symmetri/petri.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ inline bool operator<(const AugmentedToken &lhs, const AugmentedToken &rhs) {
inline bool operator>(const AugmentedToken &lhs, const AugmentedToken &rhs) {
return lhs.place > rhs.place && lhs.color > rhs.color;
}

/**
* @brief uses allocation free types
*
*/
struct SmallEvent {
size_t transition; ///< The transition that generated the event
Token state; ///< The result of the event
Clock::time_point stamp; ///< The timestamp of the event
};

using SmallLog = std::vector<SmallEvent>;

using SmallVector = gch::small_vector<size_t, 4>;
using SmallVectorInput = gch::small_vector<AugmentedToken, 4>;

Expand Down Expand Up @@ -152,7 +165,7 @@ struct Petri {
*
* @return Eventlog
*/
Eventlog getLog() const;
Eventlog getLogInternal() const;

/**
* @brief Try to fire a single transition. Does nothing if t is not active.
Expand Down Expand Up @@ -236,7 +249,7 @@ struct Petri {
std::vector<AugmentedToken> tokens; ///< The current marking
std::vector<AugmentedToken> final_marking; ///< The final marking
std::vector<size_t> scheduled_callbacks; ///< List of active transitions
Eventlog event_log; ///< The most actual event_log
SmallLog event_log_small; ///< The most actual event_log
Token state; ///< The current state of the Petri
std::string case_id; ///< The unique identifier for this Petri-run
std::atomic<std::optional<unsigned int>>
Expand Down
12 changes: 2 additions & 10 deletions symmetri/petri_utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ Reducer createReducerForCallback(const size_t t_i, const Token result,
};
model.scheduled_callbacks.erase(it);
};
// get the log
Eventlog ev;
model.net.store[t_i](ev);
const Transition &transition = model.net.transition[t_i];
if (!ev.empty()) {
model.event_log.insert(model.event_log.cend(), ev.begin(), ev.end());
}
model.event_log.push_back({model.case_id, transition, result, t_end});
model.event_log_small.push_back({t_i, result, t_end});
};
}

Expand All @@ -85,8 +78,7 @@ Reducer scheduleCallback(
&reducer_queue) {
reducer_queue->enqueue(
[start_time = Clock::now(), t_i](symmetri::Petri &model) {
model.event_log.push_back({model.case_id, model.net.transition[t_i],
Color::Started, start_time});
model.event_log_small.push_back({t_i, Color::Started, start_time});
});

return createReducerForCallback(t_i, ::fire(task), Clock::now());
Expand Down
11 changes: 4 additions & 7 deletions symmetri/symmetri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,10 @@ symmetri::Token fire(const PetriNet &app) {
void cancel(const PetriNet &app) {
app.impl->reducer_queue->enqueue([=](Petri &model) {
model.state = Color::UserExit;
// populate that eventlog with child eventlog and possible
// cancelations.
for (const auto transition_index : model.scheduled_callbacks) {
cancel(model.net.store.at(transition_index));
model.event_log.push_back({model.case_id,
model.net.transition[transition_index],
Color::UserExit, Clock::now()});
model.event_log_small.push_back(
{transition_index, Color::UserExit, Clock::now()});
}
});
}
Expand Down Expand Up @@ -187,9 +184,9 @@ Eventlog getLog(const PetriNet &app) {
std::promise<Eventlog> el;
std::future<Eventlog> el_getter = el.get_future();
app.impl->reducer_queue->enqueue(
[&](Petri &model) { el.set_value(model.getLog()); });
[&](Petri &model) { el.set_value(model.getLogInternal()); });
return el_getter.get();
} else {
return app.impl->getLog();
return app.impl->getLogInternal();
}
}

0 comments on commit 6d794bc

Please sign in to comment.