Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
typo
[simgrid.git] / src / msg / host.c
1 /*     $Id$      */
2   
3 /* Copyright (c) 2002-2007 Arnaud Legrand.                                  */
4 /* Copyright (c) 2007 Bruno Donassolo.                                      */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9   
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13
14 /** \defgroup m_host_management Management functions of Hosts
15  *  \brief This section describes the host structure of MSG
16  */
17 /** @addtogroup m_host_management
18  *     \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Hosts" --> \endhtmlonly
19  * (#m_host_t) and the functions for managing it.
20  *  
21  *  A <em>location</em> (or <em>host</em>) is any possible place where
22  *  a process may run. Thus it may be represented as a
23  *  <em>physical resource with computing capabilities</em>, some
24  *  <em>mailboxes</em> to enable running process to communicate with
25  *  remote ones, and some <em>private data</em> that can be only
26  *  accessed by local process.
27  *  \see m_host_t
28  */
29
30 /********************************* Host **************************************/
31 m_host_t __MSG_host_create(smx_host_t workstation, void *data)
32 {
33         const char * name;
34   simdata_host_t simdata = xbt_new0(s_simdata_host_t,1);
35   m_host_t host = xbt_new0(s_m_host_t,1);
36   int i;
37
38         name = SIMIX_host_get_name(workstation);
39   /* Host structure */
40   host->name = xbt_strdup(name);
41   host->simdata = simdata;
42   host->data = data;
43
44   simdata->s_host = workstation;
45
46   simdata->mbox = xbt_new0(xbt_fifo_t, msg_global->max_channel);
47   for (i = 0; i < msg_global->max_channel; i++)
48     simdata->mbox[i] = xbt_fifo_new();
49   
50         simdata->sleeping = xbt_new0(smx_cond_t, msg_global->max_channel);
51         simdata->mutex = SIMIX_mutex_init();
52         SIMIX_host_set_data(workstation, host);
53
54   /* Update global variables */
55         xbt_fifo_unshift(msg_global->host, host);
56  
57   return host;
58 }
59
60 /** \ingroup m_host_management
61  *
62  * \brief Set the user data of a #m_host_t.
63  *
64  * This functions checks whether some data has already been associated to \a host 
65    or not and attach \a data to \a host if it is possible.
66  */
67 MSG_error_t MSG_host_set_data(m_host_t host, void *data)
68 {       
69   xbt_assert0((host!=NULL), "Invalid parameters");
70   xbt_assert0((host->data == NULL), "Data already set");
71
72   /* Assign data */
73   host->data = data;
74
75   return MSG_OK;
76 }
77
78 /** \ingroup m_host_management
79  *
80  * \brief Return the user data of a #m_host_t.
81  *
82  * This functions checks whether \a host is a valid pointer or not and return
83    the user data associated to \a host if it is possible.
84  */
85 void *MSG_host_get_data(m_host_t host)
86 {
87
88   xbt_assert0((host != NULL), "Invalid parameters");
89
90   /* Return data */
91   return (host->data);
92 }
93
94 /** \ingroup m_host_management
95  *
96  * \brief Return the name of the #m_host_t.
97  *
98  * This functions checks whether \a host is a valid pointer or not and return
99    its name.
100  */
101 const char *MSG_host_get_name(m_host_t host)
102 {
103
104   xbt_assert0((host != NULL) && (host->simdata != NULL), "Invalid parameters");
105
106   /* Return data */
107   return (host->name);
108 }
109
110 /** \ingroup m_host_management
111  *
112  * \brief Return the location on which the current process is executed.
113  */
114 m_host_t MSG_host_self(void)
115 {
116   return MSG_process_get_host(MSG_process_self());
117 }
118
119 /*
120  * Real function to destroy a host.
121  * MSG_host_destroy is just  a front_end that also removes it from 
122  * msg_global->host
123  */
124 void __MSG_host_destroy(m_host_t host)
125 {
126   simdata_host_t simdata = NULL;
127   int i = 0;
128
129   xbt_assert0((host != NULL), "Invalid parameters");
130
131   /* Clean Simulator data */
132    /* SIMIX host will be cleaned when MSG_clean calls SIMIX_clean */
133   simdata = (host)->simdata;
134
135   for (i = 0; i < msg_global->max_channel; i++)
136     xbt_fifo_free(simdata->mbox[i]);
137   free(simdata->mbox);
138   free(simdata->sleeping);
139         SIMIX_mutex_destroy(simdata->mutex);
140   free(simdata);
141
142   /* Clean host structure */
143   free(host->name);
144   free(host);
145
146   return;
147 }
148
149 /** \ingroup m_host_management
150  * \brief Return the current number of #m_host_t.
151  */
152 int MSG_get_host_number(void)
153 {
154   return (xbt_fifo_size(msg_global->host));
155 }
156
157 /** \ingroup m_host_management
158  * \brief Return a array of all the #m_host_t.
159  */
160 m_host_t *MSG_get_host_table(void)
161 {
162   return ((m_host_t *)xbt_fifo_to_array(msg_global->host));
163 }
164
165 /** \ingroup m_host_management
166  * \brief Return the number of MSG tasks currently running on a
167  * #m_host_t. The external load is not taken in account.
168  */
169 int MSG_get_host_msgload(m_host_t h)
170 {
171   xbt_assert0((h!= NULL), "Invalid parameters");
172   xbt_assert0(0, "Not implemented yet");
173
174   return(0);
175 }
176
177 /** \ingroup m_host_management
178  * \brief Return the speed of the processor (in flop/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(SIMIX_host_get_speed(h->simdata->s_host));
186 }
187
188 /** \ingroup msg_gos_functions
189  * \brief Determine if a host is available.
190  *
191  * \param h host to test
192  */
193 int MSG_host_is_avail (m_host_t h)
194 {
195   xbt_assert0((h!= NULL), "Invalid parameters");
196         return (SIMIX_host_get_state(h->simdata->s_host));
197 }