Logo AND Algorithmique Numérique Distribuée

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