Logo AND Algorithmique Numérique Distribuée

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