Logo AND Algorithmique Numérique Distribuée

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