Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6b851bc4da4c2c216caf8028d2a61bbc04a7bbff
[simgrid.git] / src / simix / smx_host.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "private.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/dict.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix,
15                                 "Logging specific to SIMIX (hosts)");
16
17 /********************************* Host **************************************/
18 smx_host_t __SIMIX_host_create(const char *name,
19                                void *workstation, void *data)
20 {
21   smx_simdata_host_t simdata = xbt_new0(s_smx_simdata_host_t, 1);
22   smx_host_t host = xbt_new0(s_smx_host_t, 1);
23   s_smx_process_t proc;
24
25   /* Host structure */
26   host->name = xbt_strdup(name);
27   host->simdata = simdata;
28   host->data = data;
29
30   simdata->host = workstation;
31
32   simdata->process_list =
33     xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
34   /* Update global variables */
35
36   xbt_dict_set(simix_global->host, host->name, host, &__SIMIX_host_destroy);
37
38   return host;
39 }
40
41 /**
42  * \brief Set the user data of a #smx_host_t.
43  *
44  * This functions checks whether some data has already been associated to \a host or not and attach \a data to \a host if it is possible.
45  *      \param host SIMIX host
46  *      \param data User data
47  *
48  */
49 void SIMIX_host_set_data(smx_host_t host, void *data)
50 {
51   xbt_assert0((host != NULL), "Invalid parameters");
52   xbt_assert0((host->data == NULL), "Data already set");
53
54   /* Assign data */
55   host->data = data;
56
57   return;
58 }
59
60 /**
61  * \brief Return the user data of a #smx_host_t.
62  *
63  * This functions checks whether \a host is a valid pointer or not and return the user data associated to \a host if it is possible.
64  * \param host SIMIX host
65  */
66 void *SIMIX_host_get_data(smx_host_t host)
67 {
68   xbt_assert0((host != NULL), "Invalid parameters");
69
70   /* Return data */
71   return (host->data);
72 }
73
74 /**
75  * \brief Return the name of the #smx_host_t.
76  *
77  * This functions checks whether \a host is a valid pointer or not and return its name.
78  * \param host SIMIX host
79  */
80 const char *SIMIX_host_get_name(smx_host_t host)
81 {
82
83   xbt_assert0((host != NULL)
84               && (host->simdata != NULL), "Invalid parameters");
85
86   /* Return data */
87   return (host->name);
88 }
89
90 /**
91  * \brief Return the location on which the current process is executed.
92  *
93  * Return the host,  more details in #SIMIX_process_get_host
94  * \return SIMIX host
95  */
96 smx_host_t SIMIX_host_self(void)
97 {
98   return SIMIX_process_get_host(SIMIX_process_self());
99 }
100
101 /*
102  * Real function for destroy a host.
103  * MSG_host_destroy is just  a front_end that also removes it from
104  * msg_global->host
105  */
106 void __SIMIX_host_destroy(void *h)
107 {
108   smx_host_t host = (smx_host_t) h;
109   smx_simdata_host_t simdata = NULL;
110
111   xbt_assert0((host != NULL), "Invalid parameters");
112
113
114   /* Clean Simulator data */
115   simdata = host->simdata;
116
117   if (xbt_swag_size(simdata->process_list) != 0) {
118     char *msg =
119       bprintf("Shutting down host %s, but it's not empty:", host->name);
120     char *tmp;
121     smx_process_t process = NULL;
122
123     xbt_swag_foreach(process, simdata->process_list) {
124       tmp = bprintf("%s\n\t%s", msg, process->name);
125       free(msg);
126       msg = tmp;
127     }
128     THROW1(arg_error, 0, "%s", msg);
129   }
130
131   xbt_swag_free(simdata->process_list);
132
133   free(simdata);
134
135   /* Clean host structure */
136   free(host->name);
137   free(host);
138
139   return;
140 }
141
142 /**
143  * \brief Return the current number of #smx_host_t.
144  *
145  * \return Number of hosts
146  */
147 int SIMIX_host_get_number(void)
148 {
149   return (xbt_dict_size(simix_global->host));
150 }
151
152
153 /**
154  * \brief Return an array of all the #smx_host_t.
155  *
156  * \return List of all hosts (in a newly allocated table)
157  */
158 smx_host_t *SIMIX_host_get_table(void)
159 {
160   smx_host_t *res = xbt_new(smx_host_t, xbt_dict_size(simix_global->host));
161   smx_host_t h;
162   xbt_dict_cursor_t c;
163   char *name;
164   int i = 0;
165
166   xbt_dict_foreach(simix_global->host, c, name, h)
167     res[i++] = h;
168
169   return res;
170 }
171
172 /**
173  * \brief Return a dict of all the #smx_host_t.
174  *
175  * \return List of all hosts (as a #xbt_dict_t)
176  */
177 xbt_dict_t SIMIX_host_get_dict(void)
178 {
179   return simix_global->host;
180 }
181
182 /**
183  * \brief Return the speed of the processor.
184  *
185  * Return the speed (in Mflop/s), regardless of the current load on the machine.
186  * \param host SIMIX host
187  * \return Speed
188  */
189 double SIMIX_host_get_speed(smx_host_t host)
190 {
191   xbt_assert0((host != NULL), "Invalid parameters");
192
193   return (surf_workstation_model->extension.workstation.
194           get_speed(host->simdata->host, 1.0));
195 }
196
197 /**
198  * \brief Return the available speed of the processor.
199  *
200  * Return the available speed (in Mflop/s).
201  * \return Speed
202  */
203 double SIMIX_host_get_available_speed(smx_host_t host)
204 {
205   xbt_assert0((host != NULL), "Invalid parameters");
206
207   return (surf_workstation_model->extension.workstation.
208           get_available_speed(host->simdata->host));
209 }
210
211 /**
212  * \brief Return the host by its name
213  *
214  * Finds a smx_host_t using its name.
215  * \param name The name of an host.
216  * \return The corresponding host
217  */
218 smx_host_t SIMIX_host_get_by_name(const char *name)
219 {
220   xbt_assert0(((simix_global != NULL)
221                && (simix_global->host != NULL)), "Environment not set yet");
222
223   return xbt_dict_get_or_null(simix_global->host, name);
224 }
225
226 /**
227  * \brief Returns a xbt_dynar_t consisting of the list of properties assigned to this host
228  *
229  * \param host a host
230  * \return the dynamic array consisting of property names
231  */
232 xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
233 {
234   xbt_assert0((host != NULL), "Invalid parameters");
235
236   return surf_workstation_model->extension.workstation.get_properties(host->simdata->host);
237
238 }
239
240
241 /**
242  * \brief Return the state of a workstation
243  *
244  * Return the state of a workstation. Two states are possible, 1 if the host is active or 0 if it has crashed.
245  * \param host The SIMIX host
246  * \return 1 if host is available or 0 if not.
247  */
248 int SIMIX_host_get_state(smx_host_t host)
249 {
250   xbt_assert0((host != NULL), "Invalid parameters");
251
252   return (surf_workstation_model->extension.workstation.
253           get_state(host->simdata->host));
254
255 }