Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few more functions and I'll be able to make MSG program start and crash... :)
[simgrid.git] / src / msg / m_process.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include"private.h"
9 #include"xbt/sysdep.h"
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(m_process, msg,
12                                 "Logging specific to MSG (process)");
13
14 /******************************** Process ************************************/
15 /** \ingroup m_process_management
16  * \brief Creates and runs a new #m_process_t.
17
18  * A constructor for #m_process_t taking four arguments and returning the 
19  * corresponding object. The structure (and the corresponding thread) is
20  * created, and put in the list of ready process.
21  * \param name a name for the object. It is for user-level information
22    and can be NULL.
23  * \param code is a function describing the behavior of the agent. It
24    should then only use functions described in \ref
25    m_process_management (to create a new #m_process_t for example),
26    in \ref m_host_management (only the read-only functions i.e. whose
27    name contains the word get), in \ref m_task_management (to create
28    or destroy some #m_task_t for example) and in \ref
29    msg_gos_functions (to handle file transfers and task processing).
30  * \param data a pointer to any data may want to attach to the new
31    object.  It is for user-level information and can be NULL. It can
32    be retrieved with the function \ref MSG_process_get_data.
33  * \param host the location where the new agent is executed.
34  * \see m_process_t
35  * \return The new corresponding object.
36  */
37 m_process_t MSG_process_create(const char *name,
38                                m_process_code_t code, void *data,
39                                m_host_t host)
40 {
41   simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
42   m_process_t process = xbt_new0(s_m_process_t,1);
43   m_process_t self = NULL;
44   static int PID = 1;
45
46   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
47   /* Simulator Data */
48
49   simdata->PID = PID++;
50   simdata->host = host;
51   simdata->waiting_task = NULL;
52   simdata->argc = -1;
53   simdata->argv = NULL;
54   simdata->context = xbt_context_new(code, simdata->argc, simdata->argv);
55
56   if((self=msg_global->current_process)) {
57     simdata->PPID = MSG_process_get_PID(self);
58   } else {
59     simdata->PPID = -1;
60   }
61   simdata->last_errno=MSG_OK;
62
63
64   /* Process structure */
65   process->name = xbt_strdup(name);
66   process->simdata = simdata;
67   process->data = data;
68
69   xbt_fifo_push(host->simdata->process_list, process);
70
71   /* /////////////// FIX du current_process !!! ////////////// */
72   self = msg_global->current_process;
73   xbt_context_start(process->simdata->context);
74   msg_global->current_process = self;
75   return process;
76 }
77
78 /** \ingroup m_process_management
79  * \brief Migrates an agent to another location.
80  *
81  * This functions checks whether \a process and \a host are valid pointers
82    and change the value of the #m_host_t on which \a process is running.
83  */
84 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
85 {
86   simdata_process_t simdata = NULL;
87
88   /* Sanity check */
89
90   xbt_assert0(((process) && (process->simdata)
91           && (host)), "Invalid parameters");
92   simdata = process->simdata;
93
94   xbt_fifo_remove(simdata->host->simdata->process_list,process);
95   simdata->host = host;
96   xbt_fifo_push(host->simdata->process_list,process);
97
98   return MSG_OK;
99 }
100
101 /** \ingroup m_process_management
102  * \brief Return the user data of a #m_process_t.
103  *
104  * This functions checks whether \a process is a valid pointer or not 
105    and return the user data associated to \a process if it is possible.
106  */
107 void *MSG_process_get_data(m_process_t process)
108 {
109   xbt_assert0((process != NULL), "Invalid parameters");
110
111   return (process->data);
112 }
113
114 /** \ingroup m_process_management
115  * \brief Set the user data of a #m_process_t.
116  *
117  * This functions checks whether \a process is a valid pointer or not 
118    and set the user data associated to \a process if it is possible.
119  */
120 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
121 {
122   xbt_assert0((process != NULL), "Invalid parameters");
123   xbt_assert0((process->data == NULL), "Data already set");
124   
125   process->data = data;
126    
127   return MSG_OK;
128 }
129
130 /** \ingroup m_process_management
131  * \brief Return the location on which an agent is running.
132  *
133  * This functions checks whether \a process is a valid pointer or not 
134    and return the m_host_t corresponding to the location on which \a 
135    process is running.
136  */
137 m_host_t MSG_process_get_host(m_process_t process)
138 {
139   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
140
141   return (((simdata_process_t) process->simdata)->host);
142 }
143
144 /** \ingroup m_process_management
145  *
146  * \brief Return a #m_process_t given its PID.
147  *
148  * This functions search in the list of all the created m_process_t for a m_process_t 
149    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
150    Note that the PID are uniq in the whole simulation, not only on a given host.
151  */
152 m_process_t MSG_process_from_PID(int PID)
153 {
154   xbt_fifo_item_t i = NULL;
155   m_process_t process = NULL;
156
157   xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
158     if(MSG_process_get_PID(process) == PID) return process;
159   }
160   return NULL;
161 }
162
163 /** \ingroup m_process_management
164  * \brief Returns the process ID of \a process.
165  *
166  * This functions checks whether \a process is a valid pointer or not 
167    and return its PID.
168  */
169 int MSG_process_get_PID(m_process_t process)
170 {
171   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
172
173   return (((simdata_process_t) process->simdata)->PID);
174 }
175
176
177 /** \ingroup m_process_management
178  * \brief Returns the process ID of the parent of \a process.
179  *
180  * This functions checks whether \a process is a valid pointer or not 
181    and return its PID. Returns -1 if the agent has not been created by 
182    another agent.
183  */
184 int MSG_process_get_PPID(m_process_t process)
185 {
186   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
187
188   return (((simdata_process_t) process->simdata)->PPID);
189 }
190
191 /** \ingroup m_process_management
192  * \brief Return the name of an agent.
193  *
194  * This functions checks whether \a process is a valid pointer or not 
195    and return its name.
196  */
197 const char *MSG_process_get_name(m_process_t process)
198 {
199   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
200
201   return (process->name);
202 }
203
204 /** \ingroup m_process_management
205  * \brief Return the PID of the current agent.
206  *
207  * This functions returns the PID of the currently running #m_process_t.
208  */
209 int MSG_process_self_PID(void)
210 {
211   return (MSG_process_get_PID(MSG_process_self()));
212 }
213
214 /** \ingroup m_process_management
215  * \brief Return the PPID of the current agent.
216  *
217  * This functions returns the PID of the parent of the currently
218  * running #m_process_t.
219  */
220 int MSG_process_self_PPID(void)
221 {
222   return (MSG_process_get_PPID(MSG_process_self()));
223 }
224
225 /** \ingroup m_process_management
226  * \brief Return the current agent.
227  *
228  * This functions returns the currently running #m_process_t.
229  */
230 m_process_t MSG_process_self(void)
231 {
232   return msg_global->current_process;
233 }
234
235 /** \ingroup m_process_management
236  * \brief Suspend the process.
237  *
238  * This functions suspend the process by suspending the task on which
239  * it was waiting for the completion.
240  */
241 MSG_error_t MSG_process_suspend(m_process_t process)
242 {
243   simdata_process_t simdata = NULL;
244   simdata_task_t simdata_task = NULL;
245   int i;
246
247   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
248
249   if(process!=MSG_process_self()) {
250     simdata = process->simdata;
251     
252     xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
253
254     simdata_task = simdata->waiting_task->simdata;
255
256     xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
257                 !((simdata_task->comm)&&(simdata_task->comm)),
258                 "Got a problem in deciding which action to choose !");
259     if(simdata_task->compute) 
260       surf_workstation_resource->extension_public->suspend(simdata_task->compute);
261     else 
262       surf_workstation_resource->extension_public->suspend(simdata_task->comm);
263
264   } else {
265     m_task_t dummy = MSG_TASK_UNINITIALIZED;
266     dummy = MSG_task_create("suspended", 0.01, 0, NULL);
267
268     __MSG_task_execute(process,dummy);
269     surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
270     __MSG_wait_for_computation(process,dummy);
271
272     MSG_task_destroy(dummy);
273   }
274   return MSG_OK;
275 }
276
277 /** \ingroup m_process_management
278  * \brief Resume a suspended process.
279  *
280  * This functions resume a suspended process by resuming the task on
281  * which it was waiting for the completion.
282  */
283 MSG_error_t MSG_process_resume(m_process_t process)
284 {
285   simdata_process_t simdata = NULL;
286   simdata_task_t simdata_task = NULL;
287   int i;
288
289   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
290   CHECK_HOST();
291
292   simdata = process->simdata;
293   if(!(simdata->waiting_task)) {
294     xbt_assert0(0,"Process not waiting for anything else. Weird !");
295     return MSG_WARNING;
296   }
297   simdata_task = simdata->waiting_task->simdata;
298
299   if(simdata_task->compute) 
300     surf_workstation_resource->extension_public->resume(simdata_task->compute);
301   else 
302     surf_workstation_resource->extension_public->resume(simdata_task->comm);
303
304   MSG_RETURN(MSG_OK);
305 }
306
307 /** \ingroup m_process_management
308  * \brief Returns true if the process is suspended .
309  *
310  * This checks whether a process is suspended or not by inspecting the
311  * task on which it was waiting for the completion.
312  */
313 int MSG_process_isSuspended(m_process_t process)
314 {
315   simdata_process_t simdata = NULL;
316   simdata_task_t simdata_task = NULL;
317   int i;
318   
319   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
320
321   simdata = process->simdata;
322   if(!(simdata->waiting_task)) {
323     xbt_assert0(0,"Process not waiting for anything else. Weird !");
324     return 0;
325   }
326
327   simdata_task = simdata->waiting_task->simdata;
328
329   if(simdata_task->compute) 
330     return surf_workstation_resource->extension_public->is_suspended(simdata_task->compute);
331   else 
332     return surf_workstation_resource->extension_public->is_suspended(simdata_task->comm);
333 }