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"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
15 "Logging specific to MSG (process)");
17 /** \defgroup m_process_management Management Functions of Agents
18 * \brief This section describes the agent structure of MSG
19 * (#m_process_t) and the functions for managing it.
21 /** @addtogroup m_process_management
22 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
24 * We need to simulate many independent scheduling decisions, so
25 * the concept of <em>process</em> is at the heart of the
26 * simulator. A process may be defined as a <em>code</em>, with
27 * some <em>private data</em>, executing in a <em>location</em>.
31 /******************************** Process ************************************/
32 void __MSG_process_cleanup(void *arg)
34 /* arg is a pointer to a simix process, we can get the msg process with the field data */
35 m_process_t proc = ((smx_process_t) arg)->data;
36 xbt_fifo_remove(msg_global->process_list, proc);
37 SIMIX_process_cleanup(arg);
47 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
48 void *_MSG_process_create_from_SIMIX(const char *name,
49 xbt_main_func_t code, void *data,
50 char *hostname, int argc, char **argv, xbt_dict_t properties)
52 m_host_t host = MSG_get_host_by_name(hostname);
53 return (void *) MSG_process_create_with_environment(name, code, data, host,
54 argc, argv,properties);
57 /** \ingroup m_process_management
58 * \brief Creates and runs a new #m_process_t.
60 * Does exactly the same as #MSG_process_create_with_arguments but without
61 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
62 * \sa MSG_process_create_with_arguments
64 m_process_t MSG_process_create(const char *name,
65 xbt_main_func_t code, void *data,
68 return MSG_process_create_with_environment(name, code, data, host, -1,
72 /** \ingroup m_process_management
73 * \brief Creates and runs a new #m_process_t.
75 * A constructor for #m_process_t taking four arguments and returning the
76 * corresponding object. The structure (and the corresponding thread) is
77 * created, and put in the list of ready process.
78 * \param name a name for the object. It is for user-level information
80 * \param code is a function describing the behavior of the agent. It
81 should then only use functions described in \ref
82 m_process_management (to create a new #m_process_t for example),
83 in \ref m_host_management (only the read-only functions i.e. whose
84 name contains the word get), in \ref m_task_management (to create
85 or destroy some #m_task_t for example) and in \ref
86 msg_gos_functions (to handle file transfers and task processing).
87 * \param data a pointer to any data one may want to attach to the new
88 object. It is for user-level information and can be NULL. It can
89 be retrieved with the function \ref MSG_process_get_data.
90 * \param host the location where the new agent is executed.
91 * \param argc first argument passed to \a code
92 * \param argv second argument passed to \a code
94 * \return The new corresponding object.
97 m_process_t MSG_process_create_with_arguments(const char *name,
99 void *data, m_host_t host,
100 int argc, char **argv)
102 return MSG_process_create_with_environment(name, code, data, host,
106 /** \ingroup m_process_management
107 * \brief Creates and runs a new #m_process_t.
109 * A constructor for #m_process_t taking four arguments and returning the
110 * corresponding object. The structure (and the corresponding thread) is
111 * created, and put in the list of ready process.
112 * \param name a name for the object. It is for user-level information
114 * \param code is a function describing the behavior of the agent. It
115 should then only use functions described in \ref
116 m_process_management (to create a new #m_process_t for example),
117 in \ref m_host_management (only the read-only functions i.e. whose
118 name contains the word get), in \ref m_task_management (to create
119 or destroy some #m_task_t for example) and in \ref
120 msg_gos_functions (to handle file transfers and task processing).
121 * \param data a pointer to any data one may want to attach to the new
122 object. It is for user-level information and can be NULL. It can
123 be retrieved with the function \ref MSG_process_get_data.
124 * \param host the location where the new agent is executed.
125 * \param argc first argument passed to \a code
126 * \param argv second argument passed to \a code
127 * \param properties list a properties defined for this process
129 * \return The new corresponding object.
131 m_process_t MSG_process_create_with_environment(const char *name,
132 xbt_main_func_t code,
133 void *data, m_host_t host,
134 int argc, char **argv, xbt_dict_t properties)
136 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
137 m_process_t process = xbt_new0(s_m_process_t, 1);
138 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
141 simdata->PID = msg_global->PID++;
142 simdata->waiting_task = NULL;
143 simdata->m_host = host;
144 simdata->argc = argc;
145 simdata->argv = argv;
146 simdata->s_process = SIMIX_process_create(name, code,
147 (void *) process, host->name,
148 argc, argv, properties);
150 if (SIMIX_process_self()) {
151 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
155 simdata->last_errno = MSG_OK;
158 /* Process structure */
159 process->name = xbt_strdup(name);
160 process->simdata = simdata;
161 process->data = data;
163 xbt_fifo_unshift(msg_global->process_list, process);
169 void _MSG_process_kill_from_SIMIX(void *p)
171 MSG_process_kill((m_process_t) p);
174 /** \ingroup m_process_management
175 * \param process poor victim
177 * This function simply kills a \a process... scarry isn't it ? :)
179 void MSG_process_kill(m_process_t process)
181 simdata_process_t p_simdata = process->simdata;
183 DEBUG3("Killing %s(%d) on %s",
184 process->name, p_simdata->PID, p_simdata->m_host->name);
186 if (p_simdata->waiting_task) {
187 DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
188 if (p_simdata->waiting_task->simdata->compute) {
189 SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
190 } else if (p_simdata->waiting_task->simdata->comm) {
191 SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
195 xbt_fifo_remove(msg_global->process_list, process);
196 SIMIX_process_kill(process->simdata->s_process);
201 /** \ingroup m_process_management
202 * \brief Migrates an agent to another location.
204 * This functions checks whether \a process and \a host are valid pointers
205 and change the value of the #m_host_t on which \a process is running.
207 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
210 ("MSG_process_change_host - not implemented yet - maybe useless function");
214 /** \ingroup m_process_management
215 * \brief Return the user data of a #m_process_t.
217 * This functions checks whether \a process is a valid pointer or not
218 and return the user data associated to \a process if it is possible.
220 void *MSG_process_get_data(m_process_t process)
222 xbt_assert0((process != NULL), "Invalid parameters");
224 return (process->data);
227 /** \ingroup m_process_management
228 * \brief Set the user data of a #m_process_t.
230 * This functions checks whether \a process is a valid pointer or not
231 and set the user data associated to \a process if it is possible.
233 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
235 xbt_assert0((process != NULL), "Invalid parameters");
236 xbt_assert0((process->data == NULL), "Data already set");
238 process->data = data;
243 /** \ingroup m_process_management
244 * \brief Return the location on which an agent is running.
246 * This functions checks whether \a process is a valid pointer or not
247 and return the m_host_t corresponding to the location on which \a
250 m_host_t MSG_process_get_host(m_process_t process)
252 xbt_assert0(((process != NULL)
253 && (process->simdata)), "Invalid parameters");
255 return (((simdata_process_t) process->simdata)->m_host);
258 /** \ingroup m_process_management
260 * \brief Return a #m_process_t given its PID.
262 * This functions search in the list of all the created m_process_t for a m_process_t
263 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
264 Note that the PID are uniq in the whole simulation, not only on a given host.
266 m_process_t MSG_process_from_PID(int PID)
268 xbt_fifo_item_t i = NULL;
269 m_process_t process = NULL;
271 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
272 if (MSG_process_get_PID(process) == PID)
278 /** \ingroup m_process_management
279 * \brief Returns the process ID of \a process.
281 * This functions checks whether \a process is a valid pointer or not
282 and return its PID (or 0 in case of problem).
284 int MSG_process_get_PID(m_process_t process)
286 /* Do not raise an exception here: this function is used in the logs,
287 and it will be called back by the exception handling stuff */
288 if (process == NULL || process->simdata == NULL)
291 return (((simdata_process_t) process->simdata)->PID);
294 /** \ingroup m_process_management
295 * \brief Returns the process ID of the parent of \a process.
297 * This functions checks whether \a process is a valid pointer or not
298 and return its PID. Returns -1 if the agent has not been created by
301 int MSG_process_get_PPID(m_process_t process)
303 xbt_assert0(((process != NULL)
304 && (process->simdata)), "Invalid parameters");
306 return (((simdata_process_t) process->simdata)->PPID);
309 /** \ingroup m_process_management
310 * \brief Return the name of an agent.
312 * This functions checks whether \a process is a valid pointer or not
315 const char *MSG_process_get_name(m_process_t process)
317 xbt_assert0(((process != NULL)
318 && (process->simdata)), "Invalid parameters");
320 return (process->name);
323 /** \ingroup m_process_management
324 * \brief Returns the value of a given process property
326 * \param process a process
327 * \param name a property name
328 * \return value of a property (or NULL if the property is not set)
330 const char* MSG_process_get_property_value(m_process_t process, const char* name)
332 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
335 /** \ingroup m_process_management
336 * \brief Return the list of properties
338 * This functions returns all the parameters associated with a process
340 xbt_dict_t MSG_process_get_properties(m_process_t process)
342 xbt_assert0((process != NULL), "Invalid parameters");
344 return (SIMIX_process_get_properties(((simdata_process_t)process->simdata)->s_process));
348 /** \ingroup m_process_management
349 * \brief Return the PID of the current agent.
351 * This functions returns the PID of the currently running #m_process_t.
353 int MSG_process_self_PID(void)
355 return (MSG_process_get_PID(MSG_process_self()));
358 /** \ingroup m_process_management
359 * \brief Return the PPID of the current agent.
361 * This functions returns the PID of the parent of the currently
362 * running #m_process_t.
364 int MSG_process_self_PPID(void)
366 return (MSG_process_get_PPID(MSG_process_self()));
369 /** \ingroup m_process_management
370 * \brief Return the current agent.
372 * This functions returns the currently running #m_process_t.
374 m_process_t MSG_process_self(void)
376 smx_process_t proc = SIMIX_process_self();
378 return (m_process_t) proc->data;
385 /** \ingroup m_process_management
386 * \brief Suspend the process.
388 * This functions suspend the process by suspending the task on which
389 * it was waiting for the completion.
391 MSG_error_t MSG_process_suspend(m_process_t process)
393 xbt_assert0(((process != NULL)
394 && (process->simdata)), "Invalid parameters");
397 SIMIX_process_suspend(process->simdata->s_process);
401 /** \ingroup m_process_management
402 * \brief Resume a suspended process.
404 * This functions resume a suspended process by resuming the task on
405 * which it was waiting for the completion.
407 MSG_error_t MSG_process_resume(m_process_t process)
410 xbt_assert0(((process != NULL)
411 && (process->simdata)), "Invalid parameters");
414 SIMIX_process_resume(process->simdata->s_process);
418 /** \ingroup m_process_management
419 * \brief Returns true if the process is suspended .
421 * This checks whether a process is suspended or not by inspecting the
422 * task on which it was waiting for the completion.
424 int MSG_process_is_suspended(m_process_t process)
426 xbt_assert0(((process != NULL)
427 && (process->simdata)), "Invalid parameters");
428 return SIMIX_process_is_suspended(process->simdata->s_process);