Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent.
[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.
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.hpp"
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.hpp"
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   simgrid::simix::kernelImmediate([execution] {
115     XBT_DEBUG("Cancel synchro %p", execution.get());
116     simgrid::kernel::activity::ExecImplPtr exec =
117         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
118
119     if (exec->surf_exec)
120       exec->surf_exec->cancel();
121   });
122 }
123
124 /**
125  * \ingroup simix_process_management
126  * \brief Changes the priority of an execution synchro.
127  *
128  * This functions changes the priority only. It calls a surf function.
129  * \param execution The execution synchro
130  * \param priority The new priority
131  */
132 void simcall_execution_set_priority(smx_activity_t execution, double priority)
133 {
134   /* checking for infinite values */
135   xbt_assert(std::isfinite(priority), "priority is not finite!");
136   simgrid::simix::kernelImmediate([execution, priority] {
137
138     simgrid::kernel::activity::ExecImplPtr exec =
139         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
140     if (exec->surf_exec)
141       exec->surf_exec->setSharingWeight(priority);
142   });
143 }
144
145 /**
146  * \ingroup simix_process_management
147  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
148  *
149  * This functions changes the capping only. It calls a surf function.
150  * \param execution The execution synchro
151  * \param bound The new bound
152  */
153 void simcall_execution_set_bound(smx_activity_t execution, double bound)
154 {
155   simgrid::simix::kernelImmediate([execution, bound] {
156     simgrid::kernel::activity::ExecImplPtr exec =
157         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
158     if (exec->surf_exec)
159       static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
160   });
161 }
162
163 /**
164  * \ingroup simix_host_management
165  * \brief Waits for the completion of an execution synchro and destroy it.
166  *
167  * \param execution The execution synchro
168  */
169 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
170 {
171   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
172 }
173
174 /**
175  * \ingroup simix_process_management
176  * \brief Kills all SIMIX processes.
177  */
178 void simcall_process_killall(int reset_pid)
179 {
180   simcall_BODY_process_killall(reset_pid);
181 }
182
183 /**
184  * \ingroup simix_process_management
185  * \brief Cleans up a SIMIX process.
186  * \param process poor victim (must have already been killed)
187  */
188 void simcall_process_cleanup(smx_actor_t process)
189 {
190   simcall_BODY_process_cleanup(process);
191 }
192
193 void simcall_process_join(smx_actor_t process, double timeout)
194 {
195   simcall_BODY_process_join(process, timeout);
196 }
197
198 /**
199  * \ingroup simix_process_management
200  * \brief Suspends a process.
201  *
202  * This function suspends the process by suspending the synchro
203  * it was waiting for completion.
204  *
205  * \param process a SIMIX process
206  */
207 void simcall_process_suspend(smx_actor_t process)
208 {
209   simcall_BODY_process_suspend(process);
210 }
211
212 /**
213  * \ingroup simix_process_management
214  * \brief Returns the amount of SIMIX processes in the system
215  *
216  * Maestro internal process is not counted, only user code processes are
217  */
218 int simcall_process_count()
219 {
220   return simgrid::simix::kernelImmediate(SIMIX_process_count);
221 }
222
223 /**
224  * \ingroup simix_process_management
225  * \brief Set the user data of a #smx_actor_t.
226  *
227  * This functions sets the user data associated to \a process.
228  * \param process SIMIX process
229  * \param data User data
230  */
231 void simcall_process_set_data(smx_actor_t process, void *data)
232 {
233   simgrid::simix::kernelImmediate([process, data] { process->setUserData(data); });
234 }
235
236 /**
237  * \ingroup simix_process_management
238  * \brief Set the kill time of a process.
239  */
240 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
241 {
242
243   if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
244     return;
245   XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->cname(), process->host->getCname());
246   process->kill_timer = SIMIX_timer_set(kill_time, [process] {
247     simix_global->kill_process_function(process);
248     process->kill_timer=nullptr;
249   });
250 }
251
252 /**
253  * \ingroup simix_process_management
254  * \brief Add an on_exit function
255  * Add an on_exit function which will be executed when the process exits/is killed.
256  */
257 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
258 {
259   simcall_BODY_process_on_exit(process, fun, data);
260 }
261
262 /**
263  * \ingroup simix_process_management
264  * \brief Creates a new sleep SIMIX synchro.
265  *
266  * This function creates a SURF action and allocates the data necessary
267  * to create the SIMIX synchro. It can raise a host_error exception if the
268  * host crashed. The default SIMIX name of the synchro is "sleep".
269  *
270  *   \param duration Time duration of the sleep.
271  *   \return A result telling whether the sleep was successful
272  */
273 e_smx_state_t simcall_process_sleep(double duration)
274 {
275   /* checking for infinite values */
276   xbt_assert(std::isfinite(duration), "duration is not finite!");
277   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
278 }
279
280 /**
281  * \ingroup simix_comm_management
282  */
283 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
284                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
285                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
286 {
287   /* checking for infinite values */
288   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
289   xbt_assert(std::isfinite(rate), "rate is not finite!");
290   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
291
292   xbt_assert(mbox, "No rendez-vous point defined for send");
293
294   if (MC_is_active() || MC_record_replay_is_active()) {
295     /* the model-checker wants two separate simcalls */
296     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
297     comm = simcall_comm_isend(sender, mbox, task_size, rate,
298         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
299     simcall_comm_wait(comm, timeout);
300     comm = nullptr;
301   }
302   else {
303     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
304                          match_fun, copy_data_fun, data, timeout);
305   }
306 }
307
308 /**
309  * \ingroup simix_comm_management
310  */
311 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
312                                   size_t src_buff_size,
313                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
314                                   void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
315                                   void* data, int detached)
316 {
317   /* checking for infinite values */
318   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
319   xbt_assert(std::isfinite(rate), "rate is not finite!");
320
321   xbt_assert(mbox, "No rendez-vous point defined for isend");
322
323   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
324                                  src_buff_size, match_fun,
325                                  clean_fun, copy_data_fun, data, detached);
326 }
327
328 /**
329  * \ingroup simix_comm_management
330  */
331 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
332                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
333                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
334 {
335   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
336   xbt_assert(mbox, "No rendez-vous point defined for recv");
337
338   if (MC_is_active() || MC_record_replay_is_active()) {
339     /* the model-checker wants two separate simcalls */
340     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
341     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
342                               match_fun, copy_data_fun, data, rate);
343     simcall_comm_wait(comm, timeout);
344     comm = nullptr;
345   }
346   else {
347     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
348                            match_fun, copy_data_fun, data, timeout, rate);
349   }
350 }
351 /**
352  * \ingroup simix_comm_management
353  */
354 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
355                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
356                                   void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
357 {
358   xbt_assert(mbox, "No rendez-vous point defined for irecv");
359
360   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
361                                  match_fun, copy_data_fun, data, rate);
362 }
363
364 /**
365  * \ingroup simix_comm_management
366  */
367 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
368                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
369 {
370   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
371
372   return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
373 }
374
375 /**
376  * \ingroup simix_comm_management
377  */
378 void simcall_comm_cancel(smx_activity_t synchro)
379 {
380   simgrid::simix::kernelImmediate([synchro] {
381     simgrid::kernel::activity::CommImplPtr comm =
382         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
383     comm->cancel();
384   });
385 }
386
387 /**
388  * \ingroup simix_comm_management
389  */
390 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
391 {
392   return simcall_BODY_comm_waitany(comms, timeout);
393 }
394
395 /**
396  * \ingroup simix_comm_management
397  */
398 int simcall_comm_testany(smx_activity_t* comms, size_t count)
399 {
400   if (count == 0)
401     return -1;
402   return simcall_BODY_comm_testany(comms, count);
403 }
404
405 /**
406  * \ingroup simix_comm_management
407  */
408 void simcall_comm_wait(smx_activity_t comm, double timeout)
409 {
410   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
411   simcall_BODY_comm_wait(comm, timeout);
412 }
413
414 /**
415  * \brief Set the category of an synchro.
416  *
417  * This functions changes the category only. It calls a surf function.
418  * \param synchro The execution synchro
419  * \param category The tracing category
420  */
421 void simcall_set_category(smx_activity_t synchro, const char *category)
422 {
423   if (category == nullptr) {
424     return;
425   }
426   simcall_BODY_set_category(synchro, category);
427 }
428
429 /**
430  * \ingroup simix_comm_management
431  *
432  */
433 int simcall_comm_test(smx_activity_t comm)
434 {
435   return simcall_BODY_comm_test(comm);
436 }
437
438 /**
439  * \ingroup simix_synchro_management
440  *
441  */
442 smx_mutex_t simcall_mutex_init()
443 {
444   if (not simix_global) {
445     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
446     xbt_abort();
447   }
448   return simgrid::simix::kernelImmediate([] { return new simgrid::simix::MutexImpl(); });
449 }
450
451 /**
452  * \ingroup simix_synchro_management
453  *
454  */
455 void simcall_mutex_lock(smx_mutex_t mutex)
456 {
457   simcall_BODY_mutex_lock(mutex);
458 }
459
460 /**
461  * \ingroup simix_synchro_management
462  *
463  */
464 int simcall_mutex_trylock(smx_mutex_t mutex)
465 {
466   return simcall_BODY_mutex_trylock(mutex);
467 }
468
469 /**
470  * \ingroup simix_synchro_management
471  *
472  */
473 void simcall_mutex_unlock(smx_mutex_t mutex)
474 {
475   simcall_BODY_mutex_unlock(mutex);
476 }
477
478 /**
479  * \ingroup simix_synchro_management
480  *
481  */
482 smx_cond_t simcall_cond_init()
483 {
484   return simcall_BODY_cond_init();
485 }
486
487 /**
488  * \ingroup simix_synchro_management
489  *
490  */
491 void simcall_cond_signal(smx_cond_t cond)
492 {
493   simcall_BODY_cond_signal(cond);
494 }
495
496 /**
497  * \ingroup simix_synchro_management
498  *
499  */
500 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
501 {
502   simcall_BODY_cond_wait(cond, mutex);
503 }
504
505 /**
506  * \ingroup simix_synchro_management
507  *
508  */
509 void simcall_cond_wait_timeout(smx_cond_t cond,
510                                  smx_mutex_t mutex,
511                                  double timeout)
512 {
513   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
514   simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
515 }
516
517 /**
518  * \ingroup simix_synchro_management
519  *
520  */
521 void simcall_cond_broadcast(smx_cond_t cond)
522 {
523   simcall_BODY_cond_broadcast(cond);
524 }
525
526 /**
527  * \ingroup simix_synchro_management
528  *
529  */
530 void simcall_sem_acquire(smx_sem_t sem)
531 {
532   simcall_BODY_sem_acquire(sem);
533 }
534
535 /**
536  * \ingroup simix_synchro_management
537  *
538  */
539 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
540 {
541   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
542   simcall_BODY_sem_acquire_timeout(sem, timeout);
543 }
544
545 /**
546  * \ingroup simix_file_management
547  *
548  */
549 sg_size_t simcall_file_read(surf_file_t fd, sg_size_t size)
550 {
551   return simcall_BODY_file_read(fd, size);
552 }
553
554 /**
555  * \ingroup simix_file_management
556  *
557  */
558 sg_size_t simcall_file_write(surf_file_t fd, sg_size_t size)
559 {
560   return simcall_BODY_file_write(fd, size);
561 }
562
563 void simcall_run_kernel(std::function<void()> const& code)
564 {
565   simcall_BODY_run_kernel(&code);
566 }
567
568 void simcall_run_blocking(std::function<void()> const& code)
569 {
570   simcall_BODY_run_blocking(&code);
571 }
572
573 int simcall_mc_random(int min, int max) {
574   return simcall_BODY_mc_random(min, max);
575 }
576
577 /* ************************************************************************** */
578
579 /** @brief returns a printable string representing a simcall */
580 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
581   return simcall_names[kind];
582 }
583
584 namespace simgrid {
585 namespace simix {
586
587 void unblock(smx_actor_t process)
588 {
589   xbt_assert(SIMIX_is_maestro());
590   SIMIX_simcall_answer(&process->simcall);
591 }
592
593 }
594 }