Logo AND Algorithmique Numérique Distribuée

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