Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Useless lower-case alias.
[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
17 namespace simgrid {
18 namespace mc {
19
20 long State::expended_states_ = 0;
21
22 State::State() : num_(++expended_states_)
23 {
24   const unsigned long maxpid = Api::get().get_maxpid();
25   actor_states_.resize(maxpid);
26   transition_.reset(new Transition());
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   }
32 }
33
34 std::size_t State::count_todo() const
35 {
36   return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
37 }
38
39 Transition* State::get_transition() const
40 {
41   return transition_.get();
42 }
43
44 int State::next_transition() const
45 {
46   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
47   XBT_DEBUG("Search for an actor to run. %zu actors to consider", actors.size());
48   for (unsigned int i = 0; i < actors.size(); i++) {
49     aid_t aid                     = actors[i].copy.get_buffer()->get_pid();
50     const ActorState* actor_state = &actor_states_[aid];
51
52     /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
53     if (not actor_state->is_todo() || not simgrid::mc::session_singleton->actor_is_enabled(aid))
54       continue;
55
56     return i;
57   }
58   return -1;
59 }
60 void State::execute_next(int next)
61 {
62   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
63
64   const kernel::actor::ActorImpl* actor = actors[next].copy.get_buffer();
65   aid_t aid                       = actor->get_pid();
66   int times_considered;
67
68   simgrid::mc::ActorState* actor_state = &actor_states_[aid];
69   /* This actor is ready to be executed. Prepare its execution when simcall_handle will be called on it */
70   times_considered = actor_state->get_times_considered_and_inc();
71   if (actor->simcall_.mc_max_consider_ <= actor_state->get_times_considered())
72     actor_state->set_done();
73
74   XBT_DEBUG("Let's run actor %ld (times_considered = %d)", aid, times_considered);
75
76   Transition::executed_transitions_++;
77
78   transition_.reset(mc_model_checker->handle_simcall(aid, times_considered, true));
79   mc_model_checker->wait_for_requests();
80 }
81 } // namespace mc
82 } // namespace simgrid