3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
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. */
9 #include "xbt/sysdep.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_process, msg,
12 "Logging specific to MSG (process)");
14 /** \defgroup m_process_management Management Functions of Agents
15 * \brief This section describes the agent structure of MSG
16 * (#m_process_t) and the functions for managing it.
17 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
19 * We need to simulate many independent scheduling decisions, so
20 * the concept of <em>process</em> is at the heart of the
21 * simulator. A process may be defined as a <em>code</em>, with
22 * some <em>private data</em>, executing in a <em>location</em>.
26 /******************************** Process ************************************/
27 /** \ingroup m_process_management
28 * \brief Creates and runs a new #m_process_t.
30 * Does exactly the same as #MSG_process_create_with_arguments but without
31 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
32 * \sa MSG_process_create_with_arguments
34 m_process_t MSG_process_create(const char *name,
35 m_process_code_t code, void *data,
38 return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
41 static void MSG_process_cleanup(void *arg)
44 while(((m_process_t)arg)->simdata->paje_state) {
45 PAJE_PROCESS_POP_STATE((m_process_t)arg);
48 PAJE_PROCESS_FREE(arg);
50 xbt_fifo_remove(msg_global->process_list, arg);
51 xbt_fifo_remove(msg_global->process_to_run, arg);
52 xbt_fifo_remove(((m_process_t) arg)->simdata->host->simdata->process_list, arg);
53 free(((m_process_t) arg)->name);
54 ((m_process_t) arg)->name = NULL;
55 free(((m_process_t) arg)->simdata);
56 ((m_process_t) arg)->simdata = NULL;
60 /** \ingroup m_process_management
61 * \brief Creates and runs a new #m_process_t.
63 * A constructor for #m_process_t taking four arguments and returning the
64 * corresponding object. The structure (and the corresponding thread) is
65 * created, and put in the list of ready process.
66 * \param name a name for the object. It is for user-level information
68 * \param code is a function describing the behavior of the agent. It
69 should then only use functions described in \ref
70 m_process_management (to create a new #m_process_t for example),
71 in \ref m_host_management (only the read-only functions i.e. whose
72 name contains the word get), in \ref m_task_management (to create
73 or destroy some #m_task_t for example) and in \ref
74 msg_gos_functions (to handle file transfers and task processing).
75 * \param data a pointer to any data one may want to attach to the new
76 object. It is for user-level information and can be NULL. It can
77 be retrieved with the function \ref MSG_process_get_data.
78 * \param host the location where the new agent is executed.
79 * \param argc first argument passed to \a code
80 * \param argv second argument passed to \a code
82 * \return The new corresponding object.
84 m_process_t MSG_process_create_with_arguments(const char *name,
85 m_process_code_t code, void *data,
86 m_host_t host, int argc, char **argv)
88 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
89 m_process_t process = xbt_new0(s_m_process_t,1);
90 m_process_t self = NULL;
92 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
95 simdata->PID = msg_global->PID++;
97 simdata->waiting_task = NULL;
100 simdata->context = xbt_context_new(code, NULL, NULL,
101 MSG_process_cleanup, process,
102 simdata->argc, simdata->argv);
104 if((self=msg_global->current_process)) {
105 simdata->PPID = MSG_process_get_PID(self);
109 simdata->last_errno=MSG_OK;
112 /* Process structure */
113 process->name = xbt_strdup(name);
114 process->simdata = simdata;
115 process->data = data;
117 xbt_fifo_unshift(host->simdata->process_list, process);
119 /* *************** FIX du current_process !!! *************** */
120 self = msg_global->current_process;
121 xbt_context_start(process->simdata->context);
122 msg_global->current_process = self;
124 xbt_fifo_unshift(msg_global->process_list, process);
125 DEBUG2("Inserting %s(%s) in the to_run list",process->name,
127 xbt_fifo_unshift(msg_global->process_to_run, process);
129 PAJE_PROCESS_NEW(process);
134 /** \ingroup m_process_management
135 * \param process poor victim
137 * This function simply kills a \a process... scarry isn't it ? :)
139 void MSG_process_kill(m_process_t process)
142 simdata_process_t p_simdata = process->simdata;
143 simdata_host_t h_simdata= p_simdata->host->simdata;
145 m_process_t proc = NULL;
147 DEBUG3("Killing %s(%d) on %s",process->name, p_simdata->PID,
148 p_simdata->host->name);
150 for (i=0; i<msg_global->max_channel; i++) {
151 if (h_simdata->sleeping[i] == process) {
152 h_simdata->sleeping[i] = NULL;
157 if(p_simdata->waiting_task) {
158 xbt_dynar_foreach(p_simdata->waiting_task->simdata->sleeping,_cursor,proc) {
160 xbt_dynar_remove_at(p_simdata->waiting_task->simdata->sleeping,_cursor,&proc);
162 if(p_simdata->waiting_task->simdata->compute)
163 surf_workstation_resource->common_public->
164 action_free(p_simdata->waiting_task->simdata->compute);
165 else if (p_simdata->waiting_task->simdata->comm) {
166 surf_workstation_resource->common_public->
167 action_change_state(p_simdata->waiting_task->simdata->comm,SURF_ACTION_FAILED);
168 surf_workstation_resource->common_public->
169 action_free(p_simdata->waiting_task->simdata->comm);
171 xbt_die("UNKNOWN STATUS. Please report this bug.");
175 if ((i==msg_global->max_channel) && (process!=MSG_process_self()) &&
176 (!p_simdata->waiting_task)) {
177 xbt_die("UNKNOWN STATUS. Please report this bug.");
180 xbt_fifo_remove(msg_global->process_to_run,process);
181 xbt_fifo_remove(msg_global->process_list,process);
182 xbt_context_kill(process->simdata->context);
184 if(process==MSG_process_self()) {
185 /* I just killed myself */
190 /** \ingroup m_process_management
191 * \brief Migrates an agent to another location.
193 * This functions checks whether \a process and \a host are valid pointers
194 and change the value of the #m_host_t on which \a process is running.
196 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
198 simdata_process_t simdata = NULL;
202 xbt_assert0(((process) && (process->simdata)
203 && (host)), "Invalid parameters");
204 simdata = process->simdata;
206 xbt_fifo_remove(simdata->host->simdata->process_list,process);
207 simdata->host = host;
208 xbt_fifo_unshift(host->simdata->process_list,process);
213 /** \ingroup m_process_management
214 * \brief Return the user data of a #m_process_t.
216 * This functions checks whether \a process is a valid pointer or not
217 and return the user data associated to \a process if it is possible.
219 void *MSG_process_get_data(m_process_t process)
221 xbt_assert0((process != NULL), "Invalid parameters");
223 return (process->data);
226 /** \ingroup m_process_management
227 * \brief Set the user data of a #m_process_t.
229 * This functions checks whether \a process is a valid pointer or not
230 and set the user data associated to \a process if it is possible.
232 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
234 xbt_assert0((process != NULL), "Invalid parameters");
235 xbt_assert0((process->data == NULL), "Data already set");
237 process->data = data;
242 /** \ingroup m_process_management
243 * \brief Return the location on which an agent is running.
245 * This functions checks whether \a process is a valid pointer or not
246 and return the m_host_t corresponding to the location on which \a
249 m_host_t MSG_process_get_host(m_process_t process)
251 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
253 return (((simdata_process_t) process->simdata)->host);
256 /** \ingroup m_process_management
258 * \brief Return a #m_process_t given its PID.
260 * This functions search in the list of all the created m_process_t for a m_process_t
261 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
262 Note that the PID are uniq in the whole simulation, not only on a given host.
264 m_process_t MSG_process_from_PID(int PID)
266 xbt_fifo_item_t i = NULL;
267 m_process_t process = NULL;
269 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
270 if(MSG_process_get_PID(process) == PID) return process;
275 /** \ingroup m_process_management
276 * \brief Returns the process ID of \a process.
278 * This functions checks whether \a process is a valid pointer or not
281 int MSG_process_get_PID(m_process_t process)
283 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
285 return (((simdata_process_t) process->simdata)->PID);
288 /** \ingroup m_process_management
289 * \brief Returns the process ID of the parent of \a process.
291 * This functions checks whether \a process is a valid pointer or not
292 and return its PID. Returns -1 if the agent has not been created by
295 int MSG_process_get_PPID(m_process_t process)
297 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
299 return (((simdata_process_t) process->simdata)->PPID);
302 /** \ingroup m_process_management
303 * \brief Return the name of an agent.
305 * This functions checks whether \a process is a valid pointer or not
308 const char *MSG_process_get_name(m_process_t process)
310 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
312 return (process->name);
315 /** \ingroup m_process_management
316 * \brief Return the PID of the current agent.
318 * This functions returns the PID of the currently running #m_process_t.
320 int MSG_process_self_PID(void)
322 return (MSG_process_get_PID(MSG_process_self()));
325 /** \ingroup m_process_management
326 * \brief Return the PPID of the current agent.
328 * This functions returns the PID of the parent of the currently
329 * running #m_process_t.
331 int MSG_process_self_PPID(void)
333 return (MSG_process_get_PPID(MSG_process_self()));
336 /** \ingroup m_process_management
337 * \brief Return the current agent.
339 * This functions returns the currently running #m_process_t.
341 m_process_t MSG_process_self(void)
343 return msg_global ? msg_global->current_process : NULL;
346 /** \ingroup m_process_management
347 * \brief Suspend the process.
349 * This functions suspend the process by suspending the task on which
350 * it was waiting for the completion.
352 MSG_error_t MSG_process_suspend(m_process_t process)
354 simdata_process_t simdata = NULL;
355 simdata_task_t simdata_task = NULL;
357 XBT_IN2("(%p(%s))", process, process->name);
359 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
361 PAJE_PROCESS_PUSH_STATE(process,"S",NULL);
363 if(process!=MSG_process_self()) {
364 simdata = process->simdata;
366 xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
368 simdata_task = simdata->waiting_task->simdata;
370 simdata->suspended = 1;
371 if(simdata->blocked) {
376 xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
377 !((simdata_task->compute)&&(simdata_task->comm)),
378 "Got a problem in deciding which action to choose !");
379 simdata->suspended = 1;
380 if(simdata_task->compute)
381 surf_workstation_resource->common_public->suspend(simdata_task->compute);
383 surf_workstation_resource->common_public->suspend(simdata_task->comm);
385 m_task_t dummy = MSG_TASK_UNINITIALIZED;
386 dummy = MSG_task_create("suspended", 0.0, 0, NULL);
388 simdata = process->simdata;
389 simdata->suspended = 1;
390 __MSG_task_execute(process,dummy);
391 surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
392 __MSG_wait_for_computation(process,dummy);
393 simdata->suspended = 0;
395 MSG_task_destroy(dummy);
401 /** \ingroup m_process_management
402 * \brief Resume a suspended process.
404 * This functions resume a suspended process by resuming the task on
405 * which it was waiting for the completion.
407 MSG_error_t MSG_process_resume(m_process_t process)
409 simdata_process_t simdata = NULL;
410 simdata_task_t simdata_task = NULL;
412 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
415 XBT_IN2("(%p(%s))", process, process->name);
417 if(process == MSG_process_self()) {
422 simdata = process->simdata;
424 if(simdata->blocked) {
425 PAJE_PROCESS_POP_STATE(process);
427 simdata->suspended = 0; /* He'll wake up by itself */
432 if(!(simdata->waiting_task)) {
433 xbt_assert0(0,"Process not waiting for anything else. Weird !");
437 simdata_task = simdata->waiting_task->simdata;
440 if(simdata_task->compute) {
441 surf_workstation_resource->common_public->resume(simdata_task->compute);
442 PAJE_PROCESS_POP_STATE(process);
445 PAJE_PROCESS_POP_STATE(process);
446 surf_workstation_resource->common_public->resume(simdata_task->comm);
453 /** \ingroup m_process_management
454 * \brief Returns true if the process is suspended .
456 * This checks whether a process is suspended or not by inspecting the
457 * task on which it was waiting for the completion.
459 int MSG_process_is_suspended(m_process_t process)
461 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
463 return (process->simdata->suspended);
466 int __MSG_process_block(double max_duration, const char *info)
468 m_process_t process = MSG_process_self();
469 m_task_t dummy = MSG_TASK_UNINITIALIZED;
470 char blocked_name[512];
471 snprintf(blocked_name,512,"blocked [%s] (%s:%s)",
472 info, process->name, process->simdata->host->name);
474 XBT_IN1(": max_duration=%g",max_duration);
476 dummy = MSG_task_create(blocked_name, 0.0, 0, NULL);
478 PAJE_PROCESS_PUSH_STATE(process,"B",NULL);
480 process->simdata->blocked=1;
481 __MSG_task_execute(process,dummy);
482 surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
484 surf_workstation_resource->common_public->set_max_duration(dummy->simdata->compute,
486 __MSG_wait_for_computation(process,dummy);
487 MSG_task_destroy(dummy);
488 process->simdata->blocked=0;
490 if(process->simdata->suspended) {
491 DEBUG0("I've been suspended in the meantime");
492 MSG_process_suspend(process);
493 DEBUG0("I've been resumed, let's keep going");
496 PAJE_PROCESS_POP_STATE(process);
502 MSG_error_t __MSG_process_unblock(m_process_t process)
504 simdata_process_t simdata = NULL;
505 simdata_task_t simdata_task = NULL;
507 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
510 XBT_IN2(": %s unblocking %s", MSG_process_self()->name,process->name);
512 simdata = process->simdata;
513 if(!(simdata->waiting_task)) {
514 xbt_assert0(0,"Process not waiting for anything else. Weird !");
518 simdata_task = simdata->waiting_task->simdata;
520 xbt_assert0(simdata->blocked,"Process not blocked");
522 surf_workstation_resource->common_public->resume(simdata_task->compute);
529 int __MSG_process_isBlocked(m_process_t process)
531 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
533 return (process->simdata->blocked);