Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix: correct trace mask checking
[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_host_t smx_host = xbt_new0(s_smx_host_t, 1);
22   s_smx_process_t proc;
23
24   /* Host structure */
25   smx_host->name = xbt_strdup(name);
26   smx_host->data = data;
27   smx_host->host = workstation;
28   smx_host->process_list = xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
29
30   /* Update global variables */
31   xbt_dict_set(simix_global->host, smx_host->name, smx_host, &__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 = bprintf("Shutting down host %s, but it's not empty:", host->name);
109     char *tmp;
110     smx_process_t process = NULL;
111
112     xbt_swag_foreach(process, host->process_list) {
113       tmp = bprintf("%s\n\t%s", msg, process->name);
114       free(msg);
115       msg = tmp;
116     }
117     THROW1(arg_error, 0, "%s", msg);
118   }
119
120   xbt_swag_free(host->process_list);
121
122   /* Clean host structure */
123   free(host->name);
124   free(host);
125
126   return;
127 }
128
129 /**
130  * \brief Return the current number of #smx_host_t.
131  *
132  * \return Number of hosts
133  */
134 XBT_INLINE int SIMIX_host_get_number(void)
135 {
136   return (xbt_dict_size(simix_global->host));
137 }
138
139
140 /**
141  * \brief Return an array of all the #smx_host_t.
142  *
143  * \return List of all hosts (in a newly allocated table)
144  */
145 smx_host_t *SIMIX_host_get_table(void)
146 {
147   smx_host_t *res = xbt_new(smx_host_t, xbt_dict_size(simix_global->host));
148   smx_host_t h;
149   xbt_dict_cursor_t c;
150   char *name;
151   int i = 0;
152
153   xbt_dict_foreach(simix_global->host, c, name, h)
154     res[i++] = h;
155
156   return res;
157 }
158
159 /**
160  * \brief Return a dict of all the #smx_host_t.
161  *
162  * \return List of all hosts (as a #xbt_dict_t)
163  */
164 XBT_INLINE xbt_dict_t SIMIX_host_get_dict(void)
165 {
166   return simix_global->host;
167 }
168
169 /**
170  * \brief Return the speed of the processor.
171  *
172  * Return the speed (in Mflop/s), regardless of the current load on the machine.
173  * \param host SIMIX host
174  * \return Speed
175  */
176 XBT_INLINE double SIMIX_host_get_speed(smx_host_t host)
177 {
178   xbt_assert0((host != NULL), "Invalid parameters");
179
180   return (surf_workstation_model->extension.workstation.
181           get_speed(host->host, 1.0));
182 }
183
184 /**
185  * \brief Return the available speed of the processor.
186  *
187  * Return the available speed (in Mflop/s).
188  * \return Speed
189  */
190 XBT_INLINE double SIMIX_host_get_available_speed(smx_host_t host)
191 {
192   xbt_assert0((host != NULL), "Invalid parameters");
193
194   return (surf_workstation_model->extension.workstation.
195           get_available_speed(host->host));
196 }
197
198 /**
199  * \brief Return the host by its name
200  *
201  * Finds a smx_host_t using its name.
202  * \param name The name of an host.
203  * \return The corresponding host
204  */
205 XBT_INLINE smx_host_t SIMIX_host_get_by_name(const char *name)
206 {
207   xbt_assert0(((simix_global != NULL)
208                && (simix_global->host != NULL)), "Environment not set yet");
209
210   return xbt_dict_get_or_null(simix_global->host, name);
211 }
212
213 /**
214  * \brief Returns a xbt_dynar_t consisting of the list of properties assigned to this host
215  *
216  * \param host a host
217  * \return the dynamic array consisting of property names
218  */
219 XBT_INLINE xbt_dict_t SIMIX_host_get_properties(smx_host_t host)
220 {
221   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
222
223   return surf_workstation_model->extension.workstation.get_properties(host->host);
224 }
225
226
227 /**
228  * \brief Return the state of a workstation
229  *
230  * Return the state of a workstation. Two states are possible, 1 if the host is active or 0 if it has crashed.
231  * \param host The SIMIX host
232  * \return 1 if host is available or 0 if not.
233  */
234 XBT_INLINE int SIMIX_host_get_state(smx_host_t host)
235 {
236   xbt_assert0((host != NULL), "Invalid parameters (simix host is NULL)");
237
238   return (surf_workstation_model->extension.workstation.
239           get_state(host->host));
240 }