Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add test() for asynchronous executions
[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-2017. 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 <cmath>         /* std::isfinite() */
14
15 #include <functional>
16
17 #include "mc/mc.h"
18 #include "simgrid/s4u/VirtualMachine.hpp"
19 #include "simgrid/simix.hpp"
20 #include "simgrid/simix/blocking_simcall.hpp"
21 #include "smx_private.hpp"
22 #include "src/kernel/activity/CommImpl.hpp"
23 #include "src/mc/mc_forward.hpp"
24 #include "src/mc/mc_replay.hpp"
25 #include "src/plugins/vm/VirtualMachineImpl.hpp"
26 #include "src/simix/smx_host_private.hpp"
27 #include "xbt/ex.h"
28 #include "xbt/functional.hpp"
29
30 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
31
32 #include "popping_bodies.cpp"
33
34 void simcall_call(smx_actor_t actor)
35 {
36   if (actor != simix_global->maestro_process) {
37     XBT_DEBUG("Yield actor '%s' on simcall %s (%d)", actor->getCname(), SIMIX_simcall_name(actor->simcall.call),
38               (int)actor->simcall.call);
39     SIMIX_process_yield(actor);
40   } else {
41     SIMIX_simcall_handle(&actor->simcall, 0);
42   }
43 }
44
45 /**
46  * \ingroup simix_process_management
47  * \brief Creates a synchro that executes some computation of an host.
48  *
49  * This function creates a SURF action and allocates the data necessary
50  * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
51  *
52  * \param name Name of the execution synchro to create
53  * \param flops_amount amount Computation amount (in flops)
54  * \param priority computation priority
55  * \param bound
56  * \return A new SIMIX execution synchronization
57  */
58 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
59                                        simgrid::s4u::Host* host)
60 {
61   /* checking for infinite values */
62   xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
63   xbt_assert(std::isfinite(priority), "priority is not finite!");
64
65   return simcall_BODY_execution_start(name, flops_amount, priority, bound, host);
66 }
67
68 /**
69  * \ingroup simix_process_management
70  * \brief Creates a synchro that may involve parallel computation on
71  * several hosts and communication between them.
72  *
73  * \param name Name of the execution synchro to create
74  * \param host_nb Number of hosts where the synchro will be executed
75  * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
76  * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
77  * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
78  * amount between each pair of hosts
79  * \param amount the SURF action amount
80  * \param rate the SURF action rate
81  * \param timeout timeout
82  * \return A new SIMIX execution synchronization
83  */
84 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
85                                                 double* flops_amount, double* bytes_amount, double rate, double timeout)
86 {
87   /* checking for infinite values */
88   for (int i = 0 ; i < host_nb ; ++i) {
89     xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
90     if (bytes_amount != nullptr) {
91       for (int j = 0 ; j < host_nb ; ++j) {
92         xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
93                    "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
94       }
95     }
96   }
97
98   xbt_assert(std::isfinite(rate), "rate is not finite!");
99
100   return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
101 }
102
103 /**
104  * \ingroup simix_process_management
105  * \brief Cancels an execution synchro.
106  *
107  * This functions stops the execution. It calls a surf function.
108  * \param execution The execution synchro to cancel
109  */
110 void simcall_execution_cancel(smx_activity_t execution)
111 {
112   simgrid::kernel::activity::ExecImplPtr exec =
113       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
114   if (not exec->surfAction_)
115     return;
116   simgrid::simix::kernelImmediate([exec] {
117     XBT_DEBUG("Cancel synchro %p", exec.get());
118     if (exec->surfAction_)
119       exec->surfAction_->cancel();
120   });
121 }
122
123 /**
124  * \ingroup simix_process_management
125  * \brief Changes the priority of an execution synchro.
126  *
127  * This functions changes the priority only. It calls a surf function.
128  * \param execution The execution synchro
129  * \param priority The new priority
130  */
131 void simcall_execution_set_priority(smx_activity_t execution, double priority)
132 {
133   /* checking for infinite values */
134   xbt_assert(std::isfinite(priority), "priority is not finite!");
135   simgrid::simix::kernelImmediate([execution, priority] {
136
137     simgrid::kernel::activity::ExecImplPtr exec =
138         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
139     if (exec->surfAction_)
140       exec->surfAction_->setSharingWeight(priority);
141   });
142 }
143
144 /**
145  * \ingroup simix_process_management
146  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
147  *
148  * This functions changes the capping only. It calls a surf function.
149  * \param execution The execution synchro
150  * \param bound The new bound
151  */
152 void simcall_execution_set_bound(smx_activity_t execution, double bound)
153 {
154   simgrid::simix::kernelImmediate([execution, bound] {
155     simgrid::kernel::activity::ExecImplPtr exec =
156         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
157     if (exec->surfAction_)
158       exec->surfAction_->setBound(bound);
159   });
160 }
161
162 /**
163  * \ingroup simix_host_management
164  * \brief Waits for the completion of an execution synchro and destroy it.
165  *
166  * \param execution The execution synchro
167  */
168 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
169 {
170   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
171 }
172
173 e_smx_state_t simcall_execution_test(smx_activity_t execution)
174 {
175   return (e_smx_state_t)simcall_BODY_execution_test(execution);
176 }
177
178 /**
179  * \ingroup simix_process_management
180  * \brief Kills all SIMIX processes.
181  */
182 void simcall_process_killall(int reset_pid)
183 {
184   simcall_BODY_process_killall(reset_pid);
185 }
186
187 /**
188  * \ingroup simix_process_management
189  * \brief Cleans up a SIMIX process.
190  * \param process poor victim (must have already been killed)
191  */
192 void simcall_process_cleanup(smx_actor_t process)
193 {
194   simcall_BODY_process_cleanup(process);
195 }
196
197 void simcall_process_join(smx_actor_t process, double timeout)
198 {
199   simcall_BODY_process_join(process, timeout);
200 }
201
202 /**
203  * \ingroup simix_process_management
204  * \brief Suspends a process.
205  *
206  * This function suspends the process by suspending the synchro
207  * it was waiting for completion.
208  *
209  * \param process a SIMIX process
210  */
211 void simcall_process_suspend(smx_actor_t process)
212 {
213   simcall_BODY_process_suspend(process);
214 }
215
216 /**
217  * \ingroup simix_process_management
218  * \brief Returns the amount of SIMIX processes in the system
219  *
220  * Maestro internal process is not counted, only user code processes are
221  */
222 int simcall_process_count()
223 {
224   return simgrid::simix::kernelImmediate(SIMIX_process_count);
225 }
226
227 /**
228  * \ingroup simix_process_management
229  * \brief Set the user data of a #smx_actor_t.
230  *
231  * This functions sets the user data associated to \a process.
232  * \param process SIMIX process
233  * \param data User data
234  */
235 void simcall_process_set_data(smx_actor_t process, void *data)
236 {
237   simgrid::simix::kernelImmediate([process, data] { process->setUserData(data); });
238 }
239
240 /**
241  * \ingroup simix_process_management
242  * \brief Set the kill time of a process.
243  */
244 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
245 {
246
247   if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
248     return;
249   XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->getCname(), process->host->getCname());
250   process->kill_timer = SIMIX_timer_set(kill_time, [process] {
251     simix_global->kill_process_function(process);
252     process->kill_timer=nullptr;
253   });
254 }
255
256 /**
257  * \ingroup simix_process_management
258  * \brief Add an on_exit function
259  * Add an on_exit function which will be executed when the process exits/is killed.
260  */
261 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
262 {
263   simcall_BODY_process_on_exit(process, fun, data);
264 }
265
266 /**
267  * \ingroup simix_process_management
268  * \brief Creates a new sleep SIMIX synchro.
269  *
270  * This function creates a SURF action and allocates the data necessary
271  * to create the SIMIX synchro. It can raise a host_error exception if the
272  * host crashed. The default SIMIX name of the synchro is "sleep".
273  *
274  *   \param duration Time duration of the sleep.
275  *   \return A result telling whether the sleep was successful
276  */
277 e_smx_state_t simcall_process_sleep(double duration)
278 {
279   /* checking for infinite values */
280   xbt_assert(std::isfinite(duration), "duration is not finite!");
281   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
282 }
283
284 /**
285  * \ingroup simix_comm_management
286  */
287 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
288                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
289                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
290 {
291   /* checking for infinite values */
292   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
293   xbt_assert(std::isfinite(rate), "rate is not finite!");
294   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
295
296   xbt_assert(mbox, "No rendez-vous point defined for send");
297
298   if (MC_is_active() || MC_record_replay_is_active()) {
299     /* the model-checker wants two separate simcalls */
300     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
301     comm = simcall_comm_isend(sender, mbox, task_size, rate,
302         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
303     simcall_comm_wait(comm, timeout);
304     comm = nullptr;
305   }
306   else {
307     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
308                          match_fun, copy_data_fun, data, timeout);
309   }
310 }
311
312 /**
313  * \ingroup simix_comm_management
314  */
315 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
316                                   size_t src_buff_size,
317                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
318                                   void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
319                                   void* data, int detached)
320 {
321   /* checking for infinite values */
322   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
323   xbt_assert(std::isfinite(rate), "rate is not finite!");
324
325   xbt_assert(mbox, "No rendez-vous point defined for isend");
326
327   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
328                                  src_buff_size, match_fun,
329                                  clean_fun, copy_data_fun, data, detached);
330 }
331
332 /**
333  * \ingroup simix_comm_management
334  */
335 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
336                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
337                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
338 {
339   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
340   xbt_assert(mbox, "No rendez-vous point defined for recv");
341
342   if (MC_is_active() || MC_record_replay_is_active()) {
343     /* the model-checker wants two separate simcalls */
344     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
345     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
346                               match_fun, copy_data_fun, data, rate);
347     simcall_comm_wait(comm, timeout);
348     comm = nullptr;
349   }
350   else {
351     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
352                            match_fun, copy_data_fun, data, timeout, rate);
353   }
354 }
355 /**
356  * \ingroup simix_comm_management
357  */
358 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
359                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
360                                   void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
361 {
362   xbt_assert(mbox, "No rendez-vous point defined for irecv");
363
364   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
365                                  match_fun, copy_data_fun, data, rate);
366 }
367
368 /**
369  * \ingroup simix_comm_management
370  */
371 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
372                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
373 {
374   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
375
376   return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
377 }
378
379 /**
380  * \ingroup simix_comm_management
381  */
382 void simcall_comm_cancel(smx_activity_t synchro)
383 {
384   simgrid::simix::kernelImmediate([synchro] {
385     simgrid::kernel::activity::CommImplPtr comm =
386         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
387     comm->cancel();
388   });
389 }
390
391 /**
392  * \ingroup simix_comm_management
393  */
394 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
395 {
396   return simcall_BODY_comm_waitany(comms, timeout);
397 }
398
399 /**
400  * \ingroup simix_comm_management
401  */
402 int simcall_comm_testany(smx_activity_t* comms, size_t count)
403 {
404   if (count == 0)
405     return -1;
406   return simcall_BODY_comm_testany(comms, count);
407 }
408
409 /**
410  * \ingroup simix_comm_management
411  */
412 void simcall_comm_wait(smx_activity_t comm, double timeout)
413 {
414   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
415   simcall_BODY_comm_wait(comm, timeout);
416 }
417
418 /**
419  * \brief Set the category of an synchro.
420  *
421  * This functions changes the category only. It calls a surf function.
422  * \param synchro The execution synchro
423  * \param category The tracing category
424  */
425 void simcall_set_category(smx_activity_t synchro, const char *category)
426 {
427   if (category == nullptr) {
428     return;
429   }
430   simcall_BODY_set_category(synchro, category);
431 }
432
433 /**
434  * \ingroup simix_comm_management
435  *
436  */
437 int simcall_comm_test(smx_activity_t comm)
438 {
439   return simcall_BODY_comm_test(comm);
440 }
441
442 /**
443  * \ingroup simix_synchro_management
444  *
445  */
446 smx_mutex_t simcall_mutex_init()
447 {
448   if (not simix_global) {
449     fprintf(stderr,"You must run MSG_init before using MSG\n"); // We can't use xbt_die since we may get there before the initialization
450     xbt_abort();
451   }
452   return simgrid::simix::kernelImmediate([] { return new simgrid::simix::MutexImpl(); });
453 }
454
455 /**
456  * \ingroup simix_synchro_management
457  *
458  */
459 void simcall_mutex_lock(smx_mutex_t mutex)
460 {
461   simcall_BODY_mutex_lock(mutex);
462 }
463
464 /**
465  * \ingroup simix_synchro_management
466  *
467  */
468 int simcall_mutex_trylock(smx_mutex_t mutex)
469 {
470   return simcall_BODY_mutex_trylock(mutex);
471 }
472
473 /**
474  * \ingroup simix_synchro_management
475  *
476  */
477 void simcall_mutex_unlock(smx_mutex_t mutex)
478 {
479   simcall_BODY_mutex_unlock(mutex);
480 }
481
482 /**
483  * \ingroup simix_synchro_management
484  *
485  */
486 smx_cond_t simcall_cond_init()
487 {
488   return simcall_BODY_cond_init();
489 }
490
491 /**
492  * \ingroup simix_synchro_management
493  *
494  */
495 void simcall_cond_signal(smx_cond_t cond)
496 {
497   simcall_BODY_cond_signal(cond);
498 }
499
500 /**
501  * \ingroup simix_synchro_management
502  *
503  */
504 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
505 {
506   simcall_BODY_cond_wait(cond, mutex);
507 }
508
509 /**
510  * \ingroup simix_synchro_management
511  *
512  */
513 void simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
514 {
515   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
516   simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
517 }
518
519 /**
520  * \ingroup simix_synchro_management
521  *
522  */
523 void simcall_cond_broadcast(smx_cond_t cond)
524 {
525   simcall_BODY_cond_broadcast(cond);
526 }
527
528 /**
529  * \ingroup simix_synchro_management
530  *
531  */
532 void simcall_sem_acquire(smx_sem_t sem)
533 {
534   simcall_BODY_sem_acquire(sem);
535 }
536
537 /**
538  * \ingroup simix_synchro_management
539  *
540  */
541 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
542 {
543   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
544   simcall_BODY_sem_acquire_timeout(sem, timeout);
545 }
546
547 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
548 {
549   return simcall_BODY_storage_read(st, size);
550 }
551
552 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
553 {
554   return simcall_BODY_storage_write(st, size);
555 }
556
557 void simcall_run_kernel(std::function<void()> const& code)
558 {
559   simcall_BODY_run_kernel(&code);
560 }
561
562 void simcall_run_blocking(std::function<void()> const& code)
563 {
564   simcall_BODY_run_blocking(&code);
565 }
566
567 int simcall_mc_random(int min, int max) {
568   return simcall_BODY_mc_random(min, max);
569 }
570
571 /* ************************************************************************** */
572
573 /** @brief returns a printable string representing a simcall */
574 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
575   return simcall_names[kind];
576 }
577
578 namespace simgrid {
579 namespace simix {
580
581 void unblock(smx_actor_t process)
582 {
583   xbt_assert(SIMIX_is_maestro());
584   SIMIX_simcall_answer(&process->simcall);
585 }
586
587 }
588 }