Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cb042bf386bf9d2727e99f82c12a809ab193c996
[simgrid.git] / src / msg / 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  * (#m_host_t) and the functions for managing it.
15  *  
16  *  A <em>location</em> (or <em>host</em>) is any possible place where
17  *  a process may run. Thus it may be represented as a
18  *  <em>physical resource with computing capabilities</em>, some
19  *  <em>mailboxes</em> to enable running process to communicate with
20  *  remote ones, and some <em>private data</em> that can be only
21  *  accessed by local process.
22  *  \see m_host_t
23  */
24
25 /********************************* Host **************************************/
26 m_host_t __MSG_host_create(const char *name,
27                          void *workstation,
28                          void *data)
29 {
30   simdata_host_t simdata = xbt_new0(s_simdata_host_t,1);
31   m_host_t host = xbt_new0(s_m_host_t,1);
32   int i;
33
34   /* Host structure */
35   host->name = xbt_strdup(name);
36   host->simdata = simdata;
37   host->data = data;
38
39   simdata->host = workstation;
40
41   simdata->mbox = xbt_new0(xbt_fifo_t, msg_global->max_channel);
42   for (i = 0; i < msg_global->max_channel; i++)
43     simdata->mbox[i] = xbt_fifo_new();
44   simdata->sleeping = xbt_new0(m_process_t, msg_global->max_channel);
45   simdata->process_list = xbt_fifo_new();
46   /* Update global variables */
47
48   xbt_fifo_push(msg_global->host, host);
49
50   PAJE_HOST_NEW(host);
51
52   return host;
53 }
54
55 /** \ingroup m_host_management
56  *
57  * \brief Set the user data of a #m_host_t.
58  *
59  * This functions checks whether some data has already been associated to \a host 
60    or not and attach \a data to \a host if it is possible.
61  */
62 MSG_error_t MSG_host_set_data(m_host_t host, void *data)
63 {
64   xbt_assert0((host!=NULL), "Invalid parameters");
65   xbt_assert0((host->data == NULL), "Data already set");
66
67   /* Assign data */
68   host->data = data;
69
70   return MSG_OK;
71 }
72
73 /** \ingroup m_host_management
74  *
75  * \brief Return the user data of a #m_host_t.
76  *
77  * This functions checks whether \a host is a valid pointer or not and return
78    the user data associated to \a host if it is possible.
79  */
80 void *MSG_host_get_data(m_host_t host)
81 {
82
83   xbt_assert0((host != NULL), "Invalid parameters");
84
85   /* Return data */
86   return (host->data);
87 }
88
89 /** \ingroup m_host_management
90  *
91  * \brief Return the name of the #m_host_t.
92  *
93  * This functions checks whether \a host is a valid pointer or not and return
94    its name.
95  */
96 const char *MSG_host_get_name(m_host_t host)
97 {
98
99   xbt_assert0((host != NULL) && (host->simdata != NULL), "Invalid parameters");
100
101   /* Return data */
102   return (host->name);
103 }
104
105 /** \ingroup m_host_management
106  *
107  * \brief Return the location on which the current process is executed.
108  */
109 m_host_t MSG_host_self(void)
110 {
111   return MSG_process_get_host(MSG_process_self());
112 }
113
114 /*
115  * Real function for destroy a host.
116  * MSG_host_destroy is just  a front_end that also removes it from 
117  * msg_global->host
118  */
119 void __MSG_host_destroy(m_host_t host)
120 {
121   simdata_host_t simdata = NULL;
122   int i = 0;
123
124   xbt_assert0((host != NULL), "Invalid parameters");
125
126   PAJE_HOST_FREE(host);
127  
128   /* Clean Simulator data */
129   simdata = (host)->simdata;
130
131   for (i = 0; i < msg_global->max_channel; i++)
132     xbt_fifo_free(simdata->mbox[i]);
133   free(simdata->mbox);
134   free(simdata->sleeping);
135   xbt_assert0((xbt_fifo_size(simdata->process_list)==0),
136               "Some process are still running on this host");
137   xbt_fifo_free(simdata->process_list);
138
139   free(simdata);
140
141   /* Clean host structure */
142   free(host->name);
143   free(host);
144
145   return;
146 }
147
148 /** \ingroup m_host_management
149  * \brief Return the current number of #m_host_t.
150  */
151 int MSG_get_host_number(void)
152 {
153   return (xbt_fifo_size(msg_global->host));
154 }
155
156 /** \ingroup m_host_management
157  * \brief Return a array of all the #m_host_t.
158  */
159 m_host_t *MSG_get_host_table(void)
160 {
161   return ((m_host_t *)xbt_fifo_to_array(msg_global->host));
162 }
163
164 /** \ingroup m_host_management
165  * \brief Return the number of MSG tasks currently running on a
166  * #m_host_t. The external load is not taken in account.
167  */
168 int MSG_get_host_msgload(m_host_t h)
169 {
170   xbt_assert0((h!= NULL), "Invalid parameters");
171   xbt_assert0(0, "Not implemented yet");
172
173   return(0);
174 /*   return(surf_workstation_resource->extension_public->get_load(h->simdata->host)); */
175 }
176
177 /** \ingroup m_host_management
178  * \brief Return the speed of the processor (in Mflop/s), regardless of 
179     the current load on the machine.
180  */
181 double MSG_get_host_speed(m_host_t h)
182 {
183   xbt_assert0((h!= NULL), "Invalid parameters");
184
185   return(surf_workstation_resource->
186          extension_public->get_speed(h->simdata->host,1.0));
187 }
188
189 /** \ingroup msg_gos_functions
190  * \brief Determine if a host is available.
191  *
192  * \param h host to test
193  */
194 int MSG_host_is_avail (m_host_t h)
195 {
196   xbt_assert0((h!= NULL), "Invalid parameters");
197
198   e_surf_cpu_state_t cpustate =
199     surf_workstation_resource->extension_public->get_state(h->simdata->host);
200
201   xbt_assert0((cpustate == SURF_CPU_ON || cpustate == SURF_CPU_OFF),
202               "Invalid cpu state");
203
204   return (cpustate==SURF_CPU_ON);
205 }