Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More verbose on exception throwing (java still raises this)
[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,
19                          void *data)
20 {
21   smx_simdata_host_t simdata = xbt_new0(s_smx_simdata_host_t,1);
22   smx_host_t host = xbt_new0(s_smx_host_t,1);
23   s_smx_process_t proc;
24
25   /* Host structure */
26   host->name = xbt_strdup(name);
27   host->simdata = simdata;
28   host->data = data;
29
30   simdata->host = workstation;
31
32   simdata->process_list = 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) && (host->simdata != NULL), "Invalid parameters");
83
84   /* Return data */
85   return (host->name);
86 }
87
88 /** 
89  * \brief Return the location on which the current process is executed.
90  *
91  * Return the host,  more details in #SIMIX_process_get_host
92  * \return SIMIX host
93  */
94 smx_host_t SIMIX_host_self(void)
95 {
96   return SIMIX_process_get_host(SIMIX_process_self());
97 }
98
99 /*
100  * Real function for destroy a host.
101  * MSG_host_destroy is just  a front_end that also removes it from 
102  * msg_global->host
103  */
104 void __SIMIX_host_destroy(smx_host_t host)
105 {
106   smx_simdata_host_t simdata = NULL;
107
108   xbt_assert0((host != NULL), "Invalid parameters");
109
110  
111   /* Clean Simulator data */
112   simdata = host->simdata;
113
114   if (xbt_swag_size(simdata->process_list) != 0) {
115      char *msg=bprintf("Shutting down host %s, but it's not empty:", host->name);
116      char *tmp;
117      smx_process_t process = NULL;
118
119      xbt_swag_foreach(process, simdata->process_list) {
120         tmp = bprintf("%s\n\t%s",msg,process->name);
121         free(msg);
122         msg=tmp;
123      }
124      THROW1(arg_error,0,"%s",msg);
125   }
126       
127   xbt_swag_free(simdata->process_list);
128
129   free(simdata);
130
131   /* Clean host structure */
132   free(host->name);
133   free(host);
134
135   return;
136 }
137
138 /**
139  * \brief Return the current number of #smx_host_t.
140  *
141  * \return Number of hosts
142  */
143 int SIMIX_host_get_number(void)
144 {
145   return (xbt_fifo_size(simix_global->host));
146 }
147
148 /**
149  * \brief Return a array of all the #smx_host_t.
150  *
151  * \return List of all hosts
152  */
153 smx_host_t *SIMIX_host_get_table(void)
154 {
155   return ((smx_host_t *)xbt_fifo_to_array(simix_global->host));
156 }
157
158
159 /**
160  * \brief Return the speed of the processor.
161  *
162  * Return the speed (in Mflop/s), regardless of the current load on the machine.
163  * \param host SIMIX host
164  * \return Speed
165  */
166 double SIMIX_host_get_speed(smx_host_t host)
167 {
168   xbt_assert0((host!= NULL), "Invalid parameters");
169
170   return(surf_workstation_resource->
171          extension_public->get_speed(host->simdata->host,1.0));
172 }
173
174 /**
175  * \brief Return the available speed of the processor.
176  *
177  * Return the available speed (in Mflop/s).
178  * \return Speed
179  */
180 double SIMIX_host_get_available_speed(smx_host_t host)
181 {
182   xbt_assert0((host!= NULL), "Invalid parameters");
183
184   return(surf_workstation_resource->
185          extension_public->get_available_speed(host->simdata->host));
186 }
187
188 /**
189  * \brief Return the host by its name
190  *
191  * Finds a smx_host_t using its name.
192  * \param name The name of an host.
193  * \return The corresponding host
194  */
195 smx_host_t SIMIX_host_get_by_name(const char *name)
196 {
197   xbt_fifo_item_t i = NULL;
198   smx_host_t host = NULL;
199
200   xbt_assert0(((simix_global != NULL)
201           && (simix_global->host != NULL)), "Environment not set yet");
202
203   xbt_fifo_foreach(simix_global->host,i,host,smx_host_t) {
204     if(strcmp(host->name, name) == 0) return host;
205   }
206   return NULL;
207 }
208
209 /**
210  * \brief Return the state of a workstation
211  *
212  * Return the state of a workstation. Two states are possible, 1 if the host is active or 0 if it has crashed.
213  * \param host The SIMIX host
214  * \return 1 if host is available or 0 if not.
215  */
216 int SIMIX_host_get_state(smx_host_t host)
217 {
218   xbt_assert0((host!= NULL), "Invalid parameters");
219
220   return(surf_workstation_resource->
221          extension_public->get_state(host->simdata->host));
222
223 }
224
225