Logo AND Algorithmique Numérique Distribuée

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