Logo AND Algorithmique Numérique Distribuée

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