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. */
12 #include <boost/range/algorithm.hpp>
14 #include <xbt/functional.hpp>
16 #include <xbt/sysdep.h>
20 #include <simgrid/s4u/host.hpp>
24 #include "smx_private.h"
25 #include "src/kernel/activity/SynchroIo.hpp"
26 #include "src/kernel/activity/SynchroRaw.hpp"
27 #include "src/kernel/activity/SynchroSleep.hpp"
28 #include "src/mc/mc_replay.h"
29 #include "src/mc/remote/Client.hpp"
30 #include "src/msg/msg_private.h"
31 #include "src/surf/cpu_interface.hpp"
32 #include "src/surf/surf_interface.hpp"
35 #include "src/smpi/private.h"
38 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
40 unsigned long simix_process_maxpid = 0;
42 /** Increase the refcount for this process */
43 smx_actor_t SIMIX_process_ref(smx_actor_t process)
45 if (process != nullptr)
46 intrusive_ptr_add_ref(process);
50 /** Decrease the refcount for this process */
51 void SIMIX_process_unref(smx_actor_t process)
53 if (process != nullptr)
54 intrusive_ptr_release(process);
58 * \brief Returns the current agent.
60 * This functions returns the currently running SIMIX process.
62 * \return The SIMIX process
64 smx_actor_t SIMIX_process_self()
66 smx_context_t self_context = SIMIX_context_self();
68 return (self_context != nullptr) ? self_context->process() : nullptr;
72 * \brief Returns whether a process has pending asynchronous communications.
73 * \return true if there are asynchronous communications in this process
75 int SIMIX_process_has_pending_comms(smx_actor_t process) {
77 return xbt_fifo_size(process->comms) > 0;
81 * \brief Moves a process to the list of processes to destroy.
83 void SIMIX_process_cleanup(smx_actor_t process)
85 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
86 process->name.c_str(), process, process->waiting_synchro);
88 process->finished = true;
89 SIMIX_process_on_exit_runall(process);
91 /* Unregister from the kill timer if any */
92 if (process->kill_timer != nullptr)
93 SIMIX_timer_remove(process->kill_timer);
95 xbt_os_mutex_acquire(simix_global->mutex);
97 /* cancel non-blocking communications */
98 smx_activity_t synchro = static_cast<smx_activity_t>(xbt_fifo_pop(process->comms));
99 while (synchro != nullptr) {
100 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
102 /* make sure no one will finish the comm after this process is destroyed,
103 * because src_proc or dst_proc would be an invalid pointer */
106 if (comm->src_proc == process) {
107 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
108 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
109 comm->src_proc = nullptr;
111 /* I'm not supposed to destroy a detached comm from the sender side, */
113 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
118 else if (comm->dst_proc == process){
119 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
120 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
121 comm->dst_proc = nullptr;
123 if (comm->detached && comm->src_proc != nullptr) {
124 /* the comm will be freed right now, remove it from the sender */
125 xbt_fifo_remove(comm->src_proc->comms, comm);
130 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
132 synchro = static_cast<smx_activity_t>(xbt_fifo_pop(process->comms));
135 XBT_DEBUG("%p should not be run anymore",process);
136 simix_global->process_list.erase(process->pid);
138 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
139 xbt_swag_insert(process, simix_global->process_to_destroy);
140 process->context->iwannadie = 0;
142 xbt_os_mutex_release(simix_global->mutex);
148 * Should be called some time to time to free the memory allocated for processes
149 * that have finished (or killed).
151 void SIMIX_process_empty_trash()
153 smx_actor_t process = nullptr;
155 while ((process = (smx_actor_t) xbt_swag_extract(simix_global->process_to_destroy))) {
156 XBT_DEBUG("Getting rid of %p",process);
157 intrusive_ptr_release(process);
164 ActorImpl::~ActorImpl()
166 delete this->context;
167 if (this->properties)
168 xbt_dict_free(&this->properties);
169 if (this->comms != nullptr)
170 xbt_fifo_free(this->comms);
172 xbt_dynar_free(&this->on_exit);
175 void create_maestro(std::function<void()> code)
177 smx_actor_t maestro = nullptr;
178 /* Create maestro process and initialize it */
179 maestro = new simgrid::simix::ActorImpl();
180 maestro->pid = simix_process_maxpid++;
182 maestro->data = nullptr;
185 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
188 xbt_die("simix is not initialized, please call MSG_init first");
190 simix_global->context_factory->create_maestro(code, maestro);
193 maestro->simcall.issuer = maestro;
194 simix_global->maestro_process = maestro;
200 /** @brief Creates and runs the maestro process */
201 void SIMIX_maestro_create(void (*code)(void*), void* data)
203 simgrid::simix::create_maestro(std::bind(code, data));
207 * \brief Internal function to create a process.
209 * This function actually creates the process.
210 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
211 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
213 * \return the process created
215 smx_actor_t SIMIX_process_create(
217 std::function<void()> code,
220 xbt_dict_t properties,
221 smx_actor_t parent_process)
224 XBT_DEBUG("Start process %s on host '%s'", name, host->cname());
227 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->cname());
231 smx_actor_t process = new simgrid::simix::ActorImpl();
233 xbt_assert(code && host != nullptr, "Invalid parameters");
235 process->pid = simix_process_maxpid++;
236 process->name = simgrid::xbt::string(name);
237 process->host = host;
238 process->data = data;
239 process->simcall.issuer = process;
241 if (parent_process != nullptr) {
242 process->ppid = parent_process->pid;
243 /* SMPI process have their own data segment and each other inherit from their father */
245 if (smpi_privatize_global_variables) {
246 if (parent_process->pid != 0) {
247 SIMIX_segment_index_set(process, parent_process->segment_index);
249 SIMIX_segment_index_set(process, process->pid - 1);
255 process->code = code;
257 XBT_VERB("Create context %s", process->name.c_str());
258 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
261 process->properties = properties;
263 /* Add the process to it's host process list */
264 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
266 XBT_DEBUG("Start context '%s'", process->name.c_str());
268 /* Now insert it in the global process list and in the process to run list */
269 simix_global->process_list[process->pid] = process;
270 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
271 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
273 /* Tracing the process creation */
274 TRACE_msg_process_create(process->cname(), process->pid, process->host);
279 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname, xbt_dict_t properties,
280 smx_actor_t parent_process)
282 // This is mostly a copy/paste from SIMIX_process_new(),
283 // it'd be nice to share some code between those two functions.
285 sg_host_t host = sg_host_by_name(hostname);
286 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
289 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
294 smx_actor_t process = new simgrid::simix::ActorImpl();
296 process->pid = simix_process_maxpid++;
297 process->name = std::string(name);
298 process->host = host;
299 process->data = data;
300 process->simcall.issuer = process;
302 if (parent_process != nullptr) {
303 process->ppid = parent_process->pid;
304 /* SMPI process have their own data segment and each other inherit from their father */
306 if (smpi_privatize_global_variables) {
307 if (parent_process->pid != 0) {
308 SIMIX_segment_index_set(process, parent_process->segment_index);
310 SIMIX_segment_index_set(process, process->pid - 1);
316 /* Process data for auto-restart */
317 process->code = nullptr;
319 XBT_VERB("Create context %s", process->name.c_str());
321 xbt_die("simix is not initialized, please call MSG_init first");
322 process->context = simix_global->context_factory->attach(
323 simix_global->cleanup_process_function, process);
326 process->properties = properties;
328 /* Add the process to it's host process list */
329 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
331 /* Now insert it in the global process list and in the process to run list */
332 simix_global->process_list[process->pid] = process;
333 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
334 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
336 /* Tracing the process creation */
337 TRACE_msg_process_create(process->cname(), process->pid, process->host);
339 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
341 xbt_die("Not a suitable context");
343 context->attach_start();
347 void SIMIX_process_detach()
349 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
351 xbt_die("Not a suitable context");
353 simix_global->cleanup_process_function(context->process());
355 // Let maestro ignore we are still alive:
356 // xbt_swag_remove(context->process(), simix_global->process_list);
358 // TODO, Remove from proces list:
359 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
361 context->attach_stop();
366 * \brief Executes the processes from simix_global->process_to_run.
368 * The processes of simix_global->process_to_run are run (in parallel if
369 * possible). On exit, simix_global->process_to_run is empty, and
370 * simix_global->process_that_ran contains the list of processes that just ran.
371 * The two lists are swapped so, be careful when using them before and after a
372 * call to this function.
374 void SIMIX_process_runall()
376 SIMIX_context_runall();
378 xbt_dynar_t tmp = simix_global->process_that_ran;
379 simix_global->process_that_ran = simix_global->process_to_run;
380 simix_global->process_to_run = tmp;
381 xbt_dynar_reset(simix_global->process_to_run);
384 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_actor_t process) {
385 SIMIX_process_kill(process, simcall->issuer);
388 * \brief Internal function to kill a SIMIX process.
390 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
391 * or directly for SIMIX internal purposes.
393 * \param process poor victim
394 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
396 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
398 XBT_DEBUG("Killing process %s@%s", process->cname(), process->host->cname());
400 process->context->iwannadie = 1;
401 process->blocked = 0;
402 process->suspended = 0;
403 process->exception = nullptr;
405 /* destroy the blocking synchro if any */
406 if (process->waiting_synchro) {
408 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
409 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
410 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
411 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
412 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
414 if (exec != nullptr) {
417 } else if (comm != nullptr) {
418 xbt_fifo_remove(process->comms, process->waiting_synchro);
421 // Remove first occurrence of &process->simcall:
422 auto i = boost::range::find(
423 process->waiting_synchro->simcalls,
425 if (i != process->waiting_synchro->simcalls.end())
426 process->waiting_synchro->simcalls.remove(&process->simcall);
430 } else if (sleep != nullptr) {
431 SIMIX_process_sleep_destroy(process->waiting_synchro);
433 } else if (raw != nullptr) {
434 SIMIX_synchro_stop_waiting(process, &process->simcall);
435 delete process->waiting_synchro;
437 } else if (io != nullptr) {
438 SIMIX_io_destroy(process->waiting_synchro);
442 switch (process->waiting_synchro->type) {
443 case SIMIX_SYNC_JOIN:
444 SIMIX_process_sleep_destroy(process->waiting_synchro);
448 process->waiting_synchro = nullptr;
450 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
451 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
452 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
456 /** @brief Ask another process to raise the given exception
458 * @param process The process that should raise that exception
459 * @param cat category of exception
460 * @param value value associated to the exception
461 * @param msg string information associated to the exception
463 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
464 SMX_EXCEPTION(process, cat, value, msg);
466 if (process->suspended)
467 SIMIX_process_resume(process);
469 /* cancel the blocking synchro if any */
470 if (process->waiting_synchro) {
472 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
473 if (exec != nullptr) {
474 SIMIX_execution_cancel(process->waiting_synchro);
477 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
478 if (comm != nullptr) {
479 xbt_fifo_remove(process->comms, comm);
483 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
484 if (sleep != nullptr) {
485 SIMIX_process_sleep_destroy(process->waiting_synchro);
486 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
487 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
488 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
492 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
493 if (raw != nullptr) {
494 SIMIX_synchro_stop_waiting(process, &process->simcall);
497 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
499 SIMIX_io_destroy(process->waiting_synchro);
502 process->waiting_synchro = nullptr;
506 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
507 SIMIX_process_killall(simcall->issuer, reset_pid);
510 * \brief Kills all running processes.
511 * \param issuer this one will not be killed
513 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
515 for (auto kv : simix_global->process_list)
516 if (kv.second != issuer)
517 SIMIX_process_kill(kv.second, issuer);
520 simix_process_maxpid = reset_pid;
522 SIMIX_context_runall();
524 SIMIX_process_empty_trash();
527 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_actor_t process, sg_host_t dest)
529 process->new_host = dest;
531 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
533 xbt_assert((process != nullptr), "Invalid parameters");
534 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
535 process->host = dest;
536 xbt_swag_insert(process, dest->extension<simgrid::simix::Host>()->process_list);
540 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
542 smx_activity_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
544 if (process != simcall->issuer) {
545 SIMIX_simcall_answer(simcall);
547 sync_suspend->simcalls.push_back(simcall);
548 process->waiting_synchro = sync_suspend;
549 process->waiting_synchro->suspend();
551 /* If we are suspending ourselves, then just do not finish the simcall now */
554 smx_activity_t SIMIX_process_suspend(smx_actor_t process, smx_actor_t issuer)
556 if (process->suspended) {
557 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
561 process->suspended = 1;
563 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
564 if (process != issuer) {
566 if (process->waiting_synchro)
567 process->waiting_synchro->suspend();
568 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
572 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0);
576 void SIMIX_process_resume(smx_actor_t process)
578 XBT_IN("process = %p", process);
580 if(process->context->iwannadie) {
581 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
585 if(!process->suspended) return;
586 process->suspended = 0;
588 /* resume the synchronization that was blocking the resumed process. */
589 if (process->waiting_synchro)
590 process->waiting_synchro->resume();
595 int SIMIX_process_get_maxpid() {
596 return simix_process_maxpid;
599 int SIMIX_process_count()
601 return simix_global->process_list.size();
604 int SIMIX_process_get_PID(smx_actor_t self)
612 void* SIMIX_process_self_get_data()
614 smx_actor_t self = SIMIX_process_self();
622 void SIMIX_process_self_set_data(void *data)
624 smx_actor_t self = SIMIX_process_self();
626 SIMIX_process_set_data(self, data);
629 void SIMIX_process_set_data(smx_actor_t process, void *data)
631 process->data = data;
634 /* needs to be public and without simcall because it is called
635 by exceptions and logging events */
636 const char* SIMIX_process_self_get_name() {
638 smx_actor_t process = SIMIX_process_self();
639 if (process == nullptr || process == simix_global->maestro_process)
642 return process->name.c_str();
645 smx_actor_t SIMIX_process_get_by_name(const char* name)
647 for (auto kv : simix_global->process_list)
648 if (kv.second->name == name)
653 int SIMIX_process_is_suspended(smx_actor_t process)
655 return process->suspended;
658 xbt_dict_t SIMIX_process_get_properties(smx_actor_t process)
660 return process->properties;
663 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
665 if (process->finished) {
666 // The joined process is already finished, just wake up the issuer process right away
667 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
668 SIMIX_simcall_answer(simcall);
671 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
672 sync->simcalls.push_back(simcall);
673 simcall->issuer->waiting_synchro = sync;
676 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_activity_t synchro){
677 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
679 if (sleep->surf_sleep) {
680 sleep->surf_sleep->cancel();
682 while (!sleep->simcalls.empty()) {
683 smx_simcall_t simcall = sleep->simcalls.front();
684 sleep->simcalls.pop_front();
685 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
686 simcall->issuer->waiting_synchro = nullptr;
687 if (simcall->issuer->suspended) {
688 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
689 simcall->issuer->suspended = 0;
690 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
692 SIMIX_simcall_answer(simcall);
695 sleep->surf_sleep->unref();
696 sleep->surf_sleep = nullptr;
702 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
704 smx_activity_t res = SIMIX_process_sleep(issuer, timeout);
705 static_cast<simgrid::kernel::activity::ActivityImpl*>(res)->ref();
706 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
710 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
712 if (MC_is_active() || MC_record_replay_is_active()) {
713 MC_process_clock_add(simcall->issuer, duration);
714 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
715 SIMIX_simcall_answer(simcall);
718 smx_activity_t sync = SIMIX_process_sleep(simcall->issuer, duration);
719 sync->simcalls.push_back(simcall);
720 simcall->issuer->waiting_synchro = sync;
723 smx_activity_t SIMIX_process_sleep(smx_actor_t process, double duration)
725 sg_host_t host = process->host;
728 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->cname());
730 simgrid::kernel::activity::Sleep *synchro = new simgrid::kernel::activity::Sleep();
731 synchro->host = host;
732 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
733 synchro->surf_sleep->setData(synchro);
734 XBT_DEBUG("Create sleep synchronization %p", synchro);
739 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
741 XBT_DEBUG("Destroy synchro %p", synchro);
742 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
744 if (sleep->surf_sleep) {
745 sleep->surf_sleep->unref();
746 sleep->surf_sleep = nullptr;
752 * \brief Calling this function makes the process to yield.
754 * Only the current process can call this function, giving back the control to
757 * \param self the current process
759 void SIMIX_process_yield(smx_actor_t self)
761 XBT_DEBUG("Yield actor '%s'", self->cname());
763 /* Go into sleep and return control to maestro */
764 self->context->suspend();
766 /* Ok, maestro returned control to us */
767 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
769 if (self->new_host) {
770 SIMIX_process_change_host(self, self->new_host);
771 self->new_host = nullptr;
774 if (self->context->iwannadie){
775 XBT_DEBUG("I wanna die!");
776 self->finished = true;
777 /* execute the on_exit functions */
778 SIMIX_process_on_exit_runall(self);
779 /* Add the process to the list of process to restart, only if the host is down */
780 if (self->auto_restart && self->host->isOff()) {
781 SIMIX_host_add_auto_restart_process(self->host, self->cname(),
782 self->code, self->data,
783 SIMIX_timer_get_date(self->kill_timer),
787 XBT_DEBUG("Process %s@%s is dead", self->cname(), self->host->cname());
788 self->context->stop();
791 if (self->suspended) {
792 XBT_DEBUG("Hey! I'm suspended.");
793 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
795 SIMIX_process_suspend(self, self);
798 if (self->exception != nullptr) {
799 XBT_DEBUG("Wait, maestro left me an exception");
800 std::exception_ptr exception = std::move(self->exception);
801 self->exception = nullptr;
802 std::rethrow_exception(std::move(exception));
805 if(SMPI_switch_data_segment && self->segment_index != -1){
806 SMPI_switch_data_segment(self->segment_index);
810 /* callback: termination */
811 void SIMIX_process_exception_terminate(xbt_ex_t * e)
817 smx_context_t SIMIX_process_get_context(smx_actor_t p) {
821 void SIMIX_process_set_context(smx_actor_t p,smx_context_t c) {
826 * \brief Returns the list of processes to run.
828 xbt_dynar_t SIMIX_process_get_runnable()
830 return simix_global->process_to_run;
834 * \brief Returns the process from PID.
836 smx_actor_t SIMIX_process_from_PID(int PID)
838 if (simix_global->process_list.find(PID) == simix_global->process_list.end())
840 return simix_global->process_list.at(PID);
843 /** @brief returns a dynar containing all currently existing processes */
844 xbt_dynar_t SIMIX_processes_as_dynar() {
845 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_actor_t),nullptr);
846 for (auto kv : simix_global->process_list) {
847 smx_actor_t proc = kv.second;
848 xbt_dynar_push(res,&proc);
853 void SIMIX_process_on_exit_runall(smx_actor_t process) {
854 s_smx_process_exit_fun_t exit_fun;
855 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
856 while (!xbt_dynar_is_empty(process->on_exit)) {
857 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
858 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
862 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
863 xbt_assert(process, "current process not found: are you in maestro context ?");
865 if (!process->on_exit) {
866 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
869 s_smx_process_exit_fun_t exit_fun = {fun, data};
871 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
875 * \brief Sets the auto-restart status of the process.
876 * If set to 1, the process will be automatically restarted when its host
879 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
880 process->auto_restart = auto_restart;
883 smx_actor_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_actor_t process) {
884 return SIMIX_process_restart(process, simcall->issuer);
886 /** @brief Restart a process, starting it again from the beginning. */
887 smx_actor_t SIMIX_process_restart(smx_actor_t process, smx_actor_t issuer) {
888 XBT_DEBUG("Restarting process %s on %s", process->cname(), process->host->cname());
890 //retrieve the arguments of the old process
891 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
892 simgrid::simix::ProcessArg arg;
893 arg.name = process->name;
894 arg.code = process->code;
895 arg.host = process->host;
896 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
897 arg.data = process->data;
898 arg.properties = nullptr;
899 arg.auto_restart = process->auto_restart;
901 //kill the old process
902 SIMIX_process_kill(process, issuer);
904 //start the new process
905 smx_actor_t actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
906 arg.properties, nullptr);
907 if (arg.kill_time >= 0)
908 simcall_process_set_kill_time(actor, arg.kill_time);
909 if (arg.auto_restart)
910 simcall_process_auto_restart_set(actor, arg.auto_restart);
915 void SIMIX_segment_index_set(smx_actor_t proc, int index){
916 proc->segment_index = index;
920 * \ingroup simix_process_management
921 * \brief Creates and runs a new SIMIX process.
923 * The structure and the corresponding thread are created and put in the list of ready processes.
925 * \param name a name for the process. It is for user-level information and can be nullptr.
926 * \param code the main function of the process
927 * \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.
928 * It can be retrieved with the function \ref simcall_process_get_data.
929 * \param host where the new agent is executed.
930 * \param kill_time time when the process is killed
931 * \param argc first argument passed to \a code
932 * \param argv second argument passed to \a code
933 * \param properties the properties of the process
934 * \param auto_restart either it is autorestarting or not.
936 smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
937 char** argv, xbt_dict_t properties)
941 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
942 for (int i = 0; i != argc; ++i)
945 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
949 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
950 xbt_dict_t properties)
954 smx_actor_t self = SIMIX_process_self();
955 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
956 return SIMIX_process_create(name, std::move(code), data, host, properties, self);