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