Logo AND Algorithmique Numérique Distribuée

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