Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add some debug info
[simgrid.git] / src / mc / explo / DFSExplorer.cpp
index 2aef8ce..b23ce48 100644 (file)
@@ -84,6 +84,7 @@ RecordTrace DFSExplorer::get_record_trace() // override
 void DFSExplorer::restore_stack(std::shared_ptr<State> state)
 {
   stack_.clear();
+  execution_seq_     = odpor::Execution();
   auto current_state = state;
   stack_.emplace_front(current_state);
   // condition corresponds to reaching initial state
@@ -92,22 +93,18 @@ void DFSExplorer::restore_stack(std::shared_ptr<State> state)
     stack_.emplace_front(current_state);
   }
   XBT_DEBUG("Replaced stack by %s", get_record_trace().to_string().c_str());
-
   if (reduction_mode_ == ReductionMode::sdpor || reduction_mode_ == ReductionMode::odpor) {
-    execution_seq_ = odpor::Execution();
-
     // NOTE: The outgoing transition for the top-most
     // state of the  stack refers to that which was taken
     // as part of the last trace explored by the algorithm.
     // Thus, only the sequence of transitions leading up to,
     // but not including, the last state must be included
     // when reconstructing the Exploration for SDPOR.
-    for (auto iter = stack_.begin(); iter != stack_.end() - 1 and iter != stack_.end(); ++iter) {
-      const auto& state = *(iter);
-      execution_seq_.push_transition(state->get_transition_out());
+    for (auto iter = std::next(stack_.begin()); iter != stack_.end(); ++iter) {
+      execution_seq_.push_transition((*iter)->get_transition_in());
     }
+    XBT_DEBUG("Replaced SDPOR/ODPOR execution to reflect the new stack");
   }
-  XBT_DEBUG("Additionally replaced corresponding SDPOR execution stack");
 }
 
 void DFSExplorer::log_state() // override
@@ -115,8 +112,8 @@ void DFSExplorer::log_state() // override
   on_log_state_signal(get_remote_app());
   XBT_INFO("DFS exploration ended. %ld unique states visited; %lu backtracks (%lu transition replays, %lu states "
            "visited overall)",
-           State::get_expanded_states(), backtrack_count_, visited_states_count_,
-           Transition::get_replayed_transitions());
+           State::get_expanded_states(), backtrack_count_, Transition::get_replayed_transitions(),
+           visited_states_count_);
   Exploration::log_state();
 }
 
@@ -147,7 +144,7 @@ void DFSExplorer::run()
         XBT_ERROR("/!\\ Max depth of %d reached! THIS **WILL** BREAK the reduction, which is not sound "
                   "when stopping at a fixed depth /!\\",
                   _sg_mc_max_depth.get());
-        XBT_ERROR("/!\\ If bad things happen, disable dpor with --cfg=model-check/reduction:none /!\\");
+        XBT_ERROR("/!\\ If bad things happen, disable the reduction with --cfg=model-check/reduction:none /!\\");
       } else {
         XBT_WARN("/!\\ Max depth reached ! /!\\ ");
       }
@@ -186,11 +183,6 @@ void DFSExplorer::run()
     // in case we want to consider multiple states (eg. during backtrack)
     const aid_t next = reduction_mode_ == ReductionMode::odpor ? state->next_odpor_transition()
                                                                : std::get<0>(state->next_transition_guided());
