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 /** \ingroup m_process_management
33 * \brief Creates and runs a new #m_process_t.
35 * Does exactly the same as #MSG_process_create_with_arguments but without
36 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
37 * \sa MSG_process_create_with_arguments
39 m_process_t MSG_process_create(const char *name,
40 xbt_main_func_t code, void *data,
43 return MSG_process_create_with_arguments(name, code, data, host, -1,
47 void __MSG_process_cleanup(void *arg)
49 /* arg is a pointer to a simix process, we can get the msg process with the field data */
50 m_process_t proc = ((smx_process_t) arg)->data;
51 xbt_fifo_remove(msg_global->process_list, proc);
52 SIMIX_process_cleanup(arg);
62 /* This function creates a MSG process. It has the prototype by SIMIX_function_register_process_create */
63 void *_MSG_process_create_from_SIMIX(const char *name,
64 xbt_main_func_t code, void *data,
65 char *hostname, int argc, char **argv)
67 m_host_t host = MSG_get_host_by_name(hostname);
68 return (void *) MSG_process_create_with_arguments(name, code, data, host,
72 /* This function creates a MSG process with properties. It has the prototype by SIMIX_function_register_process_create */
73 void *_MSG_process_create_with_env_from_SIMIX(const char *name,
74 xbt_main_func_t code, void *data,
75 char *hostname, int argc, char **argv, xbt_dict_t properties)
77 m_host_t host = MSG_get_host_by_name(hostname);
78 return (void *) MSG_process_create_with_environment(name, code, data, host,
79 argc, argv,properties);
83 /** \ingroup m_process_management
84 * \brief Creates and runs a new #m_process_t.
86 * A constructor for #m_process_t taking four arguments and returning the
87 * corresponding object. The structure (and the corresponding thread) is
88 * created, and put in the list of ready process.
89 * \param name a name for the object. It is for user-level information
91 * \param code is a function describing the behavior of the agent. It
92 should then only use functions described in \ref
93 m_process_management (to create a new #m_process_t for example),
94 in \ref m_host_management (only the read-only functions i.e. whose
95 name contains the word get), in \ref m_task_management (to create
96 or destroy some #m_task_t for example) and in \ref
97 msg_gos_functions (to handle file transfers and task processing).
98 * \param data a pointer to any data one may want to attach to the new
99 object. It is for user-level information and can be NULL. It can
100 be retrieved with the function \ref MSG_process_get_data.
101 * \param host the location where the new agent is executed.
102 * \param argc first argument passed to \a code
103 * \param argv second argument passed to \a code
105 * \return The new corresponding object.
108 m_process_t MSG_process_create_with_arguments(const char *name,
109 xbt_main_func_t code,
110 void *data, m_host_t host,
111 int argc, char **argv)
113 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
114 m_process_t process = xbt_new0(s_m_process_t, 1);
115 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
118 simdata->PID = msg_global->PID++;
119 simdata->waiting_task = NULL;
120 simdata->m_host = host;
121 simdata->argc = argc;
122 simdata->argv = argv;
123 simdata->s_process = SIMIX_process_create(name, code,
124 (void *) process, host->name,
127 if (SIMIX_process_self()) {
128 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
132 simdata->last_errno = MSG_OK;
135 /* Process structure */
136 process->name = xbt_strdup(name);
137 process->simdata = simdata;
138 process->data = data ;
140 xbt_fifo_unshift(msg_global->process_list, process);
145 /** \ingroup m_process_management
146 * \brief Creates and runs a new #m_process_t.
148 * A constructor for #m_process_t taking four arguments and returning the
149 * corresponding object. The structure (and the corresponding thread) is
150 * created, and put in the list of ready process.
151 * \param name a name for the object. It is for user-level information
153 * \param code is a function describing the behavior of the agent. It
154 should then only use functions described in \ref
155 m_process_management (to create a new #m_process_t for example),
156 in \ref m_host_management (only the read-only functions i.e. whose
157 name contains the word get), in \ref m_task_management (to create
158 or destroy some #m_task_t for example) and in \ref
159 msg_gos_functions (to handle file transfers and task processing).
160 * \param data a pointer to any data one may want to attach to the new
161 object. It is for user-level information and can be NULL. It can
162 be retrieved with the function \ref MSG_process_get_data.
163 * \param host the location where the new agent is executed.
164 * \param argc first argument passed to \a code
165 * \param argv second argument passed to \a code
166 * \param properties list a properties defined for this process
168 * \return The new corresponding object.
170 m_process_t MSG_process_create_with_environment(const char *name,
171 xbt_main_func_t code,
172 void *data, m_host_t host,
173 int argc, char **argv, xbt_dict_t properties)
175 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
176 m_process_t process = xbt_new0(s_m_process_t, 1);
177 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
180 simdata->PID = msg_global->PID++;
181 simdata->waiting_task = NULL;
182 simdata->m_host = host;
183 simdata->argc = argc;
184 simdata->argv = argv;
185 simdata->s_process = SIMIX_process_create(name, code,
186 (void *) process, host->name,
187 argc, argv, properties);
189 if (SIMIX_process_self()) {
190 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
194 simdata->last_errno = MSG_OK;
197 /* Process structure */
198 process->name = xbt_strdup(name);
199 process->simdata = simdata;
200 process->data = data ;
202 xbt_fifo_unshift(msg_global->process_list, process);
208 void _MSG_process_kill_from_SIMIX(void *p)
210 MSG_process_kill((m_process_t) p);
213 /** \ingroup m_process_management
214 * \param process poor victim
216 * This function simply kills a \a process... scarry isn't it ? :)
218 void MSG_process_kill(m_process_t process)
220 simdata_process_t p_simdata = process->simdata;
222 DEBUG3("Killing %s(%d) on %s",
223 process->name, p_simdata->PID, p_simdata->m_host->name);
225 if (p_simdata->waiting_task) {
226 DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
227 if (p_simdata->waiting_task->simdata->compute) {
228 SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
229 } else if (p_simdata->waiting_task->simdata->comm) {
230 SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
234 xbt_fifo_remove(msg_global->process_list, process);
235 SIMIX_process_kill(process->simdata->s_process);
240 /** \ingroup m_process_management
241 * \brief Migrates an agent to another location.
243 * This functions checks whether \a process and \a host are valid pointers
244 and change the value of the #m_host_t on which \a process is running.
246 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
249 ("MSG_process_change_host - not implemented yet - maybe useless function");
253 /** \ingroup m_process_management
254 * \brief Return the user data of a #m_process_t.
256 * This functions checks whether \a process is a valid pointer or not
257 and return the user data associated to \a process if it is possible.
259 void *MSG_process_get_data(m_process_t process)
261 xbt_assert0((process != NULL), "Invalid parameters");
263 return (process->data);
266 /** \ingroup m_process_management
267 * \brief Set the user data of a #m_process_t.
269 * This functions checks whether \a process is a valid pointer or not
270 and set the user data associated to \a process if it is possible.
272 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
274 xbt_assert0((process != NULL), "Invalid parameters");
275 xbt_assert0((process->data == NULL), "Data already set");
277 process->data = data;
282 /** \ingroup m_process_management
283 * \brief Return the location on which an agent is running.
285 * This functions checks whether \a process is a valid pointer or not
286 and return the m_host_t corresponding to the location on which \a
289 m_host_t MSG_process_get_host(m_process_t process)
291 xbt_assert0(((process != NULL)
292 && (process->simdata)), "Invalid parameters");
294 return (((simdata_process_t) process->simdata)->m_host);
297 /** \ingroup m_process_management
299 * \brief Return a #m_process_t given its PID.
301 * This functions search in the list of all the created m_process_t for a m_process_t
302 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
303 Note that the PID are uniq in the whole simulation, not only on a given host.
305 m_process_t MSG_process_from_PID(int PID)
307 xbt_fifo_item_t i = NULL;
308 m_process_t process = NULL;
310 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
311 if (MSG_process_get_PID(process) == PID)
317 /** \ingroup m_process_management
318 * \brief Returns the process ID of \a process.
320 * This functions checks whether \a process is a valid pointer or not
321 and return its PID (or 0 in case of problem).
323 int MSG_process_get_PID(m_process_t process)
325 /* Do not raise an exception here: this function is used in the logs,
326 and it will be called back by the exception handling stuff */
327 if (process == NULL || process->simdata == NULL)
330 return (((simdata_process_t) process->simdata)->PID);
333 /** \ingroup m_process_management
334 * \brief Returns the process ID of the parent of \a process.
336 * This functions checks whether \a process is a valid pointer or not
337 and return its PID. Returns -1 if the agent has not been created by
340 int MSG_process_get_PPID(m_process_t process)
342 xbt_assert0(((process != NULL)
343 && (process->simdata)), "Invalid parameters");
345 return (((simdata_process_t) process->simdata)->PPID);
348 /** \ingroup m_process_management
349 * \brief Return the name of an agent.
351 * This functions checks whether \a process is a valid pointer or not
354 const char *MSG_process_get_name(m_process_t process)
356 xbt_assert0(((process != NULL)
357 && (process->simdata)), "Invalid parameters");
359 return (process->name);
362 /** \ingroup m_process_management
363 * \brief Returns the value of a certain process property
365 * \param process a process
366 * \param name a property name
367 * \return value of a property
369 const char* MSG_process_get_property_value(m_process_t process, char* name)
371 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
374 /** \ingroup m_process_management
375 * \brief Return the list of properties
377 * This functions returns all the parameters associated with a process
379 xbt_dict_t MSG_process_get_properties(m_process_t process)
381 xbt_assert0((process != NULL), "Invalid parameters");
383 return (SIMIX_process_get_properties(((simdata_process_t)process->simdata)->s_process));
387 /** \ingroup m_process_management
388 * \brief Return the PID of the current agent.
390 * This functions returns the PID of the currently running #m_process_t.
392 int MSG_process_self_PID(void)
394 return (MSG_process_get_PID(MSG_process_self()));
397 /** \ingroup m_process_management
398 * \brief Return the PPID of the current agent.
400 * This functions returns the PID of the parent of the currently
401 * running #m_process_t.
403 int MSG_process_self_PPID(void)
405 return (MSG_process_get_PPID(MSG_process_self()));
408 /** \ingroup m_process_management
409 * \brief Return the current agent.
411 * This functions returns the currently running #m_process_t.
413 m_process_t MSG_process_self(void)
415 smx_process_t proc = SIMIX_process_self();
417 return (m_process_t) proc->data;
424 /** \ingroup m_process_management
425 * \brief Suspend the process.
427 * This functions suspend the process by suspending the task on which
428 * it was waiting for the completion.
430 MSG_error_t MSG_process_suspend(m_process_t process)
432 xbt_assert0(((process != NULL)
433 && (process->simdata)), "Invalid parameters");
436 SIMIX_process_suspend(process->simdata->s_process);
440 /** \ingroup m_process_management
441 * \brief Resume a suspended process.
443 * This functions resume a suspended process by resuming the task on
444 * which it was waiting for the completion.
446 MSG_error_t MSG_process_resume(m_process_t process)
449 xbt_assert0(((process != NULL)
450 && (process->simdata)), "Invalid parameters");
453 SIMIX_process_resume(process->simdata->s_process);
457 /** \ingroup m_process_management
458 * \brief Returns true if the process is suspended .
460 * This checks whether a process is suspended or not by inspecting the
461 * task on which it was waiting for the completion.
463 int MSG_process_is_suspended(m_process_t process)
465 xbt_assert0(((process != NULL)
466 && (process->simdata)), "Invalid parameters");
467 return SIMIX_process_is_suspended(process->simdata->s_process);