Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change variable waiting_task to waiting_action on msg process control.
[simgrid.git] / src / msg / host.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 #include "mailbox.h"
14
15 /** \defgroup m_host_management Management functions of Hosts
16  *  \brief This section describes the host structure of MSG
17  */
18 /** @addtogroup m_host_management
19  *     \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Hosts" --> \endhtmlonly
20  * (#m_host_t) and the functions for managing it.
21  *  
22  *  A <em>location</em> (or <em>host</em>) is any possible place where
23  *  a process may run. Thus it may be represented as a
24  *  <em>physical resource with computing capabilities</em>, some
25  *  <em>mailboxes</em> to enable running process to communicate with
26  *  remote ones, and some <em>private data</em> that can be only
27  *  accessed by local process.
28  *  \see m_host_t
29  */
30
31 /********************************* Host **************************************/
32 m_host_t __MSG_host_create(smx_host_t workstation, void *data)
33 {
34   const char *name;
35   simdata_host_t simdata = xbt_new0(s_simdata_host_t, 1);
36   m_host_t host = xbt_new0(s_m_host_t, 1);
37   int i;
38
39   char alias[MAX_ALIAS_NAME + 1] = { 0 };       /* buffer used to build the key of the mailbox */
40
41   name = SIMIX_host_get_name(workstation);
42   /* Host structure */
43   host->name = xbt_strdup(name);
44   host->simdata = simdata;
45   host->data = data;
46
47   simdata->smx_host = workstation;
48
49   if (msg_global->max_channel > 0)
50     simdata->mailboxes = xbt_new0(msg_mailbox_t, msg_global->max_channel);
51
52   for (i = 0; i < msg_global->max_channel; i++) {
53     sprintf(alias, "%s:%d", name, i);
54
55     /* the key of the mailbox (in this case) is build from the name of the host and the channel number */
56     simdata->mailboxes[i] = MSG_mailbox_create(alias);
57     MSG_mailbox_set_hostname(simdata->mailboxes[i], name);
58     memset(alias, 0, MAX_ALIAS_NAME + 1);
59   }
60
61   simdata->mutex = SIMIX_mutex_init();
62   SIMIX_host_set_data(workstation, host);
63
64   /* Update global variables */
65   xbt_fifo_unshift(msg_global->host, host);
66
67   return host;
68 }
69
70 /** \ingroup m_host_management
71  *
72  * \brief Set the user data of a #m_host_t.
73  *
74  * This functions checks whether some data has already been associated to \a host 
75    or not and attach \a data to \a host if it is possible.
76  */
77 MSG_error_t MSG_host_set_data(m_host_t host, void *data)
78 {
79   xbt_assert0((host != NULL), "Invalid parameters");
80   xbt_assert0((host->data == NULL), "Data already set");
81
82   /* Assign data */
83   host->data = data;
84
85   return MSG_OK;
86 }
87
88 /** \ingroup m_host_management
89  *
90  * \brief Return the user data of a #m_host_t.
91  *
92  * This functions checks whether \a host is a valid pointer or not and return
93    the user data associated to \a host if it is possible.
94  */
95 void *MSG_host_get_data(m_host_t host)
96 {
97
98   xbt_assert0((host != NULL), "Invalid parameters");
99
100   /* Return data */
101   return (host->data);
102 }
103
104 /** \ingroup m_host_management
105  *
106  * \brief Return the name of the #m_host_t.
107  *
108  * This functions checks whether \a host is a valid pointer or not and return
109    its name.
110  */
111 const char *MSG_host_get_name(m_host_t host)
112 {
113
114   xbt_assert0((host != NULL)
115               && (host->simdata != NULL), "Invalid parameters");
116
117   /* Return data */
118   return (host->name);
119 }
120
121 /** \ingroup m_host_management
122  *
123  * \brief Return the location on which the current process is executed.
124  */
125 m_host_t MSG_host_self(void)
126 {
127   return MSG_process_get_host(MSG_process_self());
128 }
129
130 /*
131  * Real function to destroy a host.
132  * MSG_host_destroy is just  a front_end that also removes it from 
133  * msg_global->host
134  */
135 void __MSG_host_destroy(m_host_t host)
136 {
137   simdata_host_t simdata = NULL;
138   int i = 0;
139   char alias[MAX_ALIAS_NAME + 1] = { 0 };       /* buffer used to build the key of the mailbox */
140
141   xbt_assert0((host != NULL), "Invalid parameters");
142
143   /* Clean Simulator data */
144   /* SIMIX host will be cleaned when MSG_clean calls SIMIX_clean */
145   simdata = (host)->simdata;
146
147   for (i = 0; i < msg_global->max_channel; i++) {
148     sprintf(alias, "%s:%d", host->name, i);
149     MSG_mailbox_free((void *) (simdata->mailboxes[i]));
150     memset(alias, 0, MAX_ALIAS_NAME + 1);
151   }
152
153   if (msg_global->max_channel > 0)
154     free(simdata->mailboxes);
155   SIMIX_mutex_destroy(simdata->mutex);
156   free(simdata);
157
158   /* Clean host structure */
159   free(host->name);
160   free(host);
161
162
163 }
164
165 /** \ingroup m_host_management
166  * \brief Return the current number of #m_host_t.
167  */
168 int MSG_get_host_number(void)
169 {
170   return (xbt_fifo_size(msg_global->host));
171 }
172
173 /** \ingroup m_host_management
174  * \brief Return a array of all the #m_host_t.
175  */
176 m_host_t *MSG_get_host_table(void)
177 {
178   return ((m_host_t *) xbt_fifo_to_array(msg_global->host));
179 }
180
181 /** \ingroup m_host_management
182  * \brief Return the number of MSG tasks currently running on a
183  * #m_host_t. The external load is not taken in account.
184  */
185 int MSG_get_host_msgload(m_host_t h)
186 {
187   xbt_assert0((h != NULL), "Invalid parameters");
188   xbt_assert0(0, "Not implemented yet");
189
190   return (0);
191 }
192
193 /** \ingroup m_host_management
194  * \brief Return the speed of the processor (in flop/s), regardless of 
195     the current load on the machine.
196  */
197 double MSG_get_host_speed(m_host_t h)
198 {
199   xbt_assert0((h != NULL), "Invalid parameters");
200
201   return (SIMIX_host_get_speed(h->simdata->smx_host));
202 }
203
204 /** \ingroup m_host_management
205  * \brief Returns the value of a given host property
206  *
207  * \param host a host
208  * \param name a property name
209  * \return value of a property (or NULL if property not set)
210  */
211 const char *MSG_host_get_property_value(m_host_t host, const char *name)
212 {
213   return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
214 }
215
216 /** \ingroup m_host_management
217  * \brief Returns a xbt_dynar_t consisting of the list of properties assigned to this host
218  *
219  * \param host a host
220  * \return a dict containing the properties
221  */
222 xbt_dict_t MSG_host_get_properties(m_host_t host)
223 {
224   xbt_assert0((host != NULL), "Invalid parameters");
225
226   return (SIMIX_host_get_properties(host->simdata->smx_host));
227 }
228
229
230 /** \ingroup msg_gos_functions
231  * \brief Determine if a host is available.
232  *
233  * \param h host to test
234  */
235 int MSG_host_is_avail(m_host_t h)
236 {
237   xbt_assert0((h != NULL), "Invalid parameters");
238   return (SIMIX_host_get_state(h->simdata->smx_host));
239 }