Logo AND Algorithmique Numérique Distribuée

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