1 /* Copyright (c) 2011-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
14 #include <xbt/sysdep.h>
15 #include <xbt/dynar.h>
16 #include <xbt/dynar.hpp>
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"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_visited, mc,
26 "Logging specific to state equaity detection mechanisms");
31 std::vector<std::unique_ptr<simgrid::mc::VisitedState>> visited_states;
34 * \brief Save the current state
35 * \return Snapshot of the current state.
37 VisitedState::VisitedState()
39 simgrid::mc::Process* process = &(mc_model_checker->process());
40 this->heap_bytes_used = mmalloc_get_bytes_used_remote(
41 process->get_heap()->heaplimit,
42 process->get_malloc_info());
45 mc_model_checker->process().simix_processes().size();
47 this->system_state = simgrid::mc::take_snapshot(mc_stats->expanded_states);
48 this->num = mc_stats->expanded_states;
52 VisitedState::~VisitedState()
56 static void prune_visited_states()
58 while (visited_states.size() > (std::size_t) _sg_mc_visited) {
59 XBT_DEBUG("Try to remove visited state (maximum number of stored states reached)");
60 auto min_element = std::min_element(visited_states.begin(), visited_states.end(),
61 [](std::unique_ptr<simgrid::mc::VisitedState>& a, std::unique_ptr<simgrid::mc::VisitedState>& b) {
62 return a->num < b->num;
64 xbt_assert(min_element != visited_states.end());
66 visited_states.erase(min_element);
67 XBT_DEBUG("Remove visited state (maximum number of stored states reached)");
71 static int snapshot_compare(simgrid::mc::VisitedState* state1, simgrid::mc::VisitedState* state2)
73 simgrid::mc::Snapshot* s1 = state1->system_state.get();
74 simgrid::mc::Snapshot* s2 = state2->system_state.get();
75 int num1 = state1->num;
76 int num2 = state2->num;
77 return snapshot_compare(num1, s1, num2, s2);
81 * \brief Checks whether a given state has already been visited by the algorithm.
83 std::unique_ptr<simgrid::mc::VisitedState> is_visited_state(simgrid::mc::State* graph_state, bool compare_snpashots)
85 std::unique_ptr<simgrid::mc::VisitedState> new_state =
86 std::unique_ptr<simgrid::mc::VisitedState>(new VisitedState());
87 graph_state->system_state = new_state->system_state;
88 graph_state->in_visited_states = 1;
89 XBT_DEBUG("Snapshot %p of visited state %d (exploration stack state %d)",
90 new_state->system_state.get(), new_state->num, graph_state->num);
92 auto range = std::equal_range(visited_states.begin(), visited_states.end(),
93 new_state.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
95 if (compare_snpashots)
96 for (auto i = range.first; i != range.second; ++i) {
97 auto& visited_state = *i;
98 if (snapshot_compare(visited_state.get(), new_state.get()) == 0) {
99 // The state has been visited:
101 std::unique_ptr<simgrid::mc::VisitedState> old_state =
102 std::move(visited_state);
104 if (old_state->other_num == -1)
105 new_state->other_num = old_state->num;
107 new_state->other_num = old_state->other_num;
109 if (dot_output == nullptr)
110 XBT_DEBUG("State %d already visited ! (equal to state %d)",
111 new_state->num, old_state->num);
114 "State %d already visited ! (equal to state %d (state %d in dot_output))",
115 new_state->num, old_state->num, new_state->other_num);
117 /* Replace the old state with the new one (with a bigger num)
118 (when the max number of visited states is reached, the oldest
119 one is removed according to its number (= with the min number) */
120 XBT_DEBUG("Replace visited state %d with the new visited state %d",
121 old_state->num, new_state->num);
123 visited_state = std::move(new_state);
124 return std::move(old_state);
128 XBT_DEBUG("Insert new visited state %d (total : %lu)", new_state->num, (unsigned long) visited_states.size());
129 visited_states.insert(range.first, std::move(new_state));
130 prune_visited_states();