Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
For sonar...
[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::mc {
32
33 simgrid::mc::Exploration* Api::initialize(char** argv, const std::unordered_map<std::string, std::string>& env,
34                                           simgrid::mc::ExplorationAlgorithm algo)
35 {
36   session_ = std::make_unique<simgrid::mc::Session>([argv, &env] {
37     int i = 1;
38     while (argv[i] != nullptr && argv[i][0] == '-')
39       i++;
40     for (auto const& [key, val] : env) {
41       XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
42       setenv(key.c_str(), val.c_str(), 1);
43     }
44     xbt_assert(argv[i] != nullptr,
45                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
46     execvp(argv[i], argv + i);
47     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
48   });
49
50   simgrid::mc::Exploration* explo;
51   switch (algo) {
52     case ExplorationAlgorithm::CommDeterminism:
53       explo = simgrid::mc::create_communication_determinism_checker(session_.get());
54       break;
55
56     case ExplorationAlgorithm::UDPOR:
57       explo = simgrid::mc::create_udpor_checker(session_.get());
58       break;
59
60     case ExplorationAlgorithm::Safety:
61       explo = simgrid::mc::create_dfs_exploration(session_.get());
62       break;
63
64     case ExplorationAlgorithm::Liveness:
65       explo = simgrid::mc::create_liveness_checker(session_.get());
66       break;
67
68     default:
69       THROW_IMPOSSIBLE;
70   }
71
72   mc_model_checker->set_exploration(explo);
73   return explo;
74 }
75
76 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
77 {
78   return mc_model_checker->get_remote_process().actors();
79 }
80
81 unsigned long Api::get_maxpid() const
82 {
83   return mc_model_checker->get_remote_process().get_maxpid();
84 }
85
86 std::size_t Api::get_remote_heap_bytes() const
87 {
88   RemoteProcess& process    = mc_model_checker->get_remote_process();
89   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
90   return heap_bytes_used;
91 }
92
93 void Api::mc_inc_visited_states() const
94 {
95   mc_model_checker->inc_visited_states();
96 }
97
98 unsigned long Api::mc_get_visited_states() const
99 {
100   return mc_model_checker->get_visited_states();
101 }
102
103 void Api::mc_exit(int status) const
104 {
105   mc_model_checker->exit(status);
106 }
107
108 void Api::restore_state(const simgrid::mc::Snapshot* system_state) const
109 {
110   system_state->restore(&mc_model_checker->get_remote_process());
111 }
112
113 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
114 {
115   return simgrid::mc::snapshot_equal(s1, s2);
116 }
117
118 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
119 {
120   auto snapshot = new simgrid::mc::Snapshot(num_state);
121   return snapshot;
122 }
123
124 void Api::s_close()
125 {
126   session_.reset();
127   if (simgrid::mc::property_automaton != nullptr) {
128     xbt_automaton_free(simgrid::mc::property_automaton);
129     simgrid::mc::property_automaton = nullptr;
130   }
131 }
132
133 void Api::automaton_load(const char* file) const
134 {
135   if (simgrid::mc::property_automaton == nullptr)
136     simgrid::mc::property_automaton = xbt_automaton_new();
137
138   xbt_automaton_load(simgrid::mc::property_automaton, file);
139 }
140
141 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
142 {
143   unsigned int cursor = 0;
144   std::vector<int> values;
145   xbt_automaton_propositional_symbol_t ps = nullptr;
146   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
147     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
148   return values;
149 }
150
151 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
152 {
153   std::vector<xbt_automaton_state_t> automaton_stack;
154   unsigned int cursor = 0;
155   xbt_automaton_state_t automaton_state;
156   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
157     if (automaton_state->type == -1)
158       automaton_stack.push_back(automaton_state);
159   return automaton_stack;
160 }
161
162 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
163 {
164   unsigned int cursor                    = 0;
165   xbt_automaton_propositional_symbol_t p = nullptr;
166   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
167     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
168       return cursor;
169   }
170   return -1;
171 }
172
173 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
174 {
175   mc::property_automaton->current_state = automaton_state;
176 }
177
178 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
179 {
180   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
181   return transition->label;
182 }
183
184 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
185 {
186   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
187   return transition->dst;
188 }
189
190 } // namespace simgrid::mc