Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ba8115e874259d2df8a17eb4d2d0055db717f9b4
[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
32   m_process_t proc;
33
34   if (smx_proc == SIMIX_process_self()) {
35     /* avoid a SIMIX request if this function is called by the process itself */
36     proc = SIMIX_process_self_get_data();
37   }
38   else {
39     proc = SIMIX_req_process_get_data(smx_proc);
40   }
41
42 #ifdef HAVE_TRACING
43   TRACE_msg_process_end(proc);
44 #endif
45
46   if (msg_global)
47     xbt_swag_remove(proc, msg_global->process_list);
48
49   SIMIX_req_process_cleanup(smx_proc);
50
51   if (proc->name) {
52     free(proc->name);
53     proc->name = NULL;
54   }
55   if (proc->simdata) {
56     free(proc->simdata);
57     proc->simdata = NULL;
58   }
59   free(proc);
60
61   return;
62 }
63
64 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
65 void _MSG_process_create_from_SIMIX(void* process, const char *name,
66                                     xbt_main_func_t code, void *data,
67                                     char *hostname, int argc, char **argv,
68                                     xbt_dict_t properties)
69 {
70   m_host_t host = MSG_get_host_by_name(hostname);
71   m_process_t p = MSG_process_create_with_environment(name, code, data,
72                                                       host, argc, argv,
73                                                       properties);
74   *((m_process_t*) process) = p;
75 }
76
77 /** \ingroup m_process_management
78  * \brief Creates and runs a new #m_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 m_process_t MSG_process_create(const char *name,
85                                xbt_main_func_t code, void *data,
86                                m_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 #m_process_t.
94
95  * A constructor for #m_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 agent. It
101    should then only use functions described in \ref
102    m_process_management (to create a new #m_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 #m_task_t for example) and in \ref
106    msg_gos_functions (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 agent is executed.
111  * \param argc first argument passed to \a code
112  * \param argv second argument passed to \a code
113  * \see m_process_t
114  * \return The new corresponding object.
115  */
116
117 m_process_t MSG_process_create_with_arguments(const char *name,
118                                               xbt_main_func_t code,
119                                               void *data, m_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 #m_process_t.
128
129  * A constructor for #m_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 agent. It
135    should then only use functions described in \ref
136    m_process_management (to create a new #m_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 #m_task_t for example) and in \ref
140    msg_gos_functions (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 agent is executed.
145  * \param argc first argument passed to \a code
146  * \param argv second argument passed to \a code
147  * \param properties list a properties defined for this process
148  * \see m_process_t
149  * \return The new corresponding object.
150  */
151 m_process_t MSG_process_create_with_environment(const char *name,
152                                                 xbt_main_func_t code,
153                                                 void *data, m_host_t host,
154                                                 int argc, char **argv,
155                                                 xbt_dict_t properties)
156 {
157   simdata_process_t simdata = NULL;
158   m_process_t process = xbt_new0(s_m_process_t, 1);
159   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
160
161   simdata = xbt_new0(s_simdata_process_t, 1);
162
163   /* Simulator Data */
164   simdata->PID = msg_global->PID++;
165   simdata->waiting_action = NULL;
166   simdata->waiting_task = NULL;
167   simdata->m_host = host;
168   simdata->argc = argc;
169   simdata->argv = argv;
170
171   if (SIMIX_process_self()) {
172     simdata->PPID = MSG_process_get_PID(SIMIX_process_self_get_data());
173   } else {
174     simdata->PPID = -1;
175   }
176   simdata->last_errno = MSG_OK;
177
178   /* Process structure */
179   process->name = xbt_strdup(name);
180   process->simdata = simdata;
181   process->data = data;
182   xbt_swag_insert(process, msg_global->process_list);
183
184 #ifdef HAVE_TRACING
185   TRACE_msg_process_create (process);
186 #endif
187
188   /* Let's create the process: SIMIX may decide to start it right now,
189    * even before returning the flow control to us */
190   SIMIX_req_process_create(&simdata->s_process, name, code, (void *) process, host->name,
191                            argc, argv, properties);
192
193   if (!simdata->s_process) {
194     /* Undo everything we have just changed */
195     msg_global->PID--;
196     xbt_swag_remove(process, msg_global->process_list);
197     xbt_free(process->name);
198     xbt_free(process);
199     xbt_free(simdata);
200     return NULL;
201   }
202
203   return process;
204 }
205
206 void _MSG_process_kill_from_SIMIX(void *p)
207 {
208 #ifdef HAVE_TRACING
209   TRACE_msg_process_kill((m_process_t) p);
210 #endif
211   MSG_process_kill((m_process_t) p);
212 }
213
214 /** \ingroup m_process_management
215  * \param process poor victim
216  *
217  * This function simply kills a \a process... scarry isn't it ? :)
218  */
219 void MSG_process_kill(m_process_t process)
220 {
221   simdata_process_t p_simdata = process->simdata;
222
223 #ifdef HAVE_TRACING
224   TRACE_msg_process_kill(process);
225 #endif
226
227   XBT_DEBUG("Killing %s(%d) on %s",
228          process->name, p_simdata->PID, p_simdata->m_host->name);
229
230   if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
231     SIMIX_req_comm_cancel(p_simdata->waiting_task->simdata->comm);
232   }
233  
234   xbt_swag_remove(process, msg_global->process_list);
235   SIMIX_req_process_kill(process->simdata->s_process);
236
237   return;
238 }
239
240 /** \ingroup m_process_management
241  * \brief Migrates an agent to another location.
242  *
243  * This function checks whether \a process and \a host are valid pointers
244    and change the value of the #m_host_t on which \a process is running.
245  */
246 MSG_error_t MSG_process_change_host(m_host_t host)
247 {
248   m_process_t process = MSG_process_self();
249   m_host_t now = process->simdata->m_host;
250   process->simdata->m_host = host;
251 #ifdef HAVE_TRACING
252   TRACE_msg_process_change_host(process, now, host);
253 #endif
254   SIMIX_req_process_change_host(process->simdata->s_process, now->name,
255                             host->name);
256   return MSG_OK;
257 }
258
259 /** \ingroup m_process_management
260  * \brief Return the user data of a #m_process_t.
261  *
262  * This function checks whether \a process is a valid pointer or not
263    and return the user data associated to \a process if it is possible.
264  */
265 void *MSG_process_get_data(m_process_t process)
266 {
267   xbt_assert0((process != NULL), "Invalid parameters");
268
269   return (process->data);
270 }
271
272 /** \ingroup m_process_management
273  * \brief Set the user data of a #m_process_t.
274  *
275  * This function checks whether \a process is a valid pointer or not
276    and set the user data associated to \a process if it is possible.
277  */
278 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
279 {
280   xbt_assert0((process != NULL), "Invalid parameters");
281
282   process->data = data;
283
284   return MSG_OK;
285 }
286
287 /** \ingroup m_process_management
288  * \brief Return the location on which an agent is running.
289  *
290  * This function checks whether \a process is a valid pointer or not
291    and return the m_host_t corresponding to the location on which \a
292    process is running.
293  */
294 m_host_t MSG_process_get_host(m_process_t process)
295 {
296   xbt_assert0(((process != NULL)
297                && (process->simdata)), "Invalid parameters");
298
299   return (((simdata_process_t) process->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   m_process_t process = NULL;
313
314   xbt_swag_foreach(process, msg_global->process_list) {
315     if (MSG_process_get_PID(process) == PID)
316       return process;
317   }
318   return NULL;
319 }
320
321 /** \ingroup m_process_management
322  * \brief Returns the process ID of \a process.
323  *
324  * This function checks whether \a process is a valid pointer or not
325    and return its PID (or 0 in case of problem).
326  */
327 int MSG_process_get_PID(m_process_t process)
328 {
329   /* Do not raise an exception here: this function is used in the logs,
330      and it will be called back by the exception handling stuff */
331   if (process == NULL || process->simdata == NULL)
332     return 0;
333
334   return (((simdata_process_t) process->simdata)->PID);
335 }
336
337 /** \ingroup m_process_management
338  * \brief Returns the process ID of the parent of \a process.
339  *
340  * This function checks whether \a process is a valid pointer or not
341    and return its PID. Returns -1 if the agent has not been created by
342    another agent.
343  */
344 int MSG_process_get_PPID(m_process_t process)
345 {
346   xbt_assert0(((process != NULL)
347                && (process->simdata)), "Invalid parameters");
348
349   return (((simdata_process_t) process->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_assert0(process, "Invalid parameter: process is NULL");
361   xbt_assert0(process->simdata,
362               "Invalid parameter: process->simdata is NULL");
363
364   return (process->name);
365 }
366
367 /** \ingroup m_process_management
368  * \brief Returns the value of a given process property
369  *
370  * \param process a process
371  * \param name a property name
372  * \return value of a property (or NULL if the property is not set)
373  */
374 const char *MSG_process_get_property_value(m_process_t process,
375                                            const char *name)
376 {
377   return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
378 }
379
380 /** \ingroup m_process_management
381  * \brief Return the list of properties
382  *
383  * This function returns all the parameters associated with a process
384  */
385 xbt_dict_t MSG_process_get_properties(m_process_t process)
386 {
387   xbt_assert0((process != NULL), "Invalid parameters");
388
389   return (SIMIX_req_process_get_properties
390           (((simdata_process_t) process->simdata)->s_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 agent.
417  *
418  * This function returns the currently running #m_process_t.
419  */
420 m_process_t MSG_process_self(void)
421 {
422   /* we cannot make a SIMIX request here because this may create an exception or a logging
423      event, and both mechanisms call MSG_process_self() again (via xbt_getpid()) */
424   return (m_process_t) SIMIX_process_self_get_data();
425 }
426
427 /** \ingroup m_process_management
428  * \brief Suspend the process.
429  *
430  * This function suspends the process by suspending the task on which
431  * it was waiting for the completion.
432  */
433 MSG_error_t MSG_process_suspend(m_process_t process)
434 {
435   xbt_assert0(((process != NULL)
436                && (process->simdata)), "Invalid parameters");
437   CHECK_HOST();
438
439 #ifdef HAVE_TRACING
440   TRACE_msg_process_suspend(process);
441 #endif
442
443   SIMIX_req_process_suspend(process->simdata->s_process);
444   MSG_RETURN(MSG_OK);
445 }
446
447 /** \ingroup m_process_management
448  * \brief Resume a suspended process.
449  *
450  * This function resumes a suspended process by resuming the task on
451  * which it was waiting for the completion.
452  */
453 MSG_error_t MSG_process_resume(m_process_t process)
454 {
455
456   xbt_assert0(((process != NULL)
457                && (process->simdata)), "Invalid parameters");
458   CHECK_HOST();
459
460 #ifdef HAVE_TRACING
461   TRACE_msg_process_resume(process);
462 #endif
463
464   SIMIX_req_process_resume(process->simdata->s_process);
465   MSG_RETURN(MSG_OK);
466 }
467
468 /** \ingroup m_process_management
469  * \brief Returns true if the process is suspended .
470  *
471  * This checks whether a process is suspended or not by inspecting the
472  * task on which it was waiting for the completion.
473  */
474 int MSG_process_is_suspended(m_process_t process)
475 {
476   xbt_assert0(((process != NULL)
477                && (process->simdata)), "Invalid parameters");
478   return SIMIX_req_process_is_suspended(process->simdata->s_process);
479 }
480
481
482 smx_context_t MSG_process_get_smx_ctx(m_process_t process) {
483   return SIMIX_process_get_context(process->simdata->s_process);
484 }