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 "src/surf/surf_interface.hpp"
25 #include "smx_private.h"
26 #include "src/mc/mc_replay.h"
27 #include "src/mc/Client.hpp"
28 #include "src/msg/msg_private.h"
29 #include "src/kernel/activity/SynchroSleep.hpp"
30 #include "src/kernel/activity/SynchroRaw.hpp"
31 #include "src/kernel/activity/SynchroIo.hpp"
34 #include "src/smpi/private.h"
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
39 unsigned long simix_process_maxpid = 0;
41 /** Increase the refcount for this process */
42 smx_process_t SIMIX_process_ref(smx_process_t process)
44 if (process != nullptr)
45 intrusive_ptr_add_ref(process);
49 /** Decrease the refcount for this process */
50 void SIMIX_process_unref(smx_process_t process)
52 if (process != nullptr)
53 intrusive_ptr_release(process);
57 * \brief Returns the current agent.
59 * This functions returns the currently running SIMIX process.
61 * \return The SIMIX process
63 smx_process_t SIMIX_process_self()
65 smx_context_t self_context = SIMIX_context_self();
67 return (self_context != nullptr) ? self_context->process() : nullptr;
71 * \brief Returns whether a process has pending asynchronous communications.
72 * \return true if there are asynchronous communications in this process
74 int SIMIX_process_has_pending_comms(smx_process_t process) {
76 return xbt_fifo_size(process->comms) > 0;
80 * \brief Moves a process to the list of processes to destroy.
82 void SIMIX_process_cleanup(smx_process_t process)
84 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
85 process->name.c_str(), process, process->waiting_synchro);
87 process->finished = true;
88 SIMIX_process_on_exit_runall(process);
90 /* Unregister from the kill timer if any */
91 if (process->kill_timer != nullptr)
92 SIMIX_timer_remove(process->kill_timer);
94 xbt_os_mutex_acquire(simix_global->mutex);
96 /* cancel non-blocking communications */
97 smx_synchro_t synchro = static_cast<smx_synchro_t>(xbt_fifo_pop(process->comms));
98 while (synchro != nullptr) {
99 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
101 /* make sure no one will finish the comm after this process is destroyed,
102 * because src_proc or dst_proc would be an invalid pointer */
105 if (comm->src_proc == process) {
106 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
107 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
108 comm->src_proc = nullptr;
110 /* I'm not supposed to destroy a detached comm from the sender side, */
112 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
117 else if (comm->dst_proc == process){
118 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
119 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
120 comm->dst_proc = nullptr;
122 if (comm->detached && comm->src_proc != nullptr) {
123 /* the comm will be freed right now, remove it from the sender */
124 xbt_fifo_remove(comm->src_proc->comms, comm);
129 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
131 synchro = static_cast<smx_synchro_t>(xbt_fifo_pop(process->comms));
134 XBT_DEBUG("%p should not be run anymore",process);
135 xbt_swag_remove(process, simix_global->process_list);
137 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
138 xbt_swag_insert(process, simix_global->process_to_destroy);
139 process->context->iwannadie = 0;
141 xbt_os_mutex_release(simix_global->mutex);
147 * Should be called some time to time to free the memory allocated for processes
148 * that have finished (or killed).
150 void SIMIX_process_empty_trash()
152 smx_process_t process = nullptr;
154 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
155 XBT_DEBUG("Getting rid of %p",process);
156 intrusive_ptr_release(process);
163 ActorImpl::~ActorImpl()
165 delete this->context;
166 if (this->properties)
167 xbt_dict_free(&this->properties);
168 if (this->comms != nullptr)
169 xbt_fifo_free(this->comms);
171 xbt_dynar_free(&this->on_exit);
174 void create_maestro(std::function<void()> code)
176 smx_process_t maestro = nullptr;
177 /* Create maestro process and intilialize it */
178 maestro = new simgrid::simix::ActorImpl();
179 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;
201 * \brief Creates and runs the maestro process
203 void SIMIX_maestro_create(void (*code)(void*), void* data)
205 simgrid::simix::create_maestro(std::bind(code, data));
209 * \brief Stops a process.
211 * Stops the process, execute all the registered on_exit functions,
212 * register it to the list of the process to restart if needed
213 * and stops its context.
215 void SIMIX_process_stop(smx_process_t arg) {
216 arg->finished = true;
217 /* execute the on_exit functions */
218 SIMIX_process_on_exit_runall(arg);
219 /* Add the process to the list of process to restart, only if the host is down */
220 if (arg->auto_restart && arg->host->isOff()) {
221 SIMIX_host_add_auto_restart_process(arg->host, arg->name.c_str(),
222 arg->code, arg->data,
223 sg_host_get_name(arg->host),
224 SIMIX_timer_get_date(arg->kill_timer),
228 XBT_DEBUG("Process %s (%s) is dead",
229 arg->name.c_str(), sg_host_get_name(arg->host));
230 arg->context->stop();
234 * \brief Internal function to create a process.
236 * This function actually creates the process.
237 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
238 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
240 * \return the process created
242 smx_process_t SIMIX_process_create(
244 std::function<void()> code,
246 const char *hostname,
248 xbt_dict_t properties,
250 smx_process_t parent_process)
252 smx_process_t process = nullptr;
253 sg_host_t host = sg_host_by_name(hostname);
255 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
258 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
263 process = new simgrid::simix::ActorImpl();
265 xbt_assert(code && host != nullptr, "Invalid parameters");
267 process->pid = simix_process_maxpid++;
268 process->name = simgrid::xbt::string(name);
269 process->host = host;
270 process->data = data;
271 process->comms = xbt_fifo_new();
272 process->simcall.issuer = process;
273 /* Initiliaze data segment to default value */
274 SIMIX_segment_index_set(process, -1);
276 if (parent_process != nullptr) {
277 process->ppid = parent_process->pid;
278 /* SMPI process have their own data segment and each other inherit from their father */
280 if( smpi_privatize_global_variables) {
281 if (parent_process->pid != 0) {
282 SIMIX_segment_index_set(process, parent_process->segment_index);
284 SIMIX_segment_index_set(process, process->pid - 1);
292 /* Process data for auto-restart */
293 process->auto_restart = auto_restart;
294 process->code = code;
296 XBT_VERB("Create context %s", process->name.c_str());
297 process->context = SIMIX_context_new(
299 simix_global->cleanup_process_function, process);
302 process->properties = properties;
304 /* Add the process to it's host process list */
305 xbt_swag_insert(process, sg_host_simix(host)->process_list);
307 XBT_DEBUG("Start context '%s'", process->name.c_str());
309 /* Now insert it in the global process list and in the process to run list */
310 xbt_swag_insert(process, simix_global->process_list);
311 XBT_DEBUG("Inserting %s(%s) in the to_run list",
312 process->name.c_str(), sg_host_get_name(host));
313 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
315 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
316 XBT_DEBUG("Process %s(%s) will be kill at time %f",
317 process->name.c_str(), sg_host_get_name(process->host), kill_time);
318 process->kill_timer = SIMIX_timer_set(kill_time, [=]() {
319 simix_global->kill_process_function(process);
323 /* Tracing the process creation */
324 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
329 smx_process_t SIMIX_process_attach(
332 const char* hostname,
333 xbt_dict_t properties,
334 smx_process_t parent_process)
336 // This is mostly a copy/paste from SIMIX_process_new(),
337 // it'd be nice to share some code between those two functions.
339 sg_host_t host = sg_host_by_name(hostname);
340 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
343 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
348 smx_process_t process = new simgrid::simix::ActorImpl();
350 process->pid = simix_process_maxpid++;
351 process->name = std::string(name);
352 process->host = host;
353 process->data = data;
354 process->comms = xbt_fifo_new();
355 process->simcall.issuer = process;
357 /* Initiliaze data segment to default value */
358 SIMIX_segment_index_set(process, -1);
359 if (parent_process != nullptr) {
360 process->ppid = parent_process->pid;
361 /* SMPI process have their own data segment and each other inherit from their father */
363 if (smpi_privatize_global_variables) {
364 if (parent_process->pid != 0) {
365 SIMIX_segment_index_set(process, parent_process->segment_index);
367 SIMIX_segment_index_set(process, process->pid - 1);
373 /* Process data for auto-restart */
374 process->auto_restart = false;
375 process->code = nullptr;
377 XBT_VERB("Create context %s", process->name.c_str());
379 xbt_die("simix is not initialized, please call MSG_init first");
380 process->context = simix_global->context_factory->attach(
381 simix_global->cleanup_process_function, process);
384 process->properties = properties;
386 /* Add the process to it's host process list */
387 xbt_swag_insert(process, sg_host_simix(host)->process_list);
389 /* Now insert it in the global process list and in the process to run list */
390 xbt_swag_insert(process, simix_global->process_list);
391 XBT_DEBUG("Inserting %s(%s) in the to_run list",
392 process->name.c_str(), sg_host_get_name(host));
393 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
395 /* Tracing the process creation */
396 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
398 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
400 xbt_die("Not a suitable context");
402 context->attach_start();
406 void SIMIX_process_detach()
408 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
410 xbt_die("Not a suitable context");
412 simix_global->cleanup_process_function(context->process());
414 // Let maestro ignore we are still alive:
415 // xbt_swag_remove(context->process(), simix_global->process_list);
417 // TODO, Remove from proces list:
418 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
420 context->attach_stop();
425 * \brief Executes the processes from simix_global->process_to_run.
427 * The processes of simix_global->process_to_run are run (in parallel if
428 * possible). On exit, simix_global->process_to_run is empty, and
429 * simix_global->process_that_ran contains the list of processes that just ran.
430 * The two lists are swapped so, be careful when using them before and after a
431 * call to this function.
433 void SIMIX_process_runall()
435 SIMIX_context_runall();
437 xbt_dynar_t tmp = simix_global->process_that_ran;
438 simix_global->process_that_ran = simix_global->process_to_run;
439 simix_global->process_to_run = tmp;
440 xbt_dynar_reset(simix_global->process_to_run);
443 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
444 SIMIX_process_kill(process, simcall->issuer);
447 * \brief Internal function to kill a SIMIX process.
449 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
450 * or directly for SIMIX internal purposes.
452 * \param process poor victim
453 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
455 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
457 XBT_DEBUG("Killing process %s on %s",
458 process->name.c_str(), sg_host_get_name(process->host));
460 process->context->iwannadie = 1;
461 process->blocked = 0;
462 process->suspended = 0;
463 process->exception = nullptr;
465 /* destroy the blocking synchro if any */
466 if (process->waiting_synchro) {
468 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
469 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
470 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
471 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
472 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
474 if (exec != nullptr) {
477 } else if (comm != nullptr) {
478 xbt_fifo_remove(process->comms, process->waiting_synchro);
481 // Remove first occurrence of &process->simcall:
482 auto i = boost::range::find(
483 process->waiting_synchro->simcalls,
485 if (i != process->waiting_synchro->simcalls.end())
486 process->waiting_synchro->simcalls.remove(&process->simcall);
490 } else if (sleep != nullptr) {
491 SIMIX_process_sleep_destroy(process->waiting_synchro);
493 } else if (raw != nullptr) {
494 SIMIX_synchro_stop_waiting(process, &process->simcall);
495 delete process->waiting_synchro;
497 } else if (io != nullptr) {
498 SIMIX_io_destroy(process->waiting_synchro);
502 switch (process->waiting_synchro->type) {
503 case SIMIX_SYNC_JOIN:
504 SIMIX_process_sleep_destroy(process->waiting_synchro);
508 process->waiting_synchro = nullptr;
510 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
511 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
512 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
516 /** @brief Ask another process to raise the given exception
518 * @param cat category of exception
519 * @param value value associated to the exception
520 * @param msg string information associated to the exception
522 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
523 SMX_EXCEPTION(process, cat, value, msg);
525 if (process->suspended)
526 SIMIX_process_resume(process,SIMIX_process_self());
528 /* cancel the blocking synchro if any */
529 if (process->waiting_synchro) {
531 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
532 if (exec != nullptr) {
533 SIMIX_execution_cancel(process->waiting_synchro);
536 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
537 if (comm != nullptr) {
538 xbt_fifo_remove(process->comms, comm);
542 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
543 if (sleep != nullptr) {
544 SIMIX_process_sleep_destroy(process->waiting_synchro);
545 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
546 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
547 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
551 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
552 if (raw != nullptr) {
553 SIMIX_synchro_stop_waiting(process, &process->simcall);
556 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
558 SIMIX_io_destroy(process->waiting_synchro);
561 process->waiting_synchro = nullptr;
565 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
566 SIMIX_process_killall(simcall->issuer, reset_pid);
569 * \brief Kills all running processes.
570 * \param issuer this one will not be killed
572 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
574 smx_process_t p = nullptr;
576 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
578 SIMIX_process_kill(p,issuer);
583 simix_process_maxpid = reset_pid;
585 SIMIX_context_runall();
587 SIMIX_process_empty_trash();
590 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
592 process->new_host = dest;
594 void SIMIX_process_change_host(smx_process_t process, sg_host_t dest)
596 xbt_assert((process != nullptr), "Invalid parameters");
597 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
598 process->host = dest;
599 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
603 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
605 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
607 if (process != simcall->issuer) {
608 SIMIX_simcall_answer(simcall);
610 sync_suspend->simcalls.push_back(simcall);
611 process->waiting_synchro = sync_suspend;
612 process->waiting_synchro->suspend();
614 /* If we are suspending ourselves, then just do not finish the simcall now */
617 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
619 if (process->suspended) {
620 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
624 process->suspended = 1;
626 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
627 if (process != issuer) {
629 if (process->waiting_synchro)
630 process->waiting_synchro->suspend();
631 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
635 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
636 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
640 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
641 SIMIX_process_resume(process, simcall->issuer);
644 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
646 XBT_IN("process = %p, issuer = %p", process, issuer);
648 if(process->context->iwannadie) {
649 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
653 if(!process->suspended) return;
654 process->suspended = 0;
656 /* If we are resuming another process, resume the synchronization it was waiting for
657 if any. Otherwise add it to the list of process to run in the next round. */
658 if (process != issuer) {
660 if (process->waiting_synchro) {
661 process->waiting_synchro->resume();
663 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
668 int SIMIX_process_get_maxpid() {
669 return simix_process_maxpid;
672 int SIMIX_process_count()
674 return xbt_swag_size(simix_global->process_list);
677 int SIMIX_process_get_PID(smx_process_t self)
685 void* SIMIX_process_self_get_data()
687 smx_process_t self = SIMIX_process_self();
692 return SIMIX_process_get_data(self);
695 void SIMIX_process_self_set_data(void *data)
697 smx_process_t self = SIMIX_process_self();
699 SIMIX_process_set_data(self, data);
702 void* SIMIX_process_get_data(smx_process_t process)
704 return process->data;
707 void SIMIX_process_set_data(smx_process_t process, void *data)
709 process->data = data;
712 /* needs to be public and without simcall because it is called
713 by exceptions and logging events */
714 const char* SIMIX_process_self_get_name() {
716 smx_process_t process = SIMIX_process_self();
717 if (process == nullptr || process == simix_global->maestro_process)
720 return process->name.c_str();
723 smx_process_t SIMIX_process_get_by_name(const char* name)
726 xbt_swag_foreach(proc, simix_global->process_list) {
727 if (proc->name == name)
733 int SIMIX_process_is_suspended(smx_process_t process)
735 return process->suspended;
738 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
740 return process->properties;
743 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
745 if (process->finished) {
746 // The joined process is already finished, just wake up the issuer process right away
747 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
748 SIMIX_simcall_answer(simcall);
751 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
752 sync->simcalls.push_back(simcall);
753 simcall->issuer->waiting_synchro = sync;
756 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
757 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
759 if (sleep->surf_sleep) {
760 sleep->surf_sleep->cancel();
762 while (!sleep->simcalls.empty()) {
763 smx_simcall_t simcall = sleep->simcalls.front();
764 sleep->simcalls.pop_front();
765 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
766 simcall->issuer->waiting_synchro = nullptr;
767 if (simcall->issuer->suspended) {
768 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
769 simcall->issuer->suspended = 0;
770 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
772 SIMIX_simcall_answer(simcall);
775 sleep->surf_sleep->unref();
776 sleep->surf_sleep = nullptr;
782 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
784 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
785 static_cast<simgrid::kernel::activity::Synchro*>(res)->ref();
786 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
790 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
792 if (MC_is_active() || MC_record_replay_is_active()) {
793 MC_process_clock_add(simcall->issuer, duration);
794 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
795 SIMIX_simcall_answer(simcall);
798 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
799 sync->simcalls.push_back(simcall);
800 simcall->issuer->waiting_synchro = sync;
803 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
805 sg_host_t host = process->host;
807 /* check if the host is active */
809 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
811 simgrid::kernel::activity::Sleep *synchro = new simgrid::kernel::activity::Sleep();
812 synchro->host = host;
813 synchro->surf_sleep = surf_host_sleep(host, duration);
814 synchro->surf_sleep->setData(synchro);
815 XBT_DEBUG("Create sleep synchronization %p", synchro);
820 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
822 XBT_DEBUG("Destroy synchro %p", synchro);
823 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
825 if (sleep->surf_sleep) {
826 sleep->surf_sleep->unref();
827 sleep->surf_sleep = nullptr;
833 * \brief Calling this function makes the process to yield.
835 * Only the current process can call this function, giving back the control to
838 * \param self the current process
840 void SIMIX_process_yield(smx_process_t self)
842 XBT_DEBUG("Yield process '%s'", self->name.c_str());
844 /* Go into sleep and return control to maestro */
845 self->context->suspend();
847 /* Ok, maestro returned control to us */
848 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
850 if (self->new_host) {
851 SIMIX_process_change_host(self, self->new_host);
852 self->new_host = nullptr;
855 if (self->context->iwannadie){
856 XBT_DEBUG("I wanna die!");
857 SIMIX_process_stop(self);
860 if (self->suspended) {
861 XBT_DEBUG("Hey! I'm suspended.");
862 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
864 SIMIX_process_suspend(self, self);
867 if (self->exception != nullptr) {
868 XBT_DEBUG("Wait, maestro left me an exception");
869 std::exception_ptr exception = std::move(self->exception);
870 self->exception = nullptr;
871 std::rethrow_exception(std::move(exception));
874 if(SMPI_switch_data_segment && self->segment_index != -1){
875 SMPI_switch_data_segment(self->segment_index);
879 /* callback: termination */
880 void SIMIX_process_exception_terminate(xbt_ex_t * e)
886 smx_context_t SIMIX_process_get_context(smx_process_t p) {
890 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
895 * \brief Returns the list of processes to run.
897 xbt_dynar_t SIMIX_process_get_runnable()
899 return simix_global->process_to_run;
903 * \brief Returns the process from PID.
905 smx_process_t SIMIX_process_from_PID(int PID)
908 xbt_swag_foreach(proc, simix_global->process_list) {
909 if (proc->pid == static_cast<unsigned long> (PID))
915 /** @brief returns a dynar containing all currently existing processes */
916 xbt_dynar_t SIMIX_processes_as_dynar() {
918 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),nullptr);
919 xbt_swag_foreach(proc, simix_global->process_list) {
920 xbt_dynar_push(res,&proc);
925 void SIMIX_process_on_exit_runall(smx_process_t process) {
926 s_smx_process_exit_fun_t exit_fun;
927 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
928 while (!xbt_dynar_is_empty(process->on_exit)) {
929 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
930 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
934 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
935 xbt_assert(process, "current process not found: are you in maestro context ?");
937 if (!process->on_exit) {
938 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
941 s_smx_process_exit_fun_t exit_fun = {fun, data};
943 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
947 * \brief Sets the auto-restart status of the process.
948 * If set to 1, the process will be automatically restarted when its host
951 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
952 process->auto_restart = auto_restart;
955 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
956 return SIMIX_process_restart(process, simcall->issuer);
958 /** @brief Restart a process, starting it again from the beginning. */
959 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
960 XBT_DEBUG("Restarting process %s on %s", process->name.c_str(), sg_host_get_name(process->host));
962 //retrieve the arguments of the old process
963 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
964 simgrid::simix::ProcessArg arg;
965 arg.name = process->name;
966 arg.code = process->code;
967 arg.hostname = sg_host_get_name(process->host);
968 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
969 arg.data = process->data;
970 arg.properties = nullptr;
971 arg.auto_restart = process->auto_restart;
973 //kill the old process
974 SIMIX_process_kill(process, issuer);
976 //start the new process
977 if (simix_global->create_process_function)
978 return simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.hostname,
979 arg.kill_time, arg.properties, arg.auto_restart, nullptr);
981 return simcall_process_create(arg.name.c_str(), std::move(arg.code), arg.data, arg.hostname, arg.kill_time,
982 arg.properties, arg.auto_restart);
985 void SIMIX_segment_index_set(smx_process_t proc, int index){
986 proc->segment_index = index;
990 * \ingroup simix_process_management
991 * \brief Creates and runs a new SIMIX process.
993 * The structure and the corresponding thread are created and put in the list of ready processes.
995 * \param name a name for the process. It is for user-level information and can be nullptr.
996 * \param code the main function of the process
997 * \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.
998 * It can be retrieved with the function \ref simcall_process_get_data.
999 * \param hostname name of the host where the new agent is executed.
1000 * \param kill_time time when the process is killed
1001 * \param argc first argument passed to \a code
1002 * \param argv second argument passed to \a code
1003 * \param properties the properties of the process
1004 * \param auto_restart either it is autorestarting or not.
1006 smx_process_t simcall_process_create(const char *name,
1007 xbt_main_func_t code,
1009 const char *hostname,
1011 int argc, char **argv,
1012 xbt_dict_t properties,
1015 if (name == nullptr)
1017 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
1018 for (int i = 0; i != argc; ++i)
1021 smx_process_t res = simcall_process_create(name,
1022 std::move(wrapped_code),
1023 data, hostname, kill_time, properties, auto_restart);
1027 smx_process_t simcall_process_create(
1028 const char *name, std::function<void()> code, void *data,
1029 const char *hostname, double kill_time,
1030 xbt_dict_t properties, int auto_restart)
1032 if (name == nullptr)
1034 smx_process_t self = SIMIX_process_self();
1035 return simgrid::simix::kernelImmediate([&] {
1036 return SIMIX_process_create(name, std::move(code), data, hostname, kill_time, properties, auto_restart, self);