Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix null pointer dereference.
[simgrid.git] / src / simix / libsmx.cpp
1 /* libsmx.c - public interface to simix                                       */
2 /* --------                                                                   */
3 /* These functions are the only ones that are visible from the higher levels  */
4 /* (most of them simply add some documentation to the generated simcall body) */
5 /*                                                                            */
6 /* This is somehow the "libc" of SimGrid                                      */
7
8 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "mc/mc.h"
14 #include "simgrid/simix/blocking_simcall.hpp"
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/activity/ConditionVariableImpl.hpp"
17 #include "src/kernel/activity/ExecImpl.hpp"
18 #include "src/kernel/activity/IoImpl.hpp"
19 #include "src/kernel/activity/MailboxImpl.hpp"
20 #include "src/kernel/activity/MutexImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/plugins/vm/VirtualMachineImpl.hpp"
23
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
25
26 #include "popping_bodies.cpp"
27
28 /**
29  * @ingroup simix_host_management
30  * @brief Waits for the completion of an execution synchro and destroy it.
31  *
32  * @param execution The execution synchro
33  */
34 e_smx_state_t simcall_execution_wait(const smx_activity_t& execution)
35 {
36   return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
37 }
38
39 e_smx_state_t simcall_execution_test(const smx_activity_t& execution)
40 {
41   return (e_smx_state_t)simcall_BODY_execution_test(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
42 }
43
44 void simcall_process_join(smx_actor_t process, double timeout)
45 {
46   simcall_BODY_process_join(process, timeout);
47 }
48
49 /**
50  * @ingroup simix_process_management
51  * @brief Suspends an actor
52  */
53 void simcall_process_suspend(smx_actor_t process)
54 {
55   simcall_BODY_process_suspend(process);
56 }
57
58 /**
59  * @ingroup simix_process_management
60  * @brief Creates a new sleep SIMIX synchro.
61  *
62  * This function creates a SURF action and allocates the data necessary
63  * to create the SIMIX synchro. It can raise a HostFailureException if the
64  * host crashed. The default SIMIX name of the synchro is "sleep".
65  *
66  *   @param duration Time duration of the sleep.
67  *   @return A result telling whether the sleep was successful
68  */
69 e_smx_state_t simcall_process_sleep(double duration)
70 {
71   /* checking for infinite values */
72   xbt_assert(std::isfinite(duration), "duration is not finite!");
73   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
74 }
75
76 /**
77  * @ingroup simix_comm_management
78  */
79 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
80                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
81                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
82                        double timeout)
83 {
84   /* checking for infinite values */
85   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
86   xbt_assert(std::isfinite(rate), "rate is not finite!");
87   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
88
89   xbt_assert(mbox, "No rendez-vous point defined for send");
90
91   if (MC_is_active() || MC_record_replay_is_active()) {
92     /* the model-checker wants two separate simcalls */
93     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
94     comm = simcall_comm_isend(sender, mbox, task_size, rate,
95         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
96     simcall_comm_wait(comm, timeout);
97     comm = nullptr;
98   }
99   else {
100     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
101                          match_fun, copy_data_fun, data, timeout);
102   }
103 }
104
105 /**
106  * @ingroup simix_comm_management
107  */
108 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
109                                   size_t src_buff_size,
110                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
111                                   void (*clean_fun)(void*),
112                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
113                                   void* data, int detached)
114 {
115   /* checking for infinite values */
116   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
117   xbt_assert(std::isfinite(rate), "rate is not finite!");
118
119   xbt_assert(mbox, "No rendez-vous point defined for isend");
120
121   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
122                                  src_buff_size, match_fun,
123                                  clean_fun, copy_data_fun, data, detached);
124 }
125
126 /**
127  * @ingroup simix_comm_management
128  */
129 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
130                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
131                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
132                        double timeout, double rate)
133 {
134   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
135   xbt_assert(mbox, "No rendez-vous point defined for recv");
136
137   if (MC_is_active() || MC_record_replay_is_active()) {
138     /* the model-checker wants two separate simcalls */
139     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
140     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
141                               match_fun, copy_data_fun, data, rate);
142     simcall_comm_wait(comm, timeout);
143     comm = nullptr;
144   }
145   else {
146     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
147                            match_fun, copy_data_fun, data, timeout, rate);
148   }
149 }
150 /**
151  * @ingroup simix_comm_management
152  */
153 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
154                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
155                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
156                                   void* data, double rate)
157 {
158   xbt_assert(mbox, "No rendez-vous point defined for irecv");
159
160   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
161                                  match_fun, copy_data_fun, data, rate);
162 }
163
164 /**
165  * @ingroup simix_comm_management
166  */
167 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
168                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
169 {
170   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
171
172   return simgrid::simix::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
173 }
174
175 /**
176  * @ingroup simix_comm_management
177  */
178 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
179 {
180   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
181   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
182     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
183   });
184   return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
185 }
186
187 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
188 {
189   return simcall_BODY_comm_waitany(comms, count, timeout);
190 }
191
192 /**
193  * @ingroup simix_comm_management
194  */
195 int simcall_comm_testany(smx_activity_t comms[], size_t count)
196 {
197   if (count == 0)
198     return -1;
199   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
200   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
201     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
202   });
203   return simcall_BODY_comm_testany(rcomms.get(), count);
204 }
205
206 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
207 {
208   if (count == 0)
209     return -1;
210   return simcall_BODY_comm_testany(comms, count);
211 }
212
213 /**
214  * @ingroup simix_comm_management
215  */
216 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
217 {
218   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
219   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
220 }
221
222 /**
223  * @ingroup simix_comm_management
224  *
225  */
226 int simcall_comm_test(const smx_activity_t& comm)
227 {
228   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
229 }
230
231 /**
232  * @ingroup simix_synchro_management
233  *
234  */
235 smx_mutex_t simcall_mutex_init()
236 {
237   if (simix_global == nullptr) {
238     fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
239                                                                                  // get there before the initialization
240     xbt_abort();
241   }
242   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
243 }
244
245 /**
246  * @ingroup simix_synchro_management
247  *
248  */
249 void simcall_mutex_lock(smx_mutex_t mutex)
250 {
251   simcall_BODY_mutex_lock(mutex);
252 }
253
254 /**
255  * @ingroup simix_synchro_management
256  *
257  */
258 int simcall_mutex_trylock(smx_mutex_t mutex)
259 {
260   return simcall_BODY_mutex_trylock(mutex);
261 }
262
263 /**
264  * @ingroup simix_synchro_management
265  *
266  */
267 void simcall_mutex_unlock(smx_mutex_t mutex)
268 {
269   simcall_BODY_mutex_unlock(mutex);
270 }
271
272 /**
273  * @ingroup simix_synchro_management
274  *
275  */
276 smx_cond_t simcall_cond_init()
277 {
278   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
279 }
280
281 /**
282  * @ingroup simix_synchro_management
283  *
284  */
285 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
286 {
287   simcall_BODY_cond_wait(cond, mutex);
288 }
289
290 /**
291  * @ingroup simix_synchro_management
292  *
293  */
294 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
295 {
296   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
297   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
298 }
299
300 /**
301  * @ingroup simix_synchro_management
302  *
303  */
304 void simcall_sem_acquire(smx_sem_t sem)
305 {
306   simcall_BODY_sem_acquire(sem);
307 }
308
309 /**
310  * @ingroup simix_synchro_management
311  *
312  */
313 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
314 {
315   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
316   return simcall_BODY_sem_acquire_timeout(sem, timeout);
317 }
318
319 e_smx_state_t simcall_io_wait(const smx_activity_t& io)
320 {
321   return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
322 }
323
324 void simcall_run_kernel(std::function<void()> const& code)
325 {
326   simcall_BODY_run_kernel(&code);
327 }
328
329 void simcall_run_blocking(std::function<void()> const& code)
330 {
331   simcall_BODY_run_blocking(&code);
332 }
333
334 int simcall_mc_random(int min, int max) {
335   return simcall_BODY_mc_random(min, max);
336 }
337
338 /* ************************************************************************** */
339
340 /** @brief returns a printable string representing a simcall */
341 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
342   return simcall_names[kind];
343 }
344
345 namespace simgrid {
346 namespace simix {
347
348 void unblock(smx_actor_t process)
349 {
350   xbt_assert(SIMIX_is_maestro());
351   SIMIX_simcall_answer(&process->simcall);
352 }
353 } // namespace simix
354 } // namespace simgrid
355
356 /* ****************************DEPRECATED CALLS******************************* */
357 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
358 {
359   simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
360 }
361 void simcall_comm_cancel(smx_activity_t comm)
362 {
363   simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
364 }
365 void simcall_execution_cancel(smx_activity_t exec)
366 {
367   simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
368 }
369 void simcall_execution_set_priority(smx_activity_t exec, double priority)
370 {
371   simgrid::simix::simcall([exec, priority] {
372     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_priority(priority);
373   });
374 }
375
376 void simcall_execution_set_bound(smx_activity_t exec, double bound)
377 {
378   simgrid::simix::simcall(
379       [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
380 }
381
382 // deprecated
383 smx_activity_t simcall_execution_start(const std::string& name, const std::string& category, double flops_amount,
384                                        double priority, double bound, sg_host_t host)
385 {
386   return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
387     return simgrid::kernel::activity::ExecImplPtr(
388                new simgrid::kernel::activity::ExecImpl(std::move(name), std::move(category)))
389         ->set_host(host)
390         ->start(flops_amount, priority, bound);
391   });
392 }
393
394 // deprecated
395 smx_activity_t simcall_execution_parallel_start(const std::string& name, int host_nb, const sg_host_t* host_list,
396                                                 const double* flops_amount, const double* bytes_amount, double rate,
397                                                 double timeout)
398 {
399   /* Check that we are not mixing VMs and PMs in the parallel task */
400   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
401   for (int i = 1; i < host_nb; i++) {
402     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
403     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
404   }
405
406   /* checking for infinite values */
407   for (int i = 0; i < host_nb; ++i) {
408     if (flops_amount != nullptr)
409       xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
410     if (bytes_amount != nullptr) {
411       for (int j = 0; j < host_nb; ++j) {
412         xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]), "bytes_amount[%d+%d*%d] is not finite!", i, host_nb,
413                    j);
414       }
415     }
416   }
417   xbt_assert(std::isfinite(rate), "rate is not finite!");
418
419   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
420   std::vector<double> flops_parallel_amount;
421   std::vector<double> bytes_parallel_amount;
422   if (flops_amount != nullptr)
423     flops_parallel_amount = std::vector<double>(flops_amount, flops_amount + host_nb);
424   if (bytes_amount != nullptr)
425     bytes_parallel_amount = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
426   return simgrid::simix::simcall([name, hosts, flops_parallel_amount, bytes_parallel_amount, timeout] {
427     return simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(std::move(name), ""))
428         ->set_timeout(timeout)
429         ->start(hosts, flops_parallel_amount, bytes_parallel_amount);
430   });
431 }
432
433 // deprecated
434 void SIMIX_comm_finish(smx_activity_t synchro)
435 {
436   boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
437 }