Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Many changes done.
[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 /** \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
10  * 
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>.
15  *  \see m_process_t
16  */
17
18 /******************************** Process ************************************/
19 /** \ingroup m_process_management
20  * \brief Creates and runs a new #m_process_t.
21  *
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
25  */
26 m_process_t MSG_process_create(const char *name,
27                                m_process_code_t code, void *data,
28                                m_host_t host)
29 {
30   return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
31 }
32
33 static void MSG_process_cleanup(void *arg)
34 {
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);
39   free(proc->name);
40   proc->name = NULL;
41   free(proc->simdata);
42   proc->simdata = NULL;
43   free(proc);
44
45         return;
46 }
47
48 /** \ingroup m_process_management
49  * \brief Creates and runs a new #m_process_t.
50
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
55    and can be NULL.
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
69  * \see m_process_t
70  * \return The new corresponding object.
71  */
72
73
74
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)
78 {
79         m_host_t host = MSG_get_host_by_name(hostname);
80         return MSG_process_create_with_arguments(name,code,data,host,argc,argv);
81 }
82
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)
86 {
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");
90
91   /* Simulator Data */
92   simdata->PID = msg_global->PID++;
93   simdata->host = host;
94   simdata->argc = argc;
95   simdata->argv = argv;
96         simdata->smx_process = SIMIX_process_create_with_arguments(name, (smx_process_code_t)code, (void*)process, host->name, argc, argv, MSG_process_cleanup );
97
98         if (SIMIX_process_self()) {
99                 simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
100         }
101         else simdata->PPID = -1;
102   simdata->last_errno=MSG_OK;
103
104
105   /* Process structure */
106   process->name = xbt_strdup(name);
107   process->simdata = simdata;
108   process->data = data;
109
110         xbt_fifo_unshift(msg_global->process_list, process); 
111
112   return process;
113 }
114
115 /** \ingroup m_process_management
116  * \param process poor victim
117  *
118  * This function simply kills a \a process... scarry isn't it ? :)
119  */
120 void MSG_process_kill(m_process_t process)
121 {
122
123         /*
124   int i;
125   simdata_process_t p_simdata = process->simdata;
126   simdata_host_t h_simdata= p_simdata->host->simdata;
127   int _cursor;
128   m_process_t proc = NULL;
129
130   DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID, p_simdata->host->name);
131
132   for (i=0; i<msg_global->max_channel; i++) {
133     if (h_simdata->sleeping[i] == process) {
134       h_simdata->sleeping[i] = NULL; 
135       break;
136     }
137   }
138
139         if(p_simdata->waiting_task) {    
140     xbt_dynar_foreach(p_simdata->waiting_task->simdata->sleeping,_cursor,proc) {                                                                             
141       if(proc==process)
142   xbt_dynar_remove_at(p_simdata->waiting_task->simdata->sleeping,_cursor,&proc);                                                                             
143     }  
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);                                                                                                       
152     } else {
153       xbt_die("UNKNOWN STATUS. Please report this bug.");                                                                                                    
154     }  
155   }    
156
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.");                                                                                                      
160   }
161 */
162   xbt_fifo_remove(msg_global->process_list,process);
163         SIMIX_process_kill(process->simdata->smx_process);
164
165         return;
166 }
167
168 /** \ingroup m_process_management
169  * \brief Migrates an agent to another location.
170  *
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.
173  */
174 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
175 {
176         xbt_die("MSG_process_change_host - not implemented yet - maybe useless function");
177   return MSG_OK;
178 }
179
180 /** \ingroup m_process_management
181  * \brief Return the user data of a #m_process_t.
182  *
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.
185  */
186 void *MSG_process_get_data(m_process_t process)
187 {
188   xbt_assert0((process != NULL), "Invalid parameters");
189
190   return (process->data);
191 }
192
193 /** \ingroup m_process_management
194  * \brief Set the user data of a #m_process_t.
195  *
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.
198  */
199 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
200 {
201   xbt_assert0((process != NULL), "Invalid parameters");
202   xbt_assert0((process->data == NULL), "Data already set");
203   
204   process->data = data;
205    
206   return MSG_OK;
207 }
208
209 /** \ingroup m_process_management
210  * \brief Return the location on which an agent is running.
211  *
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 
214    process is running.
215  */
216 m_host_t MSG_process_get_host(m_process_t process)
217 {
218   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
219
220   return (((simdata_process_t) process->simdata)->host);
221 }
222
223 /** \ingroup m_process_management
224  *
225  * \brief Return a #m_process_t given its PID.
226  *
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.
230  */
231 m_process_t MSG_process_from_PID(int PID)
232 {
233   xbt_fifo_item_t i = NULL;
234   m_process_t process = NULL;
235
236   xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
237     if(MSG_process_get_PID(process) == PID) return process;
238   }
239   return NULL;
240 }
241
242 /** \ingroup m_process_management
243  * \brief Returns the process ID of \a process.
244  *
245  * This functions checks whether \a process is a valid pointer or not 
246    and return its PID.
247  */
248 int MSG_process_get_PID(m_process_t process)
249 {
250   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
251
252   return (((simdata_process_t) process->simdata)->PID);
253 }
254
255 /** \ingroup m_process_management
256  * \brief Returns the process ID of the parent of \a process.
257  *
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 
260    another agent.
261  */
262 int MSG_process_get_PPID(m_process_t process)
263 {
264   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
265
266   return (((simdata_process_t) process->simdata)->PPID);
267 }
268
269 /** \ingroup m_process_management
270  * \brief Return the name of an agent.
271  *
272  * This functions checks whether \a process is a valid pointer or not 
273    and return its name.
274  */
275 const char *MSG_process_get_name(m_process_t process)
276 {
277   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
278
279   return (process->name);
280 }
281
282 /** \ingroup m_process_management
283  * \brief Return the PID of the current agent.
284  *
285  * This functions returns the PID of the currently running #m_process_t.
286  */
287 int MSG_process_self_PID(void)
288 {
289   return (MSG_process_get_PID(MSG_process_self()));
290 }
291
292 /** \ingroup m_process_management
293  * \brief Return the PPID of the current agent.
294  *
295  * This functions returns the PID of the parent of the currently
296  * running #m_process_t.
297  */
298 int MSG_process_self_PPID(void)
299 {
300   return (MSG_process_get_PPID(MSG_process_self()));
301 }
302
303 /** \ingroup m_process_management
304  * \brief Return the current agent.
305  *
306  * This functions returns the currently running #m_process_t.
307  */
308 m_process_t MSG_process_self(void)
309 {
310         smx_process_t proc = SIMIX_process_self();
311         if (proc != NULL) {
312                 return (m_process_t)proc->data;
313                 }
314         else { 
315                 return NULL;
316         }
317
318 }
319
320 /** \ingroup m_process_management
321  * \brief Suspend the process.
322  *
323  * This functions suspend the process by suspending the task on which
324  * it was waiting for the completion.
325  */
326 MSG_error_t MSG_process_suspend(m_process_t process)
327 {
328         xbt_die("not implemented yet");
329         return MSG_OK;
330 }
331
332 /** \ingroup m_process_management
333  * \brief Resume a suspended process.
334  *
335  * This functions resume a suspended process by resuming the task on
336  * which it was waiting for the completion.
337  */
338 MSG_error_t MSG_process_resume(m_process_t process)
339 {
340         xbt_die("not implemented yet");
341         MSG_RETURN(MSG_OK);
342 }
343
344 /** \ingroup m_process_management
345  * \brief Returns true if the process is suspended .
346  *
347  * This checks whether a process is suspended or not by inspecting the
348  * task on which it was waiting for the completion.
349  */
350 int MSG_process_is_suspended(m_process_t process)
351 {
352         xbt_die("not implemented yet");
353         return 0;
354 }
355
356 int __MSG_process_block(double max_duration, const char *info)
357 {
358     return 1;
359 }
360
361 MSG_error_t __MSG_process_unblock(m_process_t process)
362 {
363     MSG_RETURN(MSG_OK);
364 }
365
366 int __MSG_process_isBlocked(m_process_t process)
367 {
368         return 0;
369 }