Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / VisitedState.cpp
1 /* Copyright (c) 2011-2017. 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 <unistd.h>
7 #include <sys/wait.h>
8
9 #include <memory>
10
11 #include <boost/range/algorithm.hpp>
12
13 #include <xbt/log.h>
14 #include <xbt/sysdep.h>
15
16 #include "src/mc/mc_comm_pattern.h"
17 #include "src/mc/mc_safety.h"
18 #include "src/mc/mc_private.h"
19 #include "src/mc/Process.hpp"
20 #include "src/mc/mc_smx.h"
21 #include "src/mc/VisitedState.hpp"
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_VisitedState, mc, "Logging specific to state equality detection mechanisms");
24
25 namespace simgrid {
26 namespace mc {
27
28 static int snapshot_compare(simgrid::mc::VisitedState* state1, simgrid::mc::VisitedState* state2)
29 {
30   simgrid::mc::Snapshot* s1 = state1->system_state.get();
31   simgrid::mc::Snapshot* s2 = state2->system_state.get();
32   int num1 = state1->num;
33   int num2 = state2->num;
34   return snapshot_compare(num1, s1, num2, s2);
35 }
36
37 /** @brief Save the current state */
38 VisitedState::VisitedState(unsigned long state_number)
39 {
40   simgrid::mc::Process* process = &(mc_model_checker->process());
41   this->heap_bytes_used = mmalloc_get_bytes_used_remote(
42     process->get_heap()->heaplimit,
43     process->get_malloc_info());
44
45   this->actors_count = mc_model_checker->process().actors().size();
46
47   this->system_state = simgrid::mc::take_snapshot(state_number);
48   this->num = state_number;
49   this->original_num = -1;
50 }
51
52 VisitedState::~VisitedState()
53 {
54 }
55
56 void VisitedStates::prune()
57 {
58   while (states_.size() > (std::size_t)_sg_mc_max_visited_states) {
59     XBT_DEBUG("Try to remove visited state (maximum number of stored states reached)");
60     auto min_element = boost::range::min_element(states_,
61       [](std::unique_ptr<simgrid::mc::VisitedState>& a, std::unique_ptr<simgrid::mc::VisitedState>& b) {
62         return a->num < b->num;
63       });
64     xbt_assert(min_element != states_.end());
65     // and drop it:
66     states_.erase(min_element);
67     XBT_DEBUG("Remove visited state (maximum number of stored states reached)");
68   }
69 }
70
71 /** \brief Checks whether a given state has already been visited by the algorithm.
72  */
73 std::unique_ptr<simgrid::mc::VisitedState> VisitedStates::addVisitedState(
74   unsigned long state_number, simgrid::mc::State* graph_state, bool compare_snpashots)
75 {
76   std::unique_ptr<simgrid::mc::VisitedState> new_state =
77     std::unique_ptr<simgrid::mc::VisitedState>(new VisitedState(state_number));
78   graph_state->system_state = new_state->system_state;
79   XBT_DEBUG("Snapshot %p of visited state %d (exploration stack state %d)",
80     new_state->system_state.get(), new_state->num, graph_state->num);
81
82   auto range =
83       boost::range::equal_range(states_, new_state.get(), simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
84
85   if (compare_snpashots)
86     for (auto i = range.first; i != range.second; ++i) {
87       auto& visited_state = *i;
88       if (snapshot_compare(visited_state.get(), new_state.get()) == 0) {
89         // The state has been visited:
90
91         std::unique_ptr<simgrid::mc::VisitedState> old_state =
92           std::move(visited_state);
93
94         if (old_state->original_num == -1) // I'm the copy of an original process
95           new_state->original_num = old_state->num;
96         else // I'm the copy of a copy
97           new_state->original_num = old_state->original_num;
98
99         if (dot_output == nullptr)
100           XBT_DEBUG("State %d already visited ! (equal to state %d)",
101                     new_state->num, old_state->num);
102         else
103           XBT_DEBUG("State %d already visited ! (equal to state %d (state %d in dot_output))",
104                     new_state->num, old_state->num, new_state->original_num);
105
106         /* Replace the old state with the new one (with a bigger num)
107            (when the max number of visited states is reached,  the oldest
108            one is removed according to its number (= with the min number) */
109         XBT_DEBUG("Replace visited state %d with the new visited state %d",
110           old_state->num, new_state->num);
111
112         visited_state = std::move(new_state);
113         return old_state;
114       }
115     }
116
117   XBT_DEBUG("Insert new visited state %d (total : %lu)", new_state->num, (unsigned long) states_.size());
118   states_.insert(range.first, std::move(new_state));
119   this->prune();
120   return nullptr;
121 }
122
123 }
124 }