Logo AND Algorithmique Numérique Distribuée

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