Logo AND Algorithmique Numérique Distribuée

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