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 "src/surf/surf_interface.hpp"
10 #include "smx_private.h"
11 #include "xbt/sysdep.h"
15 #include "src/mc/mc_replay.h"
16 #include "src/mc/Client.hpp"
17 #include "src/msg/msg_private.h"
19 #include "src/simix/SynchroSleep.hpp"
20 #include "src/simix/SynchroRaw.hpp"
21 #include "src/simix/SynchroIo.hpp"
24 #include "src/smpi/private.h"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
29 unsigned long simix_process_maxpid = 0;
32 * \brief Returns the current agent.
34 * This functions returns the currently running SIMIX process.
36 * \return The SIMIX process
38 smx_process_t SIMIX_process_self(void)
40 smx_context_t self_context = SIMIX_context_self();
42 return self_context ? self_context->process() : nullptr;
46 * \brief Returns whether a process has pending asynchronous communications.
47 * \return true if there are asynchronous communications in this process
49 int SIMIX_process_has_pending_comms(smx_process_t process) {
51 return xbt_fifo_size(process->comms) > 0;
55 * \brief Moves a process to the list of processes to destroy.
57 void SIMIX_process_cleanup(smx_process_t process)
59 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
60 process->name.c_str(), process, process->waiting_synchro);
62 SIMIX_process_on_exit_runall(process);
64 /* Unregister from the kill timer if any */
65 if (process->kill_timer != nullptr)
66 SIMIX_timer_remove(process->kill_timer);
68 xbt_os_mutex_acquire(simix_global->mutex);
70 /* cancel non-blocking communications */
71 smx_synchro_t synchro;
72 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
73 simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
75 /* make sure no one will finish the comm after this process is destroyed,
76 * because src_proc or dst_proc would be an invalid pointer */
79 if (comm->src_proc == process) {
80 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
81 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
82 comm->src_proc = nullptr;
84 /* I'm not supposed to destroy a detached comm from the sender side, */
86 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
91 else if (comm->dst_proc == process){
92 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
93 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
94 comm->dst_proc = nullptr;
96 if (comm->detached && comm->src_proc != nullptr) {
97 /* the comm will be freed right now, remove it from the sender */
98 xbt_fifo_remove(comm->src_proc->comms, comm);
103 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
107 XBT_DEBUG("%p should not be run anymore",process);
108 xbt_swag_remove(process, simix_global->process_list);
110 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
111 xbt_swag_insert(process, simix_global->process_to_destroy);
112 process->context->iwannadie = 0;
114 xbt_os_mutex_release(simix_global->mutex);
120 * Should be called some time to time to free the memory allocated for processes
121 * that have finished (or killed).
123 void SIMIX_process_empty_trash(void)
125 smx_process_t process = nullptr;
127 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
128 XBT_DEBUG("Getting rid of %p",process);
129 intrusive_ptr_release(process);
138 delete this->context;
139 if (this->properties)
140 xbt_dict_free(&this->properties);
141 if (this->comms != nullptr)
142 xbt_fifo_free(this->comms);
144 xbt_dynar_free(&this->on_exit);
147 void create_maestro(std::function<void()> code)
149 smx_process_t maestro = nullptr;
150 /* Create maestro process and intilialize it */
151 maestro = new simgrid::simix::Process();
152 maestro->pid = simix_process_maxpid++;
155 maestro->data = nullptr;
158 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
161 xbt_die("simix is not initialized, please call MSG_init first");
163 simix_global->context_factory->create_maestro(code, maestro);
166 maestro->simcall.issuer = maestro;
167 simix_global->maestro_process = maestro;
174 * \brief Creates and runs the maestro process
176 void SIMIX_maestro_create(void (*code)(void*), void* data)
178 simgrid::simix::create_maestro(std::bind(code, data));
182 * \brief Stops a process.
184 * Stops the process, execute all the registered on_exit functions,
185 * register it to the list of the process to restart if needed
186 * and stops its context.
188 void SIMIX_process_stop(smx_process_t arg) {
189 /* execute the on_exit functions */
190 SIMIX_process_on_exit_runall(arg);
191 /* Add the process to the list of process to restart, only if the host is down */
192 if (arg->auto_restart && arg->host->isOff()) {
193 SIMIX_host_add_auto_restart_process(arg->host, arg->name.c_str(),
194 arg->code, arg->data,
195 sg_host_get_name(arg->host),
196 SIMIX_timer_get_date(arg->kill_timer),
200 XBT_DEBUG("Process %s (%s) is dead",
201 arg->name.c_str(), sg_host_get_name(arg->host));
202 arg->context->stop();
206 * \brief Internal function to create a process.
208 * This function actually creates the process.
209 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
210 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
212 * \return the process created
214 smx_process_t SIMIX_process_create(
216 std::function<void()> code,
218 const char *hostname,
220 xbt_dict_t properties,
222 smx_process_t parent_process)
224 smx_process_t process = nullptr;
225 sg_host_t host = sg_host_by_name(hostname);
227 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
230 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
235 process = new simgrid::simix::Process();
237 xbt_assert(code && host != nullptr, "Invalid parameters");
239 process->pid = simix_process_maxpid++;
240 process->name = simgrid::xbt::string(name);
241 process->host = host;
242 process->data = data;
243 process->comms = xbt_fifo_new();
244 process->simcall.issuer = process;
245 /* Initiliaze data segment to default value */
246 SIMIX_segment_index_set(process, -1);
248 if (parent_process != nullptr) {
249 process->ppid = SIMIX_process_get_PID(parent_process);
250 /* SMPI process have their own data segment and
251 each other inherit from their father */
253 if(smpi_privatize_global_variables){
254 if( parent_process->pid != 0){
255 SIMIX_segment_index_set(process, parent_process->segment_index);
257 SIMIX_segment_index_set(process, process->pid - 1);
265 /* Process data for auto-restart */
266 process->auto_restart = auto_restart;
267 process->code = code;
269 XBT_VERB("Create context %s", process->name.c_str());
270 process->context = SIMIX_context_new(
272 simix_global->cleanup_process_function, process);
275 process->properties = properties;
277 /* Add the process to it's host process list */
278 xbt_swag_insert(process, sg_host_simix(host)->process_list);
280 XBT_DEBUG("Start context '%s'", process->name.c_str());
282 /* Now insert it in the global process list and in the process to run list */
283 xbt_swag_insert(process, simix_global->process_list);
284 XBT_DEBUG("Inserting %s(%s) in the to_run list",
285 process->name.c_str(), sg_host_get_name(host));
286 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
288 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
289 XBT_DEBUG("Process %s(%s) will be kill at time %f",
290 process->name.c_str(), sg_host_get_name(process->host), kill_time);
291 process->kill_timer = SIMIX_timer_set(kill_time, [=]() {
292 simix_global->kill_process_function(process);
296 /* Tracing the process creation */
297 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
302 smx_process_t SIMIX_process_attach(
305 const char* hostname,
306 xbt_dict_t properties,
307 smx_process_t parent_process)
309 // This is mostly a copy/paste from SIMIX_process_new(),
310 // it'd be nice to share some code between those two functions.
312 sg_host_t host = sg_host_by_name(hostname);
313 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
316 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
321 smx_process_t process = new simgrid::simix::Process();
323 process->pid = simix_process_maxpid++;
324 process->name = std::string(name);
325 process->host = host;
326 process->data = data;
327 process->comms = xbt_fifo_new();
328 process->simcall.issuer = process;
330 /* Initiliaze data segment to default value */
331 SIMIX_segment_index_set(process, -1);
332 if (parent_process != nullptr) {
333 process->ppid = SIMIX_process_get_PID(parent_process);
334 /* SMPI process have their own data segment and
335 each other inherit from their father */
337 if(smpi_privatize_global_variables){
338 if(parent_process->pid != 0){
339 SIMIX_segment_index_set(process, parent_process->segment_index);
341 SIMIX_segment_index_set(process, process->pid - 1);
347 /* Process data for auto-restart */
348 process->auto_restart = false;
349 process->code = nullptr;
351 XBT_VERB("Create context %s", process->name.c_str());
353 xbt_die("simix is not initialized, please call MSG_init first");
354 process->context = simix_global->context_factory->attach(
355 simix_global->cleanup_process_function, process);
358 process->properties = properties;
360 /* Add the process to it's host process list */
361 xbt_swag_insert(process, sg_host_simix(host)->process_list);
363 /* Now insert it in the global process list and in the process to run list */
364 xbt_swag_insert(process, simix_global->process_list);
365 XBT_DEBUG("Inserting %s(%s) in the to_run list",
366 process->name.c_str(), sg_host_get_name(host));
367 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
369 /* Tracing the process creation */
370 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
372 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
374 xbt_die("Not a suitable context");
376 context->attach_start();
380 void SIMIX_process_detach(void)
382 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
384 xbt_die("Not a suitable context");
386 simix_global->cleanup_process_function(context->process());
388 // Let maestro ignore we are still alive:
389 // xbt_swag_remove(context->process(), simix_global->process_list);
391 // TODDO, Remove from proces list:
392 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
394 context->attach_stop();
399 * \brief Executes the processes from simix_global->process_to_run.
401 * The processes of simix_global->process_to_run are run (in parallel if
402 * possible). On exit, simix_global->process_to_run is empty, and
403 * simix_global->process_that_ran contains the list of processes that just ran.
404 * The two lists are swapped so, be careful when using them before and after a
405 * call to this function.
407 void SIMIX_process_runall(void)
409 SIMIX_context_runall();
411 xbt_dynar_t tmp = simix_global->process_that_ran;
412 simix_global->process_that_ran = simix_global->process_to_run;
413 simix_global->process_to_run = tmp;
414 xbt_dynar_reset(simix_global->process_to_run);
417 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
418 SIMIX_process_kill(process, simcall->issuer);
421 * \brief Internal function to kill a SIMIX process.
423 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
424 * or directly for SIMIX internal purposes.
426 * \param process poor victim
427 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
429 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
431 XBT_DEBUG("Killing process %s on %s",
432 process->name.c_str(), sg_host_get_name(process->host));
434 process->context->iwannadie = 1;
435 process->blocked = 0;
436 process->suspended = 0;
437 process->exception = nullptr;
439 /* destroy the blocking synchro if any */
440 if (process->waiting_synchro) {
442 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
443 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
444 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
445 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
446 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
448 if (exec != nullptr) {
451 } else if (comm != nullptr) {
452 xbt_fifo_remove(process->comms, process->waiting_synchro);
455 // Remove first occurence of &process->simcall:
456 auto i = boost::range::find(
457 process->waiting_synchro->simcalls,
459 if (i != process->waiting_synchro->simcalls.end())
460 process->waiting_synchro->simcalls.remove(&process->simcall);
464 } else if (sleep != nullptr) {
465 SIMIX_process_sleep_destroy(process->waiting_synchro);
467 } else if (raw != nullptr) {
468 SIMIX_synchro_stop_waiting(process, &process->simcall);
469 delete process->waiting_synchro;
471 } else if (io != nullptr) {
472 SIMIX_io_destroy(process->waiting_synchro);
476 switch (process->waiting_synchro->type) {
477 case SIMIX_SYNC_JOIN:
478 SIMIX_process_sleep_destroy(process->waiting_synchro);
482 process->waiting_synchro = nullptr;
484 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
485 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
486 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
491 /** @brief Ask another process to raise the given exception
493 * @param cat category of exception
494 * @param value value associated to the exception
495 * @param msg string information associated to the exception
497 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
498 SMX_EXCEPTION(process, cat, value, msg);
500 if (process->suspended)
501 SIMIX_process_resume(process,SIMIX_process_self());
503 /* cancel the blocking synchro if any */
504 if (process->waiting_synchro) {
506 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
507 if (exec != nullptr) {
508 SIMIX_execution_cancel(process->waiting_synchro);
511 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
512 if (comm != nullptr) {
513 xbt_fifo_remove(process->comms, comm);
517 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
518 if (sleep != nullptr) {
519 SIMIX_process_sleep_destroy(process->waiting_synchro);
520 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
521 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
522 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
526 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
527 if (raw != nullptr) {
528 SIMIX_synchro_stop_waiting(process, &process->simcall);
531 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
533 SIMIX_io_destroy(process->waiting_synchro);
536 process->waiting_synchro = nullptr;
540 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
541 SIMIX_process_killall(simcall->issuer, reset_pid);
544 * \brief Kills all running processes.
545 * \param issuer this one will not be killed
547 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
549 smx_process_t p = nullptr;
551 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
553 SIMIX_process_kill(p,issuer);
558 simix_process_maxpid = reset_pid;
560 SIMIX_context_runall();
562 SIMIX_process_empty_trash();
565 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
567 process->new_host = dest;
569 void SIMIX_process_change_host(smx_process_t process,
572 xbt_assert((process != nullptr), "Invalid parameters");
573 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
574 process->host = dest;
575 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
579 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
581 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
583 if (process != simcall->issuer) {
584 SIMIX_simcall_answer(simcall);
586 sync_suspend->simcalls.push_back(simcall);
587 process->waiting_synchro = sync_suspend;
588 process->waiting_synchro->suspend();
590 /* If we are suspending ourselves, then just do not finish the simcall now */
593 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
595 if (process->suspended) {
596 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
600 process->suspended = 1;
602 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
603 if (process != issuer) {
605 if (process->waiting_synchro)
606 process->waiting_synchro->suspend();
607 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
611 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
612 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
616 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
617 SIMIX_process_resume(process, simcall->issuer);
620 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
622 XBT_IN("process = %p, issuer = %p", process, issuer);
624 if(process->context->iwannadie) {
625 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
629 if(!process->suspended) return;
630 process->suspended = 0;
632 /* If we are resuming another process, resume the synchronization it was waiting for
633 if any. Otherwise add it to the list of process to run in the next round. */
634 if (process != issuer) {
636 if (process->waiting_synchro) {
637 process->waiting_synchro->resume();
639 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
644 int SIMIX_process_get_maxpid(void) {
645 return simix_process_maxpid;
648 int SIMIX_process_count(void)
650 return xbt_swag_size(simix_global->process_list);
653 int SIMIX_process_get_PID(smx_process_t self){
660 int SIMIX_process_get_PPID(smx_process_t self){
667 void* SIMIX_process_self_get_data()
669 smx_process_t self = SIMIX_process_self();
674 return SIMIX_process_get_data(self);
677 void SIMIX_process_self_set_data(void *data)
679 smx_process_t self = SIMIX_process_self();
681 SIMIX_process_set_data(self, data);
684 void* SIMIX_process_get_data(smx_process_t process)
686 return process->data;
689 void SIMIX_process_set_data(smx_process_t process, void *data)
691 process->data = data;
694 sg_host_t SIMIX_process_get_host(smx_process_t process)
696 return process->host;
699 /* needs to be public and without simcall because it is called
700 by exceptions and logging events */
701 const char* SIMIX_process_self_get_name(void) {
703 smx_process_t process = SIMIX_process_self();
704 if (process == nullptr || process == simix_global->maestro_process)
707 return SIMIX_process_get_name(process);
710 const char* SIMIX_process_get_name(smx_process_t process)
712 return process->name.c_str();
715 smx_process_t SIMIX_process_get_by_name(const char* name)
718 xbt_swag_foreach(proc, simix_global->process_list) {
719 if (proc->name == name)
725 int SIMIX_process_is_suspended(smx_process_t process)
727 return process->suspended;
730 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
732 return process->properties;
735 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
737 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
738 sync->simcalls.push_back(simcall);
739 simcall->issuer->waiting_synchro = sync;
742 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
743 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
745 if (sleep->surf_sleep) {
746 sleep->surf_sleep->cancel();
748 while (!sleep->simcalls.empty()) {
749 smx_simcall_t simcall = sleep->simcalls.front();
750 sleep->simcalls.pop_front();
751 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
752 simcall->issuer->waiting_synchro = nullptr;
753 if (simcall->issuer->suspended) {
754 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
755 simcall->issuer->suspended = 0;
756 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
758 SIMIX_simcall_answer(simcall);
761 sleep->surf_sleep->unref();
762 sleep->surf_sleep = nullptr;
768 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
770 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
771 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
775 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
777 if (MC_is_active() || MC_record_replay_is_active()) {
778 MC_process_clock_add(simcall->issuer, duration);
779 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
780 SIMIX_simcall_answer(simcall);
783 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
784 sync->simcalls.push_back(simcall);
785 simcall->issuer->waiting_synchro = sync;
788 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
790 sg_host_t host = process->host;
792 /* check if the host is active */
794 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
796 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
797 synchro->host = host;
798 synchro->surf_sleep = surf_host_sleep(host, duration);
799 synchro->surf_sleep->setData(synchro);
800 XBT_DEBUG("Create sleep synchronization %p", synchro);
805 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
807 XBT_DEBUG("Destroy synchro %p", synchro);
808 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
810 if (sleep->surf_sleep) {
811 sleep->surf_sleep->unref();
812 sleep->surf_sleep = nullptr;
817 * \brief Calling this function makes the process to yield.
819 * Only the current process can call this function, giving back the control to
822 * \param self the current process
824 void SIMIX_process_yield(smx_process_t self)
826 XBT_DEBUG("Yield process '%s'", self->name.c_str());
828 /* Go into sleep and return control to maestro */
829 self->context->suspend();
831 /* Ok, maestro returned control to us */
832 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
834 if (self->new_host) {
835 SIMIX_process_change_host(self, self->new_host);
836 self->new_host = nullptr;
839 if (self->context->iwannadie){
840 XBT_DEBUG("I wanna die!");
841 SIMIX_process_stop(self);
844 if (self->suspended) {
845 XBT_DEBUG("Hey! I'm suspended.");
846 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
848 SIMIX_process_suspend(self, self);
851 if (self->exception != nullptr) {
852 XBT_DEBUG("Wait, maestro left me an exception");
853 std::exception_ptr exception = std::move(self->exception);
854 self->exception = nullptr;
855 std::rethrow_exception(std::move(exception));
858 if(SMPI_switch_data_segment && self->segment_index != -1){
859 SMPI_switch_data_segment(self->segment_index);
863 /* callback: termination */
864 void SIMIX_process_exception_terminate(xbt_ex_t * e)
870 smx_context_t SIMIX_process_get_context(smx_process_t p) {
874 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
879 * \brief Returns the list of processes to run.
881 xbt_dynar_t SIMIX_process_get_runnable(void)
883 return simix_global->process_to_run;
887 * \brief Returns the process from PID.
889 smx_process_t SIMIX_process_from_PID(int PID)
892 xbt_swag_foreach(proc, simix_global->process_list) {
893 if (proc->pid == (unsigned long) PID)
899 /** @brief returns a dynar containg all currently existing processes */
900 xbt_dynar_t SIMIX_processes_as_dynar(void) {
902 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),nullptr);
903 xbt_swag_foreach(proc, simix_global->process_list) {
904 xbt_dynar_push(res,&proc);
910 void SIMIX_process_on_exit_runall(smx_process_t process) {
911 s_smx_process_exit_fun_t exit_fun;
912 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
913 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
914 while (!xbt_dynar_is_empty(process->on_exit)) {
915 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
916 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
920 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
921 xbt_assert(process, "current process not found: are you in maestro context ?");
923 if (!process->on_exit) {
924 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
927 s_smx_process_exit_fun_t exit_fun = {fun, data};
929 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
933 * \brief Sets the auto-restart status of the process.
934 * If set to 1, the process will be automatically restarted when its host
937 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
938 process->auto_restart = auto_restart;
941 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
942 return SIMIX_process_restart(process, simcall->issuer);
944 /** @brief Restart a process, starting it again from the beginning. */
945 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
946 XBT_DEBUG("Restarting process %s on %s",
947 process->name.c_str(), sg_host_get_name(process->host));
949 //retrieve the arguments of the old process
950 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
951 simgrid::simix::ProcessArg arg;
952 arg.name = process->name;
953 arg.code = process->code;
954 arg.hostname = sg_host_get_name(process->host);
955 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
956 arg.data = process->data;
957 arg.properties = nullptr;
958 arg.auto_restart = process->auto_restart;
960 //kill the old process
961 SIMIX_process_kill(process, issuer);
963 //start the new process
964 if (simix_global->create_process_function)
965 return simix_global->create_process_function(
966 arg.name.c_str(), std::move(arg.code), arg.data,
967 arg.hostname, arg.kill_time,
968 arg.properties, arg.auto_restart,
971 return simcall_process_create(
972 arg.name.c_str(), std::move(arg.code), arg.data,
973 arg.hostname, arg.kill_time,
974 arg.properties, arg.auto_restart);
977 void SIMIX_segment_index_set(smx_process_t proc, int index){
978 proc->segment_index = index;
981 smx_process_t simcall_process_create(
982 const char *name, std::function<void()> code, void *data,
983 const char *hostname, double kill_time,
984 xbt_dict_t properties, int auto_restart)
988 smx_process_t self = SIMIX_process_self();
989 return simgrid::simix::kernel([&] {
990 return SIMIX_process_create(name,
991 std::move(code), data, hostname,
992 kill_time, properties, auto_restart,