1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg/private.h"
8 #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.
18 /** @addtogroup m_process_management
19 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Agents" --> \endhtmlonly
21 * We need to simulate many independent scheduling decisions, so
22 * the concept of <em>process</em> is at the heart of the
23 * simulator. A process may be defined as a <em>code</em>, with
24 * some <em>private data</em>, executing in a <em>location</em>.
28 /******************************** Process ************************************/
29 void __MSG_process_cleanup(smx_process_t smx_proc)
34 if (smx_proc == SIMIX_process_self()) {
35 /* avoid a SIMIX request if this function is called by the process itself */
36 proc = SIMIX_process_self_get_data();
39 proc = SIMIX_req_process_get_data(smx_proc);
43 TRACE_msg_process_end(proc);
47 xbt_swag_remove(proc, msg_global->process_list);
49 SIMIX_req_process_cleanup(smx_proc);
64 /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */
65 void _MSG_process_create_from_SIMIX(void* process, const char *name,
66 xbt_main_func_t code, void *data,
67 char *hostname, int argc, char **argv,
68 xbt_dict_t properties)
70 m_host_t host = MSG_get_host_by_name(hostname);
71 m_process_t p = MSG_process_create_with_environment(name, code, data,
74 *((m_process_t*) process) = p;
77 /** \ingroup m_process_management
78 * \brief Creates and runs a new #m_process_t.
80 * Does exactly the same as #MSG_process_create_with_arguments but without
81 providing standard arguments (\a argc, \a argv, \a start_time, \a kill_time).
82 * \sa MSG_process_create_with_arguments
84 m_process_t MSG_process_create(const char *name,
85 xbt_main_func_t code, void *data,
88 return MSG_process_create_with_environment(name, code, data, host, -1,
92 /** \ingroup m_process_management
93 * \brief Creates and runs a new #m_process_t.
95 * A constructor for #m_process_t taking four arguments and returning the
96 * corresponding object. The structure (and the corresponding thread) is
97 * created, and put in the list of ready process.
98 * \param name a name for the object. It is for user-level information
100 * \param code is a function describing the behavior of the agent. It
101 should then only use functions described in \ref
102 m_process_management (to create a new #m_process_t for example),
103 in \ref m_host_management (only the read-only functions i.e. whose
104 name contains the word get), in \ref m_task_management (to create
105 or destroy some #m_task_t for example) and in \ref
106 msg_gos_functions (to handle file transfers and task processing).
107 * \param data a pointer to any data one may want to attach to the new
108 object. It is for user-level information and can be NULL. It can
109 be retrieved with the function \ref MSG_process_get_data.
110 * \param host the location where the new agent is executed.
111 * \param argc first argument passed to \a code
112 * \param argv second argument passed to \a code
114 * \return The new corresponding object.
117 m_process_t MSG_process_create_with_arguments(const char *name,
118 xbt_main_func_t code,
119 void *data, m_host_t host,
120 int argc, char **argv)
122 return MSG_process_create_with_environment(name, code, data, host,
126 /** \ingroup m_process_management
127 * \brief Creates and runs a new #m_process_t.
129 * A constructor for #m_process_t taking four arguments and returning the
130 * corresponding object. The structure (and the corresponding thread) is
131 * created, and put in the list of ready process.
132 * \param name a name for the object. It is for user-level information
134 * \param code is a function describing the behavior of the agent. It
135 should then only use functions described in \ref
136 m_process_management (to create a new #m_process_t for example),
137 in \ref m_host_management (only the read-only functions i.e. whose
138 name contains the word get), in \ref m_task_management (to create
139 or destroy some #m_task_t for example) and in \ref
140 msg_gos_functions (to handle file transfers and task processing).
141 * \param data a pointer to any data one may want to attach to the new
142 object. It is for user-level information and can be NULL. It can
143 be retrieved with the function \ref MSG_process_get_data.
144 * \param host the location where the new agent is executed.
145 * \param argc first argument passed to \a code
146 * \param argv second argument passed to \a code
147 * \param properties list a properties defined for this process
149 * \return The new corresponding object.
151 m_process_t MSG_process_create_with_environment(const char *name,
152 xbt_main_func_t code,
153 void *data, m_host_t host,
154 int argc, char **argv,
155 xbt_dict_t properties)
157 simdata_process_t simdata = NULL;
158 m_process_t process = xbt_new0(s_m_process_t, 1);
159 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
161 simdata = xbt_new0(s_simdata_process_t, 1);
164 simdata->PID = msg_global->PID++;
165 simdata->waiting_action = NULL;
166 simdata->waiting_task = NULL;
167 simdata->m_host = host;
168 simdata->argc = argc;
169 simdata->argv = argv;
171 if (SIMIX_process_self()) {
172 simdata->PPID = MSG_process_get_PID(SIMIX_process_self_get_data());
176 simdata->last_errno = MSG_OK;
178 /* Process structure */
179 process->name = xbt_strdup(name);
180 process->simdata = simdata;
181 process->data = data;
182 xbt_swag_insert(process, msg_global->process_list);
185 TRACE_msg_process_create (process);
188 /* Let's create the process: SIMIX may decide to start it right now,
189 * even before returning the flow control to us */
190 SIMIX_req_process_create(&simdata->s_process, name, code, (void *) process, host->name,
191 argc, argv, properties);
193 if (!simdata->s_process) {
194 /* Undo everything we have just changed */
196 xbt_swag_remove(process, msg_global->process_list);
197 xbt_free(process->name);
206 void _MSG_process_kill_from_SIMIX(void *p)
209 TRACE_msg_process_kill((m_process_t) p);
211 MSG_process_kill((m_process_t) p);
214 /** \ingroup m_process_management
215 * \param process poor victim
217 * This function simply kills a \a process... scarry isn't it ? :)
219 void MSG_process_kill(m_process_t process)
221 simdata_process_t p_simdata = process->simdata;
224 TRACE_msg_process_kill(process);
227 XBT_DEBUG("Killing %s(%d) on %s",
228 process->name, p_simdata->PID, p_simdata->m_host->name);
230 if (p_simdata->waiting_task && p_simdata->waiting_task->simdata->comm) {
231 SIMIX_req_comm_cancel(p_simdata->waiting_task->simdata->comm);
234 xbt_swag_remove(process, msg_global->process_list);
235 SIMIX_req_process_kill(process->simdata->s_process);
240 /** \ingroup m_process_management
241 * \brief Migrates an agent to another location.
243 * This function checks whether \a process and \a host are valid pointers
244 and change the value of the #m_host_t on which \a process is running.
246 MSG_error_t MSG_process_change_host(m_host_t host)
248 m_process_t process = MSG_process_self();
249 m_host_t now = process->simdata->m_host;
250 process->simdata->m_host = host;
252 TRACE_msg_process_change_host(process, now, host);
254 SIMIX_req_process_change_host(process->simdata->s_process, now->name,
259 /** \ingroup m_process_management
260 * \brief Return the user data of a #m_process_t.
262 * This function checks whether \a process is a valid pointer or not
263 and return the user data associated to \a process if it is possible.
265 void *MSG_process_get_data(m_process_t process)
267 xbt_assert0((process != NULL), "Invalid parameters");
269 return (process->data);
272 /** \ingroup m_process_management
273 * \brief Set the user data of a #m_process_t.
275 * This function checks whether \a process is a valid pointer or not
276 and set the user data associated to \a process if it is possible.
278 MSG_error_t MSG_process_set_data(m_process_t process, void *data)
280 xbt_assert0((process != NULL), "Invalid parameters");
282 process->data = data;
287 /** \ingroup m_process_management
288 * \brief Return the location on which an agent is running.
290 * This function checks whether \a process is a valid pointer or not
291 and return the m_host_t corresponding to the location on which \a
294 m_host_t MSG_process_get_host(m_process_t process)
296 xbt_assert0(((process != NULL)
297 && (process->simdata)), "Invalid parameters");
299 return (((simdata_process_t) process->simdata)->m_host);
302 /** \ingroup m_process_management
304 * \brief Return a #m_process_t given its PID.
306 * This function search in the list of all the created m_process_t for a m_process_t
307 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
308 Note that the PID are uniq in the whole simulation, not only on a given host.
310 m_process_t MSG_process_from_PID(int PID)
312 m_process_t process = NULL;
314 xbt_swag_foreach(process, msg_global->process_list) {
315 if (MSG_process_get_PID(process) == PID)
321 /** \ingroup m_process_management
322 * \brief Returns the process ID of \a process.
324 * This function checks whether \a process is a valid pointer or not
325 and return its PID (or 0 in case of problem).
327 int MSG_process_get_PID(m_process_t process)
329 /* Do not raise an exception here: this function is used in the logs,
330 and it will be called back by the exception handling stuff */
331 if (process == NULL || process->simdata == NULL)
334 return (((simdata_process_t) process->simdata)->PID);
337 /** \ingroup m_process_management
338 * \brief Returns the process ID of the parent of \a process.
340 * This function checks whether \a process is a valid pointer or not
341 and return its PID. Returns -1 if the agent has not been created by
344 int MSG_process_get_PPID(m_process_t process)
346 xbt_assert0(((process != NULL)
347 && (process->simdata)), "Invalid parameters");
349 return (((simdata_process_t) process->simdata)->PPID);
352 /** \ingroup m_process_management
353 * \brief Return the name of an agent.
355 * This function checks whether \a process is a valid pointer or not
358 const char *MSG_process_get_name(m_process_t process)
360 xbt_assert0(process, "Invalid parameter: process is NULL");
361 xbt_assert0(process->simdata,
362 "Invalid parameter: process->simdata is NULL");
364 return (process->name);
367 /** \ingroup m_process_management
368 * \brief Returns the value of a given process property
370 * \param process a process
371 * \param name a property name
372 * \return value of a property (or NULL if the property is not set)
374 const char *MSG_process_get_property_value(m_process_t process,
377 return xbt_dict_get_or_null(MSG_process_get_properties(process), name);
380 /** \ingroup m_process_management
381 * \brief Return the list of properties
383 * This function returns all the parameters associated with a process
385 xbt_dict_t MSG_process_get_properties(m_process_t process)
387 xbt_assert0((process != NULL), "Invalid parameters");
389 return (SIMIX_req_process_get_properties
390 (((simdata_process_t) process->simdata)->s_process));
394 /** \ingroup m_process_management
395 * \brief Return the PID of the current agent.
397 * This function returns the PID of the currently running #m_process_t.
399 int MSG_process_self_PID(void)
401 return (MSG_process_get_PID(MSG_process_self()));
404 /** \ingroup m_process_management
405 * \brief Return the PPID of the current agent.
407 * This function returns the PID of the parent of the currently
408 * running #m_process_t.
410 int MSG_process_self_PPID(void)
412 return (MSG_process_get_PPID(MSG_process_self()));
415 /** \ingroup m_process_management
416 * \brief Return the current agent.
418 * This function returns the currently running #m_process_t.
420 m_process_t MSG_process_self(void)
422 /* we cannot make a SIMIX request here because this may create an exception or a logging
423 event, and both mechanisms call MSG_process_self() again (via xbt_getpid()) */
424 return (m_process_t) SIMIX_process_self_get_data();
427 /** \ingroup m_process_management
428 * \brief Suspend the process.
430 * This function suspends the process by suspending the task on which
431 * it was waiting for the completion.
433 MSG_error_t MSG_process_suspend(m_process_t process)
435 xbt_assert0(((process != NULL)
436 && (process->simdata)), "Invalid parameters");
440 TRACE_msg_process_suspend(process);
443 SIMIX_req_process_suspend(process->simdata->s_process);
447 /** \ingroup m_process_management
448 * \brief Resume a suspended process.
450 * This function resumes a suspended process by resuming the task on
451 * which it was waiting for the completion.
453 MSG_error_t MSG_process_resume(m_process_t process)
456 xbt_assert0(((process != NULL)
457 && (process->simdata)), "Invalid parameters");
461 TRACE_msg_process_resume(process);
464 SIMIX_req_process_resume(process->simdata->s_process);
468 /** \ingroup m_process_management
469 * \brief Returns true if the process is suspended .
471 * This checks whether a process is suspended or not by inspecting the
472 * task on which it was waiting for the completion.
474 int MSG_process_is_suspended(m_process_t process)
476 xbt_assert0(((process != NULL)
477 && (process->simdata)), "Invalid parameters");
478 return SIMIX_req_process_is_suspended(process->simdata->s_process);
482 smx_context_t MSG_process_get_smx_ctx(m_process_t process) {
483 return SIMIX_process_get_context(process->simdata->s_process);