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/ReversibleRaceCalculator.hpp"
7 #include "src/mc/explo/odpor/Execution.hpp"
10 #include <unordered_map>
11 #include <xbt/asserts.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_odpor_reversible_race, mc_dfs, "ODPOR exploration algorithm of the model-checker");
16 namespace simgrid::mc::odpor {
19 The reversible race detector should only be used if we already have the assumption
20 e1 <* e2 (see Source set: a foundation for ODPOR). In particular this means that :
22 - proc(e1) != proc(e2)
23 - there is no event e3 s.t. e1 --> e3 --> e2
26 bool ReversibleRaceCalculator::is_race_reversible(const Execution& E, Execution::EventHandle e1,
27 Execution::EventHandle e2)
29 using Action = Transition::Type;
30 using Handler = std::function<bool(const Execution&, Execution::EventHandle, const Transition*)>;
31 using HandlerMap = std::unordered_map<Action, Handler>;
33 const static HandlerMap handlers = {
34 {Action::ACTOR_JOIN, &ReversibleRaceCalculator::is_race_reversible_ActorJoin},
35 {Action::BARRIER_ASYNC_LOCK, &ReversibleRaceCalculator::is_race_reversible_BarrierAsyncLock},
36 {Action::BARRIER_WAIT, &ReversibleRaceCalculator::is_race_reversible_BarrierWait},
37 {Action::COMM_ASYNC_SEND, &ReversibleRaceCalculator::is_race_reversible_CommSend},
38 {Action::COMM_ASYNC_RECV, &ReversibleRaceCalculator::is_race_reversible_CommRecv},
39 {Action::COMM_TEST, &ReversibleRaceCalculator::is_race_reversible_CommTest},
40 {Action::COMM_WAIT, &ReversibleRaceCalculator::is_race_reversible_CommWait},
41 {Action::MUTEX_ASYNC_LOCK, &ReversibleRaceCalculator::is_race_reversible_MutexAsyncLock},
42 {Action::MUTEX_TEST, &ReversibleRaceCalculator::is_race_reversible_MutexTest},
43 {Action::MUTEX_TRYLOCK, &ReversibleRaceCalculator::is_race_reversible_MutexTrylock},
44 {Action::MUTEX_UNLOCK, &ReversibleRaceCalculator::is_race_reversible_MutexUnlock},
45 {Action::MUTEX_WAIT, &ReversibleRaceCalculator::is_race_reversible_MutexWait},
46 {Action::OBJECT_ACCESS, &ReversibleRaceCalculator::is_race_reversible_ObjectAccess},
47 {Action::RANDOM, &ReversibleRaceCalculator::is_race_reversible_Random},
48 {Action::SEM_ASYNC_LOCK, &ReversibleRaceCalculator::is_race_reversible_SemAsyncLock},
49 {Action::SEM_UNLOCK, &ReversibleRaceCalculator::is_race_reversible_SemUnlock},
50 {Action::SEM_WAIT, &ReversibleRaceCalculator::is_race_reversible_SemWait},
51 {Action::TESTANY, &ReversibleRaceCalculator::is_race_reversible_TestAny},
52 {Action::WAITANY, &ReversibleRaceCalculator::is_race_reversible_WaitAny}};
54 const auto* e2_action = E.get_transition_for_handle(e2);
55 if (const auto handler = handlers.find(e2_action->type_); handler != handlers.end()) {
56 return handler->second(E, e1, e2_action);
58 xbt_die("There is currently no specialized computation for the transition "
59 "'%s' for computing reversible races in ODPOR, so the model checker cannot "
60 "determine how to proceed. Please submit a bug report requesting "
61 "that the transition be supported in SimGrid using ODPPR and consider "
62 "using the other model-checking algorithms supported by SimGrid instead "
64 e2_action->to_string().c_str());
68 bool ReversibleRaceCalculator::is_race_reversible_ActorJoin(const Execution&, Execution::EventHandle /*e1*/,
69 const Transition* /*e2*/)
71 // ActorJoin races with another event iff its target `T` is the same as
72 // the actor executing the other transition. Clearly, then, we could not join
73 // on that actor `T` and then run a transition by `T`, so no race is reversible
77 bool ReversibleRaceCalculator::is_race_reversible_BarrierAsyncLock(const Execution&, Execution::EventHandle /*e1*/,
78 const Transition* /*e2*/)
80 // BarrierAsyncLock is always enabled
84 bool ReversibleRaceCalculator::is_race_reversible_BarrierWait(const Execution& E, Execution::EventHandle e1,
85 const Transition* /*e2*/)
87 // If the other event is a barrier lock event, then we
88 // are not reversible; otherwise we are reversible.
89 const auto e1_action = E.get_transition_for_handle(e1)->type_;
90 return e1_action != Transition::Type::BARRIER_ASYNC_LOCK;
93 bool ReversibleRaceCalculator::is_race_reversible_CommRecv(const Execution&, Execution::EventHandle /*e1*/,
94 const Transition* /*e2*/)
96 // CommRecv is always enabled
100 bool ReversibleRaceCalculator::is_race_reversible_CommSend(const Execution&, Execution::EventHandle /*e1*/,
101 const Transition* /*e2*/)
103 // CommSend is always enabled
107 bool ReversibleRaceCalculator::is_race_reversible_CommWait(const Execution& E, Execution::EventHandle e1,
108 const Transition* /*e2*/)
110 // If the other event is a communication event, then we
111 // are not reversible; otherwise we are reversible.
112 const auto e1_action = E.get_transition_for_handle(e1)->type_;
113 return e1_action != Transition::Type::COMM_ASYNC_SEND && e1_action != Transition::Type::COMM_ASYNC_RECV;
116 bool ReversibleRaceCalculator::is_race_reversible_CommTest(const Execution&, Execution::EventHandle /*e1*/,
117 const Transition* /*e2*/)
119 // CommTest is always enabled
123 bool ReversibleRaceCalculator::is_race_reversible_MutexAsyncLock(const Execution&, Execution::EventHandle /*e1*/,
124 const Transition* /*e2*/)
126 // MutexAsyncLock is always enabled
130 bool ReversibleRaceCalculator::is_race_reversible_MutexTest(const Execution&, Execution::EventHandle /*e1*/,
131 const Transition* /*e2*/)
133 // MutexTest is always enabled
137 bool ReversibleRaceCalculator::is_race_reversible_MutexTrylock(const Execution&, Execution::EventHandle /*e1*/,
138 const Transition* /*e2*/)
140 // MutexTrylock is always enabled
144 bool ReversibleRaceCalculator::is_race_reversible_MutexUnlock(const Execution&, Execution::EventHandle /*e1*/,
145 const Transition* /*e2*/)
147 // MutexUnlock is always enabled
151 bool ReversibleRaceCalculator::is_race_reversible_MutexWait(const Execution& E, Execution::EventHandle e1,
152 const Transition* /*e2*/)
154 // The only possibilities for e1 to satisfy the pre-condition are :
155 // - MUTEX_ASYNC_LOCK
158 const auto e1_action = E.get_transition_for_handle(e1)->type_;
159 xbt_assert(e1_action == Transition::Type::MUTEX_UNLOCK);
160 return e1_action != Transition::Type::MUTEX_ASYNC_LOCK && e1_action != Transition::Type::MUTEX_UNLOCK;
163 bool ReversibleRaceCalculator::is_race_reversible_SemAsyncLock(const Execution&, Execution::EventHandle /*e1*/,
164 const Transition* /*e2*/)
166 // SemAsyncLock is always enabled
170 bool ReversibleRaceCalculator::is_race_reversible_SemUnlock(const Execution&, Execution::EventHandle /*e1*/,
171 const Transition* /*e2*/)
173 // SemUnlock is always enabled
177 bool ReversibleRaceCalculator::is_race_reversible_SemWait(const Execution&, Execution::EventHandle /*e1*/,
178 const Transition* /*e2*/)
180 // TODO: Get the semantics correct here
181 // Certainement qu'il suffit de considérer les SemUnlock. ⋀ a priori,
182 // il doit même suffir de considérer le cas où leur capacity après execution est <=1
183 // ces cas disent qu'avant éxecution la capacity était de 0. Donc aucune chance de pouvoir
184 // wait avant le unlock.
188 bool ReversibleRaceCalculator::is_race_reversible_ObjectAccess(const Execution&, Execution::EventHandle /*e1*/,
189 const Transition* /*e2*/)
191 // Object access is always enabled
195 bool ReversibleRaceCalculator::is_race_reversible_Random(const Execution&, Execution::EventHandle /*e1*/,
196 const Transition* /*e2*/)
198 // Random is always enabled
202 bool ReversibleRaceCalculator::is_race_reversible_TestAny(const Execution&, Execution::EventHandle /*e1*/,
203 const Transition* /*e2*/)
205 // TestAny is always enabled
209 bool ReversibleRaceCalculator::is_race_reversible_WaitAny(const Execution&, Execution::EventHandle /*e1*/,
210 const Transition* /*e2*/)
212 // TODO: We need to check if any of the transitions
213 // waited on occurred before `e1`
217 } // namespace simgrid::mc::odpor