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,
73 /** \ingroup m_process_management
74 * \brief Creates and runs a new #m_process_t.
76 * A constructor for #m_process_t taking four arguments and returning the
77 * corresponding object. The structure (and the corresponding thread) is
78 * created, and put in the list of ready process.
79 * \param name a name for the object. It is for user-level information
81 * \param code is a function describing the behavior of the agent. It
82 should then only use functions described in \ref
83 m_process_management (to create a new #m_process_t for example),
84 in \ref m_host_management (only the read-only functions i.e. whose
85 name contains the word get), in \ref m_task_management (to create
86 or destroy some #m_task_t for example) and in \ref
87 msg_gos_functions (to handle file transfers and task processing).
88 * \param data a pointer to any data one may want to attach to the new
89 object. It is for user-level information and can be NULL. It can
90 be retrieved with the function \ref MSG_process_get_data.
91 * \param host the location where the new agent is executed.
92 * \param argc first argument passed to \a code
93 * \param argv second argument passed to \a code
95 * \return The new corresponding object.
98 m_process_t MSG_process_create_with_arguments(const char *name,
100 void *data, m_host_t host,
101 int argc, char **argv)
103 simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
104 m_process_t process = xbt_new0(s_m_process_t, 1);
105 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
108 simdata->PID = msg_global->PID++;
109 simdata->waiting_task = NULL;
110 simdata->m_host = host;
111 simdata->argc = argc;
112 simdata->argv = argv;
113 simdata->s_process = SIMIX_process_create(name, code,
114 (void *) process, host->name,
117 if (SIMIX_process_self()) {
118 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
122 simdata->last_errno = MSG_OK;
125 /* Process structure */
126 process->name = xbt_strdup(name);
127 process->simdata = simdata;
128 process->data = data;
130 xbt_fifo_unshift(msg_global->process_list, process);
136 void _MSG_process_kill_from_SIMIX(void *p)
138 MSG_process_kill((m_process_t) p);
141 /** \ingroup m_process_management
142 * \param process poor victim
144 * This function simply kills a \a process... scarry isn't it ? :)
146 void MSG_process_kill(m_process_t process)
148 simdata_process_t p_simdata = process->simdata;
150 DEBUG3("Killing %s(%d) on %s",
151 process->name, p_simdata->PID, p_simdata->m_host->name);
153 if (p_simdata->waiting_task) {
154 DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
155 if (p_simdata->waiting_task->simdata->compute) {
156 SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
157 } else if (p_simdata->waiting_task->simdata->comm) {
158 SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
162 xbt_fifo_remove(msg_global->process_list, process);
163 SIMIX_process_kill(process->simdata->s_process);
168 /** \ingroup m_process_management
169 * \brief Migrates an agent to another location.
171 * This functions checks whether \a process and \a host are valid pointers
172 and change the value of the #m_host_t on which \a process is running.
174 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
177 ("MSG_process_change_host - not implemented yet - maybe useless function");
181 /** \ingroup m_process_management
182 * \brief Return the user data of a #m_process_t.
184 * This functions checks whether \a process is a valid pointer or not
185 and return the user data associated to \a process if it is possible.
187 void *MSG_process_get_data(m_process_t process)
189 xbt_assert0((process != NULL), "Invalid parameters");
191 return (process->data);
194 /** \ingroup m_process_management
195 * \brief Set the user data of a #m_process_t.
197 * This functions checks whether \a process is a valid pointer or not
198 and set the user data associated to \a process if it is possible.
200 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
202 xbt_assert0((process != NULL), "Invalid parameters");
203 xbt_assert0((process->data == NULL), "Data already set");
205 process->data = data;
210 /** \ingroup m_process_management
211 * \brief Return the location on which an agent is running.
213 * This functions checks whether \a process is a valid pointer or not
214 and return the m_host_t corresponding to the location on which \a
217 m_host_t MSG_process_get_host(m_process_t process)
219 xbt_assert0(((process != NULL)
220 && (process->simdata)), "Invalid parameters");
222 return (((simdata_process_t) process->simdata)->m_host);
225 /** \ingroup m_process_management
227 * \brief Return a #m_process_t given its PID.
229 * This functions search in the list of all the created m_process_t for a m_process_t
230 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
231 Note that the PID are uniq in the whole simulation, not only on a given host.
233 m_process_t MSG_process_from_PID(int PID)
235 xbt_fifo_item_t i = NULL;
236 m_process_t process = NULL;
238 xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
239 if (MSG_process_get_PID(process) == PID)
245 /** \ingroup m_process_management
246 * \brief Returns the process ID of \a process.
248 * This functions checks whether \a process is a valid pointer or not
249 and return its PID (or 0 in case of problem).
251 int MSG_process_get_PID(m_process_t process)
253 /* Do not raise an exception here: this function is used in the logs,
254 and it will be called back by the exception handling stuff */
255 if (process == NULL || process->simdata == NULL)
258 return (((simdata_process_t) process->simdata)->PID);
261 /** \ingroup m_process_management
262 * \brief Returns the process ID of the parent of \a process.
264 * This functions checks whether \a process is a valid pointer or not
265 and return its PID. Returns -1 if the agent has not been created by
268 int MSG_process_get_PPID(m_process_t process)
270 xbt_assert0(((process != NULL)
271 && (process->simdata)), "Invalid parameters");
273 return (((simdata_process_t) process->simdata)->PPID);
276 /** \ingroup m_process_management
277 * \brief Return the name of an agent.
279 * This functions checks whether \a process is a valid pointer or not
282 const char *MSG_process_get_name(m_process_t process)
284 xbt_assert0(((process != NULL)
285 && (process->simdata)), "Invalid parameters");
287 return (process->name);
290 /** \ingroup m_process_management
291 * \brief Return the PID of the current agent.
293 * This functions returns the PID of the currently running #m_process_t.
295 int MSG_process_self_PID(void)
297 return (MSG_process_get_PID(MSG_process_self()));
300 /** \ingroup m_process_management
301 * \brief Return the PPID of the current agent.
303 * This functions returns the PID of the parent of the currently
304 * running #m_process_t.
306 int MSG_process_self_PPID(void)
308 return (MSG_process_get_PPID(MSG_process_self()));
311 /** \ingroup m_process_management
312 * \brief Return the current agent.
314 * This functions returns the currently running #m_process_t.
316 m_process_t MSG_process_self(void)
318 smx_process_t proc = SIMIX_process_self();
320 return (m_process_t) proc->data;
327 /** \ingroup m_process_management
328 * \brief Suspend the process.
330 * This functions suspend the process by suspending the task on which
331 * it was waiting for the completion.
333 MSG_error_t MSG_process_suspend(m_process_t process)
335 xbt_assert0(((process != NULL)
336 && (process->simdata)), "Invalid parameters");
339 SIMIX_process_suspend(process->simdata->s_process);
343 /** \ingroup m_process_management
344 * \brief Resume a suspended process.
346 * This functions resume a suspended process by resuming the task on
347 * which it was waiting for the completion.
349 MSG_error_t MSG_process_resume(m_process_t process)
352 xbt_assert0(((process != NULL)
353 && (process->simdata)), "Invalid parameters");
356 SIMIX_process_resume(process->simdata->s_process);
360 /** \ingroup m_process_management
361 * \brief Returns true if the process is suspended .
363 * This checks whether a process is suspended or not by inspecting the
364 * task on which it was waiting for the completion.
366 int MSG_process_is_suspended(m_process_t process)
368 xbt_assert0(((process != NULL)
369 && (process->simdata)), "Invalid parameters");
370 return SIMIX_process_is_suspended(process->simdata->s_process);