Logo AND Algorithmique Numérique Distribuée

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