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 printf("remove MSG process %s\n", proc->name);
38 xbt_fifo_remove(msg_global->process_list, proc);
39 SIMIX_process_cleanup(arg);
49 /** \ingroup m_process_management
50 * \brief Creates and runs a new #m_process_t.
52 * A constructor for #m_process_t taking four arguments and returning the
53 * corresponding object. The structure (and the corresponding thread) is
54 * created, and put in the list of ready process.
55 * \param name a name for the object. It is for user-level information
57 * \param code is a function describing the behavior of the agent. It
58 should then only use functions described in \ref
59 m_process_management (to create a new #m_process_t for example),
60 in \ref m_host_management (only the read-only functions i.e. whose
61 name contains the word get), in \ref m_task_management (to create
62 or destroy some #m_task_t for example) and in \ref
63 msg_gos_functions (to handle file transfers and task processing).
64 * \param data a pointer to any data one may want to attach to the new
65 object. It is for user-level information and can be NULL. It can
66 be retrieved with the function \ref MSG_process_get_data.
67 * \param host the location where the new agent is executed.
68 * \param argc first argument passed to \a code
69 * \param argv second argument passed to \a code
71 * \return The new corresponding object.
76 m_process_t __MSG_process_create_with_arguments(const char *name,
77 m_process_code_t code, void *data,
78 char * hostname, int argc, char **argv)
80 m_host_t host = MSG_get_host_by_name(hostname);
81 return MSG_process_create_with_arguments(name,code,data,host,argc,argv);
84 m_process_t MSG_process_create_with_arguments(const char *name,
85 m_process_code_t code, void *data,
86 m_host_t host, int argc, char **argv)
88 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
89 m_process_t process = xbt_new0(s_m_process_t,1);
90 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
93 simdata->PID = msg_global->PID++;
97 simdata->smx_process = SIMIX_process_create_with_arguments(name, (smx_process_code_t)code,
98 (void*)process, host->name, argc, argv, MSG_process_cleanup );
100 if (SIMIX_process_self()) {
101 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
103 else simdata->PPID = -1;
104 simdata->last_errno=MSG_OK;
107 /* Process structure */
108 process->name = xbt_strdup(name);
109 process->simdata = simdata;
110 process->data = data;
112 xbt_fifo_unshift(msg_global->process_list, process);
117 /** \ingroup m_process_management
118 * \param process poor victim
120 * This function simply kills a \a process... scarry isn't it ? :)
122 void MSG_process_kill(m_process_t process)
127 simdata_process_t p_simdata = process->simdata;
128 simdata_host_t h_simdata= p_simdata->host->simdata;
130 m_process_t proc = NULL;
132 DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID, p_simdata->host->name);
134 for (i=0; i<msg_global->max_channel; i++) {
135 if (h_simdata->sleeping[i] == process) {
136 h_simdata->sleeping[i] = NULL;
141 if(p_simdata->waiting_task) {
142 xbt_dynar_foreach(p_simdata->waiting_task->simdata->sleeping,_cursor,proc) {
144 xbt_dynar_remove_at(p_simdata->waiting_task->simdata->sleeping,_cursor,&proc);
146 if(p_simdata->waiting_task->simdata->compute)
147 surf_workstation_resource->common_public->
148 action_free(p_simdata->waiting_task->simdata->compute);
149 else if (p_simdata->waiting_task->simdata->comm) {
150 surf_workstation_resource->common_public->
151 action_change_state(p_simdata->waiting_task->simdata->comm,SURF_ACTION_FAILED);
152 surf_workstation_resource->common_public->
153 action_free(p_simdata->waiting_task->simdata->comm);
155 xbt_die("UNKNOWN STATUS. Please report this bug.");
159 if ((i==msg_global->max_channel) && (process!=MSG_process_self()) &&
160 (!p_simdata->waiting_task)) {
161 xbt_die("UNKNOWN STATUS. Please report this bug.");
164 xbt_fifo_remove(msg_global->process_list,process);
165 SIMIX_process_kill(process->simdata->smx_process);
170 /** \ingroup m_process_management
171 * \brief Migrates an agent to another location.
173 * This functions checks whether \a process and \a host are valid pointers
174 and change the value of the #m_host_t on which \a process is running.
176 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
178 xbt_die("MSG_process_change_host - not implemented yet - maybe useless function");
182 /** \ingroup m_process_management
183 * \brief Return the user data of a #m_process_t.
185 * This functions checks whether \a process is a valid pointer or not
186 and return the user data associated to \a process if it is possible.
188 void *MSG_process_get_data(m_process_t process)
190 xbt_assert0((process != NULL), "Invalid parameters");
192 return (process->data);
195 /** \ingroup m_process_management
196 * \brief Set the user data of a #m_process_t.
198 * This functions checks whether \a process is a valid pointer or not
199 and set the user data associated to \a process if it is possible.
201 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
203 xbt_assert0((process != NULL), "Invalid parameters");
204 xbt_assert0((process->data == NULL), "Data already set");
206 process->data = data;
211 /** \ingroup m_process_management
212 * \brief Return the location on which an agent is running.
214 * This functions checks whether \a process is a valid pointer or not
215 and return the m_host_t corresponding to the location on which \a
218 m_host_t MSG_process_get_host(m_process_t process)
220 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
222 return (((simdata_process_t) process->simdata)->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) return process;
244 /** \ingroup m_process_management
245 * \brief Returns the process ID of \a process.
247 * This functions checks whether \a process is a valid pointer or not
250 int MSG_process_get_PID(m_process_t process)
252 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
254 return (((simdata_process_t) process->simdata)->PID);
257 /** \ingroup m_process_management
258 * \brief Returns the process ID of the parent of \a process.
260 * This functions checks whether \a process is a valid pointer or not
261 and return its PID. Returns -1 if the agent has not been created by
264 int MSG_process_get_PPID(m_process_t process)
266 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
268 return (((simdata_process_t) process->simdata)->PPID);
271 /** \ingroup m_process_management
272 * \brief Return the name of an agent.
274 * This functions checks whether \a process is a valid pointer or not
277 const char *MSG_process_get_name(m_process_t process)
279 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
281 return (process->name);
284 /** \ingroup m_process_management
285 * \brief Return the PID of the current agent.
287 * This functions returns the PID of the currently running #m_process_t.
289 int MSG_process_self_PID(void)
291 return (MSG_process_get_PID(MSG_process_self()));
294 /** \ingroup m_process_management
295 * \brief Return the PPID of the current agent.
297 * This functions returns the PID of the parent of the currently
298 * running #m_process_t.
300 int MSG_process_self_PPID(void)
302 return (MSG_process_get_PPID(MSG_process_self()));
305 /** \ingroup m_process_management
306 * \brief Return the current agent.
308 * This functions returns the currently running #m_process_t.
310 m_process_t MSG_process_self(void)
312 smx_process_t proc = SIMIX_process_self();
314 return (m_process_t)proc->data;
322 /** \ingroup m_process_management
323 * \brief Suspend the process.
325 * This functions suspend the process by suspending the task on which
326 * it was waiting for the completion.
328 MSG_error_t MSG_process_suspend(m_process_t process)
330 xbt_die("not implemented yet");
334 /** \ingroup m_process_management
335 * \brief Resume a suspended process.
337 * This functions resume a suspended process by resuming the task on
338 * which it was waiting for the completion.
340 MSG_error_t MSG_process_resume(m_process_t process)
342 xbt_die("not implemented yet");
346 /** \ingroup m_process_management
347 * \brief Returns true if the process is suspended .
349 * This checks whether a process is suspended or not by inspecting the
350 * task on which it was waiting for the completion.
352 int MSG_process_is_suspended(m_process_t process)
354 xbt_die("not implemented yet");
358 int __MSG_process_block(double max_duration, const char *info)
363 MSG_error_t __MSG_process_unblock(m_process_t process)
368 int __MSG_process_isBlocked(m_process_t process)