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;
37 xbt_fifo_remove(msg_global->process_list, proc);
38 SIMIX_process_cleanup(arg);
48 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
49 void *_MSG_process_create_from_SIMIX(const char *name,
50 xbt_main_func_t code, void *data,
51 char *hostname, int argc, char **argv,
52 xbt_dict_t properties)
54 m_host_t host = MSG_get_host_by_name(hostname);
55 return (void *) MSG_process_create_with_environment(name, code, data, host,
56 argc, argv, properties);
59 /** \ingroup m_process_management
60 * \brief Creates and runs a new #m_process_t.
62 * Does exactly the same as #MSG_process_create_with_arguments but without
63 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
64 * \sa MSG_process_create_with_arguments
66 m_process_t MSG_process_create(const char *name,
67 xbt_main_func_t code, void *data,
70 return MSG_process_create_with_environment(name, code, data, host, -1,
74 /** \ingroup m_process_management
75 * \brief Creates and runs a new #m_process_t.
77 * A constructor for #m_process_t taking four arguments and returning the
78 * corresponding object. The structure (and the corresponding thread) is
79 * created, and put in the list of ready process.
80 * \param name a name for the object. It is for user-level information
82 * \param code is a function describing the behavior of the agent. It
83 should then only use functions described in \ref
84 m_process_management (to create a new #m_process_t for example),
85 in \ref m_host_management (only the read-only functions i.e. whose
86 name contains the word get), in \ref m_task_management (to create
87 or destroy some #m_task_t for example) and in \ref
88 msg_gos_functions (to handle file transfers and task processing).
89 * \param data a pointer to any data one may want to attach to the new
90 object. It is for user-level information and can be NULL. It can
91 be retrieved with the function \ref MSG_process_get_data.
92 * \param host the location where the new agent is executed.
93 * \param argc first argument passed to \a code
94 * \param argv second argument passed to \a code
96 * \return The new corresponding object.
99 m_process_t MSG_process_create_with_arguments(const char *name,
100 xbt_main_func_t code,
101 void *data, m_host_t host,
102 int argc, char **argv)
104 return MSG_process_create_with_environment(name, code, data, host,
108 /** \ingroup m_process_management
109 * \brief Creates and runs a new #m_process_t.
111 * A constructor for #m_process_t taking four arguments and returning the
112 * corresponding object. The structure (and the corresponding thread) is
113 * created, and put in the list of ready process.
114 * \param name a name for the object. It is for user-level information
116 * \param code is a function describing the behavior of the agent. It
117 should then only use functions described in \ref
118 m_process_management (to create a new #m_process_t for example),
119 in \ref m_host_management (only the read-only functions i.e. whose
120 name contains the word get), in \ref m_task_management (to create
121 or destroy some #m_task_t for example) and in \ref
122 msg_gos_functions (to handle file transfers and task processing).
123 * \param data a pointer to any data one may want to attach to the new
124 object. It is for user-level information and can be NULL. It can
125 be retrieved with the function \ref MSG_process_get_data.
126 * \param host the location where the new agent is executed.
127 * \param argc first argument passed to \a code
128 * \param argv second argument passed to \a code
129 * \param properties list a properties defined for this process
131 * \return The new corresponding object.
133 m_process_t MSG_process_create_with_environment(const char *name,
134 xbt_main_func_t code,
135 void *data, m_host_t host,
136 int argc, char **argv,
137 xbt_dict_t properties)
139 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
140 m_process_t process = xbt_new0(s_m_process_t, 1);
141 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
144 simdata->PID = msg_global->PID++;
145 simdata->waiting_task = NULL;
146 simdata->m_host = host;
147 simdata->argc = argc;
148 simdata->argv = argv;
149 simdata->s_process = SIMIX_process_create(name, code,
150 (void *) process, host->name,
151 argc, argv, properties);
153 if (SIMIX_process_self()) {
154 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
158 simdata->last_errno = MSG_OK;
161 /* Process structure */
162 process->name = xbt_strdup(name);
163 process->simdata = simdata;
164 process->data = data;
166 xbt_fifo_unshift(msg_global->process_list, process);
172 void _MSG_process_kill_from_SIMIX(void *p)
174 MSG_process_kill((m_process_t) p);
177 /** \ingroup m_process_management
178 * \param process poor victim
180 * This function simply kills a \a process... scarry isn't it ? :)
182 void MSG_process_kill(m_process_t process)
184 simdata_process_t p_simdata = process->simdata;
186 DEBUG3("Killing %s(%d) on %s",
187 process->name, p_simdata->PID, p_simdata->m_host->name);
189 if (p_simdata->waiting_task) {
190 DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
191 if (p_simdata->waiting_task->simdata->compute) {
192 SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
193 } else if (p_simdata->waiting_task->simdata->comm) {
194 SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
198 xbt_fifo_remove(msg_global->process_list, process);
199 SIMIX_process_kill(process->simdata->s_process);
204 /** \ingroup m_process_management
205 * \brief Migrates an agent to another location.
207 * This function checks whether \a process and \a host are valid pointers
208 and change the value of the #m_host_t on which \a process is running.
210 MSG_error_t MSG_process_change_host(m_host_t host)
212 m_process_t process = MSG_process_self();
213 m_host_t now = process->simdata->m_host;
214 process->simdata->m_host = host;
215 SIMIX_process_change_host(process->simdata->s_process, now->name,
220 /** \ingroup m_process_management
221 * \brief Return the user data of a #m_process_t.
223 * This function checks whether \a process is a valid pointer or not
224 and return the user data associated to \a process if it is possible.
226 void *MSG_process_get_data(m_process_t process)
228 xbt_assert0((process != NULL), "Invalid parameters");
230 return (process->data);
233 /** \ingroup m_process_management
234 * \brief Set the user data of a #m_process_t.
236 * This function checks whether \a process is a valid pointer or not
237 and set the user data associated to \a process if it is possible.
239 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
241 xbt_assert0((process != NULL), "Invalid parameters");
242 xbt_assert0((process->data == NULL), "Data already set");
244 process->data = data;
249 /** \ingroup m_process_management
250 * \brief Return the location on which an agent is running.
252 * This function checks whether \a process is a valid pointer or not
253 and return the m_host_t corresponding to the location on which \a
256 m_host_t MSG_process_get_host(m_process_t process)
258 xbt_assert0(((process != NULL)
259 && (process->simdata)), "Invalid parameters");
261 return (((simdata_process_t) process->simdata)->m_host);
264 /** \ingroup m_process_management
266 * \brief Return a #m_process_t given its PID.
268 * This function search in the list of all the created m_process_t for a m_process_t
269 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
270 Note that the PID are uniq in the whole simulation, not only on a given host.
272 m_process_t MSG_process_from_PID(int PID)
274 xbt_fifo_item_t i = NULL;
275 m_process_t process = NULL;
277 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
278 if (MSG_process_get_PID(process) == PID)
284 /** \ingroup m_process_management
285 * \brief Returns the process ID of \a process.
287 * This function checks whether \a process is a valid pointer or not
288 and return its PID (or 0 in case of problem).
290 int MSG_process_get_PID(m_process_t process)
292 /* Do not raise an exception here: this function is used in the logs,
293 and it will be called back by the exception handling stuff */
294 if (process == NULL || process->simdata == NULL)
297 return (((simdata_process_t) process->simdata)->PID);
300 /** \ingroup m_process_management
301 * \brief Returns the process ID of the parent of \a process.
303 * This function checks whether \a process is a valid pointer or not
304 and return its PID. Returns -1 if the agent has not been created by
307 int MSG_process_get_PPID(m_process_t process)
309 xbt_assert0(((process != NULL)
310 && (process->simdata)), "Invalid parameters");
312 return (((simdata_process_t) process->simdata)->PPID);
315 /** \ingroup m_process_management
316 * \brief Return the name of an agent.
318 * This function checks whether \a process is a valid pointer or not
321 const char *MSG_process_get_name(m_process_t process)
323 xbt_assert0(process, "Invalid parameter: process is NULL");
324 xbt_assert0(process->simdata,
325 "Invalid parameter: process->simdata is NULL");
327 return (process->name);
330 /** \ingroup m_process_management
331 * \brief Returns the value of a given process property
333 * \param process a process
334 * \param name a property name
335 * \return value of a property (or NULL if the property is not set)
337 const char *MSG_process_get_property_value(m_process_t process,
340 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
343 /** \ingroup m_process_management
344 * \brief Return the list of properties
346 * This function returns all the parameters associated with a process
348 xbt_dict_t MSG_process_get_properties(m_process_t process)
350 xbt_assert0((process != NULL), "Invalid parameters");
352 return (SIMIX_process_get_properties
353 (((simdata_process_t) process->simdata)->s_process));
357 /** \ingroup m_process_management
358 * \brief Return the PID of the current agent.
360 * This function returns the PID of the currently running #m_process_t.
362 int MSG_process_self_PID(void)
364 return (MSG_process_get_PID(MSG_process_self()));
367 /** \ingroup m_process_management
368 * \brief Return the PPID of the current agent.
370 * This function returns the PID of the parent of the currently
371 * running #m_process_t.
373 int MSG_process_self_PPID(void)
375 return (MSG_process_get_PPID(MSG_process_self()));
378 /** \ingroup m_process_management
379 * \brief Return the current agent.
381 * This function returns the currently running #m_process_t.
383 m_process_t MSG_process_self(void)
385 smx_process_t proc = SIMIX_process_self();
387 return (m_process_t) proc->data;
394 /** \ingroup m_process_management
395 * \brief Suspend the process.
397 * This function suspends the process by suspending the task on which
398 * it was waiting for the completion.
400 MSG_error_t MSG_process_suspend(m_process_t process)
402 xbt_assert0(((process != NULL)
403 && (process->simdata)), "Invalid parameters");
406 SIMIX_process_suspend(process->simdata->s_process);
410 /** \ingroup m_process_management
411 * \brief Resume a suspended process.
413 * This function resumes a suspended process by resuming the task on
414 * which it was waiting for the completion.
416 MSG_error_t MSG_process_resume(m_process_t process)
419 xbt_assert0(((process != NULL)
420 && (process->simdata)), "Invalid parameters");
423 SIMIX_process_resume(process->simdata->s_process);
427 /** \ingroup m_process_management
428 * \brief Returns true if the process is suspended .
430 * This checks whether a process is suspended or not by inspecting the
431 * task on which it was waiting for the completion.
433 int MSG_process_is_suspended(m_process_t process)
435 xbt_assert0(((process != NULL)
436 && (process->simdata)), "Invalid parameters");
437 return SIMIX_process_is_suspended(process->simdata->s_process);