Logo AND Algorithmique Numérique Distribuée

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