Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ansi C declaration of the variables (at the beginning of the blocks)
[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
14 /** \defgroup m_host_management Management functions of Hosts
15  *  \brief This section describes the host structure of MSG
16  */
17 /** @addtogroup m_host_management
18  *     \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Hosts" --> \endhtmlonly
19  * (#m_host_t) and the functions for managing it.
20  *  
21  *  A <em>location</em> (or <em>host</em>) is any possible place where
22  *  a process may run. Thus it may be represented as a
23  *  <em>physical resource with computing capabilities</em>, some
24  *  <em>mailboxes</em> to enable running process to communicate with
25  *  remote ones, and some <em>private data</em> that can be only
26  *  accessed by local process.
27  *  \see m_host_t
28  */
29
30 /********************************* Host **************************************/
31 m_host_t __MSG_host_create(smx_host_t workstation, void *data)
32 {
33   const char *name;
34   simdata_host_t simdata = xbt_new0(s_simdata_host_t, 1);
35   m_host_t host = xbt_new0(s_m_host_t, 1);
36   int i;
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   simdata->mbox = xbt_new0(xbt_fifo_t, msg_global->max_channel);
47   for (i = 0; i < msg_global->max_channel; i++)
48     simdata->mbox[i] = xbt_fifo_new();
49
50   simdata->sleeping = xbt_new0(smx_cond_t, msg_global->max_channel);
51   simdata->mutex = SIMIX_mutex_init();
52   SIMIX_host_set_data(workstation, host);
53
54   /* Update global variables */
55   xbt_fifo_unshift(msg_global->host, host);
56
57   return host;
58 }
59
60 /** \ingroup m_host_management
61  *
62  * \brief Set the user data of a #m_host_t.
63  *
64  * This functions checks whether some data has already been associated to \a host 
65    or not and attach \a data to \a host if it is possible.
66  */
67 MSG_error_t MSG_host_set_data(m_host_t host, void *data)
68 {
69   xbt_assert0((host != NULL), "Invalid parameters");
70   xbt_assert0((host->data == NULL), "Data already set");
71
72   /* Assign data */
73   host->data = data;
74
75   return MSG_OK;
76 }
77
78 /** \ingroup m_host_management
79  *
80  * \brief Return the user data of a #m_host_t.
81  *
82  * This functions checks whether \a host is a valid pointer or not and return
83    the user data associated to \a host if it is possible.
84  */
85 void *MSG_host_get_data(m_host_t host)
86 {
87
88   xbt_assert0((host != NULL), "Invalid parameters");
89
90   /* Return data */
91   return (host->data);
92 }
93
94 /** \ingroup m_host_management
95  *
96  * \brief Return the name of the #m_host_t.
97  *
98  * This functions checks whether \a host is a valid pointer or not and return
99    its name.
100  */
101 const char *MSG_host_get_name(m_host_t host)
102 {
103
104   xbt_assert0((host != NULL)
105               && (host->simdata != NULL), "Invalid parameters");
106
107   /* Return data */
108   return (host->name);
109 }
110
111 /** \ingroup m_host_management
112  *
113  * \brief Return the location on which the current process is executed.
114  */
115 m_host_t MSG_host_self(void)
116 {
117   return MSG_process_get_host(MSG_process_self());
118 }
119
120 /*
121  * Real function to destroy a host.
122  * MSG_host_destroy is just  a front_end that also removes it from 
123  * msg_global->host
124  */
125 void __MSG_host_destroy(m_host_t host)
126 {
127   simdata_host_t simdata = NULL;
128   int i = 0;
129
130   xbt_assert0((host != NULL), "Invalid parameters");
131
132   /* Clean Simulator data */
133   /* SIMIX host will be cleaned when MSG_clean calls SIMIX_clean */
134   simdata = (host)->simdata;
135
136   for (i = 0; i < msg_global->max_channel; i++)
137     xbt_fifo_free(simdata->mbox[i]);
138   free(simdata->mbox);
139   free(simdata->sleeping);
140   SIMIX_mutex_destroy(simdata->mutex);
141   free(simdata);
142
143   /* Clean host structure */
144   free(host->name);
145   free(host);
146
147   return;
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_fifo_size(msg_global->host));
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   return ((m_host_t *) xbt_fifo_to_array(msg_global->host));
164 }
165
166 /** \ingroup m_host_management
167  * \brief Return the number of MSG tasks currently running on a
168  * #m_host_t. The external load is not taken in account.
169  */
170 int MSG_get_host_msgload(m_host_t h)
171 {
172   xbt_assert0((h != NULL), "Invalid parameters");
173   xbt_assert0(0, "Not implemented yet");
174
175   return (0);
176 }
177
178 /** \ingroup m_host_management
179  * \brief Return the speed of the processor (in flop/s), regardless of 
180     the current load on the machine.
181  */
182 double MSG_get_host_speed(m_host_t h)
183 {
184   xbt_assert0((h != NULL), "Invalid parameters");
185
186   return (SIMIX_host_get_speed(h->simdata->smx_host));
187 }
188
189 /** \ingroup m_host_management
190  * \brief Returns the value of a given host property
191  *
192  * \param host a host
193  * \param name a property name
194  * \return value of a property (or NULL if property not set)
195  */
196 const char* MSG_host_get_property_value(m_host_t host, const char* name)
197 {
198   return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
199 }
200
201 /** \ingroup m_host_management
202  * \brief Returns a xbt_dynar_t consisting of the list of properties assigned to this host
203  *
204  * \param host a host
205  * \return a dict containing the properties
206  */
207 xbt_dict_t MSG_host_get_properties(m_host_t host)
208 {
209   xbt_assert0((host != NULL), "Invalid parameters");
210
211   return (SIMIX_host_get_properties(host->simdata->smx_host));
212 }
213
214
215 /** \ingroup msg_gos_functions
216  * \brief Determine if a host is available.
217  *
218  * \param h host to test
219  */
220 int MSG_host_is_avail(m_host_t h)
221 {
222   xbt_assert0((h != NULL), "Invalid parameters");
223   return (SIMIX_host_get_state(h->simdata->smx_host));
224 }