Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a distraction
[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 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)
66 {
67   m_host_t host = MSG_get_host_by_name(hostname);
68   return (void *) MSG_process_create_with_arguments(name, code, data, host,
69                                                     argc, argv);
70 }
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   simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1);
104   m_process_t process = xbt_new0(s_m_process_t, 1);
105   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
106
107   /* Simulator Data */
108   simdata->PID = msg_global->PID++;
109   simdata->waiting_task = NULL;
110   simdata->m_host = host;
111   simdata->argc = argc;
112   simdata->argv = argv;
113   simdata->s_process = SIMIX_process_create(name, code,
114                                             (void *) process, host->name,
115                                             argc, argv);
116
117   if (SIMIX_process_self()) {
118     simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
119   } else {
120     simdata->PPID = -1;
121   }
122   simdata->last_errno = MSG_OK;
123
124
125   /* Process structure */
126   process->name = xbt_strdup(name);
127   process->simdata = simdata;
128   process->data = data;
129
130   xbt_fifo_unshift(msg_global->process_list, process);
131
132   return process;
133 }
134
135
136 void _MSG_process_kill_from_SIMIX(void *p)
137 {
138   MSG_process_kill((m_process_t) p);
139 }
140
141 /** \ingroup m_process_management
142  * \param process poor victim
143  *
144  * This function simply kills a \a process... scarry isn't it ? :)
145  */
146 void MSG_process_kill(m_process_t process)
147 {
148   simdata_process_t p_simdata = process->simdata;
149
150   DEBUG3("Killing %s(%d) on %s",
151          process->name, p_simdata->PID, p_simdata->m_host->name);
152
153   if (p_simdata->waiting_task) {
154     DEBUG1("Canceling waiting task %s", p_simdata->waiting_task->name);
155     if (p_simdata->waiting_task->simdata->compute) {
156       SIMIX_action_cancel(p_simdata->waiting_task->simdata->compute);
157     } else if (p_simdata->waiting_task->simdata->comm) {
158       SIMIX_action_cancel(p_simdata->waiting_task->simdata->comm);
159     }
160   }
161
162   xbt_fifo_remove(msg_global->process_list, process);
163   SIMIX_process_kill(process->simdata->s_process);
164
165   return;
166 }
167
168 /** \ingroup m_process_management
169  * \brief Migrates an agent to another location.
170  *
171  * This functions checks whether \a process and \a host are valid pointers
172    and change the value of the #m_host_t on which \a process is running.
173  */
174 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
175 {
176   xbt_die
177       ("MSG_process_change_host - not implemented yet - maybe useless function");
178   return MSG_OK;
179 }
180
181 /** \ingroup m_process_management
182  * \brief Return the user data of a #m_process_t.
183  *
184  * This functions checks whether \a process is a valid pointer or not 
185    and return the user data associated to \a process if it is possible.
186  */
187 void *MSG_process_get_data(m_process_t process)
188 {
189   xbt_assert0((process != NULL), "Invalid parameters");
190
191   return (process->data);
192 }
193
194 /** \ingroup m_process_management
195  * \brief Set the user data of a #m_process_t.
196  *
197  * This functions checks whether \a process is a valid pointer or not 
198    and set the user data associated to \a process if it is possible.
199  */
200 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
201 {
202   xbt_assert0((process != NULL), "Invalid parameters");
203   xbt_assert0((process->data == NULL), "Data already set");
204
205   process->data = data;
206
207   return MSG_OK;
208 }
209
210 /** \ingroup m_process_management
211  * \brief Return the location on which an agent is running.
212  *
213  * This functions checks whether \a process is a valid pointer or not 
214    and return the m_host_t corresponding to the location on which \a 
215    process is running.
216  */
217 m_host_t MSG_process_get_host(m_process_t process)
218 {
219   xbt_assert0(((process != NULL)
220                && (process->simdata)), "Invalid parameters");
221
222   return (((simdata_process_t) process->simdata)->m_host);
223 }
224
225 /** \ingroup m_process_management
226  *
227  * \brief Return a #m_process_t given its PID.
228  *
229  * This functions search in the list of all the created m_process_t for a m_process_t 
230    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
231    Note that the PID are uniq in the whole simulation, not only on a given host.
232  */
233 m_process_t MSG_process_from_PID(int PID)
234 {
235   xbt_fifo_item_t i = NULL;
236   m_process_t process = NULL;
237
238   xbt_fifo_foreach(msg_global->process_list, i, process, m_process_t) {
239     if (MSG_process_get_PID(process) == PID)
240       return process;
241   }
242   return NULL;
243 }
244
245 /** \ingroup m_process_management
246  * \brief Returns the process ID of \a process.
247  *
248  * This functions checks whether \a process is a valid pointer or not 
249    and return its PID (or 0 in case of problem).
250  */
251 int MSG_process_get_PID(m_process_t process)
252 {
253   /* Do not raise an exception here: this function is used in the logs, 
254      and it will be called back by the exception handling stuff */
255   if (process == NULL || process->simdata == NULL)
256     return 0;
257
258   return (((simdata_process_t) process->simdata)->PID);
259 }
260
261 /** \ingroup m_process_management
262  * \brief Returns the process ID of the parent of \a process.
263  *
264  * This functions checks whether \a process is a valid pointer or not 
265    and return its PID. Returns -1 if the agent has not been created by 
266    another agent.
267  */
268 int MSG_process_get_PPID(m_process_t process)
269 {
270   xbt_assert0(((process != NULL)
271                && (process->simdata)), "Invalid parameters");
272
273   return (((simdata_process_t) process->simdata)->PPID);
274 }
275
276 /** \ingroup m_process_management
277  * \brief Return the name of an agent.
278  *
279  * This functions checks whether \a process is a valid pointer or not 
280    and return its name.
281  */
282 const char *MSG_process_get_name(m_process_t process)
283 {
284   xbt_assert0(((process != NULL)
285                && (process->simdata)), "Invalid parameters");
286
287   return (process->name);
288 }
289
290 /** \ingroup m_process_management
291  * \brief Return the PID of the current agent.
292  *
293  * This functions returns the PID of the currently running #m_process_t.
294  */
295 int MSG_process_self_PID(void)
296 {
297   return (MSG_process_get_PID(MSG_process_self()));
298 }
299
300 /** \ingroup m_process_management
301  * \brief Return the PPID of the current agent.
302  *
303  * This functions returns the PID of the parent of the currently
304  * running #m_process_t.
305  */
306 int MSG_process_self_PPID(void)
307 {
308   return (MSG_process_get_PPID(MSG_process_self()));
309 }
310
311 /** \ingroup m_process_management
312  * \brief Return the current agent.
313  *
314  * This functions returns the currently running #m_process_t.
315  */
316 m_process_t MSG_process_self(void)
317 {
318   smx_process_t proc = SIMIX_process_self();
319   if (proc != NULL) {
320     return (m_process_t) proc->data;
321   } else {
322     return NULL;
323   }
324
325 }
326
327 /** \ingroup m_process_management
328  * \brief Suspend the process.
329  *
330  * This functions suspend the process by suspending the task on which
331  * it was waiting for the completion.
332  */
333 MSG_error_t MSG_process_suspend(m_process_t process)
334 {
335   xbt_assert0(((process != NULL)
336                && (process->simdata)), "Invalid parameters");
337   CHECK_HOST();
338
339   SIMIX_process_suspend(process->simdata->s_process);
340   MSG_RETURN(MSG_OK);
341 }
342
343 /** \ingroup m_process_management
344  * \brief Resume a suspended process.
345  *
346  * This functions resume a suspended process by resuming the task on
347  * which it was waiting for the completion.
348  */
349 MSG_error_t MSG_process_resume(m_process_t process)
350 {
351
352   xbt_assert0(((process != NULL)
353                && (process->simdata)), "Invalid parameters");
354   CHECK_HOST();
355
356   SIMIX_process_resume(process->simdata->s_process);
357   MSG_RETURN(MSG_OK);
358 }
359
360 /** \ingroup m_process_management
361  * \brief Returns true if the process is suspended .
362  *
363  * This checks whether a process is suspended or not by inspecting the
364  * task on which it was waiting for the completion.
365  */
366 int MSG_process_is_suspended(m_process_t process)
367 {
368   xbt_assert0(((process != NULL)
369                && (process->simdata)), "Invalid parameters");
370   return SIMIX_process_is_suspended(process->simdata->s_process);
371 }