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