Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Keeping rewriting MSG
[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->put_host = NULL;
53   simdata->put_channel = -1;
54   simdata->argc = -1;
55   simdata->argv = NULL;
56   simdata->context = xbt_context_new(code, simdata->argc, simdata->argv);
57
58   if((self=msg_global->current_process)) {
59     simdata->PPID = MSG_process_get_PID(self);
60   } else {
61     simdata->PPID = -1;
62   }
63   simdata->last_errno=MSG_OK;
64
65
66   /* Process structure */
67   process->name = xbt_strdup(name);
68   process->simdata = simdata;
69   process->data = data;
70
71   xbt_fifo_push(host->simdata->process_list, process);
72
73   /* /////////////// FIX du current_process !!! ////////////// */
74   self = msg_global->current_process;
75   xbt_context_start(process->simdata->context);
76   msg_global->current_process = self;
77   return process;
78 }
79
80 /** \ingroup m_process_management
81  * \brief Migrates an agent to another location.
82  *
83  * This functions checks whether \a process and \a host are valid pointers
84    and change the value of the #m_host_t on which \a process is running.
85  */
86 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
87 {
88   simdata_process_t simdata = NULL;
89
90   /* Sanity check */
91
92   xbt_assert0(((process) && (process->simdata)
93           && (host)), "Invalid parameters");
94   simdata = process->simdata;
95
96   xbt_fifo_remove(simdata->host->simdata->process_list,process);
97   simdata->host = host;
98   xbt_fifo_push(host->simdata->process_list,process);
99
100   return MSG_OK;
101 }
102
103 /** \ingroup m_process_management
104  * \brief Return the user data of a #m_process_t.
105  *
106  * This functions checks whether \a process is a valid pointer or not 
107    and return the user data associated to \a process if it is possible.
108  */
109 void *MSG_process_get_data(m_process_t process)
110 {
111   xbt_assert0((process != NULL), "Invalid parameters");
112
113   return (process->data);
114 }
115
116 /** \ingroup m_process_management
117  * \brief Set the user data of a #m_process_t.
118  *
119  * This functions checks whether \a process is a valid pointer or not 
120    and set the user data associated to \a process if it is possible.
121  */
122 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
123 {
124   xbt_assert0((process != NULL), "Invalid parameters");
125   xbt_assert0((process->data == NULL), "Data already set");
126   
127   process->data = data;
128    
129   return MSG_OK;
130 }
131
132 /** \ingroup m_process_management
133  * \brief Return the location on which an agent is running.
134  *
135  * This functions checks whether \a process is a valid pointer or not 
136    and return the m_host_t corresponding to the location on which \a 
137    process is running.
138  */
139 m_host_t MSG_process_get_host(m_process_t process)
140 {
141   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
142
143   return (((simdata_process_t) process->simdata)->host);
144 }
145
146 /** \ingroup m_process_management
147  *
148  * \brief Return a #m_process_t given its PID.
149  *
150  * This functions search in the list of all the created m_process_t for a m_process_t 
151    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
152    Note that the PID are uniq in the whole simulation, not only on a given host.
153  */
154 m_process_t MSG_process_from_PID(int PID)
155 {
156   xbt_fifo_item_t i = NULL;
157   m_process_t process = NULL;
158
159   xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
160     if(MSG_process_get_PID(process) == PID) return process;
161   }
162   return NULL;
163 }
164
165 /** \ingroup m_process_management
166  * \brief Returns the process ID of \a process.
167  *
168  * This functions checks whether \a process is a valid pointer or not 
169    and return its PID.
170  */
171 int MSG_process_get_PID(m_process_t process)
172 {
173   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
174
175   return (((simdata_process_t) process->simdata)->PID);
176 }
177
178
179 /** \ingroup m_process_management
180  * \brief Returns the process ID of the parent of \a process.
181  *
182  * This functions checks whether \a process is a valid pointer or not 
183    and return its PID. Returns -1 if the agent has not been created by 
184    another agent.
185  */
186 int MSG_process_get_PPID(m_process_t process)
187 {
188   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
189
190   return (((simdata_process_t) process->simdata)->PPID);
191 }
192
193 /** \ingroup m_process_management
194  * \brief Return the name of an agent.
195  *
196  * This functions checks whether \a process is a valid pointer or not 
197    and return its name.
198  */
199 const char *MSG_process_get_name(m_process_t process)
200 {
201   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
202
203   return (process->name);
204 }
205
206 /** \ingroup m_process_management
207  * \brief Return the PID of the current agent.
208  *
209  * This functions returns the PID of the currently running #m_process_t.
210  */
211 int MSG_process_self_PID(void)
212 {
213   return (MSG_process_get_PID(MSG_process_self()));
214 }
215
216 /** \ingroup m_process_management
217  * \brief Return the PPID of the current agent.
218  *
219  * This functions returns the PID of the parent of the currently
220  * running #m_process_t.
221  */
222 int MSG_process_self_PPID(void)
223 {
224   return (MSG_process_get_PPID(MSG_process_self()));
225 }
226
227 /** \ingroup m_process_management
228  * \brief Return the current agent.
229  *
230  * This functions returns the currently running #m_process_t.
231  */
232 m_process_t MSG_process_self(void)
233 {
234   return msg_global->current_process;
235 }
236
237 /** \ingroup m_process_management
238  * \brief Suspend the process.
239  *
240  * This functions suspend the process by suspending the task on which
241  * it was waiting for the completion.
242  */
243 MSG_error_t MSG_process_suspend(m_process_t process)
244 {
245   simdata_process_t simdata = NULL;
246   simdata_task_t simdata_task = NULL;
247   int i;
248
249   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
250
251   if(process!=MSG_process_self()) {
252     simdata = process->simdata;
253     
254     xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
255
256     simdata_task = simdata->waiting_task->simdata;
257
258     xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
259                 !((simdata_task->comm)&&(simdata_task->comm)),
260                 "Got a problem in deciding which action to choose !");
261     if(simdata_task->compute) 
262       surf_workstation_resource->extension_public->suspend(simdata_task->compute);
263     else 
264       surf_workstation_resource->extension_public->suspend(simdata_task->comm);
265
266   } else {
267     m_task_t dummy = MSG_TASK_UNINITIALIZED;
268     dummy = MSG_task_create("suspended", 0.01, 0, NULL);
269
270     __MSG_task_execute(process,dummy);
271     surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
272     __MSG_wait_for_computation(process,dummy);
273
274     MSG_task_destroy(dummy);
275   }
276   return MSG_OK;
277 }
278
279 /** \ingroup m_process_management
280  * \brief Resume a suspended process.
281  *
282  * This functions resume a suspended process by resuming the task on
283  * which it was waiting for the completion.
284  */
285 MSG_error_t MSG_process_resume(m_process_t process)
286 {
287   simdata_process_t simdata = NULL;
288   simdata_task_t simdata_task = NULL;
289   int i;
290
291   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
292   CHECK_HOST();
293
294   simdata = process->simdata;
295   if(!(simdata->waiting_task)) {
296     xbt_assert0(0,"Process not waiting for anything else. Weird !");
297     return MSG_WARNING;
298   }
299   simdata_task = simdata->waiting_task->simdata;
300
301   if(simdata_task->compute) 
302     surf_workstation_resource->extension_public->resume(simdata_task->compute);
303   else 
304     surf_workstation_resource->extension_public->resume(simdata_task->comm);
305
306   MSG_RETURN(MSG_OK);
307 }
308
309 /** \ingroup m_process_management
310  * \brief Returns true if the process is suspended .
311  *
312  * This checks whether a process is suspended or not by inspecting the
313  * task on which it was waiting for the completion.
314  */
315 int MSG_process_isSuspended(m_process_t process)
316 {
317   simdata_process_t simdata = NULL;
318   simdata_task_t simdata_task = NULL;
319   int i;
320   
321   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
322
323   simdata = process->simdata;
324   if(!(simdata->waiting_task)) {
325     xbt_assert0(0,"Process not waiting for anything else. Weird !");
326     return 0;
327   }
328
329   simdata_task = simdata->waiting_task->simdata;
330
331   if(simdata_task->compute) 
332     return surf_workstation_resource->extension_public->is_suspended(simdata_task->compute);
333   else 
334     return surf_workstation_resource->extension_public->is_suspended(simdata_task->comm);
335 }