Logo AND Algorithmique Numérique Distribuée

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