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 ************************************/
29 void __MSG_process_cleanup(smx_process_t smx_proc)
31 /* arg is a pointer to a simix process, we can get the msg process with the field data */
32 m_process_t proc = SIMIX_req_process_get_data(smx_proc);
34 TRACE_msg_process_end(proc);
36 xbt_fifo_remove(msg_global->process_list, proc);
37 SIMIX_process_cleanup(smx_proc);
51 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
52 void *_MSG_process_create_from_SIMIX(const char *name,
53 xbt_main_func_t code, void *data,
54 char *hostname, int argc, char **argv,
55 xbt_dict_t properties)
57 m_host_t host = MSG_get_host_by_name(hostname);
58 return (void *) MSG_process_create_with_environment(name, code, data,
63 /** \ingroup m_process_management
64 * \brief Creates and runs a new #m_process_t.
66 * Does exactly the same as #MSG_process_create_with_arguments but without
67 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
68 * \sa MSG_process_create_with_arguments
70 m_process_t MSG_process_create(const char *name,
71 xbt_main_func_t code, void *data,
74 return MSG_process_create_with_environment(name, code, data, host, -1,
78 /** \ingroup m_process_management
79 * \brief Creates and runs a new #m_process_t.
81 * A constructor for #m_process_t taking four arguments and returning the
82 * corresponding object. The structure (and the corresponding thread) is
83 * created, and put in the list of ready process.
84 * \param name a name for the object. It is for user-level information
86 * \param code is a function describing the behavior of the agent. It
87 should then only use functions described in \ref
88 m_process_management (to create a new #m_process_t for example),
89 in \ref m_host_management (only the read-only functions i.e. whose
90 name contains the word get), in \ref m_task_management (to create
91 or destroy some #m_task_t for example) and in \ref
92 msg_gos_functions (to handle file transfers and task processing).
93 * \param data a pointer to any data one may want to attach to the new
94 object. It is for user-level information and can be NULL. It can
95 be retrieved with the function \ref MSG_process_get_data.
96 * \param host the location where the new agent is executed.
97 * \param argc first argument passed to \a code
98 * \param argv second argument passed to \a code
100 * \return The new corresponding object.
103 m_process_t MSG_process_create_with_arguments(const char *name,
104 xbt_main_func_t code,
105 void *data, m_host_t host,
106 int argc, char **argv)
108 return MSG_process_create_with_environment(name, code, data, host,
112 /** \ingroup m_process_management
113 * \brief Creates and runs a new #m_process_t.
115 * A constructor for #m_process_t taking four arguments and returning the
116 * corresponding object. The structure (and the corresponding thread) is
117 * created, and put in the list of ready process.
118 * \param name a name for the object. It is for user-level information
120 * \param code is a function describing the behavior of the agent. It
121 should then only use functions described in \ref
122 m_process_management (to create a new #m_process_t for example),
123 in \ref m_host_management (only the read-only functions i.e. whose
124 name contains the word get), in \ref m_task_management (to create
125 or destroy some #m_task_t for example) and in \ref
126 msg_gos_functions (to handle file transfers and task processing).
127 * \param data a pointer to any data one may want to attach to the new
128 object. It is for user-level information and can be NULL. It can
129 be retrieved with the function \ref MSG_process_get_data.
130 * \param host the location where the new agent is executed.
131 * \param argc first argument passed to \a code
132 * \param argv second argument passed to \a code
133 * \param properties list a properties defined for this process
135 * \return The new corresponding object.
137 m_process_t MSG_process_create_with_environment(const char *name,
138 xbt_main_func_t code,
139 void *data, m_host_t host,
140 int argc, char **argv,
141 xbt_dict_t properties)
143 simdata_process_t simdata = NULL;
144 m_process_t process = xbt_new0(s_m_process_t, 1);
145 smx_process_t smx_process = NULL;
146 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
148 smx_process = SIMIX_process_create(name, code, (void *) process, host->name,
149 argc, argv, properties);
155 simdata = xbt_new0(s_simdata_process_t, 1);
158 simdata->PID = msg_global->PID++;
159 simdata->waiting_action = NULL;
160 simdata->waiting_task = NULL;
161 simdata->m_host = host;
162 simdata->argc = argc;
163 simdata->argv = argv;
164 simdata->s_process = smx_process;
166 if (SIMIX_process_self()) {
167 simdata->PPID = MSG_process_get_PID(SIMIX_process_self_get_data());
171 simdata->last_errno = MSG_OK;
174 /* Process structure */
175 process->name = xbt_strdup(name);
176 process->simdata = simdata;
177 process->data = data;
179 xbt_fifo_unshift(msg_global->process_list, process);
185 void _MSG_process_kill_from_SIMIX(void *p)
188 TRACE_msg_process_kill((m_process_t) p);
190 MSG_process_kill((m_process_t) p);
193 /** \ingroup m_process_management
194 * \param process poor victim
196 * This function simply kills a \a process... scarry isn't it ? :)
198 void MSG_process_kill(m_process_t process)
200 simdata_process_t p_simdata = process->simdata;
203 TRACE_msg_process_kill(process);
206 DEBUG3("Killing %s(%d) on %s",
207 process->name, p_simdata->PID, p_simdata->m_host->name);
209 if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
210 SIMIX_req_comm_cancel(p_simdata->waiting_task->simdata->comm);
213 xbt_fifo_remove(msg_global->process_list, process);
214 SIMIX_req_process_kill(process->simdata->s_process);
219 /** \ingroup m_process_management
220 * \brief Migrates an agent to another location.
222 * This function checks whether \a process and \a host are valid pointers
223 and change the value of the #m_host_t on which \a process is running.
225 MSG_error_t MSG_process_change_host(m_host_t host)
227 m_process_t process = MSG_process_self();
228 m_host_t now = process->simdata->m_host;
229 process->simdata->m_host = host;
231 TRACE_msg_process_change_host(process, now, host);
233 SIMIX_req_process_change_host(process->simdata->s_process, now->name,
238 /** \ingroup m_process_management
239 * \brief Return the user data of a #m_process_t.
241 * This function checks whether \a process is a valid pointer or not
242 and return the user data associated to \a process if it is possible.
244 void *MSG_process_get_data(m_process_t process)
246 xbt_assert0((process != NULL), "Invalid parameters");
248 return (process->data);
251 /** \ingroup m_process_management
252 * \brief Set the user data of a #m_process_t.
254 * This function checks whether \a process is a valid pointer or not
255 and set the user data associated to \a process if it is possible.
257 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
259 xbt_assert0((process != NULL), "Invalid parameters");
261 process->data = data;
266 /** \ingroup m_process_management
267 * \brief Return the location on which an agent is running.
269 * This function checks whether \a process is a valid pointer or not
270 and return the m_host_t corresponding to the location on which \a
273 m_host_t MSG_process_get_host(m_process_t process)
275 xbt_assert0(((process != NULL)
276 && (process->simdata)), "Invalid parameters");
278 return (((simdata_process_t) process->simdata)->m_host);
281 /** \ingroup m_process_management
283 * \brief Return a #m_process_t given its PID.
285 * This function search in the list of all the created m_process_t for a m_process_t
286 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
287 Note that the PID are uniq in the whole simulation, not only on a given host.
289 m_process_t MSG_process_from_PID(int PID)
291 xbt_fifo_item_t i = NULL;
292 m_process_t process = NULL;
294 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
295 if (MSG_process_get_PID(process) == PID)
301 /** \ingroup m_process_management
302 * \brief Returns the process ID of \a process.
304 * This function checks whether \a process is a valid pointer or not
305 and return its PID (or 0 in case of problem).
307 int MSG_process_get_PID(m_process_t process)
309 /* Do not raise an exception here: this function is used in the logs,
310 and it will be called back by the exception handling stuff */
311 if (process == NULL || process->simdata == NULL)
314 return (((simdata_process_t) process->simdata)->PID);
317 /** \ingroup m_process_management
318 * \brief Returns the process ID of the parent of \a process.
320 * This function checks whether \a process is a valid pointer or not
321 and return its PID. Returns -1 if the agent has not been created by
324 int MSG_process_get_PPID(m_process_t process)
326 xbt_assert0(((process != NULL)
327 && (process->simdata)), "Invalid parameters");
329 return (((simdata_process_t) process->simdata)->PPID);
332 /** \ingroup m_process_management
333 * \brief Return the name of an agent.
335 * This function checks whether \a process is a valid pointer or not
338 const char *MSG_process_get_name(m_process_t process)
340 xbt_assert0(process, "Invalid parameter: process is NULL");
341 xbt_assert0(process->simdata,
342 "Invalid parameter: process->simdata is NULL");
344 return (process->name);
347 /** \ingroup m_process_management
348 * \brief Returns the value of a given process property
350 * \param process a process
351 * \param name a property name
352 * \return value of a property (or NULL if the property is not set)
354 const char *MSG_process_get_property_value(m_process_t process,
357 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
360 /** \ingroup m_process_management
361 * \brief Return the list of properties
363 * This function returns all the parameters associated with a process
365 xbt_dict_t MSG_process_get_properties(m_process_t process)
367 xbt_assert0((process != NULL), "Invalid parameters");
369 return (SIMIX_req_process_get_properties
370 (((simdata_process_t) process->simdata)->s_process));
374 /** \ingroup m_process_management
375 * \brief Return the PID of the current agent.
377 * This function returns the PID of the currently running #m_process_t.
379 int MSG_process_self_PID(void)
381 return (MSG_process_get_PID(MSG_process_self()));
384 /** \ingroup m_process_management
385 * \brief Return the PPID of the current agent.
387 * This function returns the PID of the parent of the currently
388 * running #m_process_t.
390 int MSG_process_self_PPID(void)
392 return (MSG_process_get_PPID(MSG_process_self()));
395 /** \ingroup m_process_management
396 * \brief Return the current agent.
398 * This function returns the currently running #m_process_t.
400 m_process_t MSG_process_self(void)
402 /* we cannot make a SIMIX request here because this may create an exception or a logging
403 event, and both mechanisms call MSG_process_self() again (via xbt_getpid()) */
404 return (m_process_t) SIMIX_process_self_get_data();
407 /** \ingroup m_process_management
408 * \brief Suspend the process.
410 * This function suspends the process by suspending the task on which
411 * it was waiting for the completion.
413 MSG_error_t MSG_process_suspend(m_process_t process)
415 xbt_assert0(((process != NULL)
416 && (process->simdata)), "Invalid parameters");
420 TRACE_msg_process_suspend(process);
423 SIMIX_req_process_suspend(process->simdata->s_process);
427 /** \ingroup m_process_management
428 * \brief Resume a suspended process.
430 * This function resumes a suspended process by resuming the task on
431 * which it was waiting for the completion.
433 MSG_error_t MSG_process_resume(m_process_t process)
436 xbt_assert0(((process != NULL)
437 && (process->simdata)), "Invalid parameters");
441 TRACE_msg_process_resume(process);
444 SIMIX_req_process_resume(process->simdata->s_process);
448 /** \ingroup m_process_management
449 * \brief Returns true if the process is suspended .
451 * This checks whether a process is suspended or not by inspecting the
452 * task on which it was waiting for the completion.
454 int MSG_process_is_suspended(m_process_t process)
456 xbt_assert0(((process != NULL)
457 && (process->simdata)), "Invalid parameters");
458 return SIMIX_req_process_is_suspended(process->simdata->s_process);
462 smx_context_t MSG_process_get_smx_ctx(m_process_t process) {
463 return SIMIX_process_get_context(process->simdata->s_process);