Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[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(xbt_strdup(alias));
54     memset(alias, 0, MAX_ALIAS_NAME + 1);
55   }
56
57   SIMIX_req_host_set_data(workstation, host);
58
59   /* Update global variables */
60   xbt_fifo_unshift(msg_global->host, 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_assert0((host != NULL), "Invalid parameters");
75   xbt_assert0((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_assert0((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_assert0((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(MSG_process_self());
123 }
124
125 /*
126  * Real function to destroy a host.
127  * MSG_host_destroy is just  a front_end that also removes it from 
128  * msg_global->host
129  */
130 void __MSG_host_destroy(m_host_t host)
131 {
132   simdata_host_t simdata = NULL;
133   int i = 0;
134   char alias[MAX_ALIAS_NAME + 1] = { 0 };       /* buffer used to build the key of the mailbox */
135
136   xbt_assert0((host != NULL), "Invalid parameters");
137
138   /* Clean Simulator data */
139   /* SIMIX host will be cleaned when MSG_clean calls SIMIX_clean */
140   simdata = (host)->simdata;
141
142   for (i = 0; i < msg_global->max_channel; i++) {
143     sprintf(alias, "%s:%d", host->name, i);
144     MSG_mailbox_free((void *) (simdata->mailboxes[i]));
145     memset(alias, 0, MAX_ALIAS_NAME + 1);
146   }
147
148   if (msg_global->max_channel > 0)
149     free(simdata->mailboxes);
150
151   free(simdata);
152
153   /* Clean host structure */
154   free(host->name);
155   free(host);
156
157
158 }
159
160 /** \ingroup m_host_management
161  * \brief Return the current number of #m_host_t.
162  */
163 int MSG_get_host_number(void)
164 {
165   return (xbt_fifo_size(msg_global->host));
166 }
167
168 /** \ingroup m_host_management
169  * \brief Return a array of all the #m_host_t.
170  */
171 m_host_t *MSG_get_host_table(void)
172 {
173   return ((m_host_t *) xbt_fifo_to_array(msg_global->host));
174 }
175
176 /** \ingroup m_host_management
177  * \brief Return the number of MSG tasks currently running on a
178  * #m_host_t. The external load is not taken in account.
179  */
180 int MSG_get_host_msgload(m_host_t h)
181 {
182   xbt_assert0((h != NULL), "Invalid parameters");
183   xbt_assert0(0, "Not implemented yet");
184
185   return (0);
186 }
187
188 /** \ingroup m_host_management
189  * \brief Return the speed of the processor (in flop/s), regardless of 
190     the current load on the machine.
191  */
192 double MSG_get_host_speed(m_host_t h)
193 {
194   xbt_assert0((h != NULL), "Invalid parameters");
195
196   return (SIMIX_req_host_get_speed(h->simdata->smx_host));
197 }
198
199 /** \ingroup m_host_management
200  * \brief Returns the value of a given host property
201  *
202  * \param host a host
203  * \param name a property name
204  * \return value of a property (or NULL if property not set)
205  */
206 const char *MSG_host_get_property_value(m_host_t host, const char *name)
207 {
208   return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
209 }
210
211 /** \ingroup m_host_management
212  * \brief Returns a xbt_dynar_t consisting of the list of properties assigned to this host
213  *
214  * \param host a host
215  * \return a dict containing the properties
216  */
217 xbt_dict_t MSG_host_get_properties(m_host_t host)
218 {
219   xbt_assert0((host != NULL), "Invalid parameters (host is NULL)");
220
221   return (SIMIX_req_host_get_properties(host->simdata->smx_host));
222 }
223
224
225 /** \ingroup msg_gos_functions
226  * \brief Determine if a host is available.
227  *
228  * \param h host to test
229  */
230 int MSG_host_is_avail(m_host_t h)
231 {
232   xbt_assert0((h != NULL), "Invalid parameters (host is NULL)");
233   return (SIMIX_req_host_get_state(h->simdata->smx_host));
234 }