1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/mc/explo/odpor/Execution.hpp"
7 #include "src/mc/api/State.hpp"
8 #include "src/mc/explo/odpor/ReversibleRaceCalculator.hpp"
9 #include "xbt/asserts.h"
10 #include "xbt/string.hpp"
15 namespace simgrid::mc::odpor {
17 std::vector<std::string> get_textual_trace(const PartialExecution& w)
19 std::vector<std::string> trace;
20 for (const auto& t : w) {
21 auto a = xbt::string_printf("Actor %ld: %s", t->aid_, t->to_string(true).c_str());
22 trace.emplace_back(std::move(a));
27 Execution::Execution(const PartialExecution& w)
29 push_partial_execution(w);
32 void Execution::push_transition(std::shared_ptr<Transition> t)
35 throw std::invalid_argument("Unexpectedly received `nullptr`");
37 ClockVector max_clock_vector;
38 for (const Event& e : this->contents_) {
39 if (e.get_transition()->depends(t.get())) {
40 max_clock_vector = ClockVector::max(max_clock_vector, e.get_clock_vector());
43 max_clock_vector[t->aid_] = this->size();
44 contents_.push_back(Event({std::move(t), max_clock_vector}));
47 void Execution::push_partial_execution(const PartialExecution& w)
49 for (const auto& t : w) {
54 std::vector<std::string> Execution::get_textual_trace() const
56 std::vector<std::string> trace;
57 for (const auto& t : this->contents_) {
58 auto a = xbt::string_printf("Actor %ld: %s", t.get_transition()->aid_, t.get_transition()->to_string(true).c_str());
59 trace.emplace_back(std::move(a));
64 std::unordered_set<Execution::EventHandle> Execution::get_racing_events_of(Execution::EventHandle target) const
66 std::unordered_set<Execution::EventHandle> racing_events;
67 std::unordered_set<Execution::EventHandle> disqualified_events;
69 // For each event of the execution
70 for (auto e_i = target; e_i != std::numeric_limits<Execution::EventHandle>::max(); e_i--) {
71 // We need `e_i -->_E target` as a necessary condition
72 if (not happens_before(e_i, target)) {
76 // Further, `proc(e_i) != proc(target)`
77 if (get_actor_with_handle(e_i) == get_actor_with_handle(target)) {
78 disqualified_events.insert(e_i);
82 // There could an event that "happens-between" the two events which would discount `e_i` as a race
83 for (auto e_j = e_i; e_j < target; e_j++) {
85 // 1. e_i --->_E e_j; and
86 // 2. disqualified_events.count(e_j) > 0
87 // then e_i --->_E target indirectly (either through
88 // e_j directly, or transitively through e_j)
89 if (disqualified_events.count(e_j) > 0 && happens_before(e_i, e_j)) {
90 disqualified_events.insert(e_i);
95 // If `e_i` wasn't disqualified in the last round,
96 // it's in a race with `target`. After marking it
97 // as such, we ensure no other event `e` can happen-before
98 // it (since this would transitively make it the event
99 // which "happens-between" `target` and `e`)
100 if (disqualified_events.count(e_i) == 0) {
101 racing_events.insert(e_i);
102 disqualified_events.insert(e_i);
106 return racing_events;
109 std::unordered_set<Execution::EventHandle> Execution::get_reversible_races_of(EventHandle handle) const
111 std::unordered_set<EventHandle> reversible_races;
112 for (EventHandle race : get_racing_events_of(handle)) {
113 if (ReversibleRaceCalculator::is_race_reversible(*this, race, handle)) {
114 reversible_races.insert(race);
117 return reversible_races;
120 Execution Execution::get_prefix_before(Execution::EventHandle handle) const
122 return Execution(std::vector<Event>{contents_.begin(), contents_.begin() + handle});
125 std::unordered_set<aid_t>
126 Execution::get_missing_source_set_actors_from(EventHandle e, const std::unordered_set<aid_t>& backtrack_set) const
128 // If this execution is empty, there are no initials
129 // relative to the last transition added to the execution
130 // since such a transition does not exist
132 return std::unordered_set<aid_t>{};
135 // To actually compute `I_[E'](v) ∩ backtrack(E')`, we must
136 // first compute `E'` and "move" in the direction of `v`.
137 // We perform a scan over `E` (this execution) and make
138 // note of any events which occur after `e` but don't
139 // "happen-after" `e` by pushing them onto `E'`. Note that
140 // correctness is still preserved in computing `v` "on-the-fly"
141 // to determine if an event `e` by actor `q` is an initial for `E'`
142 // after `v`: only those events that "occur-before" `e` in `v` could
143 // "happen-before" `ve for any valid "happens-before" relation
144 // (see property 1 in the ODPOR paper, viz. "is included in <_E")
146 // First, grab `E' := pre(e, E)` and determine what actor `p` is
147 const auto next_E_p = get_latest_event_handle().value();
148 xbt_assert(e != next_E_p,
149 "This method assumes that the event `e` (%u) and `next_[E](p)` (%u)"
150 "are in a reversible race, yet we claim to have such a race between the"
151 "same event. This indicates the the SDPOR pseudocode implementation is broken "
152 "as it supplies these values.",
154 Execution E_prime_v = get_prefix_before(e);
155 std::vector<sdpor::Execution::EventHandle> v;
156 std::unordered_set<aid_t> I_E_prime_v;
157 std::unordered_set<aid_t> disqualified_actors;
159 // Note `e + 1` here: `notdep(e, E)` is defined as the
160 // set of events that *occur-after* but don't *happen-after* `e`
161 for (auto e_prime = e + 1; e_prime <= next_E_p; ++e_prime) {
162 // Any event `e*` which occurs after `e` but which does not
163 // happen after `e` is a member of `v`. In addition to marking
164 // the event in `v`, we also "simulate" running the action `v`
166 if (not happens_before(e, e_prime) || e_prime == next_E_p) {
167 // First, push the transition onto the hypothetical execution
168 E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
169 const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
171 // When checking whether any event in `dom_[E'](v)` happens before
172 // `next_[E'](q)` below for thread `q`, we must consider that the
173 // events relative to `E` (this execution) are different than those
174 // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
175 // in `E'.v`. Since we are asking about "happens-before"
176 // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`.
178 // Note that we add `q` to v regardless of whether `q` itself has been
179 // disqualified since we've determined that `e_prime` "occurs-after" but
180 // does not "happen-after" `e`
181 v.push_back(e_prime_in_E_prime_v);
183 const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v);
184 if (disqualified_actors.count(q) > 0) { // Did we already note that `q` is not an initial?
187 const bool is_initial = std::none_of(v.begin(), v.end(), [&](const auto& e_star) {
188 return E_prime_v.happens_before(e_star, e_prime_in_E_prime_v);
191 // If the backtrack set already contains `q`, we're done:
192 // they've made note to search for (or have already searched for)
194 if (backtrack_set.count(q) > 0) {
195 return std::unordered_set<aid_t>{};
197 I_E_prime_v.insert(q);
200 // If `q` is disqualified as a candidate, clearly
201 // no event occurring after `e_prime` in `E` executed
202 // by actor `q` will qualify since any (valid) happens-before
203 // relation orders actions taken by each actor
204 disqualified_actors.insert(q);
208 xbt_assert(not I_E_prime_v.empty(),
209 "For any non-empty execution, we know that "
210 "at minimum one actor is an initial since "
211 "some execution is possible with respect to a "
212 "prefix before event `%u`, yet we didn't find anyone. "
213 "This implies the implementation of this function is broken.",
218 std::optional<PartialExecution> Execution::get_odpor_extension_from(EventHandle e, EventHandle e_prime,
219 const State& state_at_e) const
221 // `e` is assumed to be in a reversible race with `e_prime`.
222 // If `e > e_prime`, then `e` occurs-after `e_prime` which means
223 // `e` could not race with if
225 throw std::invalid_argument("ODPOR extensions can only be computed for "
226 "events in a reversible race, which is claimed, "
227 "yet the racing event 'occurs-after' the target");
235 std::vector<Execution::EventHandle> v_handles;
236 std::unordered_set<aid_t> WI_E_prime_v;
237 std::unordered_set<aid_t> disqualified_actors;
238 Execution E_prime_v = get_prefix_before(e);
239 const std::unordered_set<aid_t> sleep_E_prime = state_at_e.get_sleeping_actors();
241 // Note `e + 1` here: `notdep(e, E)` is defined as the
242 // set of events that *occur-after* but don't *happen-after* `e`
244 // SUBTLE NOTE: ODPOR requires us to compute `notdep(e, E)` EVEN THOUGH
245 // the race is between `e` and `e'`; that is, events occurring in `E`
246 // that "occur-after" `e'` may end up in the partial execution `v`.
248 // Observe that `notdep(e, E).proc(e')` will contain all transitions
249 // that don't happen-after `e` in the order they appear FOLLOWED BY
250 // THE **TRANSITION** ASSOCIATED WITH **`e'`**!!
252 // SUBTLE NOTE: Observe that any event that "happens-after" `e'`
253 // must necessarily "happen-after" `e` as well, since `e` and
254 // `e'` are presumed to be in a reversible race. Hence, we know that
255 // all events `e_star` such that `e` "happens-before" `e_star` cannot affect
256 // the enabledness of `e'`; furthermore, `e'` cannot affect the enabledness
257 // of any event independent with `e` that "occurs-after" `e'`
258 for (auto e_star = e + 1; e_star <= get_latest_event_handle().value(); ++e_star) {
259 // Any event `e*` which occurs after `e` but which does not
260 // happen after `e` is a member of `v`. In addition to marking
261 // the event in `v`, we also "simulate" running the action `v` from E'
262 // to be able to compute `--->[E'.v]`
263 if (not happens_before(e, e_star)) {
264 xbt_assert(e_star != e_prime,
265 "Invariant Violation: We claimed events %u and %u were in a reversible race, yet we also "
266 "claim that they do not happen-before one another. This is impossible: "
267 "are you sure that the two events are in a reversible race?",
269 E_prime_v.push_transition(get_event_with_handle(e_star).get_transition());
270 v.push_back(get_event_with_handle(e_star).get_transition());
272 const EventHandle e_star_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
274 // When checking whether any event in `dom_[E'](v)` happens before
275 // `next_[E'](q)` below for thread `q`, we must consider that the
276 // events relative to `E` (this execution) are different than those
277 // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
278 // in `E'.v`. Since we are asking about "happens-before"
279 // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`
280 v_handles.push_back(e_star_in_E_prime_v);
282 // Note that we add `q` to v regardless of whether `q` itself has been
283 // disqualified since `q` may itself disqualify other actors
284 // (i.e. even if `q` is disqualified from being an initial, it
285 // is still contained in the sequence `v`)
286 const aid_t q = E_prime_v.get_actor_with_handle(e_star_in_E_prime_v);
287 if (disqualified_actors.count(q) > 0) { // Did we already note that `q` is not an initial?
290 const bool is_initial = std::none_of(v_handles.begin(), v_handles.end(), [&](const auto& handle) {
291 return E_prime_v.happens_before(handle, e_star_in_E_prime_v);
294 // If the sleep set already contains `q`, we're done:
295 // we've found an initial contained in the sleep set and
296 // so the intersection is non-empty
297 if (sleep_E_prime.count(q) > 0) {
300 WI_E_prime_v.insert(q);
303 // If `q` is disqualified as a candidate, clearly
304 // no event occurring after `e_prime` in `E` executed
305 // by actor `q` will qualify since any (valid) happens-before
306 // relation orders actions taken by each actor
307 disqualified_actors.insert(q);
312 // Now we add `e_prime := <q, i>` to `E'.v` and repeat the same work
313 // It's possible `proc(e_prime)` is an initial
315 // Note the form of `v` in the pseudocode:
316 // `v := notdep(e, E).e'^
317 E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
318 v.push_back(get_event_with_handle(e_prime).get_transition());
320 const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
321 v_handles.push_back(e_prime_in_E_prime_v);
323 const bool is_initial = std::none_of(v_handles.begin(), v_handles.end(), [&](const auto& handle) {
324 return E_prime_v.happens_before(handle, e_prime_in_E_prime_v);
327 if (const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v); sleep_E_prime.count(q) > 0) {
330 WI_E_prime_v.insert(q);
334 const Execution pre_E_e = get_prefix_before(e);
335 const auto sleeping_actors = state_at_e.get_sleeping_actors();
337 // Check if any enabled actor that is independent with
338 // this execution after `v` is contained in the sleep set
339 for (const auto& [aid, astate] : state_at_e.get_actors_list()) {
340 const bool is_in_WI_E =
341 astate.is_enabled() and pre_E_e.is_independent_with_execution_of(v, astate.get_transition());
342 const bool is_in_sleep_set = sleeping_actors.count(aid) > 0;
344 // `action(aid)` is in `WI_[E](v)` but also is contained in the sleep set.
345 // This implies that the intersection between the two is non-empty
346 if (is_in_WI_E && is_in_sleep_set)
353 bool Execution::is_initial_after_execution_of(const PartialExecution& w, aid_t p) const
356 std::vector<EventHandle> w_handles;
357 for (const auto& w_i : w) {
358 // Take one step in the direction of `w`
359 E_w.push_transition(w_i);
361 // If that step happened to be executed by `p`,
362 // great: we know that `p` is contained in `w`.
363 // We now need to verify that it doens't "happen-after"
364 // any events which occur before it
365 if (w_i->aid_ == p) {
366 const auto p_handle = E_w.get_latest_event_handle().value();
367 return std::none_of(w_handles.begin(), w_handles.end(),
368 [&](const auto handle) { return E_w.happens_before(handle, p_handle); });
370 w_handles.push_back(E_w.get_latest_event_handle().value());
376 bool Execution::is_independent_with_execution_of(const PartialExecution& w, std::shared_ptr<Transition> next_E_p) const
378 // INVARIANT: Here, we assume that for any process `p_i` of `w`,
379 // the events of this execution followed by the execution of all
380 // actors occurring before `p_i` in `v` (`p_j`, `0 <= j < i`)
381 // are sufficient to enable `p_i`. This is fortunately the case
382 // with what ODPOR requires of us, viz. to ask the question about
383 // `v := notdep(e, E)` for some execution `E` and event `e` of
386 E_p_w.push_transition(std::move(next_E_p));
387 const auto p_handle = E_p_w.get_latest_event_handle().value();
389 // As we add events to `w`, verify that none
390 // of them "happen-after" the event associated with
391 // the step `next_E_p` (viz. p_handle)
392 for (const auto& w_i : w) {
393 E_p_w.push_transition(w_i);
394 const auto w_i_handle = E_p_w.get_latest_event_handle().value();
395 if (E_p_w.happens_before(p_handle, w_i_handle)) {
402 std::optional<PartialExecution> Execution::get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
403 const PartialExecution& w) const
405 // See section 4 of Abdulla. et al.'s 2017 ODPOR paper for details (specifically
406 // where the [iterative] computation of `v ~_[E] w` is described)
410 for (const auto& next_E_p : v) {
411 // Is `p in `I_[E](w)`?
412 if (const aid_t p = next_E_p->aid_; E_v.is_initial_after_execution_of(w_now, p)) {
413 // Remove `p` from w and continue
415 // INVARIANT: If `p` occurs in `w`, it had better refer to the same
416 // transition referenced by `v`. Unfortunately, we have two
417 // sources of truth here which can be manipulated at the same
418 // time as arguments to the function. If ODPOR works correctly,
419 // they should always refer to the same value; but as a sanity check,
420 // we have an assert that tests that at least the types are the same.
421 const auto action_by_p_in_w =
422 std::find_if(w_now.begin(), w_now.end(), [=](const auto& action) { return action->aid_ == p; });
423 xbt_assert(action_by_p_in_w != w_now.end(), "Invariant violated: actor `p` "
424 "is claimed to be an initial after `w` but is "
425 "not actually contained in `w`. This indicates that there "
426 "is a bug computing initials");
427 const auto& w_action = *action_by_p_in_w;
428 xbt_assert(w_action->type_ == next_E_p->type_,
429 "Invariant violated: `v` claims that actor `%ld` executes '%s' while "
430 "`w` claims that it executes '%s'. These two partial executions both "
431 "refer to `next_[E](p)`, which should be the same",
432 p, next_E_p->to_string(false).c_str(), w_action->to_string(false).c_str());
433 w_now.erase(action_by_p_in_w);
436 else if (E_v.is_independent_with_execution_of(w_now, next_E_p)) {
437 // INVARIANT: Note that it is impossible for `p` to be
438 // excluded from the set `I_[E](w)` BUT ALSO be contained in
439 // `w` itself if `E ⊢ p ◇ w` (intuitively, the fact that `E ⊢ p ◇ w`
440 // means that are able to move `p` anywhere in `w` IF it occurred, so
441 // if it really does occur we know it must then be an initial).
442 // We assert this is the case here
443 const auto action_by_p_in_w =
444 std::find_if(w_now.begin(), w_now.end(), [=](const auto& action) { return action->aid_ == p; });
445 xbt_assert(action_by_p_in_w == w_now.end(),
446 "Invariant violated: We claimed that actor `%ld` is not an initial "
447 "after `w`, yet it's independent with all actions of `w` AND occurs in `w`."
448 "This indicates that there is a bug computing initials",
451 // Neither of the two above conditions hold, so the relation fails
455 // Move one step forward in the direction of `v` and repeat
456 E_v.push_transition(next_E_p);
458 return std::optional<PartialExecution>{std::move(w_now)};
461 bool Execution::happens_before(Execution::EventHandle e1_handle, Execution::EventHandle e2_handle) const
463 // 1. "happens-before" (-->_E) is a subset of "occurs before" (<_E)
464 // and is an irreflexive relation
465 if (e1_handle >= e2_handle) {
469 // Each execution maintains a stack of clock vectors which are updated
470 // according to the procedure outlined in section 4 of the original DPOR paper
471 const Event& e2 = get_event_with_handle(e2_handle);
472 const aid_t proc_e1 = get_actor_with_handle(e1_handle);
474 if (const auto e1_in_e2_clock = e2.get_clock_vector().get(proc_e1); e1_in_e2_clock.has_value()) {
475 return e1_handle <= e1_in_e2_clock.value();
477 // If `e1` does not appear in e2's clock vector, this implies
478 // not only that the transitions associated with `e1` and `e2
479 // are independent, but further that there are no transitive
480 // dependencies between e1 and e2
484 } // namespace simgrid::mc::odpor