Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: move a check_deadlock from ModelChecker to Session, and kill cruft in mc::api
[simgrid.git] / src / mc / VisitedState.cpp
1 /* Copyright (c) 2011-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/mc/VisitedState.hpp"
7 #include "src/mc/mc_private.hpp"
8
9 #include <unistd.h>
10 #include <sys/wait.h>
11 #include <memory>
12 #include <boost/range/algorithm.hpp>
13 #include "src/mc/api.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_VisitedState, mc, "Logging specific to state equality detection mechanisms");
16
17 using api = simgrid::mc::Api;
18
19 namespace simgrid {
20 namespace mc {
21
22 /** @brief Save the current state */
23 VisitedState::VisitedState(unsigned long state_number) : num(state_number)
24 {
25   this->heap_bytes_used = api::get().get_remote_heap_bytes();
26   this->actors_count    = api::get().get_actors().size();
27   this->system_state = std::make_shared<simgrid::mc::Snapshot>(state_number);
28 }
29
30 void VisitedStates::prune()
31 {
32   while (states_.size() > (std::size_t)_sg_mc_max_visited_states) {
33     XBT_DEBUG("Try to remove visited state (maximum number of stored states reached)");
34     auto min_element = boost::range::min_element(
35         states_, [](const std::unique_ptr<simgrid::mc::VisitedState>& a,
36                     const std::unique_ptr<simgrid::mc::VisitedState>& b) { return a->num < b->num; });
37     xbt_assert(min_element != states_.end());
38     // and drop it:
39     states_.erase(min_element);
40     XBT_DEBUG("Remove visited state (maximum number of stored states reached)");
41   }
42 }
43
44 /** @brief Checks whether a given state has already been visited by the algorithm. */
45 std::unique_ptr<simgrid::mc::VisitedState>
46 VisitedStates::addVisitedState(unsigned long state_number, simgrid::mc::State* graph_state, bool compare_snapshots)
47 {
48   auto new_state             = std::make_unique<simgrid::mc::VisitedState>(state_number);
49   graph_state->system_state_ = new_state->system_state;
50   XBT_DEBUG("Snapshot %p of visited state %ld (exploration stack state %ld)", new_state->system_state.get(),
51             new_state->num, graph_state->num_);
52
53   auto range =
54       boost::range::equal_range(states_, new_state.get(), api::get().compare_pair());
55
56   if (compare_snapshots)
57     for (auto i = range.first; i != range.second; ++i) {
58       auto& visited_state = *i;
59       if (api::get().snapshot_equal(visited_state->system_state.get(), new_state->system_state.get())) {
60         // The state has been visited:
61
62         std::unique_ptr<simgrid::mc::VisitedState> old_state =
63           std::move(visited_state);
64
65         if (old_state->original_num == -1) // I'm the copy of an original process
66           new_state->original_num = old_state->num;
67         else // I'm the copy of a copy
68           new_state->original_num = old_state->original_num;
69
70         if (dot_output == nullptr)
71           XBT_DEBUG("State %ld already visited ! (equal to state %ld)", new_state->num, old_state->num);
72         else
73           XBT_DEBUG("State %ld already visited ! (equal to state %ld (state %ld in dot_output))", new_state->num,
74                     old_state->num, new_state->original_num);
75
76         /* Replace the old state with the new one (with a bigger num)
77            (when the max number of visited states is reached,  the oldest
78            one is removed according to its number (= with the min number) */
79         XBT_DEBUG("Replace visited state %ld with the new visited state %ld", old_state->num, new_state->num);
80
81         visited_state = std::move(new_state);
82         return old_state;
83       }
84     }
85
86   XBT_DEBUG("Insert new visited state %ld (total : %lu)", new_state->num, (unsigned long)states_.size());
87   states_.insert(range.first, std::move(new_state));
88   this->prune();
89   return nullptr;
90 }
91
92 }
93 }