Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functions added and changed. Keeps the cvs updated.
[simgrid.git] / src / msg_simix / msg_simix_process.c
1
2 #include "msg_simix_private.h"
3 #include "xbt/sysdep.h"
4 #include "xbt/log.h"
5
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg, "Logging specific to MSG (process)");
7
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
12  * 
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>.
17  *  \see m_process_t
18  */
19
20 /******************************** Process ************************************/
21 /** \ingroup m_process_management
22  * \brief Creates and runs a new #m_process_t.
23  *
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
27  */
28 m_process_t MSG_process_create(const char *name,
29                                m_process_code_t code, void *data,
30                                m_host_t host)
31 {
32   return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
33 }
34
35 static void MSG_process_cleanup(void *arg)
36 {
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);
41   free(proc->name);
42   proc->name = NULL;
43   free(proc->simdata);
44   proc->simdata = NULL;
45   free(proc);
46
47         return;
48 }
49
50 /** \ingroup m_process_management
51  * \brief Creates and runs a new #m_process_t.
52
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
57    and can be NULL.
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
71  * \see m_process_t
72  * \return The new corresponding object.
73  */
74
75
76
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)
80 {
81         m_host_t host = MSG_get_host_by_name(hostname);
82         return MSG_process_create_with_arguments(name,code,data,host,argc,argv);
83 }
84
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)
88 {
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");
92
93   /* Simulator Data */
94   simdata->PID = msg_global->PID++;
95         simdata->waiting_task = NULL;
96   simdata->host = host;
97   simdata->argc = argc;
98   simdata->argv = argv;
99         simdata->smx_process = SIMIX_process_create_with_arguments(name, (smx_process_code_t)code, (void*)process, host->name, argc, argv, MSG_process_cleanup );
100
101         if (SIMIX_process_self()) {
102                 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
103         }
104         else simdata->PPID = -1;
105   simdata->last_errno=MSG_OK;
106
107
108   /* Process structure */
109   process->name = xbt_strdup(name);
110   process->simdata = simdata;
111   process->data = data;
112
113         xbt_fifo_unshift(msg_global->process_list, process); 
114
115   return process;
116 }
117
118 /** \ingroup m_process_management
119  * \param process poor victim
120  *
121  * This function simply kills a \a process... scarry isn't it ? :)
122  */
123 void MSG_process_kill(m_process_t process)
124 {
125   simdata_process_t p_simdata = process->simdata;
126
127   DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID, p_simdata->host->name);
128
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);
133                 }
134     else if (p_simdata->waiting_task->simdata->comm) {
135                         SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
136     } 
137   }
138
139   xbt_fifo_remove(msg_global->process_list,process);
140         SIMIX_process_kill(process->simdata->smx_process);
141
142         return;
143 }
144
145 /** \ingroup m_process_management
146  * \brief Migrates an agent to another location.
147  *
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.
150  */
151 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
152 {
153         xbt_die("MSG_process_change_host - not implemented yet - maybe useless function");
154   return MSG_OK;
155 }
156
157 /** \ingroup m_process_management
158  * \brief Return the user data of a #m_process_t.
159  *
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.
162  */
163 void *MSG_process_get_data(m_process_t process)
164 {
165   xbt_assert0((process != NULL), "Invalid parameters");
166
167   return (process->data);
168 }
169
170 /** \ingroup m_process_management
171  * \brief Set the user data of a #m_process_t.
172  *
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.
175  */
176 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
177 {
178   xbt_assert0((process != NULL), "Invalid parameters");
179   xbt_assert0((process->data == NULL), "Data already set");
180   
181   process->data = data;
182    
183   return MSG_OK;
184 }
185
186 /** \ingroup m_process_management
187  * \brief Return the location on which an agent is running.
188  *
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 
191    process is running.
192  */
193 m_host_t MSG_process_get_host(m_process_t process)
194 {
195   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
196
197   return (((simdata_process_t) process->simdata)->host);
198 }
199
200 /** \ingroup m_process_management
201  *
202  * \brief Return a #m_process_t given its PID.
203  *
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.
207  */
208 m_process_t MSG_process_from_PID(int PID)
209 {
210   xbt_fifo_item_t i = NULL;
211   m_process_t process = NULL;
212
213   xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
214     if(MSG_process_get_PID(process) == PID) return process;
215   }
216   return NULL;
217 }
218
219 /** \ingroup m_process_management
220  * \brief Returns the process ID of \a process.
221  *
222  * This functions checks whether \a process is a valid pointer or not 
223    and return its PID.
224  */
225 int MSG_process_get_PID(m_process_t process)
226 {
227   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
228
229   return (((simdata_process_t) process->simdata)->PID);
230 }
231
232 /** \ingroup m_process_management
233  * \brief Returns the process ID of the parent of \a process.
234  *
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 
237    another agent.
238  */
239 int MSG_process_get_PPID(m_process_t process)
240 {
241   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
242
243   return (((simdata_process_t) process->simdata)->PPID);
244 }
245
246 /** \ingroup m_process_management
247  * \brief Return the name of an agent.
248  *
249  * This functions checks whether \a process is a valid pointer or not 
250    and return its name.
251  */
252 const char *MSG_process_get_name(m_process_t process)
253 {
254   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
255
256   return (process->name);
257 }
258
259 /** \ingroup m_process_management
260  * \brief Return the PID of the current agent.
261  *
262  * This functions returns the PID of the currently running #m_process_t.
263  */
264 int MSG_process_self_PID(void)
265 {
266   return (MSG_process_get_PID(MSG_process_self()));
267 }
268
269 /** \ingroup m_process_management
270  * \brief Return the PPID of the current agent.
271  *
272  * This functions returns the PID of the parent of the currently
273  * running #m_process_t.
274  */
275 int MSG_process_self_PPID(void)
276 {
277   return (MSG_process_get_PPID(MSG_process_self()));
278 }
279
280 /** \ingroup m_process_management
281  * \brief Return the current agent.
282  *
283  * This functions returns the currently running #m_process_t.
284  */
285 m_process_t MSG_process_self(void)
286 {
287         smx_process_t proc = SIMIX_process_self();
288         if (proc != NULL) {
289                 return (m_process_t)proc->data;
290         }
291         else { 
292                 return NULL;
293         }
294
295 }
296
297 /** \ingroup m_process_management
298  * \brief Suspend the process.
299  *
300  * This functions suspend the process by suspending the task on which
301  * it was waiting for the completion.
302  */
303 MSG_error_t MSG_process_suspend(m_process_t process)
304 {
305   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
306   CHECK_HOST();
307
308         SIMIX_process_suspend(process->simdata->smx_process);
309   MSG_RETURN(MSG_OK);
310 }
311
312 /** \ingroup m_process_management
313  * \brief Resume a suspended process.
314  *
315  * This functions resume a suspended process by resuming the task on
316  * which it was waiting for the completion.
317  */
318 MSG_error_t MSG_process_resume(m_process_t process)
319 {
320
321   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
322   CHECK_HOST();
323
324         SIMIX_process_resume(process->simdata->smx_process);
325   MSG_RETURN(MSG_OK);
326 }
327
328 /** \ingroup m_process_management
329  * \brief Returns true if the process is suspended .
330  *
331  * This checks whether a process is suspended or not by inspecting the
332  * task on which it was waiting for the completion.
333  */
334 int MSG_process_is_suspended(m_process_t process)
335 {
336   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
337         return SIMIX_process_is_suspended(process->simdata->smx_process);
338 }
339