1 /* Copyright (c) 2004-2014. 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 "msg_private.h"
8 #include "xbt/sysdep.h"
10 #include "simix/smx_process_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
13 "Logging specific to MSG (process)");
15 /** @addtogroup m_process_management
16 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Processes" --> \endhtmlonly
18 * We need to simulate many independent scheduling decisions, so
19 * the concept of <em>process</em> is at the heart of the
20 * simulator. A process may be defined as a <em>code</em>, with
21 * some <em>private data</em>, executing in a <em>location</em>.
25 /******************************** Process ************************************/
28 * \brief Cleans the MSG data of a process.
29 * \param smx_proc a SIMIX process
31 void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc)
33 simdata_process_t msg_proc;
35 // get the MSG process from the SIMIX process
36 if (smx_proc == SIMIX_process_self()) {
37 /* avoid a SIMIX request if this function is called by the process itself */
38 msg_proc = SIMIX_process_self_get_data(smx_proc);
39 SIMIX_process_self_set_data(smx_proc, NULL);
42 msg_proc = simcall_process_get_data(smx_proc);
43 simcall_process_set_data(smx_proc, NULL);
47 TRACE_msg_process_end(smx_proc);
49 // free the data if a function was provided
50 if (msg_proc && msg_proc->data && msg_global->process_data_cleanup) {
51 msg_global->process_data_cleanup(msg_proc->data);
54 // free the MSG process
58 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
59 void MSG_process_create_from_SIMIX(smx_process_t* process, const char *name,
60 xbt_main_func_t code, void *data,
61 const char *hostname, double kill_time,
62 int argc, char **argv,
63 xbt_dict_t properties, int auto_restart,
64 smx_process_t parent_process)
66 msg_host_t host = MSG_get_host_by_name(hostname);
67 msg_process_t p = MSG_process_create_with_environment(name, code, data,
71 MSG_process_set_kill_time(p,kill_time);
72 MSG_process_auto_restart_set(p,auto_restart);
74 *((msg_process_t*) process) = p;
77 /** \ingroup m_process_management
78 * \brief Creates and runs a new #msg_process_t.
80 * Does exactly the same as #MSG_process_create_with_arguments but without
81 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
82 * \sa MSG_process_create_with_arguments
84 msg_process_t MSG_process_create(const char *name,
85 xbt_main_func_t code, void *data,
88 return MSG_process_create_with_environment(name, code, data, host, -1,
92 /** \ingroup m_process_management
93 * \brief Creates and runs a new #msg_process_t.
95 * A constructor for #msg_process_t taking four arguments and returning the
96 * corresponding object. The structure (and the corresponding thread) is
97 * created, and put in the list of ready process.
98 * \param name a name for the object. It is for user-level information
100 * \param code is a function describing the behavior of the process. It
101 should then only use functions described in \ref
102 m_process_management (to create a new #msg_process_t for example),
103 in \ref m_host_management (only the read-only functions i.e. whose
104 name contains the word get), in \ref m_task_management (to create
105 or destroy some #msg_task_t for example) and in \ref
106 msg_task_usage (to handle file transfers and task processing).
107 * \param data a pointer to any data one may want to attach to the new
108 object. It is for user-level information and can be NULL. It can
109 be retrieved with the function \ref MSG_process_get_data.
110 * \param host the location where the new process is executed.
111 * \param argc first argument passed to \a code
112 * \param argv second argument passed to \a code
114 * \return The new corresponding object.
117 msg_process_t MSG_process_create_with_arguments(const char *name,
118 xbt_main_func_t code,
119 void *data, msg_host_t host,
120 int argc, char **argv)
122 return MSG_process_create_with_environment(name, code, data, host,
126 /** \ingroup m_process_management
127 * \brief Creates and runs a new #msg_process_t.
129 * A constructor for #msg_process_t taking four arguments and returning the
130 * corresponding object. The structure (and the corresponding thread) is
131 * created, and put in the list of ready process.
132 * \param name a name for the object. It is for user-level information
134 * \param code is a function describing the behavior of the process. It
135 should then only use functions described in \ref
136 m_process_management (to create a new #msg_process_t for example),
137 in \ref m_host_management (only the read-only functions i.e. whose
138 name contains the word get), in \ref m_task_management (to create
139 or destroy some #msg_task_t for example) and in \ref
140 msg_task_usage (to handle file transfers and task processing).
141 * \param data a pointer to any data one may want to attach to the new
142 object. It is for user-level information and can be NULL. It can
143 be retrieved with the function \ref MSG_process_get_data.
144 * \param host the location where the new process is executed.
145 * \param argc first argument passed to \a code
146 * \param argv second argument passed to \a code. WARNING, these strings are freed by the SimGrid kernel when the process exits, so they cannot be static nor shared between several processes.
147 * \param properties list a properties defined for this process
149 * \return The new corresponding object.
151 msg_process_t MSG_process_create_with_environment(const char *name,
152 xbt_main_func_t code,
153 void *data, msg_host_t host,
154 int argc, char **argv,
155 xbt_dict_t properties)
157 xbt_assert(code != NULL && host != NULL, "Invalid parameters");
158 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
159 msg_process_t process;
161 /* Simulator data for MSG */
162 simdata->waiting_action = NULL;
163 simdata->waiting_task = NULL;
164 simdata->m_host = host;
165 simdata->argc = argc;
166 simdata->argv = argv;
167 simdata->data = data;
168 simdata->last_errno = MSG_OK;
170 /* Let's create the process: SIMIX may decide to start it right now,
171 * even before returning the flow control to us */
172 simcall_process_create(&process, name, code, simdata, sg_host_name(host), -1,
173 argc, argv, properties,0);
176 TRACE_msg_process_create(name, SIMIX_process_get_PID(process), host);
180 /* Undo everything we have just changed */
186 simcall_process_on_exit(process,(int_f_pvoid_pvoid_t)TRACE_msg_process_kill,process);
192 /** \ingroup m_process_management
193 * \param process poor victim
195 * This function simply kills a \a process... scary isn't it ? :)
197 void MSG_process_kill(msg_process_t process)
199 // /* FIXME: why do we only cancel communication actions? is this useful? */
200 // simdata_process_t p_simdata = simcall_process_get_data(process);
201 // if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
202 // simcall_comm_cancel(p_simdata->waiting_task->simdata->comm);
205 simcall_process_kill(process);
211 * \brief Wait for the completion of a #msg_process_t.
213 * \param process the process to wait for
214 * \param timeout wait until the process is over, or the timeout occurs
216 msg_error_t MSG_process_join(msg_process_t process, double timeout){
217 simcall_process_join(process, timeout);
221 /** \ingroup m_process_management
222 * \brief Migrates a process to another location.
224 * This function checks whether \a process and \a host are valid pointers
225 and change the value of the #msg_host_t on which \a process is running.
227 msg_error_t MSG_process_migrate(msg_process_t process, msg_host_t host)
229 simdata_process_t simdata = simcall_process_get_data(process);
230 simdata->m_host = host;
232 msg_host_t now = simdata->m_host;
233 TRACE_msg_process_change_host(process, now, host);
235 simcall_process_change_host(process, host);
239 /** \ingroup m_process_management
240 * \brief Returns the user data of a process.
242 * This function checks whether \a process is a valid pointer or not
243 and returns the user data associated to this process.
245 void* MSG_process_get_data(msg_process_t process)
247 xbt_assert(process != NULL, "Invalid parameter");
249 /* get from SIMIX the MSG process data, and then the user data */
250 simdata_process_t simdata = simcall_process_get_data(process);
251 return simdata->data;
254 /** \ingroup m_process_management
255 * \brief Sets the user data of a process.
257 * This function checks whether \a process is a valid pointer or not
258 and sets the user data associated to this process.
260 msg_error_t MSG_process_set_data(msg_process_t process, void *data)
262 xbt_assert(process != NULL, "Invalid parameter");
264 simdata_process_t simdata = simcall_process_get_data(process);
265 simdata->data = data;
270 /** \ingroup m_process_management
271 * \brief Sets a cleanup function to be called to free the userdata of a
272 * process when a process is destroyed.
273 * \param data_cleanup a cleanup function for the userdata of a process,
274 * or NULL to call no function
276 XBT_PUBLIC(void) MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup) {
278 msg_global->process_data_cleanup = data_cleanup;
281 /** \ingroup m_process_management
282 * \brief Return the location on which a process is running.
283 * \param process a process (NULL means the current one)
284 * \return the msg_host_t corresponding to the location on which \a
285 * process is running.
287 msg_host_t MSG_process_get_host(msg_process_t process)
289 simdata_process_t simdata;
290 if (process == NULL) {
291 simdata = SIMIX_process_self_get_data(SIMIX_process_self());
294 simdata = simcall_process_get_data(process);
296 return simdata->m_host;
299 /** \ingroup m_process_management
301 * \brief Return a #msg_process_t given its PID.
303 * This function search in the list of all the created msg_process_t for a msg_process_t
304 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
305 Note that the PID are uniq in the whole simulation, not only on a given host.
307 msg_process_t MSG_process_from_PID(int PID)
309 return SIMIX_process_from_PID(PID);
312 /** @brief returns a list of all currently existing processes */
313 xbt_dynar_t MSG_processes_as_dynar(void) {
314 return SIMIX_processes_as_dynar();
316 /** @brief Return the current number MSG processes.
318 int MSG_process_get_number(void)
320 return SIMIX_process_count();
323 /** \ingroup m_process_management
324 * \brief Set the kill time of a process.
326 * \param process a process
327 * \param kill_time the time when the process is killed.
329 msg_error_t MSG_process_set_kill_time(msg_process_t process, double kill_time)
331 simcall_process_set_kill_time(process,kill_time);
335 /** \ingroup m_process_management
336 * \brief Returns the process ID of \a process.
338 * This function checks whether \a process is a valid pointer or not
339 and return its PID (or 0 in case of problem).
341 int MSG_process_get_PID(msg_process_t process)
343 /* Do not raise an exception here: this function is called by the logs
344 * and the exceptions, so it would be called back again and again */
345 if (process == NULL) {
348 return simcall_process_get_PID(process);
351 /** \ingroup m_process_management
352 * \brief Returns the process ID of the parent of \a process.
354 * This function checks whether \a process is a valid pointer or not
355 and return its PID. Returns -1 if the process has not been created by
358 int MSG_process_get_PPID(msg_process_t process)
360 xbt_assert(process != NULL, "Invalid parameter");
362 return simcall_process_get_PPID(process);
365 /** \ingroup m_process_management
366 * \brief Return the name of a process.
368 * This function checks whether \a process is a valid pointer or not
371 const char *MSG_process_get_name(msg_process_t process)
373 xbt_assert(process, "Invalid parameter");
375 return simcall_process_get_name(process);
378 /** \ingroup m_process_management
379 * \brief Returns the value of a given process property
381 * \param process a process
382 * \param name a property name
383 * \return value of a property (or NULL if the property is not set)
385 const char *MSG_process_get_property_value(msg_process_t process,
388 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
391 /** \ingroup m_process_management
392 * \brief Return the list of properties
394 * This function returns all the parameters associated with a process
396 xbt_dict_t MSG_process_get_properties(msg_process_t process)
398 xbt_assert(process != NULL, "Invalid parameter");
400 return simcall_process_get_properties(process);
404 /** \ingroup m_process_management
405 * \brief Return the PID of the current process.
407 * This function returns the PID of the currently running #msg_process_t.
409 int MSG_process_self_PID(void)
411 return MSG_process_get_PID(MSG_process_self());
414 /** \ingroup m_process_management
415 * \brief Return the PPID of the current process.
417 * This function returns the PID of the parent of the currently
418 * running #msg_process_t.
420 int MSG_process_self_PPID(void)
422 return MSG_process_get_PPID(MSG_process_self());
425 /** \ingroup m_process_management
426 * \brief Return the current process.
428 * This function returns the currently running #msg_process_t.
430 msg_process_t MSG_process_self(void)
432 return SIMIX_process_self();
435 /** \ingroup m_process_management
436 * \brief Suspend the process.
438 * This function suspends the process by suspending the task on which
439 * it was waiting for the completion.
441 msg_error_t MSG_process_suspend(msg_process_t process)
443 xbt_assert(process != NULL, "Invalid parameter");
446 TRACE_msg_process_suspend(process);
449 simcall_process_suspend(process);
453 /** \ingroup m_process_management
454 * \brief Resume a suspended process.
456 * This function resumes a suspended process by resuming the task on
457 * which it was waiting for the completion.
459 msg_error_t MSG_process_resume(msg_process_t process)
461 xbt_assert(process != NULL, "Invalid parameter");
464 TRACE_msg_process_resume(process);
467 simcall_process_resume(process);
471 /** \ingroup m_process_management
472 * \brief Returns true if the process is suspended .
474 * This checks whether a process is suspended or not by inspecting the
475 * task on which it was waiting for the completion.
477 int MSG_process_is_suspended(msg_process_t process)
479 xbt_assert(process != NULL, "Invalid parameter");
480 return simcall_process_is_suspended(process);
483 smx_context_t MSG_process_get_smx_ctx(msg_process_t process) {
484 return SIMIX_process_get_context(process);
487 * \ingroup m_process_management
488 * \brief Add a function to the list of "on_exit" functions for the current process.
489 * The on_exit functions are the functions executed when your process is killed.
490 * You should use them to free the data used by your process.
492 void MSG_process_on_exit(int_f_pvoid_pvoid_t fun, void *data) {
493 simcall_process_on_exit(MSG_process_self(),fun,data);
496 * \ingroup m_process_management
497 * \brief Sets the "auto-restart" flag of the process.
498 * If the flag is set to 1, the process will be automatically restarted when
499 * its host comes back up.
501 XBT_PUBLIC(void) MSG_process_auto_restart_set(msg_process_t process, int auto_restart) {
502 simcall_process_auto_restart_set(process,auto_restart);
505 * \ingroup m_process_management
506 * \brief Restarts a process from the beginning.
508 XBT_PUBLIC(msg_process_t) MSG_process_restart(msg_process_t process) {
509 return simcall_process_restart(process);