Logo AND Algorithmique Numérique Distribuée

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