Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new tracing mask TRACE_VOLUME to trace the msg tasks communication size and group...
[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 #include "../simix/private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
16                                 "Logging specific to MSG (process)");
17
18 /** \defgroup m_process_management Management Functions of Agents
19  *  \brief This section describes the agent structure of MSG
20  *  (#m_process_t) and the functions for managing it.
21  */
22 /** @addtogroup m_process_management
23  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
24  *
25  *  We need to simulate many independent scheduling decisions, so
26  *  the concept of <em>process</em> is at the heart of the
27  *  simulator. A process may be defined as a <em>code</em>, with
28  *  some <em>private data</em>, executing in a <em>location</em>.
29  *  \see m_process_t
30  */
31
32 /******************************** Process ************************************/
33 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 #ifdef HAVE_TRACING
38   TRACE_msg_process_end (proc);
39 #endif
40   xbt_fifo_remove(msg_global->process_list, proc);
41   SIMIX_process_cleanup(arg);
42   if (proc->name) {
43     free(proc->name);
44     proc->name = NULL;
45   }
46   if (proc->simdata) {
47     free(proc->simdata);
48     proc->simdata = NULL;
49   }
50   free(proc);
51
52   return;
53 }
54
55 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
56 void *_MSG_process_create_from_SIMIX(const char *name,
57                                      xbt_main_func_t code, void *data,
58                                      char *hostname, int argc, char **argv,
59                                      xbt_dict_t properties)
60 {
61   m_host_t host = MSG_get_host_by_name(hostname);
62   return (void *) MSG_process_create_with_environment(name, code, data, host,
63                                                       argc, argv, properties);
64 }
65
66 /** \ingroup m_process_management
67  * \brief Creates and runs a new #m_process_t.
68  *
69  * Does exactly the same as #MSG_process_create_with_arguments but without
70    providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
71  * \sa MSG_process_create_with_arguments
72  */
73 m_process_t MSG_process_create(const char *name,
74                                xbt_main_func_t code, void *data,
75                                m_host_t host)
76 {
77   return MSG_process_create_with_environment(name, code, data, host, -1,
78                                              NULL, NULL);
79 }
80
81 /** \ingroup m_process_management
82  * \brief Creates and runs a new #m_process_t.
83
84  * A constructor for #m_process_t taking four arguments and returning the
85  * corresponding object. The structure (and the corresponding thread) is
86  * created, and put in the list of ready process.
87  * \param name a name for the object. It is for user-level information
88    and can be NULL.
89  * \param code is a function describing the behavior of the agent. It
90    should then only use functions described in \ref
91    m_process_management (to create a new #m_process_t for example),
92    in \ref m_host_management (only the read-only functions i.e. whose
93    name contains the word get), in \ref m_task_management (to create
94    or destroy some #m_task_t for example) and in \ref
95    msg_gos_functions (to handle file transfers and task processing).
96  * \param data a pointer to any data one may want to attach to the new
97    object.  It is for user-level information and can be NULL. It can
98    be retrieved with the function \ref MSG_process_get_data.
99  * \param host the location where the new agent is executed.
100  * \param argc first argument passed to \a code
101  * \param argv second argument passed to \a code
102  * \see m_process_t
103  * \return The new corresponding object.
104  */
105
106 m_process_t MSG_process_create_with_arguments(const char *name,
107                                               xbt_main_func_t code,
108                                               void *data, m_host_t host,
109                                               int argc, char **argv)
110 {
111   return MSG_process_create_with_environment(name, code, data, host,
112                                              argc, argv, NULL);
113 }
114
115 /** \ingroup m_process_management
116  * \brief Creates and runs a new #m_process_t.
117
118  * A constructor for #m_process_t taking four arguments and returning the
119  * corresponding object. The structure (and the corresponding thread) is
120  * created, and put in the list of ready process.
121  * \param name a name for the object. It is for user-level information
122    and can be NULL.
123  * \param code is a function describing the behavior of the agent. It
124    should then only use functions described in \ref
125    m_process_management (to create a new #m_process_t for example),
126    in \ref m_host_management (only the read-only functions i.e. whose
127    name contains the word get), in \ref m_task_management (to create
128    or destroy some #m_task_t for example) and in \ref
129    msg_gos_functions (to handle file transfers and task processing).
130  * \param data a pointer to any data one may want to attach to the new
131    object.  It is for user-level information and can be NULL. It can
132    be retrieved with the function \ref MSG_process_get_data.
133  * \param host the location where the new agent is executed.
134  * \param argc first argument passed to \a code
135  * \param argv second argument passed to \a code
136  * \param properties list a properties defined for this process
137  * \see m_process_t
138  * \return The new corresponding object.
139  */
140 m_process_t MSG_process_create_with_environment(const char *name,
141                                                 xbt_main_func_t code,
142                                                 void *data, m_host_t host,
143                                                 int argc, char **argv,
144                                                 xbt_dict_t properties)
145 {
146   simdata_process_t simdata = NULL;
147   m_process_t process = xbt_new0(s_m_process_t, 1);
148   smx_process_t smx_process = NULL;
149   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
150
151   smx_process = SIMIX_process_create(name, code,
152                                      (void *) process, host->name,
153                                      argc, argv, properties);
154   if (!smx_process) {
155     xbt_free(process);
156     return NULL;
157   }
158
159   simdata = xbt_new0(s_simdata_process_t, 1);
160
161   /* Simulator Data */
162   simdata->PID = msg_global->PID++;
163   simdata->waiting_action = NULL;
164   simdata->waiting_task = NULL;
165   simdata->m_host = host;
166   simdata->argc = argc;
167   simdata->argv = argv;
168   simdata->s_process = smx_process;
169
170   if (SIMIX_process_self()) {
171     simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
172   } else {
173     simdata->PPID = -1;
174   }
175   simdata->last_errno = MSG_OK;
176
177
178   /* Process structure */
179   process->name = xbt_strdup(name);
180   process->simdata = simdata;
181   process->data = data;
182
183   xbt_fifo_unshift(msg_global->process_list, process);
184
185   return process;
186
187 }
188
189 void _MSG_process_kill_from_SIMIX(void *p)
190 {
191 #ifdef HAVE_TRACING
192   TRACE_msg_process_kill ((m_process_t) p);
193 #endif
194   MSG_process_kill((m_process_t) p);
195 }
196
197 /** \ingroup m_process_management
198  * \param process poor victim
199  *
200  * This function simply kills a \a process... scarry isn't it ? :)
201  */
202 void MSG_process_kill(m_process_t process)
203 {
204   simdata_process_t p_simdata = process->simdata;
205
206 #ifdef HAVE_TRACING
207   TRACE_msg_process_kill (process);
208 #endif
209
210   DEBUG3("Killing %s(%d) on %s",
211          process->name, p_simdata->PID, p_simdata->m_host->name);
212
213   if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
214       SIMIX_communication_cancel(p_simdata->waiting_task->simdata->comm);
215   }
216  
217   if (p_simdata->waiting_action) {
218     DEBUG1("Canceling waiting task %s",
219            SIMIX_action_get_name(p_simdata->waiting_action));
220     SIMIX_action_cancel(p_simdata->waiting_action);
221    }
222   
223   xbt_fifo_remove(msg_global->process_list, process);
224   SIMIX_process_kill(process->simdata->s_process);
225
226   return;
227 }
228
229 /** \ingroup m_process_management
230  * \brief Migrates an agent to another location.
231  *
232  * This function checks whether \a process and \a host are valid pointers
233    and change the value of the #m_host_t on which \a process is running.
234  */
235 MSG_error_t MSG_process_change_host(m_host_t host)
236 {
237   m_process_t process = MSG_process_self();
238   m_host_t now = process->simdata->m_host;
239   process->simdata->m_host = host;
240 #ifdef HAVE_TRACING
241   TRACE_msg_process_change_host (process, now, host);
242 #endif
243   SIMIX_process_change_host(process->simdata->s_process, now->name,
244                             host->name);
245   return MSG_OK;
246 }
247
248 /** \ingroup m_process_management
249  * \brief Return the user data of a #m_process_t.
250  *
251  * This function checks whether \a process is a valid pointer or not
252    and return the user data associated to \a process if it is possible.
253  */
254 void *MSG_process_get_data(m_process_t process)
255 {
256   xbt_assert0((process != NULL), "Invalid parameters");
257
258   return (process->data);
259 }
260
261 /** \ingroup m_process_management
262  * \brief Set the user data of a #m_process_t.
263  *
264  * This function checks whether \a process is a valid pointer or not
265    and set the user data associated to \a process if it is possible.
266  */
267 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
268 {
269   xbt_assert0((process != NULL), "Invalid parameters");
270
271   process->data = data;
272
273   return MSG_OK;
274 }
275
276 /** \ingroup m_process_management
277  * \brief Return the location on which an agent is running.
278  *
279  * This function checks whether \a process is a valid pointer or not
280    and return the m_host_t corresponding to the location on which \a
281    process is running.
282  */
283 m_host_t MSG_process_get_host(m_process_t process)
284 {
285   xbt_assert0(((process != NULL)
286                && (process->simdata)), "Invalid parameters");
287
288   return (((simdata_process_t) process->simdata)->m_host);
289 }
290
291 /** \ingroup m_process_management
292  *
293  * \brief Return a #m_process_t given its PID.
294  *
295  * This function search in the list of all the created m_process_t for a m_process_t
296    whose PID is equal to \a PID. If no host is found, \c NULL is returned.
297    Note that the PID are uniq in the whole simulation, not only on a given host.
298  */
299 m_process_t MSG_process_from_PID(int PID)
300 {
301   xbt_fifo_item_t i = NULL;
302   m_process_t process = NULL;
303
304   xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
305     if (MSG_process_get_PID(process) == PID)
306       return process;
307   }
308   return NULL;
309 }
310
311 /** \ingroup m_process_management
312  * \brief Returns the process ID of \a process.
313  *
314  * This function checks whether \a process is a valid pointer or not
315    and return its PID (or 0 in case of problem).
316  */
317 int MSG_process_get_PID(m_process_t process)
318 {
319   /* Do not raise an exception here: this function is used in the logs,
320      and it will be called back by the exception handling stuff */
321   if (process == NULL || process->simdata == NULL)
322     return 0;
323
324   return (((simdata_process_t) process->simdata)->PID);
325 }
326
327 /** \ingroup m_process_management
328  * \brief Returns the process ID of the parent of \a process.
329  *
330  * This function checks whether \a process is a valid pointer or not
331    and return its PID. Returns -1 if the agent has not been created by
332    another agent.
333  */
334 int MSG_process_get_PPID(m_process_t process)
335 {
336   xbt_assert0(((process != NULL)
337                && (process->simdata)), "Invalid parameters");
338
339   return (((simdata_process_t) process->simdata)->PPID);
340 }
341
342 /** \ingroup m_process_management
343  * \brief Return the name of an agent.
344  *
345  * This function checks whether \a process is a valid pointer or not
346    and return its name.
347  */
348 const char *MSG_process_get_name(m_process_t process)
349 {
350   xbt_assert0(process, "Invalid parameter: process is NULL");
351   xbt_assert0(process->simdata,
352               "Invalid parameter: process->simdata is NULL");
353
354   return (process->name);
355 }
356
357 /** \ingroup m_process_management
358  * \brief Returns the value of a given process property
359  *
360  * \param process a process
361  * \param name a property name
362  * \return value of a property (or NULL if the property is not set)
363  */
364 const char *MSG_process_get_property_value(m_process_t process,
365                                            const char *name)
366 {
367   return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
368 }
369
370 /** \ingroup m_process_management
371  * \brief Return the list of properties
372  *
373  * This function returns all the parameters associated with a process
374  */
375 xbt_dict_t MSG_process_get_properties(m_process_t process)
376 {
377   xbt_assert0((process != NULL), "Invalid parameters");
378
379   return (SIMIX_process_get_properties
380           (((simdata_process_t) process->simdata)->s_process));
381
382 }
383
384 /** \ingroup m_process_management
385  * \brief Return the PID of the current agent.
386  *
387  * This function returns the PID of the currently running #m_process_t.
388  */
389 int MSG_process_self_PID(void)
390 {
391   return (MSG_process_get_PID(MSG_process_self()));
392 }
393
394 /** \ingroup m_process_management
395  * \brief Return the PPID of the current agent.
396  *
397  * This function returns the PID of the parent of the currently
398  * running #m_process_t.
399  */
400 int MSG_process_self_PPID(void)
401 {
402   return (MSG_process_get_PPID(MSG_process_self()));
403 }
404
405 /** \ingroup m_process_management
406  * \brief Return the current agent.
407  *
408  * This function returns the currently running #m_process_t.
409  */
410 m_process_t MSG_process_self(void)
411 {
412   smx_process_t proc = SIMIX_process_self();
413   if (proc != NULL) {
414     return (m_process_t) proc->data;
415   } else {
416     return NULL;
417   }
418
419 }
420
421 /** \ingroup m_process_management
422  * \brief Suspend the process.
423  *
424  * This function suspends the process by suspending the task on which
425  * it was waiting for the completion.
426  */
427 MSG_error_t MSG_process_suspend(m_process_t process)
428 {
429   xbt_assert0(((process != NULL)
430                && (process->simdata)), "Invalid parameters");
431   CHECK_HOST();
432
433 #ifdef HAVE_TRACING
434   TRACE_msg_process_suspend (process);
435 #endif
436
437   SIMIX_process_suspend(process->simdata->s_process);
438   MSG_RETURN(MSG_OK);
439 }
440
441 /** \ingroup m_process_management
442  * \brief Resume a suspended process.
443  *
444  * This function resumes a suspended process by resuming the task on
445  * which it was waiting for the completion.
446  */
447 MSG_error_t MSG_process_resume(m_process_t process)
448 {
449
450   xbt_assert0(((process != NULL)
451                && (process->simdata)), "Invalid parameters");
452   CHECK_HOST();
453
454 #ifdef HAVE_TRACING
455   TRACE_msg_process_resume (process);
456 #endif
457
458   SIMIX_process_resume(process->simdata->s_process);
459   MSG_RETURN(MSG_OK);
460 }
461
462 /** \ingroup m_process_management
463  * \brief Returns true if the process is suspended .
464  *
465  * This checks whether a process is suspended or not by inspecting the
466  * task on which it was waiting for the completion.
467  */
468 int MSG_process_is_suspended(m_process_t process)
469 {
470   xbt_assert0(((process != NULL)
471                && (process->simdata)), "Invalid parameters");
472   return SIMIX_process_is_suspended(process->simdata->s_process);
473 }