Logo AND Algorithmique Numérique Distribuée

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