Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A sh.exe appeared in appeveyor path, breaking mingw
[simgrid.git] / src / msg / msg_host.c
1 /* Copyright (c) 2004-2015. 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 "src/msg/msg_private.h"
8 #include "src/msg/msg_mailbox.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "simgrid/simix.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg);
14
15 /** @addtogroup m_host_management
16  * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Hosts" --> \endhtmlonly
17  * (#msg_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 msg_host_t
26  */
27
28 /********************************* Host **************************************/
29 msg_host_t __MSG_host_create(sg_host_t host) // FIXME: don't return our parameter
30 {
31   msg_host_priv_t priv = xbt_new0(s_msg_host_priv_t, 1);
32
33 #ifdef MSG_USE_DEPRECATED
34   int i;
35   char alias[MAX_ALIAS_NAME + 1] = { 0 };       /* buffer used to build the key of the mailbox */
36
37   priv->mailboxes = (msg_global->max_channel > 0) ?
38     xbt_new0(msg_mailbox_t, msg_global->max_channel) : NULL;
39
40   for (i = 0; i < msg_global->max_channel; i++) {
41     sprintf(alias, "%s:%d", name, i);
42
43     /* the key of the mailbox (in this case) is build from the name of the host and the channel number */
44     priv->mailboxes[i] = MSG_mailbox_new(alias);
45     memset(alias, 0, MAX_ALIAS_NAME + 1);
46   }
47 #endif
48
49
50   priv->dp_objs = xbt_dict_new();
51   priv->dp_enabled = 0;
52   priv->dp_updated_by_deleted_tasks = 0;
53   priv->is_migrating = 0;
54
55   priv->affinity_mask_db = xbt_dict_new_homogeneous(NULL);
56
57   priv->file_descriptor_table = xbt_dynar_new(sizeof(int), NULL);
58   for (int i=1023; i>=0;i--)
59     xbt_dynar_push_as(priv->file_descriptor_table, int, i);
60
61   sg_host_msg_set(host,priv);
62   
63   return host;
64 }
65
66 /** \ingroup m_host_management
67  * \brief Finds a msg_host_t using its name.
68  *
69  * This is a name directory service
70  * \param name the name of an host.
71  * \return the corresponding host
72  */
73 msg_host_t MSG_host_by_name(const char *name)
74 {
75   return (msg_host_t) xbt_lib_get_elm_or_null(host_lib,name);
76 }
77
78 static const char *msg_data = "data";
79 /** \ingroup m_host_management
80  *
81  * \brief Set the user data of a #msg_host_t.
82  *
83  * This functions checks whether some data has already been associated to \a host
84    or not and attach \a data to \a host if it is possible.
85  */
86 msg_error_t MSG_host_set_data(msg_host_t host, void *data)
87 {
88   MSG_host_set_property_value(host, msg_data, data, NULL);
89   return MSG_OK;
90 }
91
92 /** \ingroup m_host_management
93  *
94  * \brief Return the user data of a #msg_host_t.
95  *
96  * This functions checks whether \a host is a valid pointer or not and return
97    the user data associated to \a host if it is possible.
98  */
99 void *MSG_host_get_data(msg_host_t host)
100 {
101   return (void *)MSG_host_get_property_value(host, msg_data);
102 }
103
104 /** \ingroup m_host_management
105  *
106  * \brief Return the name of the #msg_host_t.
107  *
108  * This functions checks whether \a host is a valid pointer or not and return
109    its name.
110  */
111 const char *MSG_host_get_name(msg_host_t host) {
112   return SIMIX_host_get_name(host);
113 }
114
115 /** \ingroup m_host_management
116  *
117  * \brief Return the location on which the current process is executed.
118  */
119 msg_host_t MSG_host_self(void)
120 {
121   return MSG_process_get_host(NULL);
122 }
123
124
125 /** \ingroup m_host_management
126  *
127  * \brief Start the host if it is off
128  *
129  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref SURF_plugin_energy for more info on DVFS.
130  */
131 void MSG_host_on(msg_host_t host)
132 {
133   simcall_host_on(host);
134 }
135
136 /** \ingroup m_host_management
137  *
138  * \brief Stop the host if it is on
139  *
140  * See also #MSG_host_is_on() and #MSG_host_is_off() to test the current state of the host and @ref SURF_plugin_energy for more info on DVFS.
141  */
142 void MSG_host_off(msg_host_t host)
143 {
144   simcall_host_off(host);
145 }
146
147 /*
148  * \brief Frees private data of a host (internal call only)
149  */
150 void __MSG_host_priv_free(msg_host_priv_t priv)
151 {
152
153   if (priv == NULL)
154           return;
155   unsigned int size = xbt_dict_size(priv->dp_objs);
156   if (size > 0)
157     XBT_WARN("dp_objs: %u pending task?", size);
158   xbt_dict_free(&priv->dp_objs);
159   xbt_dict_free(&priv->affinity_mask_db);
160   xbt_dynar_free(&priv->file_descriptor_table);
161 #ifdef MSG_USE_DEPRECATED
162   free(priv->mailboxes);
163 #endif
164
165   free(priv);
166 }
167
168 /*
169  * \brief Destroys a host (internal call only)
170  */
171 void __MSG_host_destroy(msg_host_t host) //FIXME: killme?
172 {
173   /* TODO:
174    * What happens if VMs still remain on this host?
175    * Revisit here after the surf layer gets stable.
176    **/
177   sg_host_msg_destroy(host);
178 }
179
180 /** \ingroup m_host_management
181  * \brief Return the current number MSG hosts.
182  */
183 int MSG_get_host_number(void)
184 {
185   return xbt_lib_length(host_lib);
186 }
187
188 #ifdef MSG_USE_DEPRECATED
189 msg_host_t *MSG_get_host_table(void)
190 {
191       void **array;
192     int i = 0;
193     xbt_lib_cursor_t cursor;
194     char *key;
195     void **data;
196
197     if (xbt_lib_length(host_lib) == 0)
198     return NULL;
199     else
200     array = xbt_new0(void *, xbt_lib_length(host_lib));
201
202     xbt_lib_foreach(host_lib, cursor, key, data) {
203       if(routing_get_network_element_type(key) == SURF_NETWORK_ELEMENT_HOST)
204         array[i++] = data[MSG_HOST_LEVEL];
205     }
206
207     return (msg_host_t *)array;
208 }
209 #endif
210
211 /** \ingroup m_host_management
212  * \brief Return a dynar containing all the hosts declared at a given point of time
213  * \remark The host order in the returned array is generally different from the host creation/declaration order in the XML platform (we use a hash table internally)
214  */
215 xbt_dynar_t MSG_hosts_as_dynar(void) {
216   return sg_hosts_as_dynar();
217 }
218
219 /** \ingroup m_host_management
220  * \brief Return the number of MSG tasks currently running on a
221  * #msg_host_t. The external load is not taken in account.
222  */
223 int MSG_get_host_msgload(msg_host_t h)
224 {
225   xbt_assert((h != NULL), "Invalid parameters");
226   xbt_die( "Not implemented yet");
227
228   return (0);
229 }
230
231 /** \ingroup m_host_management
232  * \brief Return the speed of the processor (in flop/s), regardless of 
233     the current load on the machine.
234  */
235 double MSG_get_host_speed(msg_host_t h)
236 {
237   xbt_assert((h != NULL), "Invalid parameters");
238
239   return sg_host_get_speed(h);
240 }
241
242
243 /** \ingroup m_host_management
244  * \brief Return the number of cores.
245  *
246  * \param host a host
247  * \return the number of cores
248  */
249 int MSG_host_get_core_number(msg_host_t host)
250 {
251   xbt_assert((host != NULL), "Invalid parameters");
252
253   return sg_host_get_core(host);
254 }
255
256 /** \ingroup m_host_management
257  * \brief Return the list of processes attached to an host.
258  *
259  * \param host a host
260  * \return a swag with the attached processes
261  */
262 xbt_swag_t MSG_host_get_process_list(msg_host_t host)
263 {
264   xbt_assert((host != NULL), "Invalid parameters");
265
266   return (simcall_host_get_process_list(host));
267 }
268
269
270 /** \ingroup m_host_management
271  * \brief Returns the value of a given host property
272  *
273  * \param host a host
274  * \param name a property name
275  * \return value of a property (or NULL if property not set)
276  */
277 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
278 {
279   return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
280 }
281
282 /** \ingroup m_host_management
283  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
284  *
285  * \param host a host
286  * \return a dict containing the properties
287  */
288 xbt_dict_t MSG_host_get_properties(msg_host_t host)
289 {
290   xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
291
292   return (simcall_host_get_properties(host));
293 }
294
295 /** \ingroup m_host_management
296  * \brief Change the value of a given host property
297  *
298  * \param host a host
299  * \param name a property name
300  * \param value what to change the property to
301  * \param free_ctn the freeing function to use to kill the value on need
302  */
303 void MSG_host_set_property_value(msg_host_t host, const char *name, char *value,void_f_pvoid_t free_ctn) {
304
305   xbt_dict_set(MSG_host_get_properties(host), name, value,free_ctn);
306 }
307
308
309 /** @ingroup m_host_management
310  *
311  * @brief Determine if a host is up and running.
312  *
313  * See also #MSG_host_on() and #MSG_host_off() to switch the host ON and OFF and @ref SURF_plugin_energy for more info on DVFS.
314  *
315  * @param host host to test
316  * @return Returns true if the host is up and running, and false if it's currently down
317  */
318 int MSG_host_is_on(msg_host_t host)
319 {
320   xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
321   return sg_host_get_state(host);
322 }
323 /** @ingroup m_host_management
324  *
325  * @brief Determine if a host is currently off.
326  *
327  * See also #MSG_host_on() and #MSG_host_off() to switch the host ON and OFF and @ref SURF_plugin_energy for more info on DVFS.
328  */
329 int MSG_host_is_off(msg_host_t host)
330 {
331   xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
332   return !(sg_host_get_state(host));
333 }
334
335 /** \ingroup m_host_management
336  * \brief Set the parameters of a given host
337  *
338  * \param host a host
339  * \param params a prameter object
340  */
341 void MSG_host_set_params(msg_host_t host, vm_params_t params)
342 {
343   simcall_host_set_params(host, params);
344 }
345
346 /** \ingroup m_host_management
347  * \brief Get the parameters of a given host
348  *
349  * \param host a host
350  * \param params a prameter object
351  */
352 void MSG_host_get_params(msg_host_t host, vm_params_t params)
353 {
354   simcall_host_get_params(host, params);
355 }
356
357 /** \ingroup m_host_management
358  * \brief Return the speed of the processor (in flop/s) at a given pstate. See also @ref SURF_plugin_energy.
359  *
360  * \param  host host to test
361  * \param pstate_index pstate to test
362  * \return Returns the processor speed associated with pstate_index
363  */
364 double MSG_host_get_power_peak_at(msg_host_t host, int pstate_index) {
365           xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
366           return (simcall_host_get_power_peak_at(host, pstate_index));
367 }
368
369 /** \ingroup m_host_management
370  * \brief Return the current speed of the processor (in flop/s)
371  *
372  * \param  host host to test
373  * \return Returns the current processor speed
374  */
375 double MSG_host_get_current_power_peak(msg_host_t host) {
376           xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
377           return simcall_host_get_current_power_peak(host);
378 }
379
380 /** \ingroup m_host_management
381  * \brief Return the total count of pstates defined for a host. See also @ref SURF_plugin_energy.
382  *
383  * \param  host host to test
384  */
385 int MSG_host_get_nb_pstates(msg_host_t host) {
386
387           xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
388           return (simcall_host_get_nb_pstates(host));
389 }
390
391 /** \ingroup m_host_management
392  * \brief Sets the speed of the processor (in flop/s) at a given pstate. See also @ref SURF_plugin_energy.
393  *
394  * \param  host host to test
395  * \param pstate_index pstate to switch to
396  */
397 void MSG_host_set_pstate(msg_host_t host, int pstate_index) {
398           xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
399
400           simcall_host_set_pstate(host, pstate_index);
401 }
402 /** \ingroup m_host_management
403  * \brief Gets the pstate at which the given host is currently running. See also @ref SURF_plugin_energy.
404  *
405  * \param  host host to test
406  */
407 int MSG_host_get_pstate(msg_host_t host) {
408           return sg_host_get_pstate(host);
409 }
410
411 /** \ingroup m_host_management
412  * \brief Return the total energy consumed by a host (in Joules). See also @ref SURF_plugin_energy.
413  *
414  * \param  host host to test
415  * \return Returns the consumed energy
416  */
417 double MSG_host_get_consumed_energy(msg_host_t host) {
418           xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
419           return sg_host_get_consumed_energy(host);
420 }
421 /** \ingroup m_host_management
422  * \brief Returns the amount of watt dissipated at the given pstate when the host is idling
423  *
424  */
425 double MSG_host_get_wattmin_at(msg_host_t host, int pstate){
426         return simcall_host_get_wattmin_at(host, pstate);
427 }
428 /** \ingroup m_host_management
429  * \brief Returns the amount of watt dissipated at the given pstate when the host burns CPU at 100%
430  *
431  */
432 double MSG_host_get_wattmax_at(msg_host_t host, int pstate){
433         return simcall_host_get_wattmax_at(host, pstate);
434 }
435
436 /** \ingroup m_host_management
437  * \brief Return the list of mount point names on an host.
438  * \param host a host
439  * \return a dict containing all mount point on the host (mount_name => msg_storage_t)
440  */
441 xbt_dict_t MSG_host_get_mounted_storage_list(msg_host_t host)
442 {
443   xbt_assert((host != NULL), "Invalid parameters");
444   return (simcall_host_get_mounted_storage_list(host));
445 }
446
447 /** \ingroup m_host_management
448  * \brief Return the list of storages attached to an host.
449  * \param host a host
450  * \return a dynar containing all storages (name) attached to the host
451  */
452 xbt_dynar_t MSG_host_get_attached_storage_list(msg_host_t host)
453 {
454   xbt_assert((host != NULL), "Invalid parameters");
455   return (simcall_host_get_attached_storage_list(host));
456 }
457
458 /** \ingroup m_host_management
459  * \brief Return the content of mounted storages on an host.
460  * \param host a host
461  * \return a dict containing content (as a dict) of all storages mounted on the host
462  */
463 xbt_dict_t MSG_host_get_storage_content(msg_host_t host)
464 {
465   xbt_assert((host != NULL), "Invalid parameters");
466   xbt_dict_t contents = xbt_dict_new_homogeneous(NULL);
467   msg_storage_t storage;
468   char* storage_name;
469   char* mount_name;
470   xbt_dict_cursor_t cursor = NULL;
471
472   xbt_dict_t storage_list = simcall_host_get_mounted_storage_list(host);
473
474   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
475     storage = (msg_storage_t)xbt_lib_get_elm_or_null(storage_lib,storage_name);
476     xbt_dict_t content = simcall_storage_get_content(storage);
477     xbt_dict_set(contents,mount_name, content,NULL);
478   }
479   xbt_dict_free(&storage_list);
480   return contents;
481 }
482
483 int __MSG_host_get_file_descriptor_id(msg_host_t host){
484   msg_host_priv_t priv = sg_host_msg(host);
485   xbt_assert(!xbt_dynar_is_empty(priv->file_descriptor_table),
486     "Too much files are opened! Some have to be closed.");
487   return xbt_dynar_pop_as(priv->file_descriptor_table, int);
488 }
489
490 void __MSG_host_release_file_descriptor_id(msg_host_t host, int id){
491   msg_host_priv_t priv = sg_host_msg(host);
492   xbt_dynar_push_as(priv->file_descriptor_table, int, id);
493 }