Logo AND Algorithmique Numérique Distribuée

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