Logo AND Algorithmique Numérique Distribuée

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