Logo AND Algorithmique Numérique Distribuée

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