Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc
[simgrid.git] / src / msg / msg_process.c
1 /* Copyright (c) 2004-2014. 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/smx_process_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
13                                 "Logging specific to MSG (process)");
14
15 /** @addtogroup m_process_management
16  * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Processes" --> \endhtmlonly
17  *
18  *  We need to simulate many independent scheduling decisions, so
19  *  the concept of <em>process</em> is at the heart of the
20  *  simulator. A process may be defined as a <em>code</em>, with
21  *  some <em>private data</em>, executing in a <em>location</em>.
22  *  \see msg_process_t
23  */
24
25 /******************************** Process ************************************/
26
27 /**
28  * \brief Cleans the MSG data of a process.
29  * \param smx_proc a SIMIX process
30  */
31 void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc)
32 {
33   simdata_process_t msg_proc;
34
35   // get the MSG process from the SIMIX process
36   if (smx_proc == SIMIX_process_self()) {
37     /* avoid a SIMIX request if this function is called by the process itself */
38     msg_proc = SIMIX_process_self_get_data(smx_proc);
39     SIMIX_process_self_set_data(smx_proc, NULL);
40   }
41   else {
42     msg_proc = simcall_process_get_data(smx_proc);
43     simcall_process_set_data(smx_proc, NULL);
44   }
45
46 #ifdef HAVE_TRACING
47   TRACE_msg_process_end(smx_proc);
48 #endif
49   // free the data if a function was provided
50   if (msg_proc && msg_proc->data && msg_global->process_data_cleanup) {
51     msg_global->process_data_cleanup(msg_proc->data);
52   }
53
54   // free the MSG process
55   xbt_free(msg_proc);
56 }
57
58 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
59 void MSG_process_create_from_SIMIX(smx_process_t* process, const char *name,
60                                    xbt_main_func_t code, void *data,
61                                    const char *hostname, double kill_time,
62                                    int argc, char **argv,
63                                    xbt_dict_t properties, int auto_restart,
64                                    smx_process_t parent_process)
65 {
66   msg_host_t host = MSG_get_host_by_name(hostname);
67   msg_process_t p = MSG_process_create_with_environment(name, code, data,
68                                                       host, argc, argv,
69                                                       properties);
70   if (p) {
71     MSG_process_set_kill_time(p,kill_time);
72     MSG_process_auto_restart_set(p,auto_restart);
73   }
74   *((msg_process_t*) process) = p;
75 }
76
77 /** \ingroup m_process_management
78  * \brief Creates and runs a new #msg_process_t.
79  *
80  * Does exactly the same as #MSG_process_create_with_arguments but without
81    providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
82  * \sa MSG_process_create_with_arguments
83  */
84 msg_process_t MSG_process_create(const char *name,
85                                xbt_main_func_t code, void *data,
86                                msg_host_t host)
87 {
88   return MSG_process_create_with_environment(name, code, data, host, -1,
89                                              NULL, NULL);
90 }
91
92 /** \ingroup m_process_management
93  * \brief Creates and runs a new #msg_process_t.
94
95  * A constructor for #msg_process_t taking four arguments and returning the
96  * corresponding object. The structure (and the corresponding thread) is
97  * created, and put in the list of ready process.
98  * \param name a name for the object. It is for user-level information
99    and can be NULL.
100  * \param code is a function describing the behavior of the process. It
101    should then only use functions described in \ref
102    m_process_management (to create a new #msg_process_t for example),
103    in \ref m_host_management (only the read-only functions i.e. whose
104    name contains the word get), in \ref m_task_management (to create
105    or destroy some #msg_task_t for example) and in \ref
106    msg_task_usage (to handle file transfers and task processing).
107  * \param data a pointer to any data one may want to attach to the new
108    object.  It is for user-level information and can be NULL. It can
109    be retrieved with the function \ref MSG_process_get_data.
110  * \param host the location where the new process is executed.
111  * \param argc first argument passed to \a code
112  * \param argv second argument passed to \a code
113  * \see msg_process_t
114  * \return The new corresponding object.
115  */
116
117 msg_process_t MSG_process_create_with_arguments(const char *name,
118                                               xbt_main_func_t code,
119                                               void *data, msg_host_t host,
120                                               int argc, char **argv)
121 {
122   return MSG_process_create_with_environment(name, code, data, host,
123                                              argc, argv, NULL);
124 }
125
126 /** \ingroup m_process_management
127  * \brief Creates and runs a new #msg_process_t.
128
129  * A constructor for #msg_process_t taking four arguments and returning the
130  * corresponding object. The structure (and the corresponding thread) is
131  * created, and put in the list of ready process.
132  * \param name a name for the object. It is for user-level information
133    and can be NULL.
134  * \param code is a function describing the behavior of the process. It
135    should then only use functions described in \ref
136    m_process_management (to create a new #msg_process_t for example),
137    in \ref m_host_management (only the read-only functions i.e. whose
138    name contains the word get), in \ref m_task_management (to create
139    or destroy some #msg_task_t for example) and in \ref
140    msg_task_usage (to handle file transfers and task processing).
141  * \param data a pointer to any data one may want to attach to the new
142    object.  It is for user-level information and can be NULL. It can
143    be retrieved with the function \ref MSG_process_get_data.
144  * \param host the location where the new process is executed.
145  * \param argc first argument passed to \a code
146  * \param argv second argument passed to \a code. WARNING, these strings are freed by the SimGrid kernel when the process exits, so they cannot be static nor shared between several processes.
147  * \param properties list a properties defined for this process
148  * \see msg_process_t
149  * \return The new corresponding object.
150  */
151 msg_process_t MSG_process_create_with_environment(const char *name,
152                                                 xbt_main_func_t code,
153                                                 void *data, msg_host_t host,
154                                                 int argc, char **argv,
155                                                 xbt_dict_t properties)
156 {
157   xbt_assert(code != NULL && host != NULL, "Invalid parameters");
158   simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
159   msg_process_t process;
160
161   /* Simulator data for MSG */
162   simdata->waiting_action = NULL;
163   simdata->waiting_task = NULL;
164   simdata->m_host = host;
165   simdata->argc = argc;
166   simdata->argv = argv;
167   simdata->data = data;
168   simdata->last_errno = MSG_OK;
169
170   /* Let's create the process: SIMIX may decide to start it right now,
171    * even before returning the flow control to us */
172  simcall_process_create(&process, name, code, simdata, sg_host_name(host), -1,
173                            argc, argv, properties,0);
174
175 #ifdef HAVE_TRACING
176   TRACE_msg_process_create(name, SIMIX_process_get_PID(process), host);
177 #endif
178
179   if (!process) {
180     /* Undo everything we have just changed */
181     xbt_free(simdata);
182     return NULL;
183   }
184   else {
185     #ifdef HAVE_TRACING
186     simcall_process_on_exit(process,(int_f_pvoid_pvoid_t)TRACE_msg_process_kill,process);
187     #endif
188   }
189   return process;
190 }
191
192 /** \ingroup m_process_management
193  * \param process poor victim
194  *
195  * This function simply kills a \a process... scary isn't it ? :)
196  */
197 void MSG_process_kill(msg_process_t process)
198 {
199 //  /* FIXME: why do we only cancel communication actions? is this useful? */
200 //  simdata_process_t p_simdata = simcall_process_get_data(process);
201 //  if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
202 //    simcall_comm_cancel(p_simdata->waiting_task->simdata->comm);
203 //  }
204
205   simcall_process_kill(process);
206
207   return;
208 }
209
210 /**
211 * \brief Wait for the completion of a #msg_process_t.
212 *
213 * \param process the process to wait for
214 * \param timeout wait until the process is over, or the timeout occurs
215 */
216 msg_error_t MSG_process_join(msg_process_t process, double timeout){
217   simcall_process_join(process, timeout);
218   return MSG_OK;
219 }
220
221 /** \ingroup m_process_management
222  * \brief Migrates a process to another location.
223  *
224  * This function checks whether \a process and \a host are valid pointers
225    and change the value of the #msg_host_t on which \a process is running.
226  */
227 msg_error_t MSG_process_migrate(msg_process_t process, msg_host_t host)
228 {
229   simdata_process_t simdata = simcall_process_get_data(process);
230   simdata->m_host = host;
231 #ifdef HAVE_TRACING
232   msg_host_t now = simdata->m_host;
233   TRACE_msg_process_change_host(process, now, host);
234 #endif
235   simcall_process_change_host(process, host);
236   return MSG_OK;
237 }
238
239 /** \ingroup m_process_management
240  * \brief Returns the user data of a process.
241  *
242  * This function checks whether \a process is a valid pointer or not
243    and returns the user data associated to this process.
244  */
245 void* MSG_process_get_data(msg_process_t process)
246 {
247   xbt_assert(process != NULL, "Invalid parameter");
248
249   /* get from SIMIX the MSG process data, and then the user data */
250   simdata_process_t simdata = simcall_process_get_data(process);
251   return simdata->data;
252 }
253
254 /** \ingroup m_process_management
255  * \brief Sets the user data of a process.
256  *
257  * This function checks whether \a process is a valid pointer or not
258    and sets the user data associated to this process.
259  */
260 msg_error_t MSG_process_set_data(msg_process_t process, void *data)
261 {
262   xbt_assert(process != NULL, "Invalid parameter");
263
264   simdata_process_t simdata = simcall_process_get_data(process);
265   simdata->data = data;
266
267   return MSG_OK;
268 }
269
270 /** \ingroup m_process_management
271  * \brief Sets a cleanup function to be called to free the userdata of a
272  * process when a process is destroyed.
273  * \param data_cleanup a cleanup function for the userdata of a process,
274  * or NULL to call no function
275  */
276 XBT_PUBLIC(void) MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup) {
277
278   msg_global->process_data_cleanup = data_cleanup;
279 }
280
281 /** \ingroup m_process_management
282  * \brief Return the location on which a process is running.
283  * \param process a process (NULL means the current one)
284  * \return the msg_host_t corresponding to the location on which \a
285  * process is running.
286  */
287 msg_host_t MSG_process_get_host(msg_process_t process)
288 {
289   simdata_process_t simdata;
290   if (process == NULL) {
291     simdata = SIMIX_process_self_get_data(SIMIX_process_self());
292   }
293   else {
294     simdata = simcall_process_get_data(process);
295   }
296   return simdata->m_host;
297 }
298
299 /** \ingroup m_process_management
300  *
301  * \brief Return a #msg_process_t given its PID.
302  *
303  * This function search in the list of all the created msg_process_t for a msg_process_t
304    whose PID is equal to \a PID. If no host is found, \c NULL is returned.
305    Note that the PID are uniq in the whole simulation, not only on a given host.
306  */
307 msg_process_t MSG_process_from_PID(int PID)
308 {
309   return SIMIX_process_from_PID(PID);
310 }
311
312 /** @brief returns a list of all currently existing processes */
313 xbt_dynar_t MSG_processes_as_dynar(void) {
314   return SIMIX_processes_as_dynar();
315 }
316 /** @brief Return the current number MSG processes.
317  */
318 int MSG_process_get_number(void)
319 {
320   return SIMIX_process_count();
321 }
322
323 /** \ingroup m_process_management
324  * \brief Set the kill time of a process.
325  *
326  * \param process a process
327  * \param kill_time the time when the process is killed.
328  */
329 msg_error_t MSG_process_set_kill_time(msg_process_t process, double kill_time)
330 {
331   simcall_process_set_kill_time(process,kill_time);
332   return MSG_OK;
333 }
334
335 /** \ingroup m_process_management
336  * \brief Returns the process ID of \a process.
337  *
338  * This function checks whether \a process is a valid pointer or not
339    and return its PID (or 0 in case of problem).
340  */
341 int MSG_process_get_PID(msg_process_t process)
342 {
343   /* Do not raise an exception here: this function is called by the logs
344    * and the exceptions, so it would be called back again and again */
345   if (process == NULL) {
346     return 0;
347   }
348   return simcall_process_get_PID(process);
349 }
350
351 /** \ingroup m_process_management
352  * \brief Returns the process ID of the parent of \a process.
353  *
354  * This function checks whether \a process is a valid pointer or not
355    and return its PID. Returns -1 if the process has not been created by
356    any other process.
357  */
358 int MSG_process_get_PPID(msg_process_t process)
359 {
360   xbt_assert(process != NULL, "Invalid parameter");
361
362   return simcall_process_get_PPID(process);
363 }
364
365 /** \ingroup m_process_management
366  * \brief Return the name of a process.
367  *
368  * This function checks whether \a process is a valid pointer or not
369    and return its name.
370  */
371 const char *MSG_process_get_name(msg_process_t process)
372 {
373   xbt_assert(process, "Invalid parameter");
374
375   return simcall_process_get_name(process);
376 }
377
378 /** \ingroup m_process_management
379  * \brief Returns the value of a given process property
380  *
381  * \param process a process
382  * \param name a property name
383  * \return value of a property (or NULL if the property is not set)
384  */
385 const char *MSG_process_get_property_value(msg_process_t process,
386                                            const char *name)
387 {
388   return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
389 }
390
391 /** \ingroup m_process_management
392  * \brief Return the list of properties
393  *
394  * This function returns all the parameters associated with a process
395  */
396 xbt_dict_t MSG_process_get_properties(msg_process_t process)
397 {
398   xbt_assert(process != NULL, "Invalid parameter");
399
400   return simcall_process_get_properties(process);
401
402 }
403
404 /** \ingroup m_process_management
405  * \brief Return the PID of the current process.
406  *
407  * This function returns the PID of the currently running #msg_process_t.
408  */
409 int MSG_process_self_PID(void)
410 {
411   return MSG_process_get_PID(MSG_process_self());
412 }
413
414 /** \ingroup m_process_management
415  * \brief Return the PPID of the current process.
416  *
417  * This function returns the PID of the parent of the currently
418  * running #msg_process_t.
419  */
420 int MSG_process_self_PPID(void)
421 {
422   return MSG_process_get_PPID(MSG_process_self());
423 }
424
425 /** \ingroup m_process_management
426  * \brief Return the current process.
427  *
428  * This function returns the currently running #msg_process_t.
429  */
430 msg_process_t MSG_process_self(void)
431 {
432   return SIMIX_process_self();
433 }
434
435 /** \ingroup m_process_management
436  * \brief Suspend the process.
437  *
438  * This function suspends the process by suspending the task on which
439  * it was waiting for the completion.
440  */
441 msg_error_t MSG_process_suspend(msg_process_t process)
442 {
443   xbt_assert(process != NULL, "Invalid parameter");
444
445 #ifdef HAVE_TRACING
446   TRACE_msg_process_suspend(process);
447 #endif
448
449   simcall_process_suspend(process);
450   MSG_RETURN(MSG_OK);
451 }
452
453 /** \ingroup m_process_management
454  * \brief Resume a suspended process.
455  *
456  * This function resumes a suspended process by resuming the task on
457  * which it was waiting for the completion.
458  */
459 msg_error_t MSG_process_resume(msg_process_t process)
460 {
461   xbt_assert(process != NULL, "Invalid parameter");
462
463 #ifdef HAVE_TRACING
464   TRACE_msg_process_resume(process);
465 #endif
466
467   simcall_process_resume(process);
468   MSG_RETURN(MSG_OK);
469 }
470
471 /** \ingroup m_process_management
472  * \brief Returns true if the process is suspended .
473  *
474  * This checks whether a process is suspended or not by inspecting the
475  * task on which it was waiting for the completion.
476  */
477 int MSG_process_is_suspended(msg_process_t process)
478 {
479   xbt_assert(process != NULL, "Invalid parameter");
480   return simcall_process_is_suspended(process);
481 }
482
483 smx_context_t MSG_process_get_smx_ctx(msg_process_t process) {
484   return SIMIX_process_get_context(process);
485 }
486 /**
487  * \ingroup m_process_management
488  * \brief Add a function to the list of "on_exit" functions for the current process.
489  * The on_exit functions are the functions executed when your process is killed.
490  * You should use them to free the data used by your process.
491  */
492 void MSG_process_on_exit(int_f_pvoid_pvoid_t fun, void *data) {
493   simcall_process_on_exit(MSG_process_self(),fun,data);
494 }
495 /**
496  * \ingroup m_process_management
497  * \brief Sets the "auto-restart" flag of the process.
498  * If the flag is set to 1, the process will be automatically restarted when
499  * its host comes back up.
500  */
501 XBT_PUBLIC(void) MSG_process_auto_restart_set(msg_process_t process, int auto_restart) {
502   simcall_process_auto_restart_set(process,auto_restart);
503 }
504 /*
505  * \ingroup m_process_management
506  * \brief Restarts a process from the beginning.
507  */
508 XBT_PUBLIC(msg_process_t) MSG_process_restart(msg_process_t process) {
509   return simcall_process_restart(process);
510 }