Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f0fbc1ce536e79990ac3bc2a41b6136fe0fe68ea
[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 long State::expended_states_ = 0;
22
23 State::State() : num_(++expended_states_)
24 {
25   const unsigned long maxpid = api::get().get_maxpid();
26   actor_states_.resize(maxpid);
27   /* Stateful model checking */
28   if ((_sg_mc_checkpoint > 0 && (num_ % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
29     auto snapshot_ptr = api::get().take_snapshot(num_);
30     system_state_ = std::shared_ptr<simgrid::mc::Snapshot>(snapshot_ptr);
31     if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
32       copy_incomplete_comm_pattern();
33       copy_index_comm_pattern();
34     }
35   }
36 }
37
38 std::size_t State::count_todo() const
39 {
40   return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
41 }
42
43 Transition* State::get_transition() const
44 {
45   return const_cast<Transition*>(&this->transition_);
46 }
47
48 int State::next_transition() const
49 {
50   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
51   XBT_DEBUG("Search for an actor to run. %zu actors to consider", actors.size());
52   for (unsigned int i = 0; i < actors.size(); i++) {
53     aid_t aid                     = actors[i].copy.get_buffer()->get_pid();
54     const ActorState* actor_state = &actor_states_[aid];
55
56     /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
57     if (not actor_state->is_todo() || not simgrid::mc::session_singleton->actor_is_enabled(aid))
58       continue;
59
60     return i;
61   }
62   return -1;
63 }
64 RemotePtr<simgrid::kernel::actor::SimcallObserver> State::execute_next(int next)
65 {
66   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
67
68   kernel::actor::ActorImpl* actor = actors[next].copy.get_buffer();
69   aid_t aid                       = actor->get_pid();
70   int times_considered;
71
72   simgrid::mc::ActorState* actor_state = &actor_states_[aid];
73   /* This actor is ready to be executed. Prepare its execution when simcall_handle will be called on it */
74   if (actor->simcall_.observer_ != nullptr) {
75     times_considered = actor_state->get_times_considered_and_inc();
76     if (actor->simcall_.mc_max_consider_ <= actor_state->get_times_considered())
77       actor_state->set_done();
78   } else {
79     times_considered = 0;
80     actor_state->set_done();
81   }
82
83   transition_.init(aid, times_considered);
84   executed_req_ = actor->simcall_;
85
86   XBT_DEBUG("Let's run actor %ld, going for transition %s", aid, transition_.to_cstring());
87
88   return transition_.replay();
89 }
90
91 void State::copy_incomplete_comm_pattern()
92 {
93   incomplete_comm_pattern_.clear();
94   const unsigned long maxpid = api::get().get_maxpid();
95   for (unsigned long i = 0; i < maxpid; i++) {
96     std::vector<simgrid::mc::PatternCommunication> res;
97     for (auto const& comm : incomplete_communications_pattern[i])
98       res.push_back(comm->dup());
99     incomplete_comm_pattern_.push_back(std::move(res));
100   }
101 }
102
103 void State::copy_index_comm_pattern()
104 {
105   communication_indices_.clear();
106   for (auto const& list_process_comm : initial_communications_pattern)
107     this->communication_indices_.push_back(list_process_comm.index_comm);
108 }
109
110 }
111 }