Logo AND Algorithmique Numérique Distribuée

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