Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename mc::CheckerAlgorithm to mc::ExplorationAlgorithm, because that's what it is
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-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 "api.hpp"
7
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/explo/Exploration.hpp"
13 #include "src/mc/mc_base.hpp"
14 #include "src/mc/mc_exit.hpp"
15 #include "src/mc/mc_pattern.hpp"
16 #include "src/mc/mc_private.hpp"
17 #include "src/mc/remote/RemoteProcess.hpp"
18 #include "src/surf/HostImpl.hpp"
19
20 #include <xbt/asserts.h>
21 #include <xbt/log.h>
22 #include "simgrid/s4u/Host.hpp"
23 #include "xbt/string.hpp"
24 #if HAVE_SMPI
25 #include "src/smpi/include/smpi_request.hpp"
26 #endif
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30
31 namespace simgrid {
32 namespace mc {
33
34 simgrid::mc::Exploration* Api::initialize(char** argv, simgrid::mc::ExplorationAlgorithm algo)
35 {
36   session_ = std::make_unique<simgrid::mc::Session>([argv] {
37     int i = 1;
38     while (argv[i] != nullptr && argv[i][0] == '-')
39       i++;
40     xbt_assert(argv[i] != nullptr,
41                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
42     execvp(argv[i], argv + i);
43     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
44   });
45
46   simgrid::mc::Exploration* explo;
47   switch (algo) {
48     case ExplorationAlgorithm::CommDeterminism:
49       explo = simgrid::mc::create_communication_determinism_checker(session_.get());
50       break;
51
52     case ExplorationAlgorithm::UDPOR:
53       explo = simgrid::mc::create_udpor_checker(session_.get());
54       break;
55
56     case ExplorationAlgorithm::Safety:
57       explo = simgrid::mc::create_safety_checker(session_.get());
58       break;
59
60     case ExplorationAlgorithm::Liveness:
61       explo = simgrid::mc::create_liveness_checker(session_.get());
62       break;
63
64     default:
65       THROW_IMPOSSIBLE;
66   }
67
68   mc_model_checker->set_exploration(explo);
69   return explo;
70 }
71
72 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
73 {
74   return mc_model_checker->get_remote_process().actors();
75 }
76
77 unsigned long Api::get_maxpid() const
78 {
79   return mc_model_checker->get_remote_process().get_maxpid();
80 }
81
82 std::size_t Api::get_remote_heap_bytes() const
83 {
84   RemoteProcess& process    = mc_model_checker->get_remote_process();
85   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
86   return heap_bytes_used;
87 }
88
89 void Api::mc_inc_visited_states() const
90 {
91   mc_model_checker->visited_states++;
92 }
93
94 unsigned long Api::mc_get_visited_states() const
95 {
96   return mc_model_checker->visited_states;
97 }
98
99 void Api::mc_exit(int status) const
100 {
101   mc_model_checker->exit(status);
102 }
103
104 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
105 {
106   system_state->restore(&mc_model_checker->get_remote_process());
107 }
108
109 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
110 {
111   return simgrid::mc::snapshot_equal(s1, s2);
112 }
113
114 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
115 {
116   auto snapshot = new simgrid::mc::Snapshot(num_state);
117   return snapshot;
118 }
119
120 void Api::s_close()
121 {
122   session_.reset();
123   if (simgrid::mc::property_automaton != nullptr) {
124     xbt_automaton_free(simgrid::mc::property_automaton);
125     simgrid::mc::property_automaton = nullptr;
126   }
127 }
128
129 void Api::automaton_load(const char* file) const
130 {
131   if (simgrid::mc::property_automaton == nullptr)
132     simgrid::mc::property_automaton = xbt_automaton_new();
133
134   xbt_automaton_load(simgrid::mc::property_automaton, file);
135 }
136
137 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
138 {
139   unsigned int cursor = 0;
140   std::vector<int> values;
141   xbt_automaton_propositional_symbol_t ps = nullptr;
142   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
143     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
144   return values;
145 }
146
147 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
148 {
149   std::vector<xbt_automaton_state_t> automaton_stack;
150   unsigned int cursor = 0;
151   xbt_automaton_state_t automaton_state;
152   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
153     if (automaton_state->type == -1)
154       automaton_stack.push_back(automaton_state);
155   return automaton_stack;
156 }
157
158 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
159 {
160   unsigned int cursor                    = 0;
161   xbt_automaton_propositional_symbol_t p = nullptr;
162   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
163     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
164       return cursor;
165   }
166   return -1;
167 }
168
169 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
170 {
171   mc::property_automaton->current_state = automaton_state;
172 }
173
174 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
175 {
176   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
177   return transition->label;
178 }
179
180 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
181 {
182   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
183   return transition->dst;
184 }
185
186 } // namespace mc
187 } // namespace simgrid