1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include <boost/range/algorithm.hpp>
9 #include <xbt/functional.hpp>
11 #include "src/surf/surf_interface.hpp"
12 #include "smx_private.h"
13 #include "xbt/sysdep.h"
17 #include "src/mc/mc_replay.h"
18 #include "src/mc/Client.hpp"
19 #include "src/msg/msg_private.h"
21 #include "src/simix/SynchroSleep.hpp"
22 #include "src/simix/SynchroRaw.hpp"
23 #include "src/simix/SynchroIo.hpp"
26 #include "src/smpi/private.h"
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
31 unsigned long simix_process_maxpid = 0;
33 /** Increase the refcount for this process */
34 smx_process_t SIMIX_process_ref(smx_process_t process)
36 if (process != nullptr)
37 intrusive_ptr_add_ref(process);
41 /** Decrease the refcount for this process */
42 void SIMIX_process_unref(smx_process_t process)
44 if (process != nullptr)
45 intrusive_ptr_release(process);
49 * \brief Returns the current agent.
51 * This functions returns the currently running SIMIX process.
53 * \return The SIMIX process
55 smx_process_t SIMIX_process_self(void)
57 smx_context_t self_context = SIMIX_context_self();
59 return self_context ? self_context->process() : nullptr;
63 * \brief Returns whether a process has pending asynchronous communications.
64 * \return true if there are asynchronous communications in this process
66 int SIMIX_process_has_pending_comms(smx_process_t process) {
68 return xbt_fifo_size(process->comms) > 0;
72 * \brief Moves a process to the list of processes to destroy.
74 void SIMIX_process_cleanup(smx_process_t process)
76 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
77 process->name.c_str(), process, process->waiting_synchro);
79 process->finished = true;
80 SIMIX_process_on_exit_runall(process);
82 /* Unregister from the kill timer if any */
83 if (process->kill_timer != nullptr)
84 SIMIX_timer_remove(process->kill_timer);
86 xbt_os_mutex_acquire(simix_global->mutex);
88 /* cancel non-blocking communications */
89 smx_synchro_t synchro;
90 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
91 simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
93 /* make sure no one will finish the comm after this process is destroyed,
94 * because src_proc or dst_proc would be an invalid pointer */
97 if (comm->src_proc == process) {
98 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
99 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
100 comm->src_proc = nullptr;
102 /* I'm not supposed to destroy a detached comm from the sender side, */
104 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
109 else if (comm->dst_proc == process){
110 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
111 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
112 comm->dst_proc = nullptr;
114 if (comm->detached && comm->src_proc != nullptr) {
115 /* the comm will be freed right now, remove it from the sender */
116 xbt_fifo_remove(comm->src_proc->comms, comm);
121 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
125 XBT_DEBUG("%p should not be run anymore",process);
126 xbt_swag_remove(process, simix_global->process_list);
128 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
129 xbt_swag_insert(process, simix_global->process_to_destroy);
130 process->context->iwannadie = 0;
132 xbt_os_mutex_release(simix_global->mutex);
138 * Should be called some time to time to free the memory allocated for processes
139 * that have finished (or killed).
141 void SIMIX_process_empty_trash(void)
143 smx_process_t process = nullptr;
145 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
146 XBT_DEBUG("Getting rid of %p",process);
147 intrusive_ptr_release(process);
156 delete this->context;
157 if (this->properties)
158 xbt_dict_free(&this->properties);
159 if (this->comms != nullptr)
160 xbt_fifo_free(this->comms);
162 xbt_dynar_free(&this->on_exit);
165 void create_maestro(std::function<void()> code)
167 smx_process_t maestro = nullptr;
168 /* Create maestro process and intilialize it */
169 maestro = new simgrid::simix::Process();
170 maestro->pid = simix_process_maxpid++;
173 maestro->data = nullptr;
176 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
179 xbt_die("simix is not initialized, please call MSG_init first");
181 simix_global->context_factory->create_maestro(code, maestro);
184 maestro->simcall.issuer = maestro;
185 simix_global->maestro_process = maestro;
192 * \brief Creates and runs the maestro process
194 void SIMIX_maestro_create(void (*code)(void*), void* data)
196 simgrid::simix::create_maestro(std::bind(code, data));
200 * \brief Stops a process.
202 * Stops the process, execute all the registered on_exit functions,
203 * register it to the list of the process to restart if needed
204 * and stops its context.
206 void SIMIX_process_stop(smx_process_t arg) {
207 arg->finished = true;
208 /* execute the on_exit functions */
209 SIMIX_process_on_exit_runall(arg);
210 /* Add the process to the list of process to restart, only if the host is down */
211 if (arg->auto_restart && arg->host->isOff()) {
212 SIMIX_host_add_auto_restart_process(arg->host, arg->name.c_str(),
213 arg->code, arg->data,
214 sg_host_get_name(arg->host),
215 SIMIX_timer_get_date(arg->kill_timer),
219 XBT_DEBUG("Process %s (%s) is dead",
220 arg->name.c_str(), sg_host_get_name(arg->host));
221 arg->context->stop();
225 * \brief Internal function to create a process.
227 * This function actually creates the process.
228 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
229 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
231 * \return the process created
233 smx_process_t SIMIX_process_create(
235 std::function<void()> code,
237 const char *hostname,
239 xbt_dict_t properties,
241 smx_process_t parent_process)
243 smx_process_t process = nullptr;
244 sg_host_t host = sg_host_by_name(hostname);
246 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
249 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
254 process = new simgrid::simix::Process();
256 xbt_assert(code && host != nullptr, "Invalid parameters");
258 process->pid = simix_process_maxpid++;
259 process->name = simgrid::xbt::string(name);
260 process->host = host;
261 process->data = data;
262 process->comms = xbt_fifo_new();
263 process->simcall.issuer = process;
264 /* Initiliaze data segment to default value */
265 SIMIX_segment_index_set(process, -1);
267 if (parent_process != nullptr) {
268 process->ppid = SIMIX_process_get_PID(parent_process);
269 /* SMPI process have their own data segment and
270 each other inherit from their father */
272 if(smpi_privatize_global_variables){
273 if( parent_process->pid != 0){
274 SIMIX_segment_index_set(process, parent_process->segment_index);
276 SIMIX_segment_index_set(process, process->pid - 1);
284 /* Process data for auto-restart */
285 process->auto_restart = auto_restart;
286 process->code = code;
288 XBT_VERB("Create context %s", process->name.c_str());
289 process->context = SIMIX_context_new(
291 simix_global->cleanup_process_function, process);
294 process->properties = properties;
296 /* Add the process to it's host process list */
297 xbt_swag_insert(process, sg_host_simix(host)->process_list);
299 XBT_DEBUG("Start context '%s'", process->name.c_str());
301 /* Now insert it in the global process list and in the process to run list */
302 xbt_swag_insert(process, simix_global->process_list);
303 XBT_DEBUG("Inserting %s(%s) in the to_run list",
304 process->name.c_str(), sg_host_get_name(host));
305 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
307 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
308 XBT_DEBUG("Process %s(%s) will be kill at time %f",
309 process->name.c_str(), sg_host_get_name(process->host), kill_time);
310 process->kill_timer = SIMIX_timer_set(kill_time, [=]() {
311 simix_global->kill_process_function(process);
315 /* Tracing the process creation */
316 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
321 smx_process_t SIMIX_process_attach(
324 const char* hostname,
325 xbt_dict_t properties,
326 smx_process_t parent_process)
328 // This is mostly a copy/paste from SIMIX_process_new(),
329 // it'd be nice to share some code between those two functions.
331 sg_host_t host = sg_host_by_name(hostname);
332 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
335 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
340 smx_process_t process = new simgrid::simix::Process();
342 process->pid = simix_process_maxpid++;
343 process->name = std::string(name);
344 process->host = host;
345 process->data = data;
346 process->comms = xbt_fifo_new();
347 process->simcall.issuer = process;
349 /* Initiliaze data segment to default value */
350 SIMIX_segment_index_set(process, -1);
351 if (parent_process != nullptr) {
352 process->ppid = SIMIX_process_get_PID(parent_process);
353 /* SMPI process have their own data segment and
354 each other inherit from their father */
356 if(smpi_privatize_global_variables){
357 if(parent_process->pid != 0){
358 SIMIX_segment_index_set(process, parent_process->segment_index);
360 SIMIX_segment_index_set(process, process->pid - 1);
366 /* Process data for auto-restart */
367 process->auto_restart = false;
368 process->code = nullptr;
370 XBT_VERB("Create context %s", process->name.c_str());
372 xbt_die("simix is not initialized, please call MSG_init first");
373 process->context = simix_global->context_factory->attach(
374 simix_global->cleanup_process_function, process);
377 process->properties = properties;
379 /* Add the process to it's host process list */
380 xbt_swag_insert(process, sg_host_simix(host)->process_list);
382 /* Now insert it in the global process list and in the process to run list */
383 xbt_swag_insert(process, simix_global->process_list);
384 XBT_DEBUG("Inserting %s(%s) in the to_run list",
385 process->name.c_str(), sg_host_get_name(host));
386 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
388 /* Tracing the process creation */
389 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
391 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
393 xbt_die("Not a suitable context");
395 context->attach_start();
399 void SIMIX_process_detach(void)
401 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
403 xbt_die("Not a suitable context");
405 simix_global->cleanup_process_function(context->process());
407 // Let maestro ignore we are still alive:
408 // xbt_swag_remove(context->process(), simix_global->process_list);
410 // TODDO, Remove from proces list:
411 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
413 context->attach_stop();
418 * \brief Executes the processes from simix_global->process_to_run.
420 * The processes of simix_global->process_to_run are run (in parallel if
421 * possible). On exit, simix_global->process_to_run is empty, and
422 * simix_global->process_that_ran contains the list of processes that just ran.
423 * The two lists are swapped so, be careful when using them before and after a
424 * call to this function.
426 void SIMIX_process_runall(void)
428 SIMIX_context_runall();
430 xbt_dynar_t tmp = simix_global->process_that_ran;
431 simix_global->process_that_ran = simix_global->process_to_run;
432 simix_global->process_to_run = tmp;
433 xbt_dynar_reset(simix_global->process_to_run);
436 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
437 SIMIX_process_kill(process, simcall->issuer);
440 * \brief Internal function to kill a SIMIX process.
442 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
443 * or directly for SIMIX internal purposes.
445 * \param process poor victim
446 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
448 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
450 XBT_DEBUG("Killing process %s on %s",
451 process->name.c_str(), sg_host_get_name(process->host));
453 process->context->iwannadie = 1;
454 process->blocked = 0;
455 process->suspended = 0;
456 process->exception = nullptr;
458 /* destroy the blocking synchro if any */
459 if (process->waiting_synchro) {
461 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
462 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
463 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
464 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
465 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
467 if (exec != nullptr) {
470 } else if (comm != nullptr) {
471 xbt_fifo_remove(process->comms, process->waiting_synchro);
474 // Remove first occurence of &process->simcall:
475 auto i = boost::range::find(
476 process->waiting_synchro->simcalls,
478 if (i != process->waiting_synchro->simcalls.end())
479 process->waiting_synchro->simcalls.remove(&process->simcall);
483 } else if (sleep != nullptr) {
484 SIMIX_process_sleep_destroy(process->waiting_synchro);
486 } else if (raw != nullptr) {
487 SIMIX_synchro_stop_waiting(process, &process->simcall);
488 delete process->waiting_synchro;
490 } else if (io != nullptr) {
491 SIMIX_io_destroy(process->waiting_synchro);
495 switch (process->waiting_synchro->type) {
496 case SIMIX_SYNC_JOIN:
497 SIMIX_process_sleep_destroy(process->waiting_synchro);
501 process->waiting_synchro = nullptr;
503 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
504 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
505 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
510 /** @brief Ask another process to raise the given exception
512 * @param cat category of exception
513 * @param value value associated to the exception
514 * @param msg string information associated to the exception
516 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
517 SMX_EXCEPTION(process, cat, value, msg);
519 if (process->suspended)
520 SIMIX_process_resume(process,SIMIX_process_self());
522 /* cancel the blocking synchro if any */
523 if (process->waiting_synchro) {
525 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
526 if (exec != nullptr) {
527 SIMIX_execution_cancel(process->waiting_synchro);
530 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
531 if (comm != nullptr) {
532 xbt_fifo_remove(process->comms, comm);
536 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
537 if (sleep != nullptr) {
538 SIMIX_process_sleep_destroy(process->waiting_synchro);
539 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
540 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
541 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
545 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
546 if (raw != nullptr) {
547 SIMIX_synchro_stop_waiting(process, &process->simcall);
550 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
552 SIMIX_io_destroy(process->waiting_synchro);
555 process->waiting_synchro = nullptr;
559 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
560 SIMIX_process_killall(simcall->issuer, reset_pid);
563 * \brief Kills all running processes.
564 * \param issuer this one will not be killed
566 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
568 smx_process_t p = nullptr;
570 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
572 SIMIX_process_kill(p,issuer);
577 simix_process_maxpid = reset_pid;
579 SIMIX_context_runall();
581 SIMIX_process_empty_trash();
584 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
586 process->new_host = dest;
588 void SIMIX_process_change_host(smx_process_t process,
591 xbt_assert((process != nullptr), "Invalid parameters");
592 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
593 process->host = dest;
594 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
598 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
600 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
602 if (process != simcall->issuer) {
603 SIMIX_simcall_answer(simcall);
605 sync_suspend->simcalls.push_back(simcall);
606 process->waiting_synchro = sync_suspend;
607 process->waiting_synchro->suspend();
609 /* If we are suspending ourselves, then just do not finish the simcall now */
612 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
614 if (process->suspended) {
615 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
619 process->suspended = 1;
621 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
622 if (process != issuer) {
624 if (process->waiting_synchro)
625 process->waiting_synchro->suspend();
626 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
630 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
631 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
635 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
636 SIMIX_process_resume(process, simcall->issuer);
639 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
641 XBT_IN("process = %p, issuer = %p", process, issuer);
643 if(process->context->iwannadie) {
644 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
648 if(!process->suspended) return;
649 process->suspended = 0;
651 /* If we are resuming another process, resume the synchronization it was waiting for
652 if any. Otherwise add it to the list of process to run in the next round. */
653 if (process != issuer) {
655 if (process->waiting_synchro) {
656 process->waiting_synchro->resume();
658 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
663 int SIMIX_process_get_maxpid(void) {
664 return simix_process_maxpid;
667 int SIMIX_process_count(void)
669 return xbt_swag_size(simix_global->process_list);
672 int SIMIX_process_get_PID(smx_process_t self){
679 int SIMIX_process_get_PPID(smx_process_t self){
686 void* SIMIX_process_self_get_data()
688 smx_process_t self = SIMIX_process_self();
693 return SIMIX_process_get_data(self);
696 void SIMIX_process_self_set_data(void *data)
698 smx_process_t self = SIMIX_process_self();
700 SIMIX_process_set_data(self, data);
703 void* SIMIX_process_get_data(smx_process_t process)
705 return process->data;
708 void SIMIX_process_set_data(smx_process_t process, void *data)
710 process->data = data;
713 sg_host_t SIMIX_process_get_host(smx_process_t process)
715 return process->host;
718 /* needs to be public and without simcall because it is called
719 by exceptions and logging events */
720 const char* SIMIX_process_self_get_name(void) {
722 smx_process_t process = SIMIX_process_self();
723 if (process == nullptr || process == simix_global->maestro_process)
726 return SIMIX_process_get_name(process);
729 const char* SIMIX_process_get_name(smx_process_t process)
731 return process->name.c_str();
734 smx_process_t SIMIX_process_get_by_name(const char* name)
737 xbt_swag_foreach(proc, simix_global->process_list) {
738 if (proc->name == name)
744 int SIMIX_process_is_suspended(smx_process_t process)
746 return process->suspended;
749 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
751 return process->properties;
754 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
756 if (process->finished) {
757 // The joined process is already finished, just wake up the issuer process right away
758 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
759 SIMIX_simcall_answer(simcall);
762 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
763 sync->simcalls.push_back(simcall);
764 simcall->issuer->waiting_synchro = sync;
767 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
768 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
770 if (sleep->surf_sleep) {
771 sleep->surf_sleep->cancel();
773 while (!sleep->simcalls.empty()) {
774 smx_simcall_t simcall = sleep->simcalls.front();
775 sleep->simcalls.pop_front();
776 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
777 simcall->issuer->waiting_synchro = nullptr;
778 if (simcall->issuer->suspended) {
779 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
780 simcall->issuer->suspended = 0;
781 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
783 SIMIX_simcall_answer(simcall);
786 sleep->surf_sleep->unref();
787 sleep->surf_sleep = nullptr;
793 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
795 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
796 static_cast<simgrid::simix::Synchro*>(res)->ref();
797 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
801 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
803 if (MC_is_active() || MC_record_replay_is_active()) {
804 MC_process_clock_add(simcall->issuer, duration);
805 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
806 SIMIX_simcall_answer(simcall);
809 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
810 sync->simcalls.push_back(simcall);
811 simcall->issuer->waiting_synchro = sync;
814 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
816 sg_host_t host = process->host;
818 /* check if the host is active */
820 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
822 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
823 synchro->host = host;
824 synchro->surf_sleep = surf_host_sleep(host, duration);
825 synchro->surf_sleep->setData(synchro);
826 XBT_DEBUG("Create sleep synchronization %p", synchro);
831 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
833 XBT_DEBUG("Destroy synchro %p", synchro);
834 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
836 if (sleep->surf_sleep) {
837 sleep->surf_sleep->unref();
838 sleep->surf_sleep = nullptr;
844 * \brief Calling this function makes the process to yield.
846 * Only the current process can call this function, giving back the control to
849 * \param self the current process
851 void SIMIX_process_yield(smx_process_t self)
853 XBT_DEBUG("Yield process '%s'", self->name.c_str());
855 /* Go into sleep and return control to maestro */
856 self->context->suspend();
858 /* Ok, maestro returned control to us */
859 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
861 if (self->new_host) {
862 SIMIX_process_change_host(self, self->new_host);
863 self->new_host = nullptr;
866 if (self->context->iwannadie){
867 XBT_DEBUG("I wanna die!");
868 SIMIX_process_stop(self);
871 if (self->suspended) {
872 XBT_DEBUG("Hey! I'm suspended.");
873 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
875 SIMIX_process_suspend(self, self);
878 if (self->exception != nullptr) {
879 XBT_DEBUG("Wait, maestro left me an exception");
880 std::exception_ptr exception = std::move(self->exception);
881 self->exception = nullptr;
882 std::rethrow_exception(std::move(exception));
885 if(SMPI_switch_data_segment && self->segment_index != -1){
886 SMPI_switch_data_segment(self->segment_index);
890 /* callback: termination */
891 void SIMIX_process_exception_terminate(xbt_ex_t * e)
897 smx_context_t SIMIX_process_get_context(smx_process_t p) {
901 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
906 * \brief Returns the list of processes to run.
908 xbt_dynar_t SIMIX_process_get_runnable(void)
910 return simix_global->process_to_run;
914 * \brief Returns the process from PID.
916 smx_process_t SIMIX_process_from_PID(int PID)
919 xbt_swag_foreach(proc, simix_global->process_list) {
920 if (proc->pid == (unsigned long) PID)
926 /** @brief returns a dynar containg all currently existing processes */
927 xbt_dynar_t SIMIX_processes_as_dynar(void) {
929 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),nullptr);
930 xbt_swag_foreach(proc, simix_global->process_list) {
931 xbt_dynar_push(res,&proc);
937 void SIMIX_process_on_exit_runall(smx_process_t process) {
938 s_smx_process_exit_fun_t exit_fun;
939 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
940 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
941 while (!xbt_dynar_is_empty(process->on_exit)) {
942 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
943 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
947 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
948 xbt_assert(process, "current process not found: are you in maestro context ?");
950 if (!process->on_exit) {
951 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
954 s_smx_process_exit_fun_t exit_fun = {fun, data};
956 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
960 * \brief Sets the auto-restart status of the process.
961 * If set to 1, the process will be automatically restarted when its host
964 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
965 process->auto_restart = auto_restart;
968 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
969 return SIMIX_process_restart(process, simcall->issuer);
971 /** @brief Restart a process, starting it again from the beginning. */
972 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
973 XBT_DEBUG("Restarting process %s on %s",
974 process->name.c_str(), sg_host_get_name(process->host));
976 //retrieve the arguments of the old process
977 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
978 simgrid::simix::ProcessArg arg;
979 arg.name = process->name;
980 arg.code = process->code;
981 arg.hostname = sg_host_get_name(process->host);
982 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
983 arg.data = process->data;
984 arg.properties = nullptr;
985 arg.auto_restart = process->auto_restart;
987 //kill the old process
988 SIMIX_process_kill(process, issuer);
990 //start the new process
991 if (simix_global->create_process_function)
992 return simix_global->create_process_function(
993 arg.name.c_str(), std::move(arg.code), arg.data,
994 arg.hostname, arg.kill_time,
995 arg.properties, arg.auto_restart,
998 return simcall_process_create(
999 arg.name.c_str(), std::move(arg.code), arg.data,
1000 arg.hostname, arg.kill_time,
1001 arg.properties, arg.auto_restart);
1004 void SIMIX_segment_index_set(smx_process_t proc, int index){
1005 proc->segment_index = index;
1009 * \ingroup simix_process_management
1010 * \brief Creates and runs a new SIMIX process.
1012 * The structure and the corresponding thread are created and put in the list of ready processes.
1014 * \param name a name for the process. It is for user-level information and can be nullptr.
1015 * \param code the main function of the process
1016 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can be nullptr.
1017 * It can be retrieved with the function \ref simcall_process_get_data.
1018 * \param hostname name of the host where the new agent is executed.
1019 * \param kill_time time when the process is killed
1020 * \param argc first argument passed to \a code
1021 * \param argv second argument passed to \a code
1022 * \param properties the properties of the process
1023 * \param auto_restart either it is autorestarting or not.
1025 smx_process_t simcall_process_create(const char *name,
1026 xbt_main_func_t code,
1028 const char *hostname,
1030 int argc, char **argv,
1031 xbt_dict_t properties,
1034 if (name == nullptr)
1036 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
1037 for (int i = 0; i != argc; ++i)
1040 smx_process_t res = simcall_process_create(name,
1041 std::move(wrapped_code),
1042 data, hostname, kill_time, properties, auto_restart);
1046 smx_process_t simcall_process_create(
1047 const char *name, std::function<void()> code, void *data,
1048 const char *hostname, double kill_time,
1049 xbt_dict_t properties, int auto_restart)
1051 if (name == nullptr)
1053 smx_process_t self = SIMIX_process_self();
1054 return simgrid::simix::kernelImmediate([&] {
1055 return SIMIX_process_create(name,
1056 std::move(code), data, hostname,
1057 kill_time, properties, auto_restart,