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 enforced 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, xbt_dict_t properties)
67 m_host_t host = MSG_get_host_by_name(hostname);
68 return (void *) MSG_process_create_with_environment(name, code, data, host,
69 argc, argv,properties);
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 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
103 m_process_t process = xbt_new0(s_m_process_t, 1);
104 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
107 simdata->PID = msg_global->PID++;
108 simdata->waiting_task = NULL;
109 simdata->m_host = host;
110 simdata->argc = argc;
111 simdata->argv = argv;
112 simdata->s_process = SIMIX_process_create(name, code,
113 (void *) process, host->name,
116 if (SIMIX_process_self()) {
117 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
121 simdata->last_errno = MSG_OK;
124 /* Process structure */
125 process->name = xbt_strdup(name);
126 process->simdata = simdata;
127 process->data = data ;
129 xbt_fifo_unshift(msg_global->process_list, process);
134 /** \ingroup m_process_management
135 * \brief Creates and runs a new #m_process_t.
137 * A constructor for #m_process_t taking four arguments and returning the
138 * corresponding object. The structure (and the corresponding thread) is
139 * created, and put in the list of ready process.
140 * \param name a name for the object. It is for user-level information
142 * \param code is a function describing the behavior of the agent. It
143 should then only use functions described in \ref
144 m_process_management (to create a new #m_process_t for example),
145 in \ref m_host_management (only the read-only functions i.e. whose
146 name contains the word get), in \ref m_task_management (to create
147 or destroy some #m_task_t for example) and in \ref
148 msg_gos_functions (to handle file transfers and task processing).
149 * \param data a pointer to any data one may want to attach to the new
150 object. It is for user-level information and can be NULL. It can
151 be retrieved with the function \ref MSG_process_get_data.
152 * \param host the location where the new agent is executed.
153 * \param argc first argument passed to \a code
154 * \param argv second argument passed to \a code
155 * \param properties list a properties defined for this process
157 * \return The new corresponding object.
159 m_process_t MSG_process_create_with_environment(const char *name,
160 xbt_main_func_t code,
161 void *data, m_host_t host,
162 int argc, char **argv, xbt_dict_t properties)
164 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
165 m_process_t process = xbt_new0(s_m_process_t, 1);
166 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
169 simdata->PID = msg_global->PID++;
170 simdata->waiting_task = NULL;
171 simdata->m_host = host;
172 simdata->argc = argc;
173 simdata->argv = argv;
174 simdata->s_process = SIMIX_process_create(name, code,
175 (void *) process, host->name,
176 argc, argv, properties);
178 if (SIMIX_process_self()) {
179 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
183 simdata->last_errno = MSG_OK;
186 /* Process structure */
187 process->name = xbt_strdup(name);
188 process->simdata = simdata;
189 process->data = data ;
191 xbt_fifo_unshift(msg_global->process_list, process);
197 void _MSG_process_kill_from_SIMIX(void *p)
199 MSG_process_kill((m_process_t) p);
202 /** \ingroup m_process_management
203 * \param process poor victim
205 * This function simply kills a \a process... scarry isn't it ? :)
207 void MSG_process_kill(m_process_t process)
209 simdata_process_t p_simdata = process->simdata;
211 DEBUG3("Killing %s(%d) on %s",
212 process->name, p_simdata->PID, p_simdata->m_host->name);
214 if (p_simdata->waiting_task) {
215 DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
216 if (p_simdata->waiting_task->simdata->compute) {
217 SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
218 } else if (p_simdata->waiting_task->simdata->comm) {
219 SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
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 functions 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_process_t process, m_host_t host)
238 ("MSG_process_change_host - not implemented yet - maybe useless function");
242 /** \ingroup m_process_management
243 * \brief Return the user data of a #m_process_t.
245 * This functions checks whether \a process is a valid pointer or not
246 and return the user data associated to \a process if it is possible.
248 void *MSG_process_get_data(m_process_t process)
250 xbt_assert0((process != NULL), "Invalid parameters");
252 return (process->data);
255 /** \ingroup m_process_management
256 * \brief Set the user data of a #m_process_t.
258 * This functions checks whether \a process is a valid pointer or not
259 and set the user data associated to \a process if it is possible.
261 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
263 xbt_assert0((process != NULL), "Invalid parameters");
264 xbt_assert0((process->data == NULL), "Data already set");
266 process->data = data;
271 /** \ingroup m_process_management
272 * \brief Return the location on which an agent is running.
274 * This functions checks whether \a process is a valid pointer or not
275 and return the m_host_t corresponding to the location on which \a
278 m_host_t MSG_process_get_host(m_process_t process)
280 xbt_assert0(((process != NULL)
281 && (process->simdata)), "Invalid parameters");
283 return (((simdata_process_t) process->simdata)->m_host);
286 /** \ingroup m_process_management
288 * \brief Return a #m_process_t given its PID.
290 * This functions search in the list of all the created m_process_t for a m_process_t
291 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
292 Note that the PID are uniq in the whole simulation, not only on a given host.
294 m_process_t MSG_process_from_PID(int PID)
296 xbt_fifo_item_t i = NULL;
297 m_process_t process = NULL;
299 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
300 if (MSG_process_get_PID(process) == PID)
306 /** \ingroup m_process_management
307 * \brief Returns the process ID of \a process.
309 * This functions checks whether \a process is a valid pointer or not
310 and return its PID (or 0 in case of problem).
312 int MSG_process_get_PID(m_process_t process)
314 /* Do not raise an exception here: this function is used in the logs,
315 and it will be called back by the exception handling stuff */
316 if (process == NULL || process->simdata == NULL)
319 return (((simdata_process_t) process->simdata)->PID);
322 /** \ingroup m_process_management
323 * \brief Returns the process ID of the parent of \a process.
325 * This functions checks whether \a process is a valid pointer or not
326 and return its PID. Returns -1 if the agent has not been created by
329 int MSG_process_get_PPID(m_process_t process)
331 xbt_assert0(((process != NULL)
332 && (process->simdata)), "Invalid parameters");
334 return (((simdata_process_t) process->simdata)->PPID);
337 /** \ingroup m_process_management
338 * \brief Return the name of an agent.
340 * This functions checks whether \a process is a valid pointer or not
343 const char *MSG_process_get_name(m_process_t process)
345 xbt_assert0(((process != NULL)
346 && (process->simdata)), "Invalid parameters");
348 return (process->name);
351 /** \ingroup m_process_management
352 * \brief Returns the value of a certain process property
354 * \param process a process
355 * \param name a property name
356 * \return value of a property
358 const char* MSG_process_get_property_value(m_process_t process, char* name)
360 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
363 /** \ingroup m_process_management
364 * \brief Return the list of properties
366 * This functions returns all the parameters associated with a process
368 xbt_dict_t MSG_process_get_properties(m_process_t process)
370 xbt_assert0((process != NULL), "Invalid parameters");
372 return (SIMIX_process_get_properties(((simdata_process_t)process->simdata)->s_process));
376 /** \ingroup m_process_management
377 * \brief Return the PID of the current agent.
379 * This functions returns the PID of the currently running #m_process_t.
381 int MSG_process_self_PID(void)
383 return (MSG_process_get_PID(MSG_process_self()));
386 /** \ingroup m_process_management
387 * \brief Return the PPID of the current agent.
389 * This functions returns the PID of the parent of the currently
390 * running #m_process_t.
392 int MSG_process_self_PPID(void)
394 return (MSG_process_get_PPID(MSG_process_self()));
397 /** \ingroup m_process_management
398 * \brief Return the current agent.
400 * This functions returns the currently running #m_process_t.
402 m_process_t MSG_process_self(void)
404 smx_process_t proc = SIMIX_process_self();
406 return (m_process_t) proc->data;
413 /** \ingroup m_process_management
414 * \brief Suspend the process.
416 * This functions suspend the process by suspending the task on which
417 * it was waiting for the completion.
419 MSG_error_t MSG_process_suspend(m_process_t process)
421 xbt_assert0(((process != NULL)
422 && (process->simdata)), "Invalid parameters");
425 SIMIX_process_suspend(process->simdata->s_process);
429 /** \ingroup m_process_management
430 * \brief Resume a suspended process.
432 * This functions resume a suspended process by resuming the task on
433 * which it was waiting for the completion.
435 MSG_error_t MSG_process_resume(m_process_t process)
438 xbt_assert0(((process != NULL)
439 && (process->simdata)), "Invalid parameters");
442 SIMIX_process_resume(process->simdata->s_process);
446 /** \ingroup m_process_management
447 * \brief Returns true if the process is suspended .
449 * This checks whether a process is suspended or not by inspecting the
450 * task on which it was waiting for the completion.
452 int MSG_process_is_suspended(m_process_t process)
454 xbt_assert0(((process != NULL)
455 && (process->simdata)), "Invalid parameters");
456 return SIMIX_process_is_suspended(process->simdata->s_process);