Logo AND Algorithmique Numérique Distribuée

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