Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill the useless _MSG_process_create_with_env_from_SIMIX(). _MSG_process_create_from_...
[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 /** \ingroup m_process_management
33  * \brief Creates and runs a new #m_process_t.
34  *
35  * Does exactly the same as #MSG_process_create_with_arguments but without 
36    providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
37  * \sa MSG_process_create_with_arguments
38  */
39 m_process_t MSG_process_create(const char *name,
40                                xbt_main_func_t code, void *data,
41                                m_host_t host)
42 {
43   return MSG_process_create_with_arguments(name, code, data, host, -1,
44                                            NULL);
45 }
46
47 void __MSG_process_cleanup(void *arg)
48 {
49   /* arg is a pointer to a simix process, we can get the msg process with the field data */
50   m_process_t proc = ((smx_process_t) arg)->data;
51   xbt_fifo_remove(msg_global->process_list, proc);
52   SIMIX_process_cleanup(arg);
53   free(proc->name);
54   proc->name = NULL;
55   free(proc->simdata);
56   proc->simdata = NULL;
57   free(proc);
58
59   return;
60 }
61
62 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
63 void *_MSG_process_create_from_SIMIX(const char *name,
64                                      xbt_main_func_t code, void *data,
65                                      char *hostname, int argc, char **argv, xbt_dict_t properties)
66 {
67   m_host_t host = MSG_get_host_by_name(hostname);
68   return (void *) MSG_process_create_with_environment(name, code, data, host,
69                                                       argc, argv,properties);
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   simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
103   m_process_t process = xbt_new0(s_m_process_t, 1);
104   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
105
106   /* Simulator Data */
107   simdata->PID = msg_global->PID++;
108   simdata->waiting_task = NULL;
109   simdata->m_host = host;
110   simdata->argc = argc;
111   simdata->argv = argv;
112   simdata->s_process = SIMIX_process_create(name, code,
113                                             (void *) process, host->name,
114                                             argc, argv, NULL);
115
116   if (SIMIX_process_self()) {
117     simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
118   } else {
119     simdata->PPID = -1;
120   }
121   simdata->last_errno = MSG_OK;
122
123
124   /* Process structure */
125   process->name = xbt_strdup(name);
126   process->simdata = simdata;
127   process->data = data ;
128
129   xbt_fifo_unshift(msg_global->process_list, process);
130
131   return process;
132 }
133
134 /** \ingroup m_process_management
135  * \brief Creates and runs a new #m_process_t.
136
137  * A constructor for #m_process_t taking four arguments and returning the 
138  * corresponding object. The structure (and the corresponding thread) is
139  * created, and put in the list of ready process.
140  * \param name a name for the object. It is for user-level information
141    and can be NULL.
142  * \param code is a function describing the behavior of the agent. It
143    should then only use functions described in \ref
144    m_process_management (to create a new #m_process_t for example),
145    in \ref m_host_management (only the read-only functions i.e. whose
146    name contains the word get), in \ref m_task_management (to create
147    or destroy some #m_task_t for example) and in \ref
148    msg_gos_functions (to handle file transfers and task processing).
149  * \param data a pointer to any data one may want to attach to the new
150    object.  It is for user-level information and can be NULL. It can
151    be retrieved with the function \ref MSG_process_get_data.
152  * \param host the location where the new agent is executed.
153  * \param argc first argument passed to \a code
154  * \param argv second argument passed to \a code
155  * \param properties list a properties defined for this process
156  * \see m_process_t
157  * \return The new corresponding object.
158  */
159 m_process_t MSG_process_create_with_environment(const char *name,
160                                               xbt_main_func_t code,
161                                               void *data, m_host_t host,
162                                               int argc, char **argv, xbt_dict_t properties)
163 {
164   simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
165   m_process_t process = xbt_new0(s_m_process_t, 1);
166   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
167
168   /* Simulator Data */
169   simdata->PID = msg_global->PID++;
170   simdata->waiting_task = NULL;
171   simdata->m_host = host;
172   simdata->argc = argc;
173   simdata->argv = argv;
174   simdata->s_process = SIMIX_process_create(name, code,
175                                             (void *) process, host->name,
176                                             argc, argv, properties);
177
178   if (SIMIX_process_self()) {
179     simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
180   } else {
181     simdata->PPID = -1;
182   }
183   simdata->last_errno = MSG_OK;
184
185
186   /* Process structure */
187   process->name = xbt_strdup(name);
188   process->simdata = simdata;
189   process->data = data ;
190
191   xbt_fifo_unshift(msg_global->process_list, process);
192
193   return process;
194
195 }
196
197 void _MSG_process_kill_from_SIMIX(void *p)
198 {
199   MSG_process_kill((m_process_t) p);
200 }
201
202 /** \ingroup m_process_management
203  * \param process poor victim
204  *
205  * This function simply kills a \a process... scarry isn't it ? :)
206  */
207 void MSG_process_kill(m_process_t process)
208 {
209   simdata_process_t p_simdata = process->simdata;
210
211   DEBUG3("Killing %s(%d) on %s",
212          process->name, p_simdata->PID, p_simdata->m_host->name);
213
214   if (p_simdata->waiting_task) {
215     DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
216     if (p_simdata->waiting_task->simdata->compute) {
217       SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
218     } else if (p_simdata->waiting_task->simdata->comm) {
219       SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
220     }
221   }
222
223   xbt_fifo_remove(msg_global->process_list, process);
224   SIMIX_process_kill(process->simdata->s_process);
225
226   return;
227 }
228
229 /** \ingroup m_process_management
230  * \brief Migrates an agent to another location.
231  *
232  * This functions checks whether \a process and \a host are valid pointers
233    and change the value of the #m_host_t on which \a process is running.
234  */
235 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
236 {
237   xbt_die
238       ("MSG_process_change_host - not implemented yet - maybe useless function");
239   return MSG_OK;
240 }
241
242 /** \ingroup m_process_management
243  * \brief Return the user data of a #m_process_t.
244  *
245  * This functions checks whether \a process is a valid pointer or not 
246    and return the user data associated to \a process if it is possible.
247  */
248 void *MSG_process_get_data(m_process_t process)
249 {
250   xbt_assert0((process != NULL), "Invalid parameters");
251
252   return (process->data);
253 }
254
255 /** \ingroup m_process_management
256  * \brief Set the user data of a #m_process_t.
257  *
258  * This functions checks whether \a process is a valid pointer or not 
259    and set the user data associated to \a process if it is possible.
260  */
261 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
262 {
263   xbt_assert0((process != NULL), "Invalid parameters");
264   xbt_assert0((process->data == NULL), "Data already set");
265
266   process->data = data;
267
268   return MSG_OK;
269 }
270
271 /** \ingroup m_process_management
272  * \brief Return the location on which an agent is running.
273  *
274  * This functions checks whether \a process is a valid pointer or not 
275    and return the m_host_t corresponding to the location on which \a 
276    process is running.
277  */
278 m_host_t MSG_process_get_host(m_process_t process)
279 {
280   xbt_assert0(((process != NULL)
281                && (process->simdata)), "Invalid parameters");
282
283   return (((simdata_process_t) process->simdata)->m_host);
284 }
285
286 /** \ingroup m_process_management
287  *
288  * \brief Return a #m_process_t given its PID.
289  *
290  * This functions search in the list of all the created m_process_t for a m_process_t 
291    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
292    Note that the PID are uniq in the whole simulation, not only on a given host.
293  */
294 m_process_t MSG_process_from_PID(int PID)
295 {
296   xbt_fifo_item_t i = NULL;
297   m_process_t process = NULL;
298
299   xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
300     if (MSG_process_get_PID(process) == PID)
301       return process;
302   }
303   return NULL;
304 }
305
306 /** \ingroup m_process_management
307  * \brief Returns the process ID of \a process.
308  *
309  * This functions checks whether \a process is a valid pointer or not 
310    and return its PID (or 0 in case of problem).
311  */
312 int MSG_process_get_PID(m_process_t process)
313 {
314   /* Do not raise an exception here: this function is used in the logs, 
315      and it will be called back by the exception handling stuff */
316   if (process == NULL || process->simdata == NULL)
317     return 0;
318
319   return (((simdata_process_t) process->simdata)->PID);
320 }
321
322 /** \ingroup m_process_management
323  * \brief Returns the process ID of the parent of \a process.
324  *
325  * This functions checks whether \a process is a valid pointer or not 
326    and return its PID. Returns -1 if the agent has not been created by 
327    another agent.
328  */
329 int MSG_process_get_PPID(m_process_t process)
330 {
331   xbt_assert0(((process != NULL)
332                && (process->simdata)), "Invalid parameters");
333
334   return (((simdata_process_t) process->simdata)->PPID);
335 }
336
337 /** \ingroup m_process_management
338  * \brief Return the name of an agent.
339  *
340  * This functions checks whether \a process is a valid pointer or not 
341    and return its name.
342  */
343 const char *MSG_process_get_name(m_process_t process)
344 {
345   xbt_assert0(((process != NULL)
346                && (process->simdata)), "Invalid parameters");
347
348   return (process->name);
349 }
350
351 /** \ingroup m_process_management
352  * \brief Returns the value of a certain process property
353  *
354  * \param process a process
355  * \param name a property name
356  * \return value of a property
357  */
358 const char* MSG_process_get_property_value(m_process_t process, char* name)
359 {
360   return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
361 }
362
363 /** \ingroup m_process_management
364  * \brief Return the list of properties
365  *
366  * This functions returns all the parameters associated with a process
367  */
368 xbt_dict_t MSG_process_get_properties(m_process_t process)
369 {
370    xbt_assert0((process != NULL), "Invalid parameters");
371
372   return (SIMIX_process_get_properties(((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 functions 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 functions 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 functions returns the currently running #m_process_t.
401  */
402 m_process_t MSG_process_self(void)
403 {
404   smx_process_t proc = SIMIX_process_self();
405   if (proc != NULL) {
406     return (m_process_t) proc->data;
407   } else {
408     return NULL;
409   }
410
411 }
412
413 /** \ingroup m_process_management
414  * \brief Suspend the process.
415  *
416  * This functions suspend the process by suspending the task on which
417  * it was waiting for the completion.
418  */
419 MSG_error_t MSG_process_suspend(m_process_t process)
420 {
421   xbt_assert0(((process != NULL)
422                && (process->simdata)), "Invalid parameters");
423   CHECK_HOST();
424
425   SIMIX_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 functions resume 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   SIMIX_process_resume(process->simdata->s_process);
443   MSG_RETURN(MSG_OK);
444 }
445
446 /** \ingroup m_process_management
447  * \brief Returns true if the process is suspended .
448  *
449  * This checks whether a process is suspended or not by inspecting the
450  * task on which it was waiting for the completion.
451  */
452 int MSG_process_is_suspended(m_process_t process)
453 {
454   xbt_assert0(((process != NULL)
455                && (process->simdata)), "Invalid parameters");
456   return SIMIX_process_is_suspended(process->simdata->s_process);
457 }