2 #include "msg_simix_private.h"
3 #include "xbt/sysdep.h"
6 /** \defgroup m_process_management Management Functions of Agents
7 * \brief This section describes the agent structure of MSG
8 * (#m_process_t) and the functions for managing it.
9 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
11 * We need to simulate many independent scheduling decisions, so
12 * the concept of <em>process</em> is at the heart of the
13 * simulator. A process may be defined as a <em>code</em>, with
14 * some <em>private data</em>, executing in a <em>location</em>.
18 /******************************** Process ************************************/
19 /** \ingroup m_process_management
20 * \brief Creates and runs a new #m_process_t.
22 * Does exactly the same as #MSG_process_create_with_arguments but without
23 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
24 * \sa MSG_process_create_with_arguments
26 m_process_t MSG_process_create(const char *name,
27 m_process_code_t code, void *data,
30 return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
33 static 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 /** \ingroup m_process_management
49 * \brief Creates and runs a new #m_process_t.
51 * A constructor for #m_process_t taking four arguments and returning the
52 * corresponding object. The structure (and the corresponding thread) is
53 * created, and put in the list of ready process.
54 * \param name a name for the object. It is for user-level information
56 * \param code is a function describing the behavior of the agent. It
57 should then only use functions described in \ref
58 m_process_management (to create a new #m_process_t for example),
59 in \ref m_host_management (only the read-only functions i.e. whose
60 name contains the word get), in \ref m_task_management (to create
61 or destroy some #m_task_t for example) and in \ref
62 msg_gos_functions (to handle file transfers and task processing).
63 * \param data a pointer to any data one may want to attach to the new
64 object. It is for user-level information and can be NULL. It can
65 be retrieved with the function \ref MSG_process_get_data.
66 * \param host the location where the new agent is executed.
67 * \param argc first argument passed to \a code
68 * \param argv second argument passed to \a code
70 * \return The new corresponding object.
75 m_process_t __MSG_process_create_with_arguments(const char *name,
76 m_process_code_t code, void *data,
77 char * hostname, int argc, char **argv)
79 m_host_t host = MSG_get_host_by_name(hostname);
80 return MSG_process_create_with_arguments(name,code,data,host,argc,argv);
83 m_process_t MSG_process_create_with_arguments(const char *name,
84 m_process_code_t code, void *data,
85 m_host_t host, int argc, char **argv)
87 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
88 m_process_t process = xbt_new0(s_m_process_t,1);
89 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
92 simdata->PID = msg_global->PID++;
96 simdata->smx_process = SIMIX_process_create_with_arguments(name, (smx_process_code_t)code, (void*)process, host->name, argc, argv, MSG_process_cleanup );
98 if (SIMIX_process_self()) {
99 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
101 else simdata->PPID = -1;
102 simdata->last_errno=MSG_OK;
105 /* Process structure */
106 process->name = xbt_strdup(name);
107 process->simdata = simdata;
108 process->data = data;
110 xbt_fifo_unshift(msg_global->process_list, process);
115 /** \ingroup m_process_management
116 * \param process poor victim
118 * This function simply kills a \a process... scarry isn't it ? :)
120 void MSG_process_kill(m_process_t process)
125 simdata_process_t p_simdata = process->simdata;
126 simdata_host_t h_simdata= p_simdata->host->simdata;
128 m_process_t proc = NULL;
130 DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID, p_simdata->host->name);
132 for (i=0; i<msg_global->max_channel; i++) {
133 if (h_simdata->sleeping[i] == process) {
134 h_simdata->sleeping[i] = NULL;
139 if(p_simdata->waiting_task) {
140 xbt_dynar_foreach(p_simdata->waiting_task->simdata->sleeping,_cursor,proc) {
142 xbt_dynar_remove_at(p_simdata->waiting_task->simdata->sleeping,_cursor,&proc);
144 if(p_simdata->waiting_task->simdata->compute)
145 surf_workstation_resource->common_public->
146 action_free(p_simdata->waiting_task->simdata->compute);
147 else if (p_simdata->waiting_task->simdata->comm) {
148 surf_workstation_resource->common_public->
149 action_change_state(p_simdata->waiting_task->simdata->comm,SURF_ACTION_FAILED);
150 surf_workstation_resource->common_public->
151 action_free(p_simdata->waiting_task->simdata->comm);
153 xbt_die("UNKNOWN STATUS. Please report this bug.");
157 if ((i==msg_global->max_channel) && (process!=MSG_process_self()) &&
158 (!p_simdata->waiting_task)) {
159 xbt_die("UNKNOWN STATUS. Please report this bug.");
162 xbt_fifo_remove(msg_global->process_list,process);
163 SIMIX_process_kill(process->simdata->smx_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)
176 xbt_die("MSG_process_change_host - not implemented yet - maybe useless function");
180 /** \ingroup m_process_management
181 * \brief Return the user data of a #m_process_t.
183 * This functions checks whether \a process is a valid pointer or not
184 and return the user data associated to \a process if it is possible.
186 void *MSG_process_get_data(m_process_t process)
188 xbt_assert0((process != NULL), "Invalid parameters");
190 return (process->data);
193 /** \ingroup m_process_management
194 * \brief Set the user data of a #m_process_t.
196 * This functions checks whether \a process is a valid pointer or not
197 and set the user data associated to \a process if it is possible.
199 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
201 xbt_assert0((process != NULL), "Invalid parameters");
202 xbt_assert0((process->data == NULL), "Data already set");
204 process->data = data;
209 /** \ingroup m_process_management
210 * \brief Return the location on which an agent is running.
212 * This functions checks whether \a process is a valid pointer or not
213 and return the m_host_t corresponding to the location on which \a
216 m_host_t MSG_process_get_host(m_process_t process)
218 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
220 return (((simdata_process_t) process->simdata)->host);
223 /** \ingroup m_process_management
225 * \brief Return a #m_process_t given its PID.
227 * This functions search in the list of all the created m_process_t for a m_process_t
228 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
229 Note that the PID are uniq in the whole simulation, not only on a given host.
231 m_process_t MSG_process_from_PID(int PID)
233 xbt_fifo_item_t i = NULL;
234 m_process_t process = NULL;
236 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
237 if(MSG_process_get_PID(process) == PID) return process;
242 /** \ingroup m_process_management
243 * \brief Returns the process ID of \a process.
245 * This functions checks whether \a process is a valid pointer or not
248 int MSG_process_get_PID(m_process_t process)
250 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
252 return (((simdata_process_t) process->simdata)->PID);
255 /** \ingroup m_process_management
256 * \brief Returns the process ID of the parent of \a process.
258 * This functions checks whether \a process is a valid pointer or not
259 and return its PID. Returns -1 if the agent has not been created by
262 int MSG_process_get_PPID(m_process_t process)
264 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
266 return (((simdata_process_t) process->simdata)->PPID);
269 /** \ingroup m_process_management
270 * \brief Return the name of an agent.
272 * This functions checks whether \a process is a valid pointer or not
275 const char *MSG_process_get_name(m_process_t process)
277 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
279 return (process->name);
282 /** \ingroup m_process_management
283 * \brief Return the PID of the current agent.
285 * This functions returns the PID of the currently running #m_process_t.
287 int MSG_process_self_PID(void)
289 return (MSG_process_get_PID(MSG_process_self()));
292 /** \ingroup m_process_management
293 * \brief Return the PPID of the current agent.
295 * This functions returns the PID of the parent of the currently
296 * running #m_process_t.
298 int MSG_process_self_PPID(void)
300 return (MSG_process_get_PPID(MSG_process_self()));
303 /** \ingroup m_process_management
304 * \brief Return the current agent.
306 * This functions returns the currently running #m_process_t.
308 m_process_t MSG_process_self(void)
310 smx_process_t proc = SIMIX_process_self();
312 return (m_process_t)proc->data;
320 /** \ingroup m_process_management
321 * \brief Suspend the process.
323 * This functions suspend the process by suspending the task on which
324 * it was waiting for the completion.
326 MSG_error_t MSG_process_suspend(m_process_t process)
328 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
331 SIMIX_process_suspend(process->simdata->smx_process);
335 /** \ingroup m_process_management
336 * \brief Resume a suspended process.
338 * This functions resume a suspended process by resuming the task on
339 * which it was waiting for the completion.
341 MSG_error_t MSG_process_resume(m_process_t process)
344 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
347 SIMIX_process_resume(process->simdata->smx_process);
351 /** \ingroup m_process_management
352 * \brief Returns true if the process is suspended .
354 * This checks whether a process is suspended or not by inspecting the
355 * task on which it was waiting for the completion.
357 int MSG_process_is_suspended(m_process_t process)
359 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
360 return SIMIX_process_is_suspended(process->simdata->smx_process);