Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d36c64ab44d3529267cbaa0e2b271d78d208e18e
[simgrid.git] / src / msg / m_process.c
1 /*     $Id$      */
2
3 /* Copyright (c) 2002-2007 Arnaud Legrand.                                  */
4 /* Copyright (c) 2007 Bruno Donassolo.                                      */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
15                                 "Logging specific to MSG (process)");
16
17 /** \defgroup m_process_management Management Functions of Agents
18  *  \brief This section describes the agent structure of MSG
19  *  (#m_process_t) and the functions for managing it.
20  */
21 /** @addtogroup m_process_management
22  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
23  * 
24  *  We need to simulate many independent scheduling decisions, so
25  *  the concept of <em>process</em> is at the heart of the
26  *  simulator. A process may be defined as a <em>code</em>, with
27  *  some <em>private data</em>, executing in a <em>location</em>.
28  *  \see m_process_t
29  */
30
31 /******************************** Process ************************************/
32 void __MSG_process_cleanup(void *arg)
33 {
34   /* arg is a pointer to a simix process, we can get the msg process with the field data */
35   m_process_t proc = ((smx_process_t) arg)->data;
36   xbt_fifo_remove(msg_global->process_list, proc);
37   SIMIX_process_cleanup(arg);
38   free(proc->name);
39   proc->name = NULL;
40   free(proc->simdata);
41   proc->simdata = NULL;
42   free(proc);
43
44   return;
45 }
46
47 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
48 void *_MSG_process_create_from_SIMIX(const char *name,
49                                      xbt_main_func_t code, void *data,
50                                      char *hostname, int argc, char **argv, xbt_dict_t properties)
51 {
52   m_host_t host = MSG_get_host_by_name(hostname);
53   return (void *) MSG_process_create_with_environment(name, code, data, host,
54                                                       argc, argv,properties);
55 }
56
57 /** \ingroup m_process_management
58  * \brief Creates and runs a new #m_process_t.
59  *
60  * Does exactly the same as #MSG_process_create_with_arguments but without 
61    providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
62  * \sa MSG_process_create_with_arguments
63  */
64 m_process_t MSG_process_create(const char *name,
65                                xbt_main_func_t code, void *data,
66                                m_host_t host)
67 {
68   return MSG_process_create_with_environment(name, code, data, host, -1,
69                                              NULL,NULL);
70 }
71
72 /** \ingroup m_process_management
73  * \brief Creates and runs a new #m_process_t.
74
75  * A constructor for #m_process_t taking four arguments and returning the 
76  * corresponding object. The structure (and the corresponding thread) is
77  * created, and put in the list of ready process.
78  * \param name a name for the object. It is for user-level information
79    and can be NULL.
80  * \param code is a function describing the behavior of the agent. It
81    should then only use functions described in \ref
82    m_process_management (to create a new #m_process_t for example),
83    in \ref m_host_management (only the read-only functions i.e. whose
84    name contains the word get), in \ref m_task_management (to create
85    or destroy some #m_task_t for example) and in \ref
86    msg_gos_functions (to handle file transfers and task processing).
87  * \param data a pointer to any data one may want to attach to the new
88    object.  It is for user-level information and can be NULL. It can
89    be retrieved with the function \ref MSG_process_get_data.
90  * \param host the location where the new agent is executed.
91  * \param argc first argument passed to \a code
92  * \param argv second argument passed to \a code
93  * \see m_process_t
94  * \return The new corresponding object.
95  */
96
97 m_process_t MSG_process_create_with_arguments(const char *name,
98                                               xbt_main_func_t code,
99                                               void *data, m_host_t host,
100                                               int argc, char **argv)
101 {
102   return MSG_process_create_with_environment(name, code, data, host, 
103                                              argc,argv,NULL);
104 }
105
106 /** \ingroup m_process_management
107  * \brief Creates and runs a new #m_process_t.
108
109  * A constructor for #m_process_t taking four arguments and returning the 
110  * corresponding object. The structure (and the corresponding thread) is
111  * created, and put in the list of ready process.
112  * \param name a name for the object. It is for user-level information
113    and can be NULL.
114  * \param code is a function describing the behavior of the agent. It
115    should then only use functions described in \ref
116    m_process_management (to create a new #m_process_t for example),
117    in \ref m_host_management (only the read-only functions i.e. whose
118    name contains the word get), in \ref m_task_management (to create
119    or destroy some #m_task_t for example) and in \ref
120    msg_gos_functions (to handle file transfers and task processing).
121  * \param data a pointer to any data one may want to attach to the new
122    object.  It is for user-level information and can be NULL. It can
123    be retrieved with the function \ref MSG_process_get_data.
124  * \param host the location where the new agent is executed.
125  * \param argc first argument passed to \a code
126  * \param argv second argument passed to \a code
127  * \param properties list a properties defined for this process
128  * \see m_process_t
129  * \return The new corresponding object.
130  */
131 m_process_t MSG_process_create_with_environment(const char *name,
132                                               xbt_main_func_t code,
133                                               void *data, m_host_t host,
134                                               int argc, char **argv, xbt_dict_t properties)
135 {
136   simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
137   m_process_t process = xbt_new0(s_m_process_t, 1);
138   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
139
140   /* Simulator Data */
141   simdata->PID = msg_global->PID++;
142   simdata->waiting_task = NULL;
143   simdata->m_host = host;
144   simdata->argc = argc;
145   simdata->argv = argv;
146   simdata->s_process = SIMIX_process_create(name, code,
147                                             (void *) process, host->name,
148                                             argc, argv, properties);
149
150   if (SIMIX_process_self()) {
151     simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
152   } else {
153     simdata->PPID = -1;
154   }
155   simdata->last_errno = MSG_OK;
156
157
158   /* Process structure */
159   process->name = xbt_strdup(name);
160   process->simdata = simdata;
161   process->data = data;
162
163   xbt_fifo_unshift(msg_global->process_list, process);
164
165   return process;
166
167 }
168
169 void _MSG_process_kill_from_SIMIX(void *p)
170 {
171   MSG_process_kill((m_process_t) p);
172 }
173
174 /** \ingroup m_process_management
175  * \param process poor victim
176  *
177  * This function simply kills a \a process... scarry isn't it ? :)
178  */
179 void MSG_process_kill(m_process_t process)
180 {
181   simdata_process_t p_simdata = process->simdata;
182
183   DEBUG3("Killing %s(%d) on %s",
184          process->name, p_simdata->PID, p_simdata->m_host->name);
185
186   if (p_simdata->waiting_task) {
187     DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
188     if (p_simdata->waiting_task->simdata->compute) {
189       SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
190     } else if (p_simdata->waiting_task->simdata->comm) {
191       SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
192     }
193   }
194
195   xbt_fifo_remove(msg_global->process_list, process);
196   SIMIX_process_kill(process->simdata->s_process);
197
198   return;
199 }
200
201 /** \ingroup m_process_management
202  * \brief Migrates an agent to another location.
203  *
204  * This functions checks whether \a process and \a host are valid pointers
205    and change the value of the #m_host_t on which \a process is running.
206  */
207 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
208 {
209   xbt_die
210       ("MSG_process_change_host - not implemented yet - maybe useless function");
211   return MSG_OK;
212 }
213
214 /** \ingroup m_process_management
215  * \brief Return the user data of a #m_process_t.
216  *
217  * This functions checks whether \a process is a valid pointer or not 
218    and return the user data associated to \a process if it is possible.
219  */
220 void *MSG_process_get_data(m_process_t process)
221 {
222   xbt_assert0((process != NULL), "Invalid parameters");
223
224   return (process->data);
225 }
226
227 /** \ingroup m_process_management
228  * \brief Set the user data of a #m_process_t.
229  *
230  * This functions checks whether \a process is a valid pointer or not 
231    and set the user data associated to \a process if it is possible.
232  */
233 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
234 {
235   xbt_assert0((process != NULL), "Invalid parameters");
236   xbt_assert0((process->data == NULL), "Data already set");
237
238   process->data = data;
239
240   return MSG_OK;
241 }
242
243 /** \ingroup m_process_management
244  * \brief Return the location on which an agent is running.
245  *
246  * This functions checks whether \a process is a valid pointer or not 
247    and return the m_host_t corresponding to the location on which \a 
248    process is running.
249  */
250 m_host_t MSG_process_get_host(m_process_t process)
251 {
252   xbt_assert0(((process != NULL)
253                && (process->simdata)), "Invalid parameters");
254
255   return (((simdata_process_t) process->simdata)->m_host);
256 }
257
258 /** \ingroup m_process_management
259  *
260  * \brief Return a #m_process_t given its PID.
261  *
262  * This functions search in the list of all the created m_process_t for a m_process_t 
263    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
264    Note that the PID are uniq in the whole simulation, not only on a given host.
265  */
266 m_process_t MSG_process_from_PID(int PID)
267 {
268   xbt_fifo_item_t i = NULL;
269   m_process_t process = NULL;
270
271   xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
272     if (MSG_process_get_PID(process) == PID)
273       return process;
274   }
275   return NULL;
276 }
277
278 /** \ingroup m_process_management
279  * \brief Returns the process ID of \a process.
280  *
281  * This functions checks whether \a process is a valid pointer or not 
282    and return its PID (or 0 in case of problem).
283  */
284 int MSG_process_get_PID(m_process_t process)
285 {
286   /* Do not raise an exception here: this function is used in the logs, 
287      and it will be called back by the exception handling stuff */
288   if (process == NULL || process->simdata == NULL)
289     return 0;
290
291   return (((simdata_process_t) process->simdata)->PID);
292 }
293
294 /** \ingroup m_process_management
295  * \brief Returns the process ID of the parent of \a process.
296  *
297  * This functions checks whether \a process is a valid pointer or not 
298    and return its PID. Returns -1 if the agent has not been created by 
299    another agent.
300  */
301 int MSG_process_get_PPID(m_process_t process)
302 {
303   xbt_assert0(((process != NULL)
304                && (process->simdata)), "Invalid parameters");
305
306   return (((simdata_process_t) process->simdata)->PPID);
307 }
308
309 /** \ingroup m_process_management
310  * \brief Return the name of an agent.
311  *
312  * This functions checks whether \a process is a valid pointer or not 
313    and return its name.
314  */
315 const char *MSG_process_get_name(m_process_t process)
316 {
317   xbt_assert0(((process != NULL)
318                && (process->simdata)), "Invalid parameters");
319
320   return (process->name);
321 }
322
323 /** \ingroup m_process_management
324  * \brief Returns the value of a given process property
325  *
326  * \param process a process
327  * \param name a property name
328  * \return value of a property (or NULL if the property is not set)
329  */
330 const char* MSG_process_get_property_value(m_process_t process, const char* name)
331 {
332   return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
333 }
334
335 /** \ingroup m_process_management
336  * \brief Return the list of properties
337  *
338  * This functions returns all the parameters associated with a process
339  */
340 xbt_dict_t MSG_process_get_properties(m_process_t process)
341 {
342    xbt_assert0((process != NULL), "Invalid parameters");
343
344   return (SIMIX_process_get_properties(((simdata_process_t)process->simdata)->s_process));
345
346 }
347
348 /** \ingroup m_process_management
349  * \brief Return the PID of the current agent.
350  *
351  * This functions returns the PID of the currently running #m_process_t.
352  */
353 int MSG_process_self_PID(void)
354 {
355   return (MSG_process_get_PID(MSG_process_self()));
356 }
357
358 /** \ingroup m_process_management
359  * \brief Return the PPID of the current agent.
360  *
361  * This functions returns the PID of the parent of the currently
362  * running #m_process_t.
363  */
364 int MSG_process_self_PPID(void)
365 {
366   return (MSG_process_get_PPID(MSG_process_self()));
367 }
368
369 /** \ingroup m_process_management
370  * \brief Return the current agent.
371  *
372  * This functions returns the currently running #m_process_t.
373  */
374 m_process_t MSG_process_self(void)
375 {
376   smx_process_t proc = SIMIX_process_self();
377   if (proc != NULL) {
378     return (m_process_t) proc->data;
379   } else {
380     return NULL;
381   }
382
383 }
384
385 /** \ingroup m_process_management
386  * \brief Suspend the process.
387  *
388  * This functions suspend the process by suspending the task on which
389  * it was waiting for the completion.
390  */
391 MSG_error_t MSG_process_suspend(m_process_t process)
392 {
393   xbt_assert0(((process != NULL)
394                && (process->simdata)), "Invalid parameters");
395   CHECK_HOST();
396
397   SIMIX_process_suspend(process->simdata->s_process);
398   MSG_RETURN(MSG_OK);
399 }
400
401 /** \ingroup m_process_management
402  * \brief Resume a suspended process.
403  *
404  * This functions resume a suspended process by resuming the task on
405  * which it was waiting for the completion.
406  */
407 MSG_error_t MSG_process_resume(m_process_t process)
408 {
409
410   xbt_assert0(((process != NULL)
411                && (process->simdata)), "Invalid parameters");
412   CHECK_HOST();
413
414   SIMIX_process_resume(process->simdata->s_process);
415   MSG_RETURN(MSG_OK);
416 }
417
418 /** \ingroup m_process_management
419  * \brief Returns true if the process is suspended .
420  *
421  * This checks whether a process is suspended or not by inspecting the
422  * task on which it was waiting for the completion.
423  */
424 int MSG_process_is_suspended(m_process_t process)
425 {
426   xbt_assert0(((process != NULL)
427                && (process->simdata)), "Invalid parameters");
428   return SIMIX_process_is_suspended(process->simdata->s_process);
429 }