Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / msg / host.c
1 /* Copyright (c) 2002-2007 Arnaud Legrand.                                  */
2 /* Copyright (c) 2007 Bruno Donassolo.                                      */
3 /* All rights reserved.                                                     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "msg/private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "mailbox.h"
12
13 /** \defgroup m_host_management Management functions of Hosts
14  *  \brief This section describes the host structure of MSG
15  */
16 /** @addtogroup m_host_management
17  *     \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Hosts" --> \endhtmlonly
18  * (#m_host_t) and the functions for managing it.
19  *  
20  *  A <em>location</em> (or <em>host</em>) is any possible place where
21  *  a process may run. Thus it may be represented as a
22  *  <em>physical resource with computing capabilities</em>, some
23  *  <em>mailboxes</em> to enable running process to communicate with
24  *  remote ones, and some <em>private data</em> that can be only
25  *  accessed by local process.
26  *  \see m_host_t
27  */
28
29 /********************************* Host **************************************/
30 m_host_t __MSG_host_create(smx_host_t workstation, void *data)
31 {
32   const char *name;
33   simdata_host_t simdata = xbt_new0(s_simdata_host_t, 1);
34   m_host_t host = xbt_new0(s_m_host_t, 1);
35   int i;
36
37   char alias[MAX_ALIAS_NAME + 1] = { 0 };       /* buffer used to build the key of the mailbox */
38
39   name = SIMIX_host_get_name(workstation);
40   /* Host structure */
41   host->name = xbt_strdup(name);
42   host->simdata = simdata;
43   host->data = data;
44
45   simdata->smx_host = workstation;
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_create(alias);
55     memset(alias, 0, MAX_ALIAS_NAME + 1);
56   }
57
58   simdata->mutex = SIMIX_mutex_init();
59   SIMIX_host_set_data(workstation, host);
60
61   /* Update global variables */
62   xbt_fifo_unshift(msg_global->host, host);
63
64   return host;
65 }
66
67 /** \ingroup m_host_management
68  *
69  * \brief Set the user data of a #m_host_t.
70  *
71  * This functions checks whether some data has already been associated to \a host 
72    or not and attach \a data to \a host if it is possible.
73  */
74 MSG_error_t MSG_host_set_data(m_host_t host, void *data)
75 {
76   xbt_assert0((host != NULL), "Invalid parameters");
77   xbt_assert0((host->data == NULL), "Data already set");
78
79   /* Assign data */
80   host->data = data;
81
82   return MSG_OK;
83 }
84
85 /** \ingroup m_host_management
86  *
87  * \brief Return the user data of a #m_host_t.
88  *
89  * This functions checks whether \a host is a valid pointer or not and return
90    the user data associated to \a host if it is possible.
91  */
92 void *MSG_host_get_data(m_host_t host)
93 {
94
95   xbt_assert0((host != NULL), "Invalid parameters");
96
97   /* Return data */
98   return (host->data);
99 }
100
101 /** \ingroup m_host_management
102  *
103  * \brief Return the name of the #m_host_t.
104  *
105  * This functions checks whether \a host is a valid pointer or not and return
106    its name.
107  */
108 const char *MSG_host_get_name(m_host_t host)
109 {
110
111   xbt_assert0((host != NULL)
112               && (host->simdata != NULL), "Invalid parameters");
113
114   /* Return data */
115   return (host->name);
116 }
117
118 /** \ingroup m_host_management
119  *
120  * \brief Return the location on which the current process is executed.
121  */
122 m_host_t MSG_host_self(void)
123 {
124   return MSG_process_get_host(MSG_process_self());
125 }
126
127 /*
128  * Real function to destroy a host.
129  * MSG_host_destroy is just  a front_end that also removes it from 
130  * msg_global->host
131  */
132 void __MSG_host_destroy(m_host_t host)
133 {
134   simdata_host_t simdata = NULL;
135   int i = 0;
136   char alias[MAX_ALIAS_NAME + 1] = { 0 };       /* buffer used to build the key of the mailbox */
137
138   xbt_assert0((host != NULL), "Invalid parameters");
139
140   /* Clean Simulator data */
141   /* SIMIX host will be cleaned when MSG_clean calls SIMIX_clean */
142   simdata = (host)->simdata;
143
144   for (i = 0; i < msg_global->max_channel; i++) {
145     sprintf(alias, "%s:%d", host->name, i);
146     MSG_mailbox_free((void *) (simdata->mailboxes[i]));
147     memset(alias, 0, MAX_ALIAS_NAME + 1);
148   }
149
150   if (msg_global->max_channel > 0)
151     free(simdata->mailboxes);
152   SIMIX_mutex_destroy(simdata->mutex);
153   free(simdata);
154
155   /* Clean host structure */
156   free(host->name);
157   free(host);
158
159
160 }
161
162 /** \ingroup m_host_management
163  * \brief Return the current number of #m_host_t.
164  */
165 int MSG_get_host_number(void)
166 {
167   return (xbt_fifo_size(msg_global->host));
168 }
169
170 /** \ingroup m_host_management
171  * \brief Return a array of all the #m_host_t.
172  */
173 m_host_t *MSG_get_host_table(void)
174 {
175   return ((m_host_t *) xbt_fifo_to_array(msg_global->host));
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_assert0((h != NULL), "Invalid parameters");
185   xbt_assert0(0, "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_assert0((h != NULL), "Invalid parameters");
197
198   return (SIMIX_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_assert0((host != NULL), "Invalid parameters (host is NULL)");
222
223   return (SIMIX_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_assert0((h != NULL), "Invalid parameters (host is NULL)");
235   return (SIMIX_host_get_state(h->simdata->smx_host));
236 }