Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
68d7517f36bab6579b690e4f7569081af537b576
[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/api.hpp"
8 #include "src/mc/mc_config.hpp"
9
10 #include <boost/range/algorithm.hpp>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC states");
13
14 namespace simgrid {
15 namespace mc {
16
17 long State::expended_states_ = 0;
18
19 State::State() : num_(++expended_states_)
20 {
21   const unsigned long maxpid = Api::get().get_maxpid();
22   actor_states_.resize(maxpid);
23   transition_.reset(new Transition());
24   /* Stateful model checking */
25   if ((_sg_mc_checkpoint > 0 && (num_ % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
26     auto snapshot_ptr = Api::get().take_snapshot(num_);
27     system_state_     = std::shared_ptr<simgrid::mc::Snapshot>(snapshot_ptr);
28   }
29 }
30
31 std::size_t State::count_todo() const
32 {
33   return boost::range::count_if(this->actor_states_, [](simgrid::mc::ActorState const& a) { return a.is_todo(); });
34 }
35
36 Transition* State::get_transition() const
37 {
38   return transition_.get();
39 }
40
41 int State::next_transition() const
42 {
43   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
44   XBT_DEBUG("Search for an actor to run. %zu actors to consider", actors.size());
45   for (unsigned int i = 0; i < actors.size(); i++) {
46     /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
47     if (aid_t aid = actors[i].copy.get_buffer()->get_pid();
48         not actor_states_[aid].is_todo() || not Api::get().get_session().actor_is_enabled(aid))
49       continue;
50
51     return i;
52   }
53   return -1;
54 }
55 void State::execute_next(int next)
56 {
57   std::vector<ActorInformation>& actors = mc_model_checker->get_remote_process().actors();
58
59   const kernel::actor::ActorImpl* actor = actors[next].copy.get_buffer();
60   aid_t aid                       = actor->get_pid();
61   int times_considered;
62
63   simgrid::mc::ActorState* actor_state = &actor_states_[aid];
64   /* This actor is ready to be executed. Prepare its execution when simcall_handle will be called on it */
65   times_considered = actor_state->get_times_considered_and_inc();
66   if (actor->simcall_.mc_max_consider_ <= actor_state->get_times_considered())
67     actor_state->set_done();
68
69   XBT_DEBUG("Let's run actor %ld (times_considered = %d)", aid, times_considered);
70
71   Transition::executed_transitions_++;
72
73   transition_.reset(mc_model_checker->handle_simcall(aid, times_considered, true));
74   mc_model_checker->wait_for_requests();
75 }
76 } // namespace mc
77 } // namespace simgrid