1 /* Copyright (c) 2011-2021. 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>
13 #include "src/mc/api.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_VisitedState, mc, "Logging specific to state equality detection mechanisms");
17 using api = simgrid::mc::Api;
22 /** @brief Save the current state */
23 VisitedState::VisitedState(unsigned long state_number) : num(state_number)
25 this->heap_bytes_used = api::get().get_remote_heap_bytes();
26 this->actors_count = api::get().get_actors_size();
27 this->system_state = std::make_shared<simgrid::mc::Snapshot>(state_number);
30 void VisitedStates::prune()
32 while (states_.size() > (std::size_t)_sg_mc_max_visited_states) {
33 XBT_DEBUG("Try to remove visited state (maximum number of stored states reached)");
34 auto min_element = boost::range::min_element(
35 states_, [](const std::unique_ptr<simgrid::mc::VisitedState>& a,
36 const std::unique_ptr<simgrid::mc::VisitedState>& b) { return a->num < b->num; });
37 xbt_assert(min_element != states_.end());
39 states_.erase(min_element);
40 XBT_DEBUG("Remove visited state (maximum number of stored states reached)");
44 /** @brief Checks whether a given state has already been visited by the algorithm. */
45 std::unique_ptr<simgrid::mc::VisitedState>
46 VisitedStates::addVisitedState(unsigned long state_number, simgrid::mc::State* graph_state, bool compare_snapshots)
48 auto new_state = std::make_unique<simgrid::mc::VisitedState>(state_number);
49 graph_state->system_state_ = new_state->system_state;
50 XBT_DEBUG("Snapshot %p of visited state %d (exploration stack state %d)", new_state->system_state.get(),
51 new_state->num, graph_state->num_);
54 boost::range::equal_range(states_, new_state.get(), api::get().compare_pair());
56 if (compare_snapshots)
57 for (auto i = range.first; i != range.second; ++i) {
58 auto& visited_state = *i;
59 if (api::get().snapshot_equal(visited_state->system_state.get(), new_state->system_state.get())) {
60 // The state has been visited:
62 std::unique_ptr<simgrid::mc::VisitedState> old_state =
63 std::move(visited_state);
65 if (old_state->original_num == -1) // I'm the copy of an original process
66 new_state->original_num = old_state->num;
67 else // I'm the copy of a copy
68 new_state->original_num = old_state->original_num;
70 if (dot_output == nullptr)
71 XBT_DEBUG("State %d already visited ! (equal to state %d)",
72 new_state->num, old_state->num);
74 XBT_DEBUG("State %d already visited ! (equal to state %d (state %d in dot_output))",
75 new_state->num, old_state->num, new_state->original_num);
77 /* Replace the old state with the new one (with a bigger num)
78 (when the max number of visited states is reached, the oldest
79 one is removed according to its number (= with the min number) */
80 XBT_DEBUG("Replace visited state %d with the new visited state %d",
81 old_state->num, new_state->num);
83 visited_state = std::move(new_state);
88 XBT_DEBUG("Insert new visited state %d (total : %lu)", new_state->num, (unsigned long) states_.size());
89 states_.insert(range.first, std::move(new_state));