Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
931ec4e19c407452b4ee165b931ed24bb4224dba
[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 = SIMIX_req_process_get_data(smx_proc);
46     SIMIX_req_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   SIMIX_req_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 = SIMIX_req_process_get_data(process);
215   if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
216     SIMIX_req_comm_cancel(p_simdata->waiting_task->simdata->comm);
217   }
218  
219   SIMIX_req_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 = SIMIX_req_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   SIMIX_req_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 = SIMIX_req_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 = SIMIX_req_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 = SIMIX_req_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   /* FIXME: reimplement this function using SIMIX when we have a good PID.
313    * In the meantime, I guess nobody uses it so it should not break anything. */
314   THROW_UNIMPLEMENTED;
315 }
316
317 /** \ingroup m_process_management
318  * \brief Returns the process ID of \a process.
319  *
320  * This function 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 called by the logs
326    * and the exceptions, so it would be called back again and again */
327   if (process == NULL) {
328     return 0;
329   }
330
331   simdata_process_t simdata = SIMIX_req_process_get_data(process);
332
333   return simdata != NULL ? simdata->PID : 0;
334 }
335
336 /** \ingroup m_process_management
337  * \brief Returns the process ID of the parent of \a process.
338  *
339  * This function checks whether \a process is a valid pointer or not
340    and return its PID. Returns -1 if the agent has not been created by
341    another agent.
342  */
343 int MSG_process_get_PPID(m_process_t process)
344 {
345   xbt_assert(process != NULL, "Invalid parameter");
346
347   simdata_process_t simdata = SIMIX_req_process_get_data(process);
348
349   return simdata->PPID;
350 }
351
352 /** \ingroup m_process_management
353  * \brief Return the name of an agent.
354  *
355  * This function checks whether \a process is a valid pointer or not
356    and return its name.
357  */
358 const char *MSG_process_get_name(m_process_t process)
359 {
360   xbt_assert(process, "Invalid parameter");
361
362   return SIMIX_req_process_get_name(process);
363 }
364
365 /** \ingroup m_process_management
366  * \brief Returns the value of a given process property
367  *
368  * \param process a process
369  * \param name a property name
370  * \return value of a property (or NULL if the property is not set)
371  */
372 const char *MSG_process_get_property_value(m_process_t process,
373                                            const char *name)
374 {
375   return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
376 }
377
378 /** \ingroup m_process_management
379  * \brief Return the list of properties
380  *
381  * This function returns all the parameters associated with a process
382  */
383 xbt_dict_t MSG_process_get_properties(m_process_t process)
384 {
385   xbt_assert(process != NULL, "Invalid parameter");
386
387   return SIMIX_req_process_get_properties(process);
388
389 }
390
391 /** \ingroup m_process_management
392  * \brief Return the PID of the current agent.
393  *
394  * This function returns the PID of the currently running #m_process_t.
395  */
396 int MSG_process_self_PID(void)
397 {
398   return MSG_process_get_PID(MSG_process_self());
399 }
400
401 /** \ingroup m_process_management
402  * \brief Return the PPID of the current agent.
403  *
404  * This function returns the PID of the parent of the currently
405  * running #m_process_t.
406  */
407 int MSG_process_self_PPID(void)
408 {
409   return MSG_process_get_PPID(MSG_process_self());
410 }
411
412 /** \ingroup m_process_management
413  * \brief Return the current process.
414  *
415  * This function returns the currently running #m_process_t.
416  */
417 m_process_t MSG_process_self(void)
418 {
419   return SIMIX_process_self();
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_assert(process != NULL, "Invalid parameter");
431   CHECK_HOST();
432
433 #ifdef HAVE_TRACING
434   TRACE_msg_process_suspend(process);
435 #endif
436
437   SIMIX_req_process_suspend(process);
438   MSG_RETURN(MSG_OK);
439 }
440
441 /** \ingroup m_process_management
442  * \brief Resume a suspended process.
443  *
444  * This function resumes a suspended process by resuming the task on
445  * which it was waiting for the completion.
446  */
447 MSG_error_t MSG_process_resume(m_process_t process)
448 {
449   xbt_assert(process != NULL, "Invalid parameter");
450   CHECK_HOST();
451
452 #ifdef HAVE_TRACING
453   TRACE_msg_process_resume(process);
454 #endif
455
456   SIMIX_req_process_resume(process);
457   MSG_RETURN(MSG_OK);
458 }
459
460 /** \ingroup m_process_management
461  * \brief Returns true if the process is suspended .
462  *
463  * This checks whether a process is suspended or not by inspecting the
464  * task on which it was waiting for the completion.
465  */
466 int MSG_process_is_suspended(m_process_t process)
467 {
468   xbt_assert(process != NULL, "Invalid parameter");
469   return SIMIX_req_process_is_suspended(process);
470 }
471
472 smx_context_t MSG_process_get_smx_ctx(m_process_t process) {
473   return SIMIX_process_get_context(process);
474 }