Logo AND Algorithmique Numérique Distribuée

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