Logo AND Algorithmique Numérique Distribuée

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