Logo AND Algorithmique Numérique Distribuée

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