Logo AND Algorithmique Numérique Distribuée

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