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"
10 #include "../simix/private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
13 "Logging specific to MSG (process)");
15 /** \defgroup m_process_management Management Functions of Agents
16 * \brief This section describes the agent structure of MSG
17 * (#m_process_t) and the functions for managing it.
19 /** @addtogroup m_process_management
20 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
22 * We need to simulate many independent scheduling decisions, so
23 * the concept of <em>process</em> is at the heart of the
24 * simulator. A process may be defined as a <em>code</em>, with
25 * some <em>private data</em>, executing in a <em>location</em>.
29 /******************************** Process ************************************/
30 void __MSG_process_cleanup(void *arg)
32 /* arg is a pointer to a simix process, we can get the msg process with the field data */
33 m_process_t proc = ((smx_process_t) arg)->data;
35 TRACE_msg_process_end(proc);
37 xbt_fifo_remove(msg_global->process_list, proc);
38 SIMIX_process_cleanup(arg);
52 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
53 void *_MSG_process_create_from_SIMIX(const char *name,
54 xbt_main_func_t code, void *data,
55 char *hostname, int argc, char **argv,
56 xbt_dict_t properties)
58 m_host_t host = MSG_get_host_by_name(hostname);
59 return (void *) MSG_process_create_with_environment(name, code, data,
64 /** \ingroup m_process_management
65 * \brief Creates and runs a new #m_process_t.
67 * Does exactly the same as #MSG_process_create_with_arguments but without
68 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
69 * \sa MSG_process_create_with_arguments
71 m_process_t MSG_process_create(const char *name,
72 xbt_main_func_t code, void *data,
75 return MSG_process_create_with_environment(name, code, data, host, -1,
79 /** \ingroup m_process_management
80 * \brief Creates and runs a new #m_process_t.
82 * A constructor for #m_process_t taking four arguments and returning the
83 * corresponding object. The structure (and the corresponding thread) is
84 * created, and put in the list of ready process.
85 * \param name a name for the object. It is for user-level information
87 * \param code is a function describing the behavior of the agent. It
88 should then only use functions described in \ref
89 m_process_management (to create a new #m_process_t for example),
90 in \ref m_host_management (only the read-only functions i.e. whose
91 name contains the word get), in \ref m_task_management (to create
92 or destroy some #m_task_t for example) and in \ref
93 msg_gos_functions (to handle file transfers and task processing).
94 * \param data a pointer to any data one may want to attach to the new
95 object. It is for user-level information and can be NULL. It can
96 be retrieved with the function \ref MSG_process_get_data.
97 * \param host the location where the new agent is executed.
98 * \param argc first argument passed to \a code
99 * \param argv second argument passed to \a code
101 * \return The new corresponding object.
104 m_process_t MSG_process_create_with_arguments(const char *name,
105 xbt_main_func_t code,
106 void *data, m_host_t host,
107 int argc, char **argv)
109 return MSG_process_create_with_environment(name, code, data, host,
113 /** \ingroup m_process_management
114 * \brief Creates and runs a new #m_process_t.
116 * A constructor for #m_process_t taking four arguments and returning the
117 * corresponding object. The structure (and the corresponding thread) is
118 * created, and put in the list of ready process.
119 * \param name a name for the object. It is for user-level information
121 * \param code is a function describing the behavior of the agent. It
122 should then only use functions described in \ref
123 m_process_management (to create a new #m_process_t for example),
124 in \ref m_host_management (only the read-only functions i.e. whose
125 name contains the word get), in \ref m_task_management (to create
126 or destroy some #m_task_t for example) and in \ref
127 msg_gos_functions (to handle file transfers and task processing).
128 * \param data a pointer to any data one may want to attach to the new
129 object. It is for user-level information and can be NULL. It can
130 be retrieved with the function \ref MSG_process_get_data.
131 * \param host the location where the new agent is executed.
132 * \param argc first argument passed to \a code
133 * \param argv second argument passed to \a code
134 * \param properties list a properties defined for this process
136 * \return The new corresponding object.
138 m_process_t MSG_process_create_with_environment(const char *name,
139 xbt_main_func_t code,
140 void *data, m_host_t host,
141 int argc, char **argv,
142 xbt_dict_t properties)
144 simdata_process_t simdata = NULL;
145 m_process_t process = xbt_new0(s_m_process_t, 1);
146 smx_process_t smx_process = NULL;
147 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
149 smx_process = SIMIX_process_create(name, code,
150 (void *) process, host->name,
151 argc, argv, properties);
157 simdata = xbt_new0(s_simdata_process_t, 1);
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->s_process = smx_process;
168 if (SIMIX_process_self()) {
169 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
173 simdata->last_errno = MSG_OK;
176 /* Process structure */
177 process->name = xbt_strdup(name);
178 process->simdata = simdata;
179 process->data = data;
181 xbt_fifo_unshift(msg_global->process_list, process);
187 void _MSG_process_kill_from_SIMIX(void *p)
190 TRACE_msg_process_kill((m_process_t) p);
192 MSG_process_kill((m_process_t) p);
195 /** \ingroup m_process_management
196 * \param process poor victim
198 * This function simply kills a \a process... scarry isn't it ? :)
200 void MSG_process_kill(m_process_t process)
202 simdata_process_t p_simdata = process->simdata;
205 TRACE_msg_process_kill(process);
208 DEBUG3("Killing %s(%d) on %s",
209 process->name, p_simdata->PID, p_simdata->m_host->name);
211 if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
212 SIMIX_communication_cancel(p_simdata->waiting_task->simdata->comm);
215 if (p_simdata->waiting_action) {
216 DEBUG1("Canceling waiting task %s",
217 SIMIX_action_get_name(p_simdata->waiting_action));
218 SIMIX_action_cancel(p_simdata->waiting_action);
221 xbt_fifo_remove(msg_global->process_list, process);
222 SIMIX_process_kill(process->simdata->s_process);
227 /** \ingroup m_process_management
228 * \brief Migrates an agent to another location.
230 * This function checks whether \a process and \a host are valid pointers
231 and change the value of the #m_host_t on which \a process is running.
233 MSG_error_t MSG_process_change_host(m_host_t host)
235 m_process_t process = MSG_process_self();
236 m_host_t now = process->simdata->m_host;
237 process->simdata->m_host = host;
239 TRACE_msg_process_change_host(process, now, host);
241 SIMIX_process_change_host(process->simdata->s_process, now->name,
246 /** \ingroup m_process_management
247 * \brief Return the user data of a #m_process_t.
249 * This function checks whether \a process is a valid pointer or not
250 and return the user data associated to \a process if it is possible.
252 void *MSG_process_get_data(m_process_t process)
254 xbt_assert0((process != NULL), "Invalid parameters");
256 return (process->data);
259 /** \ingroup m_process_management
260 * \brief Set the user data of a #m_process_t.
262 * This function checks whether \a process is a valid pointer or not
263 and set the user data associated to \a process if it is possible.
265 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
267 xbt_assert0((process != NULL), "Invalid parameters");
269 process->data = data;
274 /** \ingroup m_process_management
275 * \brief Return the location on which an agent is running.
277 * This function checks whether \a process is a valid pointer or not
278 and return the m_host_t corresponding to the location on which \a
281 m_host_t MSG_process_get_host(m_process_t process)
283 xbt_assert0(((process != NULL)
284 && (process->simdata)), "Invalid parameters");
286 return (((simdata_process_t) process->simdata)->m_host);
289 /** \ingroup m_process_management
291 * \brief Return a #m_process_t given its PID.
293 * This function search in the list of all the created m_process_t for a m_process_t
294 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
295 Note that the PID are uniq in the whole simulation, not only on a given host.
297 m_process_t MSG_process_from_PID(int PID)
299 xbt_fifo_item_t i = NULL;
300 m_process_t process = NULL;
302 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
303 if (MSG_process_get_PID(process) == PID)
309 /** \ingroup m_process_management
310 * \brief Returns the process ID of \a process.
312 * This function checks whether \a process is a valid pointer or not
313 and return its PID (or 0 in case of problem).
315 int MSG_process_get_PID(m_process_t process)
317 /* Do not raise an exception here: this function is used in the logs,
318 and it will be called back by the exception handling stuff */
319 if (process == NULL || process->simdata == NULL)
322 return (((simdata_process_t) process->simdata)->PID);
325 /** \ingroup m_process_management
326 * \brief Returns the process ID of the parent of \a process.
328 * This function checks whether \a process is a valid pointer or not
329 and return its PID. Returns -1 if the agent has not been created by
332 int MSG_process_get_PPID(m_process_t process)
334 xbt_assert0(((process != NULL)
335 && (process->simdata)), "Invalid parameters");
337 return (((simdata_process_t) process->simdata)->PPID);
340 /** \ingroup m_process_management
341 * \brief Return the name of an agent.
343 * This function checks whether \a process is a valid pointer or not
346 const char *MSG_process_get_name(m_process_t process)
348 xbt_assert0(process, "Invalid parameter: process is NULL");
349 xbt_assert0(process->simdata,
350 "Invalid parameter: process->simdata is NULL");
352 return (process->name);
355 /** \ingroup m_process_management
356 * \brief Returns the value of a given process property
358 * \param process a process
359 * \param name a property name
360 * \return value of a property (or NULL if the property is not set)
362 const char *MSG_process_get_property_value(m_process_t process,
365 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
368 /** \ingroup m_process_management
369 * \brief Return the list of properties
371 * This function returns all the parameters associated with a process
373 xbt_dict_t MSG_process_get_properties(m_process_t process)
375 xbt_assert0((process != NULL), "Invalid parameters");
377 return (SIMIX_process_get_properties
378 (((simdata_process_t) process->simdata)->s_process));
382 /** \ingroup m_process_management
383 * \brief Return the PID of the current agent.
385 * This function returns the PID of the currently running #m_process_t.
387 int MSG_process_self_PID(void)
389 return (MSG_process_get_PID(MSG_process_self()));
392 /** \ingroup m_process_management
393 * \brief Return the PPID of the current agent.
395 * This function returns the PID of the parent of the currently
396 * running #m_process_t.
398 int MSG_process_self_PPID(void)
400 return (MSG_process_get_PPID(MSG_process_self()));
403 /** \ingroup m_process_management
404 * \brief Return the current agent.
406 * This function returns the currently running #m_process_t.
408 m_process_t MSG_process_self(void)
410 smx_process_t proc = SIMIX_process_self();
412 return (m_process_t) proc->data;
419 /** \ingroup m_process_management
420 * \brief Suspend the process.
422 * This function suspends the process by suspending the task on which
423 * it was waiting for the completion.
425 MSG_error_t MSG_process_suspend(m_process_t process)
427 xbt_assert0(((process != NULL)
428 && (process->simdata)), "Invalid parameters");
432 TRACE_msg_process_suspend(process);
435 SIMIX_process_suspend(process->simdata->s_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)
448 xbt_assert0(((process != NULL)
449 && (process->simdata)), "Invalid parameters");
453 TRACE_msg_process_resume(process);
456 SIMIX_process_resume(process->simdata->s_process);
460 /** \ingroup m_process_management
461 * \brief Returns true if the process is suspended .
463 * This checks whether a process is suspended or not by inspecting the
464 * task on which it was waiting for the completion.
466 int MSG_process_is_suspended(m_process_t process)
468 xbt_assert0(((process != NULL)
469 && (process->simdata)), "Invalid parameters");
470 return SIMIX_process_is_suspended(process->simdata->s_process);