Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make mc::State extendable so that CommDet does not polute it in any case
[simgrid.git] / src / mc / api / 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/api/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   transition_.reset(new Transition());
28   /* Stateful model checking */
29   if ((_sg_mc_checkpoint > 0 && (num_ % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
30     auto snapshot_ptr = api::get().take_snapshot(num_);
31     system_state_     = std::shared_ptr<simgrid::mc::Snapshot>(snapshot_ptr);
32   }
33 }
34
35 std::size_t State::count_todo() const
36 {
37   return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
38 }
39
40 Transition* State::get_transition() const
41 {
42   return transition_.get();
43 }
44
45 int State::next_transition() const
46 {
47   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
48   XBT_DEBUG("Search for an actor to run. %zu actors to consider", actors.size());
49   for (unsigned int i = 0; i < actors.size(); i++) {
50     aid_t aid                     = actors[i].copy.get_buffer()->get_pid();
51     const ActorState* actor_state = &actor_states_[aid];
52
53     /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
54     if (not actor_state->is_todo() || not simgrid::mc::session_singleton->actor_is_enabled(aid))
55       continue;
56
57     return i;
58   }
59   return -1;
60 }
61 void State::execute_next(int next)
62 {
63   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
64
65   const kernel::actor::ActorImpl* actor = actors[next].copy.get_buffer();
66   aid_t aid                       = actor->get_pid();
67   int times_considered;
68
69   simgrid::mc::ActorState* actor_state = &actor_states_[aid];
70   /* This actor is ready to be executed. Prepare its execution when simcall_handle will be called on it */
71   if (actor->simcall_.observer_ != nullptr) {
72     times_considered = actor_state->get_times_considered_and_inc();
73     if (actor->simcall_.mc_max_consider_ <= actor_state->get_times_considered())
74       actor_state->set_done();
75   } else {
76     times_considered = 0;
77     actor_state->set_done();
78   }
79
80   executed_req_ = actor->simcall_;
81
82   XBT_DEBUG("Let's run actor %ld (times_considered = %d)", aid, times_considered);
83
84   Transition::executed_transitions_++;
85
86   transition_.reset(mc_model_checker->handle_simcall(aid, times_considered, true));
87   mc_model_checker->wait_for_requests();
88 }
89 } // namespace mc
90 } // namespace simgrid