Logo AND Algorithmique Numérique Distribuée

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