Logo AND Algorithmique Numérique Distribuée

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