Logo AND Algorithmique Numérique Distribuée

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