1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
12 "Logging specific to MSG (process)");
14 /** \defgroup m_process_management Management Functions of Agents
15 * \brief This section describes the agent structure of MSG
16 * (#m_process_t) and the functions for managing it.
18 /** @addtogroup m_process_management
19 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
21 * We need to simulate many independent scheduling decisions, so
22 * the concept of <em>process</em> is at the heart of the
23 * simulator. A process may be defined as a <em>code</em>, with
24 * some <em>private data</em>, executing in a <em>location</em>.
28 /******************************** Process ************************************/
31 * \brief Cleans the MSG data of a process.
32 * \param smx_proc a SIMIX process
34 void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc)
36 simdata_process_t msg_proc;
38 // get the MSG process from the SIMIX process
39 if (smx_proc == SIMIX_process_self()) {
40 /* avoid a SIMIX request if this function is called by the process itself */
41 msg_proc = SIMIX_process_self_get_data(smx_proc);
42 SIMIX_process_self_set_data(smx_proc, NULL);
45 msg_proc = SIMIX_req_process_get_data(smx_proc);
46 SIMIX_req_process_set_data(smx_proc, NULL);
50 TRACE_msg_process_end(smx_proc);
53 // free the data if a function was provided
54 if (msg_proc->data && msg_global->process_data_cleanup) {
55 msg_global->process_data_cleanup(msg_proc->data);
58 // free the MSG process
62 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
63 void MSG_process_create_from_SIMIX(smx_process_t* process, const char *name,
64 xbt_main_func_t code, void *data,
65 const char *hostname, int argc, char **argv,
66 xbt_dict_t properties)
68 m_host_t host = MSG_get_host_by_name(hostname);
69 m_process_t p = MSG_process_create_with_environment(name, code, data,
72 *((m_process_t*) process) = p;
75 /** \ingroup m_process_management
76 * \brief Creates and runs a new #m_process_t.
78 * Does exactly the same as #MSG_process_create_with_arguments but without
79 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
80 * \sa MSG_process_create_with_arguments
82 m_process_t MSG_process_create(const char *name,
83 xbt_main_func_t code, void *data,
86 return MSG_process_create_with_environment(name, code, data, host, -1,
90 /** \ingroup m_process_management
91 * \brief Creates and runs a new #m_process_t.
93 * A constructor for #m_process_t taking four arguments and returning the
94 * corresponding object. The structure (and the corresponding thread) is
95 * created, and put in the list of ready process.
96 * \param name a name for the object. It is for user-level information
98 * \param code is a function describing the behavior of the agent. It
99 should then only use functions described in \ref
100 m_process_management (to create a new #m_process_t for example),
101 in \ref m_host_management (only the read-only functions i.e. whose
102 name contains the word get), in \ref m_task_management (to create
103 or destroy some #m_task_t for example) and in \ref
104 msg_gos_functions (to handle file transfers and task processing).
105 * \param data a pointer to any data one may want to attach to the new
106 object. It is for user-level information and can be NULL. It can
107 be retrieved with the function \ref MSG_process_get_data.
108 * \param host the location where the new agent is executed.
109 * \param argc first argument passed to \a code
110 * \param argv second argument passed to \a code
112 * \return The new corresponding object.
115 m_process_t MSG_process_create_with_arguments(const char *name,
116 xbt_main_func_t code,
117 void *data, m_host_t host,
118 int argc, char **argv)
120 return MSG_process_create_with_environment(name, code, data, host,
124 /** \ingroup m_process_management
125 * \brief Creates and runs a new #m_process_t.
127 * A constructor for #m_process_t taking four arguments and returning the
128 * corresponding object. The structure (and the corresponding thread) is
129 * created, and put in the list of ready process.
130 * \param name a name for the object. It is for user-level information
132 * \param code is a function describing the behavior of the agent. It
133 should then only use functions described in \ref
134 m_process_management (to create a new #m_process_t for example),
135 in \ref m_host_management (only the read-only functions i.e. whose
136 name contains the word get), in \ref m_task_management (to create
137 or destroy some #m_task_t for example) and in \ref
138 msg_gos_functions (to handle file transfers and task processing).
139 * \param data a pointer to any data one may want to attach to the new
140 object. It is for user-level information and can be NULL. It can
141 be retrieved with the function \ref MSG_process_get_data.
142 * \param host the location where the new agent is executed.
143 * \param argc first argument passed to \a code
144 * \param argv second argument passed to \a code
145 * \param properties list a properties defined for this process
147 * \return The new corresponding object.
149 m_process_t MSG_process_create_with_environment(const char *name,
150 xbt_main_func_t code,
151 void *data, m_host_t host,
152 int argc, char **argv,
153 xbt_dict_t properties)
155 xbt_assert(code != NULL && host != NULL, "Invalid parameters");
156 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
159 /* Simulator data for MSG */
160 simdata->PID = msg_global->PID++;
161 simdata->waiting_action = NULL;
162 simdata->waiting_task = NULL;
163 simdata->m_host = host;
164 simdata->argc = argc;
165 simdata->argv = argv;
166 simdata->data = data;
167 simdata->last_errno = MSG_OK;
169 if (SIMIX_process_self()) {
170 simdata->PPID = MSG_process_get_PID(MSG_process_self());
176 TRACE_msg_process_create(name, simdata->PID, simdata->m_host);
179 /* Let's create the process: SIMIX may decide to start it right now,
180 * even before returning the flow control to us */
181 SIMIX_req_process_create(&process, name, code, simdata, host->name,
182 argc, argv, properties);
185 /* Undo everything we have just changed */
194 void MSG_process_kill_from_SIMIX(smx_process_t p)
197 TRACE_msg_process_kill(p);
202 /** \ingroup m_process_management
203 * \param process poor victim
205 * This function simply kills a \a process... scary isn't it ? :)
207 void MSG_process_kill(m_process_t process)
210 TRACE_msg_process_kill(process);
213 /* FIXME: why do we only cancel communication actions? is this useful? */
214 simdata_process_t p_simdata = SIMIX_req_process_get_data(process);
215 if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
216 SIMIX_req_comm_cancel(p_simdata->waiting_task->simdata->comm);
219 SIMIX_req_process_kill(process);
224 /** \ingroup m_process_management
225 * \brief Migrates an agent to another location.
227 * This function checks whether \a process and \a host are valid pointers
228 and change the value of the #m_host_t on which \a process is running.
230 MSG_error_t MSG_process_migrate(m_process_t process, m_host_t host)
232 simdata_process_t simdata = SIMIX_req_process_get_data(process);
233 simdata->m_host = host;
235 m_host_t now = simdata->m_host;
236 TRACE_msg_process_change_host(process, now, host);
238 SIMIX_req_process_change_host(process, host->simdata->smx_host);
242 /** \ingroup m_process_management
243 * \brief Returns the user data of a process.
245 * This function checks whether \a process is a valid pointer or not
246 and returns the user data associated to this process.
248 void* MSG_process_get_data(m_process_t process)
250 xbt_assert(process != NULL, "Invalid parameter");
252 /* get from SIMIX the MSG process data, and then the user data */
253 simdata_process_t simdata = SIMIX_req_process_get_data(process);
254 return simdata->data;
257 /** \ingroup m_process_management
258 * \brief Sets the user data of a process.
260 * This function checks whether \a process is a valid pointer or not
261 and sets the user data associated to this process.
263 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
265 xbt_assert(process != NULL, "Invalid parameter");
267 simdata_process_t simdata = SIMIX_req_process_get_data(process);
268 simdata->data = data;
273 /** \ingroup m_process_management
274 * \brief Sets a cleanup function to be called to free the userdata of a
275 * process when a process is destroyed.
276 * \param data_cleanup a cleanup function for the userdata of a process,
277 * or NULL to call no function
279 XBT_PUBLIC(void) MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup) {
281 msg_global->process_data_cleanup = data_cleanup;
284 /** \ingroup m_process_management
285 * \brief Return the location on which an agent is running.
286 * \param process a process (NULL means the current one)
287 * \return the m_host_t corresponding to the location on which \a
288 * process is running.
290 m_host_t MSG_process_get_host(m_process_t process)
292 simdata_process_t simdata;
293 if (process == NULL) {
294 simdata = SIMIX_process_self_get_data(SIMIX_process_self());
297 simdata = SIMIX_req_process_get_data(process);
299 return simdata->m_host;
302 /** \ingroup m_process_management
304 * \brief Return a #m_process_t given its PID.
306 * This function search in the list of all the created m_process_t for a m_process_t
307 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
308 Note that the PID are uniq in the whole simulation, not only on a given host.
310 m_process_t MSG_process_from_PID(int PID)
312 return SIMIX_process_from_PID(PID);
315 /** \ingroup m_process_management
316 * \brief Returns the process ID of \a process.
318 * This function checks whether \a process is a valid pointer or not
319 and return its PID (or 0 in case of problem).
321 int MSG_process_get_PID(m_process_t process)
323 /* Do not raise an exception here: this function is called by the logs
324 * and the exceptions, so it would be called back again and again */
325 if (process == NULL) {
329 simdata_process_t simdata = SIMIX_req_process_get_data(process);
331 return simdata != NULL ? simdata->PID : 0;
334 /** \ingroup m_process_management
335 * \brief Returns the process ID of the parent of \a process.
337 * This function checks whether \a process is a valid pointer or not
338 and return its PID. Returns -1 if the agent has not been created by
341 int MSG_process_get_PPID(m_process_t process)
343 xbt_assert(process != NULL, "Invalid parameter");
345 simdata_process_t simdata = SIMIX_req_process_get_data(process);
347 return simdata->PPID;
350 /** \ingroup m_process_management
351 * \brief Return the name of an agent.
353 * This function checks whether \a process is a valid pointer or not
356 const char *MSG_process_get_name(m_process_t process)
358 xbt_assert(process, "Invalid parameter");
360 return SIMIX_req_process_get_name(process);
363 /** \ingroup m_process_management
364 * \brief Returns the value of a given process property
366 * \param process a process
367 * \param name a property name
368 * \return value of a property (or NULL if the property is not set)
370 const char *MSG_process_get_property_value(m_process_t process,
373 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
376 /** \ingroup m_process_management
377 * \brief Return the list of properties
379 * This function returns all the parameters associated with a process
381 xbt_dict_t MSG_process_get_properties(m_process_t process)
383 xbt_assert(process != NULL, "Invalid parameter");
385 return SIMIX_req_process_get_properties(process);
389 /** \ingroup m_process_management
390 * \brief Return the PID of the current agent.
392 * This function returns the PID of the currently running #m_process_t.
394 int MSG_process_self_PID(void)
396 return MSG_process_get_PID(MSG_process_self());
399 /** \ingroup m_process_management
400 * \brief Return the PPID of the current agent.
402 * This function returns the PID of the parent of the currently
403 * running #m_process_t.
405 int MSG_process_self_PPID(void)
407 return MSG_process_get_PPID(MSG_process_self());
410 /** \ingroup m_process_management
411 * \brief Return the current process.
413 * This function returns the currently running #m_process_t.
415 m_process_t MSG_process_self(void)
417 return SIMIX_process_self();
420 /** \ingroup m_process_management
421 * \brief Suspend the process.
423 * This function suspends the process by suspending the task on which
424 * it was waiting for the completion.
426 MSG_error_t MSG_process_suspend(m_process_t process)
428 xbt_assert(process != NULL, "Invalid parameter");
432 TRACE_msg_process_suspend(process);
435 SIMIX_req_process_suspend(process);
439 /** \ingroup m_process_management
440 * \brief Resume a suspended process.
442 * This function resumes a suspended process by resuming the task on
443 * which it was waiting for the completion.
445 MSG_error_t MSG_process_resume(m_process_t process)
447 xbt_assert(process != NULL, "Invalid parameter");
451 TRACE_msg_process_resume(process);
454 SIMIX_req_process_resume(process);
458 /** \ingroup m_process_management
459 * \brief Returns true if the process is suspended .
461 * This checks whether a process is suspended or not by inspecting the
462 * task on which it was waiting for the completion.
464 int MSG_process_is_suspended(m_process_t process)
466 xbt_assert(process != NULL, "Invalid parameter");
467 return SIMIX_req_process_is_suspended(process);
470 smx_context_t MSG_process_get_smx_ctx(m_process_t process) {
471 return SIMIX_process_get_context(process);