-    xbt_assert(!state->is_actor_sleeping(next),
-               "We decided to schedule an actor (%ld) that is in the sleep set "
-               "of the current state. By definition, this should be impossible; "
-               "and yet it happened, somehow...",
-               next);
 
     if (next < 0) { // If there is no more transition in the current state, backtrack.
       XBT_VERB("%lu actors remain, but none of them need to be interleaved (depth %zu).", state->get_actor_count(),
@@ -206,19 +198,19 @@ void DFSExplorer::run()
       continue;
     }
 
-    if (_sg_mc_sleep_set && XBT_LOG_ISENABLED(mc_dfs, xbt_log_priority_verbose)) {
+    if (XBT_LOG_ISENABLED(mc_dfs, xbt_log_priority_verbose)) {
       XBT_VERB("Sleep set actually containing:");
-      for (auto& [aid, transition] : state->get_sleep_set())
+      for (const auto& [aid, transition] : state->get_sleep_set())
         XBT_VERB("  <%ld,%s>", aid, transition->to_string().c_str());
     }
 
     /* Actually answer the request: let's execute the selected request (MCed does one step) */
-    const auto executed_transition = state->execute_next(next, get_remote_app());
+    auto executed_transition = state->execute_next(next, get_remote_app());
     on_transition_execute_signal(state->get_transition_out().get(), get_remote_app());
 
     // If there are processes to interleave and the maximum depth has not been
     // reached then perform one step of the exploration algorithm.
-    XBT_VERB("Execute %ld: %.60s (stack depth: %zu, state: %ld, %zu interleaves)", state->get_transition_out()->aid_,
+    XBT_VERB("Execute %ld: %.150s (stack depth: %zu, state: %ld, %zu interleaves)", state->get_transition_out()->aid_,
              state->get_transition_out()->to_string().c_str(), stack_.size(), state->get_num(), state->count_todo());
 
     /* Create the new expanded state (copy the state of MCed into our MCer data) */
@@ -301,20 +293,23 @@ void DFSExplorer::run()
        * we want to be sure that search "in that direction"
        */
       execution_seq_.push_transition(std::move(executed_transition));
-      xbt_assert(execution_seq_.get_latest_event_handle().has_value(),
-                 "No events are contained in the SDPOR/OPDPOR execution "
-                 "even though one was just added");
+      xbt_assert(execution_seq_.get_latest_event_handle().has_value(), "No events are contained in the SDPOR execution "
+                                                                       "even though one was just added");
 
       const auto next_E_p = execution_seq_.get_latest_event_handle().value();
-      for (const auto racing_event_handle : execution_seq_.get_reversible_races_of(next_E_p)) {
-        const std::shared_ptr<State> prev_state = stack_[racing_event_handle];
-        // NOTE: To incorporate the idea of attempting to select the "best"
-        // backtrack point into SDPOR, instead of selecting the `first` initial,
-        // we should instead compute all choices and decide which is best
-        if (const auto q =
-                execution_seq_.get_first_sdpor_initial_from(racing_event_handle, prev_state->get_backtrack_set());
-            q.has_value()) {
-          prev_state->consider_one(q.value());
+      for (const auto e_race : execution_seq_.get_reversible_races_of(next_E_p)) {
+        State* prev_state  = stack_[e_race].get();
+        const auto choices = execution_seq_.get_missing_source_set_actors_from(e_race, prev_state->get_backtrack_set());
+        if (not choices.empty()) {
+          // NOTE: To incorporate the idea of attempting to select the "best"
+          // backtrack point into SDPOR, instead of selecting the `first` initial,
+          // we should instead compute all choices and decide which is best
+          //
+          // Here, we choose the actor with the lowest ID to ensure
+          // we get deterministic results
+          const auto q =
+              std::min_element(choices.begin(), choices.end(), [](const aid_t a1, const aid_t a2) { return a1 < a2; });
+          prev_state->consider_one(*q);
           opened_states_.emplace_back(std::move(prev_state));
         }
       }
@@ -377,7 +372,7 @@ std::shared_ptr<State> DFSExplorer::best_opened_state()
       continue;
     if (valid != current)
       *valid = std::move(*current);
-    if (best == end(opened_states_) || prio > best_prio) {
+    if (best == end(opened_states_) || prio < best_prio) {
       best_prio = prio;
       best      = valid;
     }
@@ -402,12 +397,16 @@ std::shared_ptr<State> DFSExplorer::next_odpor_state()
 {
   for (auto iter = stack_.rbegin(); iter != stack_.rend(); ++iter) {
     const auto& state = *iter;
-    state->do_odpor_backtrack_cleanup();
-    if (!state->has_empty_tree()) {
+    state->do_odpor_unwind();
+    XBT_DEBUG("\tPerformed ODPOR 'clean-up'. Sleep set has:");
+    for (const auto& [aid, transition] : state->get_sleep_set())
+      XBT_DEBUG("\t  <%ld,%s>", aid, transition->to_string().c_str());
+    if (not state->has_empty_tree()) {
+       XBT_DEBUG("\t    found the following non-empty WuT:\n"
+                 "%s", state->string_of_wut().c_str());
       return state;
     }
   }
-  xbt_die("There are no more states for ODPOR");
   return nullptr;
 }
 
@@ -431,10 +430,37 @@ void DFSExplorer::backtrack()
         XBT_DEBUG("ODPOR: Reversible race detected between events `%u` and `%u`", e, e_prime);
         State& prev_state = *stack_[e];
         if (const auto v = execution_seq_.get_odpor_extension_from(e, e_prime, prev_state); v.has_value()) {
-          XBT_DEBUG("ODPOR: Reversible race unaccounted for in the wakeup tree for "
-                    "the execution prior to event `%u`, inserting a sequence",
-                    e);
-          prev_state.mark_path_interesting_for_odpor(v.value(), execution_seq_.get_prefix_before(e));
+          switch (prev_state.insert_into_wakeup_tree(v.value(), execution_seq_.get_prefix_before(e))) {
+            case odpor::WakeupTree::InsertionResult::root: {
+              XBT_DEBUG("ODPOR: Reversible race with `%u`(%ld: %.20s) unaccounted for in the wakeup tree for "
+                        "the execution prior to event `%u`(%ld: %.20s):",
+                        e_prime, stack_[e_prime]->get_transition_out()->aid_,
+                        stack_[e_prime]->get_transition_out()->to_string(true).c_str(), e,
+                        prev_state.get_transition_out()->aid_,
+                        prev_state.get_transition_out()->to_string(true).c_str());
+              break;
+            }
+            case odpor::WakeupTree::InsertionResult::interior_node: {
+              XBT_DEBUG("ODPOR: Reversible race with `%u` partially accounted for in the wakeup tree for "
+                        "the execution prior to event `%u`:",
+                        e_prime, e);
+              break;
+            }
+            case odpor::WakeupTree::InsertionResult::leaf: {
+              XBT_DEBUG("ODPOR: Reversible race with `%u` accounted for in the wakeup tree for "
+                        "the execution prior to event `%u`:",
+                        e_prime, e);
+              break;
+            }
+          }
+          for (const auto& seq : simgrid::mc::odpor::get_textual_trace(v.value())) {
+            XBT_DEBUG(" %s", seq.c_str());
+          }
+        } else {
+          XBT_DEBUG("ODPOR: Ignoring race: `sleep(E')` intersects `WI_[E'](v := notdep(%u, E))`", e);
+          XBT_DEBUG("Sleep set contains:");
+          for (const auto& [aid, transition] : prev_state.get_sleep_set())
+            XBT_DEBUG("  <%ld,%s>", aid, transition->to_string().c_str());
         }
       }
     }