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 process->comms.size() > 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>(process->comms.front());
99 while (!process->comms.empty()) {
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 comm->src_proc->comms.remove(comm);
130 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
132 process->comms.pop_front();
133 synchro = static_cast<smx_activity_t>(process->comms.front());
136 XBT_DEBUG("%p should not be run anymore",process);
137 simix_global->process_list.erase(process->pid);
139 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
140 xbt_swag_insert(process, simix_global->process_to_destroy);
141 process->context->iwannadie = 0;
143 xbt_os_mutex_release(simix_global->mutex);
149 * Should be called some time to time to free the memory allocated for processes
150 * that have finished (or killed).
152 void SIMIX_process_empty_trash()
154 smx_actor_t process = nullptr;
156 while ((process = (smx_actor_t) xbt_swag_extract(simix_global->process_to_destroy))) {
157 XBT_DEBUG("Getting rid of %p",process);
158 intrusive_ptr_release(process);
165 ActorImpl::~ActorImpl()
167 delete this->context;
168 xbt_dict_free(&this->properties);
169 xbt_dynar_free(&this->on_exit);
172 void create_maestro(std::function<void()> code)
174 smx_actor_t maestro = nullptr;
175 /* Create maestro process and initialize it */
176 maestro = new simgrid::simix::ActorImpl();
177 maestro->pid = simix_process_maxpid++;
179 maestro->data = nullptr;
182 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
185 xbt_die("simix is not initialized, please call MSG_init first");
187 simix_global->context_factory->create_maestro(code, maestro);
190 maestro->simcall.issuer = maestro;
191 simix_global->maestro_process = maestro;
197 /** @brief Creates and runs the maestro process */
198 void SIMIX_maestro_create(void (*code)(void*), void* data)
200 simgrid::simix::create_maestro(std::bind(code, data));
204 * \brief Internal function to create a process.
206 * This function actually creates the process.
207 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
208 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
210 * \return the process created
212 smx_actor_t SIMIX_process_create(
214 std::function<void()> code,
217 xbt_dict_t properties,
218 smx_actor_t parent_process)
221 XBT_DEBUG("Start process %s on host '%s'", name, host->cname());
224 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->cname());
228 smx_actor_t process = new simgrid::simix::ActorImpl();
230 xbt_assert(code && host != nullptr, "Invalid parameters");
232 process->pid = simix_process_maxpid++;
233 process->name = simgrid::xbt::string(name);
234 process->host = host;
235 process->data = data;
236 process->simcall.issuer = process;
238 if (parent_process != nullptr) {
239 process->ppid = parent_process->pid;
240 /* SMPI process have their own data segment and each other inherit from their father */
242 if (smpi_privatize_global_variables) {
243 if (parent_process->pid != 0) {
244 SIMIX_segment_index_set(process, parent_process->segment_index);
246 SIMIX_segment_index_set(process, process->pid - 1);
252 process->code = code;
254 XBT_VERB("Create context %s", process->name.c_str());
255 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
258 process->properties = properties;
260 /* Add the process to it's host process list */
261 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
263 XBT_DEBUG("Start context '%s'", process->name.c_str());
265 /* Now insert it in the global process list and in the process to run list */
266 simix_global->process_list[process->pid] = process;
267 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
268 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
270 /* Tracing the process creation */
271 TRACE_msg_process_create(process->cname(), process->pid, process->host);
276 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname, xbt_dict_t properties,
277 smx_actor_t parent_process)
279 // This is mostly a copy/paste from SIMIX_process_new(),
280 // it'd be nice to share some code between those two functions.
282 sg_host_t host = sg_host_by_name(hostname);
283 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
286 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
291 smx_actor_t process = new simgrid::simix::ActorImpl();
293 process->pid = simix_process_maxpid++;
294 process->name = std::string(name);
295 process->host = host;
296 process->data = data;
297 process->simcall.issuer = process;
299 if (parent_process != nullptr) {
300 process->ppid = parent_process->pid;
301 /* SMPI process have their own data segment and each other inherit from their father */
303 if (smpi_privatize_global_variables) {
304 if (parent_process->pid != 0) {
305 SIMIX_segment_index_set(process, parent_process->segment_index);
307 SIMIX_segment_index_set(process, process->pid - 1);
313 /* Process data for auto-restart */
314 process->code = nullptr;
316 XBT_VERB("Create context %s", process->name.c_str());
318 xbt_die("simix is not initialized, please call MSG_init first");
319 process->context = simix_global->context_factory->attach(
320 simix_global->cleanup_process_function, process);
323 process->properties = properties;
325 /* Add the process to it's host process list */
326 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
328 /* Now insert it in the global process list and in the process to run list */
329 simix_global->process_list[process->pid] = process;
330 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
331 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
333 /* Tracing the process creation */
334 TRACE_msg_process_create(process->cname(), process->pid, process->host);
336 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
338 xbt_die("Not a suitable context");
340 context->attach_start();
344 void SIMIX_process_detach()
346 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
348 xbt_die("Not a suitable context");
350 simix_global->cleanup_process_function(context->process());
352 // Let maestro ignore we are still alive:
353 // xbt_swag_remove(context->process(), simix_global->process_list);
355 // TODO, Remove from proces list:
356 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
358 context->attach_stop();
363 * \brief Executes the processes from simix_global->process_to_run.
365 * The processes of simix_global->process_to_run are run (in parallel if
366 * possible). On exit, simix_global->process_to_run is empty, and
367 * simix_global->process_that_ran contains the list of processes that just ran.
368 * The two lists are swapped so, be careful when using them before and after a
369 * call to this function.
371 void SIMIX_process_runall()
373 SIMIX_context_runall();
375 xbt_dynar_t tmp = simix_global->process_that_ran;
376 simix_global->process_that_ran = simix_global->process_to_run;
377 simix_global->process_to_run = tmp;
378 xbt_dynar_reset(simix_global->process_to_run);
381 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_actor_t process) {
382 SIMIX_process_kill(process, simcall->issuer);
385 * \brief Internal function to kill a SIMIX process.
387 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
388 * or directly for SIMIX internal purposes.
390 * \param process poor victim
391 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
393 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
395 XBT_DEBUG("Killing process %s@%s", process->cname(), process->host->cname());
397 process->context->iwannadie = 1;
398 process->blocked = 0;
399 process->suspended = 0;
400 process->exception = nullptr;
402 /* destroy the blocking synchro if any */
403 if (process->waiting_synchro) {
405 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
406 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
407 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
408 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
409 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
411 if (exec != nullptr) {
414 } else if (comm != nullptr) {
415 process->comms.remove(process->waiting_synchro);
418 // Remove first occurrence of &process->simcall:
419 auto i = boost::range::find(
420 process->waiting_synchro->simcalls,
422 if (i != process->waiting_synchro->simcalls.end())
423 process->waiting_synchro->simcalls.remove(&process->simcall);
427 } else if (sleep != nullptr) {
428 SIMIX_process_sleep_destroy(process->waiting_synchro);
430 } else if (raw != nullptr) {
431 SIMIX_synchro_stop_waiting(process, &process->simcall);
432 delete process->waiting_synchro;
434 } else if (io != nullptr) {
435 SIMIX_io_destroy(process->waiting_synchro);
439 switch (process->waiting_synchro->type) {
440 case SIMIX_SYNC_JOIN:
441 SIMIX_process_sleep_destroy(process->waiting_synchro);
445 process->waiting_synchro = nullptr;
447 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
448 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
449 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
453 /** @brief Ask another process to raise the given exception
455 * @param process The process that should raise that exception
456 * @param cat category of exception
457 * @param value value associated to the exception
458 * @param msg string information associated to the exception
460 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
461 SMX_EXCEPTION(process, cat, value, msg);
463 if (process->suspended)
464 SIMIX_process_resume(process);
466 /* cancel the blocking synchro if any */
467 if (process->waiting_synchro) {
469 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
470 if (exec != nullptr) {
471 SIMIX_execution_cancel(process->waiting_synchro);
474 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
475 if (comm != nullptr) {
476 process->comms.remove(comm);
480 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
481 if (sleep != nullptr) {
482 SIMIX_process_sleep_destroy(process->waiting_synchro);
483 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
484 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
485 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
489 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
490 if (raw != nullptr) {
491 SIMIX_synchro_stop_waiting(process, &process->simcall);
494 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
496 SIMIX_io_destroy(process->waiting_synchro);
499 process->waiting_synchro = nullptr;
503 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
504 SIMIX_process_killall(simcall->issuer, reset_pid);
507 * \brief Kills all running processes.
508 * \param issuer this one will not be killed
510 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
512 for (auto kv : simix_global->process_list)
513 if (kv.second != issuer)
514 SIMIX_process_kill(kv.second, issuer);
517 simix_process_maxpid = reset_pid;
519 SIMIX_context_runall();
521 SIMIX_process_empty_trash();
524 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_actor_t process, sg_host_t dest)
526 process->new_host = dest;
528 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
530 xbt_assert((process != nullptr), "Invalid parameters");
531 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
532 process->host = dest;
533 xbt_swag_insert(process, dest->extension<simgrid::simix::Host>()->process_list);
537 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
539 smx_activity_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
541 if (process != simcall->issuer) {
542 SIMIX_simcall_answer(simcall);
544 sync_suspend->simcalls.push_back(simcall);
545 process->waiting_synchro = sync_suspend;
546 process->waiting_synchro->suspend();
548 /* If we are suspending ourselves, then just do not finish the simcall now */
551 smx_activity_t SIMIX_process_suspend(smx_actor_t process, smx_actor_t issuer)
553 if (process->suspended) {
554 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
558 process->suspended = 1;
560 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
561 if (process != issuer) {
563 if (process->waiting_synchro)
564 process->waiting_synchro->suspend();
565 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
569 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0);
573 void SIMIX_process_resume(smx_actor_t process)
575 XBT_IN("process = %p", process);
577 if(process->context->iwannadie) {
578 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
582 if(!process->suspended) return;
583 process->suspended = 0;
585 /* resume the synchronization that was blocking the resumed process. */
586 if (process->waiting_synchro)
587 process->waiting_synchro->resume();
592 int SIMIX_process_get_maxpid() {
593 return simix_process_maxpid;
596 int SIMIX_process_count()
598 return simix_global->process_list.size();
601 int SIMIX_process_get_PID(smx_actor_t self)
609 void* SIMIX_process_self_get_data()
611 smx_actor_t self = SIMIX_process_self();
619 void SIMIX_process_self_set_data(void *data)
621 smx_actor_t self = SIMIX_process_self();
623 SIMIX_process_set_data(self, data);
626 void SIMIX_process_set_data(smx_actor_t process, void *data)
628 process->data = data;
631 /* needs to be public and without simcall because it is called
632 by exceptions and logging events */
633 const char* SIMIX_process_self_get_name() {
635 smx_actor_t process = SIMIX_process_self();
636 if (process == nullptr || process == simix_global->maestro_process)
639 return process->name.c_str();
642 smx_actor_t SIMIX_process_get_by_name(const char* name)
644 for (auto kv : simix_global->process_list)
645 if (kv.second->name == name)
650 int SIMIX_process_is_suspended(smx_actor_t process)
652 return process->suspended;
655 xbt_dict_t SIMIX_process_get_properties(smx_actor_t process)
657 return process->properties;
660 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
662 if (process->finished) {
663 // The joined process is already finished, just wake up the issuer process right away
664 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
665 SIMIX_simcall_answer(simcall);
668 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
669 sync->simcalls.push_back(simcall);
670 simcall->issuer->waiting_synchro = sync;
673 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_activity_t synchro){
674 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
676 if (sleep->surf_sleep) {
677 sleep->surf_sleep->cancel();
679 while (!sleep->simcalls.empty()) {
680 smx_simcall_t simcall = sleep->simcalls.front();
681 sleep->simcalls.pop_front();
682 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
683 simcall->issuer->waiting_synchro = nullptr;
684 if (simcall->issuer->suspended) {
685 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
686 simcall->issuer->suspended = 0;
687 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
689 SIMIX_simcall_answer(simcall);
692 sleep->surf_sleep->unref();
693 sleep->surf_sleep = nullptr;
696 // intrusive_ptr_release(process); // FIXME: We are leaking here. See comment in SIMIX_process_join()
700 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
702 smx_activity_t res = SIMIX_process_sleep(issuer, timeout);
703 static_cast<simgrid::kernel::activity::ActivityImpl*>(res)->ref();
704 /* We are leaking the process here, but if we don't take the ref, we get a "use after free".
705 * The correct solution would be to derivate the type SynchroSleep into a SynchroProcessJoin,
706 * but the code is not clean enough for now for this.
707 * The C API should first be properly replaced with the C++ one, which is a fair amount of work.
709 intrusive_ptr_add_ref(process);
710 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
714 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
716 if (MC_is_active() || MC_record_replay_is_active()) {
717 MC_process_clock_add(simcall->issuer, duration);
718 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
719 SIMIX_simcall_answer(simcall);
722 smx_activity_t sync = SIMIX_process_sleep(simcall->issuer, duration);
723 sync->simcalls.push_back(simcall);
724 simcall->issuer->waiting_synchro = sync;
727 smx_activity_t SIMIX_process_sleep(smx_actor_t process, double duration)
729 sg_host_t host = process->host;
732 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->cname());
734 simgrid::kernel::activity::Sleep *synchro = new simgrid::kernel::activity::Sleep();
735 synchro->host = host;
736 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
737 synchro->surf_sleep->setData(synchro);
738 XBT_DEBUG("Create sleep synchronization %p", synchro);
743 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
745 XBT_DEBUG("Destroy synchro %p", synchro);
746 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
748 if (sleep->surf_sleep) {
749 sleep->surf_sleep->unref();
750 sleep->surf_sleep = nullptr;
756 * \brief Calling this function makes the process to yield.
758 * Only the current process can call this function, giving back the control to
761 * \param self the current process
763 void SIMIX_process_yield(smx_actor_t self)
765 XBT_DEBUG("Yield actor '%s'", self->cname());
767 /* Go into sleep and return control to maestro */
768 self->context->suspend();
770 /* Ok, maestro returned control to us */
771 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
773 if (self->new_host) {
774 SIMIX_process_change_host(self, self->new_host);
775 self->new_host = nullptr;
778 if (self->context->iwannadie){
779 XBT_DEBUG("I wanna die!");
780 self->finished = true;
781 /* execute the on_exit functions */
782 SIMIX_process_on_exit_runall(self);
783 /* Add the process to the list of process to restart, only if the host is down */
784 if (self->auto_restart && self->host->isOff()) {
785 SIMIX_host_add_auto_restart_process(self->host, self->cname(),
786 self->code, self->data,
787 SIMIX_timer_get_date(self->kill_timer),
791 XBT_DEBUG("Process %s@%s is dead", self->cname(), self->host->cname());
792 self->context->stop();
795 if (self->suspended) {
796 XBT_DEBUG("Hey! I'm suspended.");
797 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
799 SIMIX_process_suspend(self, self);
802 if (self->exception != nullptr) {
803 XBT_DEBUG("Wait, maestro left me an exception");
804 std::exception_ptr exception = std::move(self->exception);
805 self->exception = nullptr;
806 std::rethrow_exception(std::move(exception));
809 if(SMPI_switch_data_segment && self->segment_index != -1){
810 SMPI_switch_data_segment(self->segment_index);
814 /* callback: termination */
815 void SIMIX_process_exception_terminate(xbt_ex_t * e)
821 smx_context_t SIMIX_process_get_context(smx_actor_t p) {
825 void SIMIX_process_set_context(smx_actor_t p,smx_context_t c) {
830 * \brief Returns the list of processes to run.
832 xbt_dynar_t SIMIX_process_get_runnable()
834 return simix_global->process_to_run;
838 * \brief Returns the process from PID.
840 smx_actor_t SIMIX_process_from_PID(int PID)
842 if (simix_global->process_list.find(PID) == simix_global->process_list.end())
844 return simix_global->process_list.at(PID);
847 /** @brief returns a dynar containing all currently existing processes */
848 xbt_dynar_t SIMIX_processes_as_dynar() {
849 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_actor_t),nullptr);
850 for (auto kv : simix_global->process_list) {
851 smx_actor_t proc = kv.second;
852 xbt_dynar_push(res,&proc);
857 void SIMIX_process_on_exit_runall(smx_actor_t process) {
858 s_smx_process_exit_fun_t exit_fun;
859 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
860 while (!xbt_dynar_is_empty(process->on_exit)) {
861 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
862 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
866 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
867 xbt_assert(process, "current process not found: are you in maestro context ?");
869 if (!process->on_exit) {
870 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
873 s_smx_process_exit_fun_t exit_fun = {fun, data};
875 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
879 * \brief Sets the auto-restart status of the process.
880 * If set to 1, the process will be automatically restarted when its host
883 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
884 process->auto_restart = auto_restart;
887 smx_actor_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_actor_t process) {
888 return SIMIX_process_restart(process, simcall->issuer);
890 /** @brief Restart a process, starting it again from the beginning. */
891 smx_actor_t SIMIX_process_restart(smx_actor_t process, smx_actor_t issuer) {
892 XBT_DEBUG("Restarting process %s on %s", process->cname(), process->host->cname());
894 //retrieve the arguments of the old process
895 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
896 simgrid::simix::ProcessArg arg;
897 arg.name = process->name;
898 arg.code = process->code;
899 arg.host = process->host;
900 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
901 arg.data = process->data;
902 arg.properties = nullptr;
903 arg.auto_restart = process->auto_restart;
905 //kill the old process
906 SIMIX_process_kill(process, issuer);
908 //start the new process
909 smx_actor_t actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
910 arg.properties, nullptr);
911 if (arg.kill_time >= 0)
912 simcall_process_set_kill_time(actor, arg.kill_time);
913 if (arg.auto_restart)
914 simcall_process_auto_restart_set(actor, arg.auto_restart);
919 void SIMIX_segment_index_set(smx_actor_t proc, int index){
920 proc->segment_index = index;
924 * \ingroup simix_process_management
925 * \brief Creates and runs a new SIMIX process.
927 * The structure and the corresponding thread are created and put in the list of ready processes.
929 * \param name a name for the process. It is for user-level information and can be nullptr.
930 * \param code the main function of the process
931 * \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.
932 * It can be retrieved with the function \ref simcall_process_get_data.
933 * \param host where the new agent is executed.
934 * \param kill_time time when the process is killed
935 * \param argc first argument passed to \a code
936 * \param argv second argument passed to \a code
937 * \param properties the properties of the process
938 * \param auto_restart either it is autorestarting or not.
940 smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
941 char** argv, xbt_dict_t properties)
945 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
946 for (int i = 0; i != argc; ++i)
949 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
953 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
954 xbt_dict_t properties)
958 smx_actor_t self = SIMIX_process_self();
959 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
960 return SIMIX_process_create(name, std::move(code), data, host, properties, self);