2 #include "msg_simix_private.h"
3 #include "xbt/sysdep.h"
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg, "Logging specific to MSG (process)");
8 /** \defgroup m_process_management Management Functions of Agents
9 * \brief This section describes the agent structure of MSG
10 * (#m_process_t) and the functions for managing it.
11 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
13 * We need to simulate many independent scheduling decisions, so
14 * the concept of <em>process</em> is at the heart of the
15 * simulator. A process may be defined as a <em>code</em>, with
16 * some <em>private data</em>, executing in a <em>location</em>.
20 /******************************** Process ************************************/
21 /** \ingroup m_process_management
22 * \brief Creates and runs a new #m_process_t.
24 * Does exactly the same as #MSG_process_create_with_arguments but without
25 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
26 * \sa MSG_process_create_with_arguments
28 m_process_t MSG_process_create(const char *name,
29 m_process_code_t code, void *data,
32 return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
35 static void MSG_process_cleanup(void *arg)
37 /* arg is a pointer to a simix process, we can get the msg process with the field data */
38 m_process_t proc = ((smx_process_t)arg)->data;
39 xbt_fifo_remove(msg_global->process_list, proc);
40 SIMIX_process_cleanup(arg);
50 /** \ingroup m_process_management
51 * \brief Creates and runs a new #m_process_t.
53 * A constructor for #m_process_t taking four arguments and returning the
54 * corresponding object. The structure (and the corresponding thread) is
55 * created, and put in the list of ready process.
56 * \param name a name for the object. It is for user-level information
58 * \param code is a function describing the behavior of the agent. It
59 should then only use functions described in \ref
60 m_process_management (to create a new #m_process_t for example),
61 in \ref m_host_management (only the read-only functions i.e. whose
62 name contains the word get), in \ref m_task_management (to create
63 or destroy some #m_task_t for example) and in \ref
64 msg_gos_functions (to handle file transfers and task processing).
65 * \param data a pointer to any data one may want to attach to the new
66 object. It is for user-level information and can be NULL. It can
67 be retrieved with the function \ref MSG_process_get_data.
68 * \param host the location where the new agent is executed.
69 * \param argc first argument passed to \a code
70 * \param argv second argument passed to \a code
72 * \return The new corresponding object.
77 m_process_t __MSG_process_create_with_arguments(const char *name,
78 m_process_code_t code, void *data,
79 char * hostname, int argc, char **argv)
81 m_host_t host = MSG_get_host_by_name(hostname);
82 return MSG_process_create_with_arguments(name,code,data,host,argc,argv);
85 m_process_t MSG_process_create_with_arguments(const char *name,
86 m_process_code_t code, void *data,
87 m_host_t host, int argc, char **argv)
89 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
90 m_process_t process = xbt_new0(s_m_process_t,1);
91 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
94 simdata->PID = msg_global->PID++;
95 simdata->waiting_task = NULL;
99 simdata->smx_process = SIMIX_process_create(name, (smx_process_code_t)code, (void*)process, host->name, argc, argv, MSG_process_cleanup );
101 if (SIMIX_process_self()) {
102 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
104 else simdata->PPID = -1;
105 simdata->last_errno=MSG_OK;
108 /* Process structure */
109 process->name = xbt_strdup(name);
110 process->simdata = simdata;
111 process->data = data;
113 xbt_fifo_unshift(msg_global->process_list, process);
118 /** \ingroup m_process_management
119 * \param process poor victim
121 * This function simply kills a \a process... scarry isn't it ? :)
123 void MSG_process_kill(m_process_t process)
125 simdata_process_t p_simdata = process->simdata;
127 DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID, p_simdata->host->name);
129 if(p_simdata->waiting_task) {
130 DEBUG1("Canceling waiting task %s",p_simdata->waiting_task->name);
131 if(p_simdata->waiting_task->simdata->compute) {
132 SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
134 else if (p_simdata->waiting_task->simdata->comm) {
135 SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
139 xbt_fifo_remove(msg_global->process_list,process);
140 SIMIX_process_kill(process->simdata->smx_process);
145 /** \ingroup m_process_management
146 * \brief Migrates an agent to another location.
148 * This functions checks whether \a process and \a host are valid pointers
149 and change the value of the #m_host_t on which \a process is running.
151 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
153 xbt_die("MSG_process_change_host - not implemented yet - maybe useless function");
157 /** \ingroup m_process_management
158 * \brief Return the user data of a #m_process_t.
160 * This functions checks whether \a process is a valid pointer or not
161 and return the user data associated to \a process if it is possible.
163 void *MSG_process_get_data(m_process_t process)
165 xbt_assert0((process != NULL), "Invalid parameters");
167 return (process->data);
170 /** \ingroup m_process_management
171 * \brief Set the user data of a #m_process_t.
173 * This functions checks whether \a process is a valid pointer or not
174 and set the user data associated to \a process if it is possible.
176 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
178 xbt_assert0((process != NULL), "Invalid parameters");
179 xbt_assert0((process->data == NULL), "Data already set");
181 process->data = data;
186 /** \ingroup m_process_management
187 * \brief Return the location on which an agent is running.
189 * This functions checks whether \a process is a valid pointer or not
190 and return the m_host_t corresponding to the location on which \a
193 m_host_t MSG_process_get_host(m_process_t process)
195 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
197 return (((simdata_process_t) process->simdata)->host);
200 /** \ingroup m_process_management
202 * \brief Return a #m_process_t given its PID.
204 * This functions search in the list of all the created m_process_t for a m_process_t
205 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
206 Note that the PID are uniq in the whole simulation, not only on a given host.
208 m_process_t MSG_process_from_PID(int PID)
210 xbt_fifo_item_t i = NULL;
211 m_process_t process = NULL;
213 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
214 if(MSG_process_get_PID(process) == PID) return process;
219 /** \ingroup m_process_management
220 * \brief Returns the process ID of \a process.
222 * This functions checks whether \a process is a valid pointer or not
225 int MSG_process_get_PID(m_process_t process)
227 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
229 return (((simdata_process_t) process->simdata)->PID);
232 /** \ingroup m_process_management
233 * \brief Returns the process ID of the parent of \a process.
235 * This functions checks whether \a process is a valid pointer or not
236 and return its PID. Returns -1 if the agent has not been created by
239 int MSG_process_get_PPID(m_process_t process)
241 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
243 return (((simdata_process_t) process->simdata)->PPID);
246 /** \ingroup m_process_management
247 * \brief Return the name of an agent.
249 * This functions checks whether \a process is a valid pointer or not
252 const char *MSG_process_get_name(m_process_t process)
254 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
256 return (process->name);
259 /** \ingroup m_process_management
260 * \brief Return the PID of the current agent.
262 * This functions returns the PID of the currently running #m_process_t.
264 int MSG_process_self_PID(void)
266 return (MSG_process_get_PID(MSG_process_self()));
269 /** \ingroup m_process_management
270 * \brief Return the PPID of the current agent.
272 * This functions returns the PID of the parent of the currently
273 * running #m_process_t.
275 int MSG_process_self_PPID(void)
277 return (MSG_process_get_PPID(MSG_process_self()));
280 /** \ingroup m_process_management
281 * \brief Return the current agent.
283 * This functions returns the currently running #m_process_t.
285 m_process_t MSG_process_self(void)
287 smx_process_t proc = SIMIX_process_self();
289 return (m_process_t)proc->data;
297 /** \ingroup m_process_management
298 * \brief Suspend the process.
300 * This functions suspend the process by suspending the task on which
301 * it was waiting for the completion.
303 MSG_error_t MSG_process_suspend(m_process_t process)
305 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
308 SIMIX_process_suspend(process->simdata->smx_process);
312 /** \ingroup m_process_management
313 * \brief Resume a suspended process.
315 * This functions resume a suspended process by resuming the task on
316 * which it was waiting for the completion.
318 MSG_error_t MSG_process_resume(m_process_t process)
321 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
324 SIMIX_process_resume(process->simdata->smx_process);
328 /** \ingroup m_process_management
329 * \brief Returns true if the process is suspended .
331 * This checks whether a process is suspended or not by inspecting the
332 * task on which it was waiting for the completion.
334 int MSG_process_is_suspended(m_process_t process)
336 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
337 return SIMIX_process_is_suspended(process->simdata->smx_process);