Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simix Initial structure
[simgrid.git] / src / simix / smx_host.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11
12 /** \defgroup m_host_management Management functions of Hosts
13  *  \brief This section describes the host structure of MSG
14  * 
15  *     \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Hosts" --> \endhtmlonly
16  * (#m_host_t) and the functions for managing it.
17  *  
18  *  A <em>location</em> (or <em>host</em>) is any possible place where
19  *  a process may run. Thus it may be represented as a
20  *  <em>physical resource with computing capabilities</em>, some
21  *  <em>mailboxes</em> to enable running process to communicate with
22  *  remote ones, and some <em>private data</em> that can be only
23  *  accessed by local process.
24  *  \see m_host_t
25  */
26
27 /********************************* Host **************************************/
28 smx_host_t __SIMIX_host_create(const char *name,
29                          void *workstation,
30                          void *data)
31 {
32   simdata_host_t simdata = xbt_new0(s_simdata_host_t,1);
33   smx_host_t host = xbt_new0(s_smx_host_t,1);
34   s_smx_process_t proc;
35
36   /* Host structure */
37   host->name = xbt_strdup(name);
38   host->simdata = simdata;
39   host->data = data;
40
41   simdata->host = workstation;
42
43   simdata->process_list = xbt_swag_new(xbt_swag_offset(proc, process_hookup));
44   /* Update global variables */
45
46   xbt_fifo_unshift(simix_global->host, host);
47
48   return host;
49 }
50
51 /** \ingroup m_host_management
52  *
53  * \brief Set the user data of a #m_host_t.
54  *
55  * This functions checks whether some data has already been associated to \a host 
56    or not and attach \a data to \a host if it is possible.
57  */
58 SIMIX_error_t SIMIX_host_set_data(smx_host_t host, void *data)
59 {
60   xbt_assert0((host!=NULL), "Invalid parameters");
61   xbt_assert0((host->data == NULL), "Data already set");
62
63   /* Assign data */
64   host->data = data;
65
66   return SIMIX_OK;
67 }
68
69 /** \ingroup m_host_management
70  *
71  * \brief Return the user data of a #m_host_t.
72  *
73  * This functions checks whether \a host is a valid pointer or not and return
74    the user data associated to \a host if it is possible.
75  */
76 void *SIMIX_host_get_data(smx_host_t host)
77 {
78
79   xbt_assert0((host != NULL), "Invalid parameters");
80
81   /* Return data */
82   return (host->data);
83 }
84
85 /** \ingroup m_host_management
86  *
87  * \brief Return the name of the #m_host_t.
88  *
89  * This functions checks whether \a host is a valid pointer or not and return
90    its name.
91  */
92 const char *SIMIX_host_get_name(smx_host_t host)
93 {
94
95   xbt_assert0((host != NULL) && (host->simdata != NULL), "Invalid parameters");
96
97   /* Return data */
98   return (host->name);
99 }
100
101 /** \ingroup m_host_management
102  *
103  * \brief Return the location on which the current process is executed.
104  */
105 smx_host_t SIMIX_host_self(void)
106 {
107   return SIMIX_process_get_host(SIMIX_process_self());
108 }
109
110 /*
111  * Real function for destroy a host.
112  * MSG_host_destroy is just  a front_end that also removes it from 
113  * msg_global->host
114  */
115 void __SIMIX_host_destroy(smx_host_t host)
116 {
117   simdata_host_t simdata = NULL;
118
119   xbt_assert0((host != NULL), "Invalid parameters");
120
121  
122   /* Clean Simulator data */
123   simdata = (host)->simdata;
124
125   xbt_assert0((xbt_swag_size(simdata->process_list)==0),
126               "Some process are still running on this host");
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 /** \ingroup m_host_management
139  * \brief Return the current number of #m_host_t.
140  */
141 int SIMIX_get_host_number(void)
142 {
143   return (xbt_fifo_size(simix_global->host));
144 }
145
146 /** \ingroup m_host_management
147  * \brief Return a array of all the #m_host_t.
148  */
149 smx_host_t *SIMIX_get_host_table(void)
150 {
151   return ((smx_host_t *)xbt_fifo_to_array(simix_global->host));
152 }
153
154 /** \ingroup m_host_management
155  * \brief Return the number of MSG tasks currently running on a
156  * #m_host_t. The external load is not taken in account.
157  */
158 /*
159 int MSG_get_host_msgload(m_host_t h)
160 {
161   xbt_assert0((h!= NULL), "Invalid parameters");
162   xbt_assert0(0, "Not implemented yet");
163
164   return(0);
165 //   return(surf_workstation_resource->extension_public->get_load(h->simdata->host));
166 }
167 */
168
169 /** \ingroup m_host_management
170  * \brief Return the speed of the processor (in Mflop/s), regardless of 
171     the current load on the machine.
172  */
173 double SIMIX_get_host_speed(smx_host_t h)
174 {
175   xbt_assert0((h!= NULL), "Invalid parameters");
176
177   return(surf_workstation_resource->
178          extension_public->get_speed(h->simdata->host,1.0));
179 }
180
181 /** \ingroup msg_gos_functions
182  * \brief Determine if a host is available.
183  *
184  * \param h host to test
185  */
186 int SIMIX_host_is_avail (smx_host_t h)
187 {
188   e_surf_cpu_state_t cpustate;
189   xbt_assert0((h!= NULL), "Invalid parameters");
190
191   cpustate =
192     surf_workstation_resource->extension_public->get_state(h->simdata->host);
193
194   xbt_assert0((cpustate == SURF_CPU_ON || cpustate == SURF_CPU_OFF),
195               "Invalid cpu state");
196
197   return (cpustate==SURF_CPU_ON);
198 }