Logo AND Algorithmique Numérique Distribuée

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