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 const auto a = xbt::string_printf("Actor %ld: %s", t->aid_, t->to_string(true).c_str());
22 trace.push_back(std::move(a));
27 void Execution::push_transition(std::shared_ptr<Transition> t)
30 throw std::invalid_argument("Unexpectedly received `nullptr`");
32 ClockVector max_clock_vector;
33 for (const Event& e : this->contents_) {
34 if (e.get_transition()->depends(t.get())) {
35 max_clock_vector = ClockVector::max(max_clock_vector, e.get_clock_vector());
38 max_clock_vector[t->aid_] = this->size();
39 contents_.push_back(Event({std::move(t), max_clock_vector}));
42 std::vector<std::string> Execution::get_textual_trace() const
44 std::vector<std::string> trace;
45 for (const auto& t : this->contents_) {
47 xbt::string_printf("Actor %ld: %s", t.get_transition()->aid_, t.get_transition()->to_string(true).c_str());
48 trace.push_back(std::move(a));
53 std::unordered_set<Execution::EventHandle> Execution::get_racing_events_of(Execution::EventHandle target) const
55 std::unordered_set<Execution::EventHandle> racing_events;
56 std::unordered_set<Execution::EventHandle> disqualified_events;
58 // For each event of the execution
59 for (auto e_i = target; e_i != std::numeric_limits<Execution::EventHandle>::max(); e_i--) {
60 // We need `e_i -->_E target` as a necessary condition
61 if (not happens_before(e_i, target)) {
65 // Further, `proc(e_i) != proc(target)`
66 if (get_actor_with_handle(e_i) == get_actor_with_handle(target)) {
67 disqualified_events.insert(e_i);
71 // There could an event that "happens-between" the two events which would discount `e_i` as a race
72 for (auto e_j = e_i; e_j < target; e_j++) {
74 // 1. e_i --->_E e_j; and
75 // 2. disqualified_events.count(e_j) > 0
76 // then e_i --->_E target indirectly (either through
77 // e_j directly, or transitively through e_j)
78 if (disqualified_events.count(e_j) > 0 and happens_before(e_i, e_j)) {
79 disqualified_events.insert(e_i);
84 // If `e_i` wasn't disqualified in the last round,
85 // it's in a race with `target`. After marking it
86 // as such, we ensure no other event `e` can happen-before
87 // it (since this would transitively make it the event
88 // which "happens-between" `target` and `e`)
89 if (disqualified_events.count(e_i) == 0) {
90 racing_events.insert(e_i);
91 disqualified_events.insert(e_i);
98 std::unordered_set<Execution::EventHandle> Execution::get_reversible_races_of(EventHandle handle) const
100 std::unordered_set<EventHandle> reversible_races;
101 for (EventHandle race : get_racing_events_of(handle)) {
102 if (ReversibleRaceCalculator::is_race_reversible(*this, race, handle)) {
103 reversible_races.insert(race);
106 return reversible_races;
109 Execution Execution::get_prefix_before(Execution::EventHandle handle) const
111 return Execution(std::vector<Event>{contents_.begin(), contents_.begin() + handle});
114 std::unordered_set<aid_t>
115 Execution::get_missing_source_set_actors_from(EventHandle e, const std::unordered_set<aid_t>& backtrack_set) const
117 // If this execution is empty, there are no initials
118 // relative to the last transition added to the execution
119 // since such a transition does not exist
121 return std::unordered_set<aid_t>{};
124 // To actually compute `I_[E'](v) ∩ backtrack(E')`, we must
125 // first compute `E'` and "move" in the direction of `v`.
126 // We perform a scan over `E` (this execution) and make
127 // note of any events which occur after `e` but don't
128 // "happen-after" `e` by pushing them onto `E'`. Note that
129 // correctness is still preserved in computing `v` "on-the-fly"
130 // to determine if an event `e` by actor `q` is an initial for `E'`
131 // after `v`: only those events that "occur-before" `e` in `v` could
132 // "happen-before" `ve for any valid "happens-before" relation
133 // (see property 1 in the ODPOR paper, viz. "is included in <_E")
135 // First, grab `E' := pre(e, E)` and determine what actor `p` is
136 const auto next_E_p = get_latest_event_handle().value();
137 xbt_assert(e != next_E_p,
138 "This method assumes that the event `e` (%u) and `next_[E](p)` (%u)"
139 "are in a reversible race, yet we claim to have such a race between the"
140 "same event. This indicates the the SDPOR pseudocode implementation is broken "
141 "as it supplies these values.",
143 Execution E_prime_v = get_prefix_before(e);
144 std::vector<sdpor::Execution::EventHandle> v;
145 std::unordered_set<aid_t> I_E_prime_v;
146 std::unordered_set<aid_t> disqualified_actors;
148 // Note `e + 1` here: `notdep(e, E)` is defined as the
149 // set of events that *occur-after* but don't *happen-after* `e`
150 for (auto e_prime = e + 1; e_prime <= next_E_p; ++e_prime) {
151 // Any event `e*` which occurs after `e` but which does not
152 // happen after `e` is a member of `v`. In addition to marking
153 // the event in `v`, we also "simulate" running the action `v`
155 if (not happens_before(e, e_prime) or e_prime == next_E_p) {
156 // First, push the transition onto the hypothetical execution
157 E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
158 const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
160 // When checking whether any event in `dom_[E'](v)` happens before
161 // `next_[E'](q)` below for thread `q`, we must consider that the
162 // events relative to `E` (this execution) are different than those
163 // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
164 // in `E'.v`. Since we are asking about "happens-before"
165 // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`.
167 // Note that we add `q` to v regardless of whether `q` itself has been
168 // disqualified since we've determined that `e_prime` "occurs-after" but
169 // does not "happen-after" `e`
170 v.push_back(e_prime_in_E_prime_v);
172 const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v);
173 if (disqualified_actors.count(q) > 0) { // Did we already note that `q` is not an initial?
176 const bool is_initial = std::none_of(v.begin(), v.end(), [&](const auto& e_star) {
177 return E_prime_v.happens_before(e_star, e_prime_in_E_prime_v);
180 // If the backtrack set already contains `q`, we're done:
181 // they've made note to search for (or have already searched for)
183 if (backtrack_set.count(q) > 0) {
184 return std::unordered_set<aid_t>{};
186 I_E_prime_v.insert(q);
189 // If `q` is disqualified as a candidate, clearly
190 // no event occurring after `e_prime` in `E` executed
191 // by actor `q` will qualify since any (valid) happens-before
192 // relation orders actions taken by each actor
193 disqualified_actors.insert(q);
197 xbt_assert(!I_E_prime_v.empty(),
198 "For any non-empty execution, we know that "
199 "at minimum one actor is an initial since "
200 "some execution is possible with respect to a "
201 "prefix before event `%u`, yet we didn't find anyone. "
202 "This implies the implementation of this function is broken.",
207 std::optional<PartialExecution> Execution::get_odpor_extension_from(EventHandle e, EventHandle e_prime,
208 const State& state_at_e) const
210 // `e` is assumed to be in a reversible race with `e_prime`.
211 // If `e > e_prime`, then `e` occurs-after `e_prime` which means
212 // `e` could not race with if
214 throw std::invalid_argument("ODPOR extensions can only be computed for "
215 "events in a reversible race, which is claimed, "
216 "yet the racing event 'occurs-after' the target");
224 Execution E_prime_v = get_prefix_before(e);
225 std::unordered_set<aid_t> disqualified_actors = state_at_e.get_sleeping_actors();
226 std::vector<sdpor::Execution::EventHandle> v_handles;
227 bool located_actor_in_initial = false;
229 // Note `e + 1` here: `notdep(e, E)` is defined as the
230 // set of events that *occur-after* but don't *happen-after* `e`
232 // SUBTLE NOTE: ODPOR requires us to compute `notdep(e, E)` EVEN THOUGH
233 // the race is between `e` and `e'`; that is, events occurring in `E`
234 // that "occur-after" `e'` may end up in the partial execution `v`.
236 // Observe that `notdep(e, E).proc(e')` will contain all transitions
237 // that don't happen-after `e` in the order they appear FOLLOWED BY
238 // THE **TRANSITION** ASSOCIATED WITH **`e'`**!!
240 // SUBTLE NOTE: Observe that any event that "happens-after" `e'`
241 // must necessarily "happen-after" `e` as well, since `e` and
242 // `e'` are presumed to be in a reversible race. Hence, we know that
243 // all events `e_star` that `e` "happens-before" cannot affect
244 // the enabledness of `e'`; furthermore, `e'` cannot affect the enabledness
245 // of any event independent with `e` that "occurs-after" `e'`
246 for (auto e_star = e + 1; e_star <= get_latest_event_handle().value(); ++e_star) {
247 // Any event `e*` which occurs after `e` but which does not
248 // happen after `e` is a member of `v`. In addition to marking
249 // the event in `v`, we also "simulate" running the action `v` from E'
250 // to be able to compute `--->[E'.v]`
251 if (not happens_before(e, e_star)) {
252 xbt_assert(e_star != e_prime,
253 "Invariant Violation: We claimed events %u and %u were in a reversible race, yet we also "
254 "claim that they do not happen-before one another. This is impossible: "
255 "are you sure that the two events are in a reversible race?",
257 E_prime_v.push_transition(get_event_with_handle(e_star).get_transition());
258 v.push_back(get_event_with_handle(e_star).get_transition());
260 const EventHandle e_star_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
262 // When checking whether any event in `dom_[E'](v)` happens before
263 // `next_[E'](q)` below for thread `q`, we must consider that the
264 // events relative to `E` (this execution) are different than those
265 // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
266 // in `E'.v`. Since we are asking about "happens-before"
267 // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`
268 v_handles.push_back(e_star_in_E_prime_v);
270 if (located_actor_in_initial) {
271 // It suffices that we find one initial. If we've already found
272 // one, we simply need to finish building `v`
276 // Note that we add `q` to v regardless of whether `q` itself has been
277 // disqualified since `q` may itself disqualify other actors
278 // (i.e. even if `q` is disqualified from being an initial, it
279 // is still contained in the sequence `v`)
280 const aid_t q = E_prime_v.get_actor_with_handle(e_star_in_E_prime_v);
281 if (disqualified_actors.count(q) > 0) {
284 const bool is_initial = std::none_of(v_handles.begin(), v_handles.end(), [&](const auto& e_loc) {
285 return E_prime_v.happens_before(e_loc, e_star_in_E_prime_v);
288 located_actor_in_initial = true;
290 // If `q` is disqualified as a candidate, clearly
291 // no event occurring after `e_prime` in `E` executed
292 // by actor `q` will qualify since any (valid) happens-before
293 // relation orders actions taken by each actor
294 disqualified_actors.insert(q);
299 // Now we add `e_prime := <q, i>` to `E'.v` and repeat the same work
301 v.push_back(get_event_with_handle(e_prime).get_transition());
303 if (not located_actor_in_initial) {
304 // It's possible `proc(e_prime)` is an initial
305 E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
306 const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
307 v_handles.push_back(e_prime_in_E_prime_v);
309 const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v);
310 located_actor_in_initial = disqualified_actors.count(q) == 0 and
311 std::none_of(v_handles.begin(), v_handles.end(), [&](const auto& e_loc) {
312 return E_prime_v.happens_before(e_loc, e_prime_in_E_prime_v);
317 /** Some actor `p` in `v` is an initial for `E' := pre(e, E)`*/
318 if (located_actor_in_initial) {
322 const Execution pre_E_e = get_prefix_before(e);
323 const auto sleeping_actors = state_at_e.get_sleeping_actors();
325 // Otherwise, for each enabled actor also not in the sleep set, check if
326 // any of them are independent with this execution after `v`. This
327 // completes the check for weak initials
328 for (const auto& [aid, astate] : state_at_e.get_actors_list()) {
329 // TODO: We have to be able to react appropriately here when adding new
330 // types of transitions (multiple choices can be made :( )
331 if (astate.is_enabled() and sleeping_actors.count(aid) == 0 and
332 pre_E_e.is_independent_with_execution_of(v, astate.get_transition(0))) {
340 bool Execution::is_initial_after_execution_of(const PartialExecution& w, aid_t p) const
343 std::vector<EventHandle> w_handles;
344 for (const auto& w_i : w) {
345 // Take one step in the direction of `w`
346 E_w.push_transition(w_i);
348 // If that step happened to be executed by `p`,
349 // great: we know that `p` is contained in `w`.
350 // We now need to verify that it doens't "happen-after"
351 // any events which occur before it
352 if (w_i->aid_ == p) {
353 const auto p_handle = E_w.get_latest_event_handle().value();
354 return std::none_of(w_handles.begin(), w_handles.end(),
355 [&](const auto handle) { return E_w.happens_before(handle, p_handle); });
357 w_handles.push_back(E_w.get_latest_event_handle().value());
363 bool Execution::is_independent_with_execution_of(const PartialExecution& w, std::shared_ptr<Transition> next_E_p) const
365 // INVARIANT: Here, we assume that for any process `p_i` of `w`,
366 // the events of this execution followed by the execution of all
367 // actors occurring before `p_i` in `v` (`p_j`, `0 <= j < i`)
368 // are sufficient to enable `p_i`. This is fortunately the case
369 // with what ODPOR requires of us, viz. to ask the question about
370 // `v := notdep(e, E)` for some execution `E` and event `e` of
373 E_p_w.push_transition(std::move(next_E_p));
374 const auto p_handle = E_p_w.get_latest_event_handle().value();
376 // As we add events to `w`, verify that none
377 // of them "happen-after" the event associated with
378 // the step `next_E_p` (viz. p_handle)
379 for (const auto& w_i : w) {
380 E_p_w.push_transition(w_i);
381 const auto w_i_handle = E_p_w.get_latest_event_handle().value();
382 if (E_p_w.happens_before(p_handle, w_i_handle)) {
389 std::optional<PartialExecution> Execution::get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
390 const PartialExecution& w) const
392 // See section 4 of Abdulla. et al.'s 2017 ODPOR paper for details (specifically
393 // where the [iterative] computation of `v ~_[E] w` is described)
397 for (const auto& next_E_p : v) {
398 const aid_t p = next_E_p->aid_;
400 // Is `p in `I_[E](w)`?
401 if (E_v.is_initial_after_execution_of(w_now, p)) {
402 // Remove `p` from w and continue
404 // TODO: If `p` occurs in `w`, it had better refer to the same
405 // transition referenced by `v`. Unfortunately, we have two
406 // sources of truth here which can be manipulated at the same
407 // time as arguments to the function. If ODPOR works correctly,
408 // they should always refer to the same value; but as a sanity check,
409 // we have an assert that tests that at least the types are the same.
410 const auto action_by_p_in_w =
411 std::find_if(w_now.begin(), w_now.end(), [=](const auto& action) { return action->aid_ == p; });
412 xbt_assert(action_by_p_in_w != w_now.end(), "Invariant violated: actor `p` "
413 "is claimed to be an initial after `w` but is "
414 "not actually contained in `w`. This indicates that there "
415 "is a bug computing initials");
416 const auto& w_action = *action_by_p_in_w;
417 xbt_assert(w_action->type_ == next_E_p->type_,
418 "Invariant violated: `v` claims that actor `%ld` executes '%s' while "
419 "`w` claims that it executes '%s'. These two partial executions both "
420 "refer to `next_[E](p)`, which should be the same",
421 p, next_E_p->to_string(false).c_str(), w_action->to_string(false).c_str());
422 w_now.erase(action_by_p_in_w);
425 else if (E_v.is_independent_with_execution_of(w, next_E_p)) {
426 // INVARIANT: Note that it is impossible for `p` to be
427 // excluded from the set `I_[E](w)` BUT ALSO be contained in
428 // `w` itself if `E ⊢ p ◇ w` (intuitively, the fact that `E ⊢ p ◇ w`
429 // means that are able to move `p` anywhere in `w` IF it occurred, so
430 // if it really does occur we know it must then be an initial).
431 // We assert this is the case here
432 const auto action_by_p_in_w =
433 std::find_if(w_now.begin(), w_now.end(), [=](const auto& action) { return action->aid_ == p; });
434 xbt_assert(action_by_p_in_w == w_now.end(),
435 "Invariant violated: We claimed that actor `%ld` is not an initial "
436 "after `w`, yet it's independent with all actions of `w` AND occurs in `w`."
437 "This indicates that there is a bug computing initials",
440 // Neither of the two above conditions hold, so the relation fails
444 // Move one step forward in the direction of `v` and repeat
445 E_v.push_transition(next_E_p);
447 return std::optional<PartialExecution>{std::move(w_now)};
450 bool Execution::happens_before(Execution::EventHandle e1_handle, Execution::EventHandle e2_handle) const
452 // 1. "happens-before" (-->_E) is a subset of "occurs before" (<_E)
453 // and is an irreflexive relation
454 if (e1_handle >= e2_handle) {
458 // Each execution maintains a stack of clock vectors which are updated
459 // according to the procedure outlined in section 4 of the original DPOR paper
460 const Event& e2 = get_event_with_handle(e2_handle);
461 const aid_t proc_e1 = get_actor_with_handle(e1_handle);
463 if (const auto e1_in_e2_clock = e2.get_clock_vector().get(proc_e1); e1_in_e2_clock.has_value()) {
464 return e1_handle <= e1_in_e2_clock.value();
466 // If `e1` does not appear in e2's clock vector, this implies
467 // not only that the transitions associated with `e1` and `e2
468 // are independent, but further that there are no transitive
469 // dependencies between e1 and e2
473 } // namespace simgrid::mc::odpor