Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the selection of the next transition to execute to mc::State
[simgrid.git] / src / mc / mc_state.cpp
1 /* Copyright (c) 2008-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/mc/mc_state.hpp"
7 #include "src/mc/Session.hpp"
8 #include "src/mc/api.hpp"
9 #include "src/mc/mc_config.hpp"
10
11 #include <boost/range/algorithm.hpp>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC states");
14
15 using simgrid::mc::remote;
16 using api = simgrid::mc::Api;
17
18 namespace simgrid {
19 namespace mc {
20
21 State::State(unsigned long state_number) : num_(state_number)
22 {
23   const unsigned long maxpid = api::get().get_maxpid();
24   actor_states_.resize(maxpid);
25   /* Stateful model checking */
26   if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
27     auto snapshot_ptr = api::get().take_snapshot(num_);
28     system_state_ = std::shared_ptr<simgrid::mc::Snapshot>(snapshot_ptr);
29     if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
30       copy_incomplete_comm_pattern();
31       copy_index_comm_pattern();
32     }
33   }
34 }
35
36 std::size_t State::count_todo() const
37 {
38   return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
39 }
40
41 Transition State::get_transition() const
42 {
43   return this->transition_;
44 }
45
46 int State::next_transition() const
47 {
48   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
49   XBT_DEBUG("Search for an actor to run. %zu actors to consider", actors.size());
50   for (unsigned int i = 0; i < actors.size(); i++) {
51     aid_t aid                     = actors[i].copy.get_buffer()->get_pid();
52     const ActorState* actor_state = &actor_states_[aid];
53
54     /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
55     if (not actor_state->is_todo() || not simgrid::mc::session_singleton->actor_is_enabled(aid))
56       continue;
57
58     return i;
59   }
60   return -1;
61 }
62
63 void State::copy_incomplete_comm_pattern()
64 {
65   incomplete_comm_pattern_.clear();
66   const unsigned long maxpid = api::get().get_maxpid();
67   for (unsigned long i = 0; i < maxpid; i++) {
68     std::vector<simgrid::mc::PatternCommunication> res;
69     for (auto const& comm : incomplete_communications_pattern[i])
70       res.push_back(comm->dup());
71     incomplete_comm_pattern_.push_back(std::move(res));
72   }
73 }
74
75 void State::copy_index_comm_pattern()
76 {
77   communication_indices_.clear();
78   for (auto const& list_process_comm : initial_communications_pattern)
79     this->communication_indices_.push_back(list_process_comm.index_comm);
80 }
81
82 }
83 }