Logo AND Algorithmique Numérique Distribuée

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