Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Integrate Bruno's work on SIMIX onto main stream. Tests are broken, but it looks...
[simgrid.git] / src / msg / m_process.c
1 /*     $Id$      */
2   
3 /* Copyright (c) 2002-2007 Arnaud Legrand.                                  */
4 /* Copyright (c) 2007 Bruno Donassolo.                                      */
5 /* All rights reserved.                                                     */
6
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. */
9   
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg, "Logging specific to MSG (process)");
15
16 /** \defgroup m_process_management Management Functions of Agents
17  *  \brief This section describes the agent structure of MSG
18  *  (#m_process_t) and the functions for managing it.
19  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
20  * 
21  *  We need to simulate many independent scheduling decisions, so
22  *  the concept of <em>process</em> is at the heart of the
23  *  simulator. A process may be defined as a <em>code</em>, with
24  *  some <em>private data</em>, executing in a <em>location</em>.
25  *  \see m_process_t
26  */
27
28 /******************************** Process ************************************/
29 /** \ingroup m_process_management
30  * \brief Creates and runs a new #m_process_t.
31  *
32  * Does exactly the same as #MSG_process_create_with_arguments but without 
33    providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
34  * \sa MSG_process_create_with_arguments
35  */
36 m_process_t MSG_process_create(const char *name,
37                                m_process_code_t code, void *data,
38                                m_host_t host)
39 {
40   return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
41 }
42
43 static void MSG_process_cleanup(void *arg)
44 {
45         /* arg is a pointer to a simix process, we can get the msg process with the field data */
46         m_process_t proc = ((smx_process_t)arg)->data;
47   xbt_fifo_remove(msg_global->process_list, proc);
48         SIMIX_process_cleanup(arg);
49   free(proc->name);
50   proc->name = NULL;
51   free(proc->simdata);
52   proc->simdata = NULL;
53   free(proc);
54
55         return;
56 }
57
58 /** \ingroup m_process_management
59  * \brief Creates and runs a new #m_process_t.
60
61  * A constructor for #m_process_t taking four arguments and returning the 
62  * corresponding object. The structure (and the corresponding thread) is
63  * created, and put in the list of ready process.
64  * \param name a name for the object. It is for user-level information
65    and can be NULL.
66  * \param code is a function describing the behavior of the agent. It
67    should then only use functions described in \ref
68    m_process_management (to create a new #m_process_t for example),
69    in \ref m_host_management (only the read-only functions i.e. whose
70    name contains the word get), in \ref m_task_management (to create
71    or destroy some #m_task_t for example) and in \ref
72    msg_gos_functions (to handle file transfers and task processing).
73  * \param data a pointer to any data one may want to attach to the new
74    object.  It is for user-level information and can be NULL. It can
75    be retrieved with the function \ref MSG_process_get_data.
76  * \param host the location where the new agent is executed.
77  * \param argc first argument passed to \a code
78  * \param argv second argument passed to \a code
79  * \see m_process_t
80  * \return The new corresponding object.
81  */
82
83
84
85 m_process_t __MSG_process_create_with_arguments(const char *name,
86                                               m_process_code_t code, void *data,
87                                               char * hostname, int argc, char **argv)
88 {
89         m_host_t host = MSG_get_host_by_name(hostname);
90         return MSG_process_create_with_arguments(name,code,data,host,argc,argv);
91 }
92
93 m_process_t MSG_process_create_with_arguments(const char *name,
94                                               m_process_code_t code, void *data,
95                                               m_host_t host, int argc, char **argv)
96 {
97   simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
98   m_process_t process = xbt_new0(s_m_process_t,1);
99   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
100
101   /* Simulator Data */
102   simdata->PID = msg_global->PID++;
103         simdata->waiting_task = NULL;
104   simdata->host = host;
105   simdata->argc = argc;
106   simdata->argv = argv;
107         simdata->smx_process = SIMIX_process_create(name, (smx_process_code_t)code, (void*)process, host->name, argc, argv, MSG_process_cleanup );
108
109         if (SIMIX_process_self()) {
110                 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
111         }
112         else simdata->PPID = -1;
113   simdata->last_errno=MSG_OK;
114
115
116   /* Process structure */
117   process->name = xbt_strdup(name);
118   process->simdata = simdata;
119   process->data = data;
120
121         xbt_fifo_unshift(msg_global->process_list, process); 
122
123   return process;
124 }
125
126 /** \ingroup m_process_management
127  * \param process poor victim
128  *
129  * This function simply kills a \a process... scarry isn't it ? :)
130  */
131 void MSG_process_kill(m_process_t process)
132 {
133   simdata_process_t p_simdata = process->simdata;
134
135   DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID, p_simdata->host->name);
136
137         if(p_simdata->waiting_task) {
138                 DEBUG1("Canceling waiting task %s",p_simdata->waiting_task->name);
139     if(p_simdata->waiting_task->simdata->compute) {
140                         SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
141                 }
142     else if (p_simdata->waiting_task->simdata->comm) {
143                         SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
144     } 
145   }
146
147   xbt_fifo_remove(msg_global->process_list,process);
148         SIMIX_process_kill(process->simdata->smx_process);
149
150         return;
151 }
152
153 /** \ingroup m_process_management
154  * \brief Migrates an agent to another location.
155  *
156  * This functions checks whether \a process and \a host are valid pointers
157    and change the value of the #m_host_t on which \a process is running.
158  */
159 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
160 {
161         xbt_die("MSG_process_change_host - not implemented yet - maybe useless function");
162   return MSG_OK;
163 }
164
165 /** \ingroup m_process_management
166  * \brief Return the user data of a #m_process_t.
167  *
168  * This functions checks whether \a process is a valid pointer or not 
169    and return the user data associated to \a process if it is possible.
170  */
171 void *MSG_process_get_data(m_process_t process)
172 {
173   xbt_assert0((process != NULL), "Invalid parameters");
174
175   return (process->data);
176 }
177
178 /** \ingroup m_process_management
179  * \brief Set the user data of a #m_process_t.
180  *
181  * This functions checks whether \a process is a valid pointer or not 
182    and set the user data associated to \a process if it is possible.
183  */
184 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
185 {
186   xbt_assert0((process != NULL), "Invalid parameters");
187   xbt_assert0((process->data == NULL), "Data already set");
188   
189   process->data = data;
190    
191   return MSG_OK;
192 }
193
194 /** \ingroup m_process_management
195  * \brief Return the location on which an agent is running.
196  *
197  * This functions checks whether \a process is a valid pointer or not 
198    and return the m_host_t corresponding to the location on which \a 
199    process is running.
200  */
201 m_host_t MSG_process_get_host(m_process_t process)
202 {
203   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
204
205   return (((simdata_process_t) process->simdata)->host);
206 }
207
208 /** \ingroup m_process_management
209  *
210  * \brief Return a #m_process_t given its PID.
211  *
212  * This functions search in the list of all the created m_process_t for a m_process_t 
213    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
214    Note that the PID are uniq in the whole simulation, not only on a given host.
215  */
216 m_process_t MSG_process_from_PID(int PID)
217 {
218   xbt_fifo_item_t i = NULL;
219   m_process_t process = NULL;
220
221   xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
222     if(MSG_process_get_PID(process) == PID) return process;
223   }
224   return NULL;
225 }
226
227 /** \ingroup m_process_management
228  * \brief Returns the process ID of \a process.
229  *
230  * This functions checks whether \a process is a valid pointer or not 
231    and return its PID.
232  */
233 int MSG_process_get_PID(m_process_t process)
234 {
235   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
236
237   return (((simdata_process_t) process->simdata)->PID);
238 }
239
240 /** \ingroup m_process_management
241  * \brief Returns the process ID of the parent of \a process.
242  *
243  * This functions checks whether \a process is a valid pointer or not 
244    and return its PID. Returns -1 if the agent has not been created by 
245    another agent.
246  */
247 int MSG_process_get_PPID(m_process_t process)
248 {
249   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
250
251   return (((simdata_process_t) process->simdata)->PPID);
252 }
253
254 /** \ingroup m_process_management
255  * \brief Return the name of an agent.
256  *
257  * This functions checks whether \a process is a valid pointer or not 
258    and return its name.
259  */
260 const char *MSG_process_get_name(m_process_t process)
261 {
262   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
263
264   return (process->name);
265 }
266
267 /** \ingroup m_process_management
268  * \brief Return the PID of the current agent.
269  *
270  * This functions returns the PID of the currently running #m_process_t.
271  */
272 int MSG_process_self_PID(void)
273 {
274   return (MSG_process_get_PID(MSG_process_self()));
275 }
276
277 /** \ingroup m_process_management
278  * \brief Return the PPID of the current agent.
279  *
280  * This functions returns the PID of the parent of the currently
281  * running #m_process_t.
282  */
283 int MSG_process_self_PPID(void)
284 {
285   return (MSG_process_get_PPID(MSG_process_self()));
286 }
287
288 /** \ingroup m_process_management
289  * \brief Return the current agent.
290  *
291  * This functions returns the currently running #m_process_t.
292  */
293 m_process_t MSG_process_self(void)
294 {
295         smx_process_t proc = SIMIX_process_self();
296         if (proc != NULL) {
297                 return (m_process_t)proc->data;
298         }
299         else { 
300                 return NULL;
301         }
302
303 }
304
305 /** \ingroup m_process_management
306  * \brief Suspend the process.
307  *
308  * This functions suspend the process by suspending the task on which
309  * it was waiting for the completion.
310  */
311 MSG_error_t MSG_process_suspend(m_process_t process)
312 {
313   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
314   CHECK_HOST();
315
316         SIMIX_process_suspend(process->simdata->smx_process);
317   MSG_RETURN(MSG_OK);
318 }
319
320 /** \ingroup m_process_management
321  * \brief Resume a suspended process.
322  *
323  * This functions resume a suspended process by resuming the task on
324  * which it was waiting for the completion.
325  */
326 MSG_error_t MSG_process_resume(m_process_t process)
327 {
328
329   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
330   CHECK_HOST();
331
332         SIMIX_process_resume(process->simdata->smx_process);
333   MSG_RETURN(MSG_OK);
334 }
335
336 /** \ingroup m_process_management
337  * \brief Returns true if the process is suspended .
338  *
339  * This checks whether a process is suspended or not by inspecting the
340  * task on which it was waiting for the completion.
341  */
342 int MSG_process_is_suspended(m_process_t process)
343 {
344   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
345         return SIMIX_process_is_suspended(process->simdata->smx_process);
346 }
347