3 /* Copyright (c) 2002-2007 Arnaud Legrand. */
4 /* Copyright (c) 2007 Bruno Donassolo. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
13 #include "../simix/private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
16 "Logging specific to MSG (process)");
18 /** \defgroup m_process_management Management Functions of Agents
19 * \brief This section describes the agent structure of MSG
20 * (#m_process_t) and the functions for managing it.
22 /** @addtogroup m_process_management
23 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
25 * We need to simulate many independent scheduling decisions, so
26 * the concept of <em>process</em> is at the heart of the
27 * simulator. A process may be defined as a <em>code</em>, with
28 * some <em>private data</em>, executing in a <em>location</em>.
32 /******************************** Process ************************************/
33 void __MSG_process_cleanup(void *arg)
35 /* arg is a pointer to a simix process, we can get the msg process with the field data */
36 m_process_t proc = ((smx_process_t) arg)->data;
38 TRACE_msg_process_end (proc);
40 xbt_fifo_remove(msg_global->process_list, proc);
41 SIMIX_process_cleanup(arg);
55 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
56 void *_MSG_process_create_from_SIMIX(const char *name,
57 xbt_main_func_t code, void *data,
58 char *hostname, int argc, char **argv,
59 xbt_dict_t properties)
61 m_host_t host = MSG_get_host_by_name(hostname);
62 return (void *) MSG_process_create_with_environment(name, code, data, host,
63 argc, argv, properties);
66 /** \ingroup m_process_management
67 * \brief Creates and runs a new #m_process_t.
69 * Does exactly the same as #MSG_process_create_with_arguments but without
70 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
71 * \sa MSG_process_create_with_arguments
73 m_process_t MSG_process_create(const char *name,
74 xbt_main_func_t code, void *data,
77 return MSG_process_create_with_environment(name, code, data, host, -1,
81 /** \ingroup m_process_management
82 * \brief Creates and runs a new #m_process_t.
84 * A constructor for #m_process_t taking four arguments and returning the
85 * corresponding object. The structure (and the corresponding thread) is
86 * created, and put in the list of ready process.
87 * \param name a name for the object. It is for user-level information
89 * \param code is a function describing the behavior of the agent. It
90 should then only use functions described in \ref
91 m_process_management (to create a new #m_process_t for example),
92 in \ref m_host_management (only the read-only functions i.e. whose
93 name contains the word get), in \ref m_task_management (to create
94 or destroy some #m_task_t for example) and in \ref
95 msg_gos_functions (to handle file transfers and task processing).
96 * \param data a pointer to any data one may want to attach to the new
97 object. It is for user-level information and can be NULL. It can
98 be retrieved with the function \ref MSG_process_get_data.
99 * \param host the location where the new agent is executed.
100 * \param argc first argument passed to \a code
101 * \param argv second argument passed to \a code
103 * \return The new corresponding object.
106 m_process_t MSG_process_create_with_arguments(const char *name,
107 xbt_main_func_t code,
108 void *data, m_host_t host,
109 int argc, char **argv)
111 return MSG_process_create_with_environment(name, code, data, host,
115 /** \ingroup m_process_management
116 * \brief Creates and runs a new #m_process_t.
118 * A constructor for #m_process_t taking four arguments and returning the
119 * corresponding object. The structure (and the corresponding thread) is
120 * created, and put in the list of ready process.
121 * \param name a name for the object. It is for user-level information
123 * \param code is a function describing the behavior of the agent. It
124 should then only use functions described in \ref
125 m_process_management (to create a new #m_process_t for example),
126 in \ref m_host_management (only the read-only functions i.e. whose
127 name contains the word get), in \ref m_task_management (to create
128 or destroy some #m_task_t for example) and in \ref
129 msg_gos_functions (to handle file transfers and task processing).
130 * \param data a pointer to any data one may want to attach to the new
131 object. It is for user-level information and can be NULL. It can
132 be retrieved with the function \ref MSG_process_get_data.
133 * \param host the location where the new agent is executed.
134 * \param argc first argument passed to \a code
135 * \param argv second argument passed to \a code
136 * \param properties list a properties defined for this process
138 * \return The new corresponding object.
140 m_process_t MSG_process_create_with_environment(const char *name,
141 xbt_main_func_t code,
142 void *data, m_host_t host,
143 int argc, char **argv,
144 xbt_dict_t properties)
146 simdata_process_t simdata = NULL;
147 m_process_t process = xbt_new0(s_m_process_t, 1);
148 smx_process_t smx_process = NULL;
149 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
151 smx_process = SIMIX_process_create(name, code,
152 (void *) process, host->name,
153 argc, argv, properties);
159 simdata = xbt_new0(s_simdata_process_t, 1);
162 simdata->PID = msg_global->PID++;
163 simdata->waiting_action = NULL;
164 simdata->waiting_task = NULL;
165 simdata->m_host = host;
166 simdata->argc = argc;
167 simdata->argv = argv;
168 simdata->s_process = smx_process;
170 if (SIMIX_process_self()) {
171 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
175 simdata->last_errno = MSG_OK;
178 /* Process structure */
179 process->name = xbt_strdup(name);
180 process->simdata = simdata;
181 process->data = data;
183 xbt_fifo_unshift(msg_global->process_list, process);
189 void _MSG_process_kill_from_SIMIX(void *p)
192 TRACE_msg_process_kill ((m_process_t) p);
194 MSG_process_kill((m_process_t) p);
197 /** \ingroup m_process_management
198 * \param process poor victim
200 * This function simply kills a \a process... scarry isn't it ? :)
202 void MSG_process_kill(m_process_t process)
204 simdata_process_t p_simdata = process->simdata;
207 TRACE_msg_process_kill (process);
210 DEBUG3("Killing %s(%d) on %s",
211 process->name, p_simdata->PID, p_simdata->m_host->name);
213 if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
214 SIMIX_communication_cancel(p_simdata->waiting_task->simdata->comm);
217 if (p_simdata->waiting_action) {
218 DEBUG1("Canceling waiting task %s",
219 SIMIX_action_get_name(p_simdata->waiting_action));
220 SIMIX_action_cancel(p_simdata->waiting_action);
223 xbt_fifo_remove(msg_global->process_list, process);
224 SIMIX_process_kill(process->simdata->s_process);
229 /** \ingroup m_process_management
230 * \brief Migrates an agent to another location.
232 * This function checks whether \a process and \a host are valid pointers
233 and change the value of the #m_host_t on which \a process is running.
235 MSG_error_t MSG_process_change_host(m_host_t host)
237 m_process_t process = MSG_process_self();
238 m_host_t now = process->simdata->m_host;
239 process->simdata->m_host = host;
241 TRACE_msg_process_change_host (process, now, host);
243 SIMIX_process_change_host(process->simdata->s_process, now->name,
248 /** \ingroup m_process_management
249 * \brief Return the user data of a #m_process_t.
251 * This function checks whether \a process is a valid pointer or not
252 and return the user data associated to \a process if it is possible.
254 void *MSG_process_get_data(m_process_t process)
256 xbt_assert0((process != NULL), "Invalid parameters");
258 return (process->data);
261 /** \ingroup m_process_management
262 * \brief Set the user data of a #m_process_t.
264 * This function checks whether \a process is a valid pointer or not
265 and set the user data associated to \a process if it is possible.
267 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
269 xbt_assert0((process != NULL), "Invalid parameters");
271 process->data = data;
276 /** \ingroup m_process_management
277 * \brief Return the location on which an agent is running.
279 * This function checks whether \a process is a valid pointer or not
280 and return the m_host_t corresponding to the location on which \a
283 m_host_t MSG_process_get_host(m_process_t process)
285 xbt_assert0(((process != NULL)
286 && (process->simdata)), "Invalid parameters");
288 return (((simdata_process_t) process->simdata)->m_host);
291 /** \ingroup m_process_management
293 * \brief Return a #m_process_t given its PID.
295 * This function search in the list of all the created m_process_t for a m_process_t
296 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
297 Note that the PID are uniq in the whole simulation, not only on a given host.
299 m_process_t MSG_process_from_PID(int PID)
301 xbt_fifo_item_t i = NULL;
302 m_process_t process = NULL;
304 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
305 if (MSG_process_get_PID(process) == PID)
311 /** \ingroup m_process_management
312 * \brief Returns the process ID of \a process.
314 * This function checks whether \a process is a valid pointer or not
315 and return its PID (or 0 in case of problem).
317 int MSG_process_get_PID(m_process_t process)
319 /* Do not raise an exception here: this function is used in the logs,
320 and it will be called back by the exception handling stuff */
321 if (process == NULL || process->simdata == NULL)
324 return (((simdata_process_t) process->simdata)->PID);
327 /** \ingroup m_process_management
328 * \brief Returns the process ID of the parent of \a process.
330 * This function checks whether \a process is a valid pointer or not
331 and return its PID. Returns -1 if the agent has not been created by
334 int MSG_process_get_PPID(m_process_t process)
336 xbt_assert0(((process != NULL)
337 && (process->simdata)), "Invalid parameters");
339 return (((simdata_process_t) process->simdata)->PPID);
342 /** \ingroup m_process_management
343 * \brief Return the name of an agent.
345 * This function checks whether \a process is a valid pointer or not
348 const char *MSG_process_get_name(m_process_t process)
350 xbt_assert0(process, "Invalid parameter: process is NULL");
351 xbt_assert0(process->simdata,
352 "Invalid parameter: process->simdata is NULL");
354 return (process->name);
357 /** \ingroup m_process_management
358 * \brief Returns the value of a given process property
360 * \param process a process
361 * \param name a property name
362 * \return value of a property (or NULL if the property is not set)
364 const char *MSG_process_get_property_value(m_process_t process,
367 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
370 /** \ingroup m_process_management
371 * \brief Return the list of properties
373 * This function returns all the parameters associated with a process
375 xbt_dict_t MSG_process_get_properties(m_process_t process)
377 xbt_assert0((process != NULL), "Invalid parameters");
379 return (SIMIX_process_get_properties
380 (((simdata_process_t) process->simdata)->s_process));
384 /** \ingroup m_process_management
385 * \brief Return the PID of the current agent.
387 * This function returns the PID of the currently running #m_process_t.
389 int MSG_process_self_PID(void)
391 return (MSG_process_get_PID(MSG_process_self()));
394 /** \ingroup m_process_management
395 * \brief Return the PPID of the current agent.
397 * This function returns the PID of the parent of the currently
398 * running #m_process_t.
400 int MSG_process_self_PPID(void)
402 return (MSG_process_get_PPID(MSG_process_self()));
405 /** \ingroup m_process_management
406 * \brief Return the current agent.
408 * This function returns the currently running #m_process_t.
410 m_process_t MSG_process_self(void)
412 smx_process_t proc = SIMIX_process_self();
414 return (m_process_t) proc->data;
421 /** \ingroup m_process_management
422 * \brief Suspend the process.
424 * This function suspends the process by suspending the task on which
425 * it was waiting for the completion.
427 MSG_error_t MSG_process_suspend(m_process_t process)
429 xbt_assert0(((process != NULL)
430 && (process->simdata)), "Invalid parameters");
434 TRACE_msg_process_suspend (process);
437 SIMIX_process_suspend(process->simdata->s_process);
441 /** \ingroup m_process_management
442 * \brief Resume a suspended process.
444 * This function resumes a suspended process by resuming the task on
445 * which it was waiting for the completion.
447 MSG_error_t MSG_process_resume(m_process_t process)
450 xbt_assert0(((process != NULL)
451 && (process->simdata)), "Invalid parameters");
455 TRACE_msg_process_resume (process);
458 SIMIX_process_resume(process->simdata->s_process);
462 /** \ingroup m_process_management
463 * \brief Returns true if the process is suspended .
465 * This checks whether a process is suspended or not by inspecting the
466 * task on which it was waiting for the completion.
468 int MSG_process_is_suspended(m_process_t process)
470 xbt_assert0(((process != NULL)
471 && (process->simdata)), "Invalid parameters");
472 return SIMIX_process_is_suspended(process->simdata->s_process);