1 /* Copyright (c) 2011-2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/mc/VisitedState.hpp"
7 #include "src/mc/mc_private.hpp"
12 #include <boost/range/algorithm.hpp>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_VisitedState, mc, "Logging specific to state equality detection mechanisms");
19 static int snapshot_compare(simgrid::mc::VisitedState* state1, simgrid::mc::VisitedState* state2)
21 simgrid::mc::Snapshot* s1 = state1->system_state.get();
22 simgrid::mc::Snapshot* s2 = state2->system_state.get();
23 return snapshot_compare(s1, s2);
26 /** @brief Save the current state */
27 VisitedState::VisitedState(unsigned long state_number) : num(state_number)
29 simgrid::mc::RemoteClient* process = &(mc_model_checker->process());
30 this->heap_bytes_used = mmalloc_get_bytes_used_remote(
31 process->get_heap()->heaplimit,
32 process->get_malloc_info());
34 this->actors_count = mc_model_checker->process().actors().size();
36 this->system_state = simgrid::mc::take_snapshot(state_number);
37 this->original_num = -1;
40 void VisitedStates::prune()
42 while (states_.size() > (std::size_t)_sg_mc_max_visited_states) {
43 XBT_DEBUG("Try to remove visited state (maximum number of stored states reached)");
44 auto min_element = boost::range::min_element(states_,
45 [](std::unique_ptr<simgrid::mc::VisitedState>& a, std::unique_ptr<simgrid::mc::VisitedState>& b) {
46 return a->num < b->num;
48 xbt_assert(min_element != states_.end());
50 states_.erase(min_element);
51 XBT_DEBUG("Remove visited state (maximum number of stored states reached)");
55 /** @brief Checks whether a given state has already been visited by the algorithm. */
56 std::unique_ptr<simgrid::mc::VisitedState> VisitedStates::addVisitedState(
57 unsigned long state_number, simgrid::mc::State* graph_state, bool compare_snpashots)
59 std::unique_ptr<simgrid::mc::VisitedState> new_state =
60 std::unique_ptr<simgrid::mc::VisitedState>(new VisitedState(state_number));
61 graph_state->system_state = new_state->system_state;
62 XBT_DEBUG("Snapshot %p of visited state %d (exploration stack state %d)",
63 new_state->system_state.get(), new_state->num, graph_state->num);
66 boost::range::equal_range(states_, new_state.get(), simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
68 if (compare_snpashots)
69 for (auto i = range.first; i != range.second; ++i) {
70 auto& visited_state = *i;
71 if (snapshot_compare(visited_state.get(), new_state.get()) == 0) {
72 // The state has been visited:
74 std::unique_ptr<simgrid::mc::VisitedState> old_state =
75 std::move(visited_state);
77 if (old_state->original_num == -1) // I'm the copy of an original process
78 new_state->original_num = old_state->num;
79 else // I'm the copy of a copy
80 new_state->original_num = old_state->original_num;
82 if (dot_output == nullptr)
83 XBT_DEBUG("State %d already visited ! (equal to state %d)",
84 new_state->num, old_state->num);
86 XBT_DEBUG("State %d already visited ! (equal to state %d (state %d in dot_output))",
87 new_state->num, old_state->num, new_state->original_num);
89 /* Replace the old state with the new one (with a bigger num)
90 (when the max number of visited states is reached, the oldest
91 one is removed according to its number (= with the min number) */
92 XBT_DEBUG("Replace visited state %d with the new visited state %d",
93 old_state->num, new_state->num);
95 visited_state = std::move(new_state);
100 XBT_DEBUG("Insert new visited state %d (total : %lu)", new_state->num, (unsigned long) states_.size());
101 states_.insert(range.first, std::move(new_state));