Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a generic resource; use it as ancestor to specific ones
[simgrid.git] / src / simdag / sd_link.c
1 #include "private.h"
2 #include "simdag/simdag.h"
3 #include "xbt/dict.h"
4 #include "xbt/sysdep.h"
5 #include "surf/surf.h"
6
7 /* Creates a link and registers it in SD.
8  */
9 SD_link_t __SD_link_create(void *surf_link, void *data)
10 {
11
12   SD_link_t link;
13   const char *name;
14
15   SD_CHECK_INIT_DONE();
16   xbt_assert0(surf_link != NULL, "surf_link is NULL !");
17
18   link = xbt_new(s_SD_link_t, 1);
19   link->surf_link = surf_link;
20   link->data = data;            /* user data */
21   if (surf_workstation_model->extension.workstation.link_shared(surf_link))
22     link->sharing_policy = SD_LINK_SHARED;
23   else
24     link->sharing_policy = SD_LINK_FATPIPE;
25
26   name = SD_link_get_name(link);
27   xbt_dict_set(sd_global->links, name, link, __SD_link_destroy);        /* add the link to the dictionary */
28   sd_global->link_count++;
29
30   return link;
31 }
32
33 /**
34  * \brief Returns the link list
35  *
36  * Use SD_link_get_number() to know the array size.
37  *
38  * \return an array of \ref SD_link_t containing all links
39  * \see SD_link_get_number()
40  */
41 const SD_link_t *SD_link_get_list(void)
42 {
43
44   xbt_dict_cursor_t cursor;
45   char *key;
46   void *data;
47   int i;
48
49   SD_CHECK_INIT_DONE();
50   xbt_assert0(SD_link_get_number() > 0, "There is no link!");
51
52   if (sd_global->link_list == NULL) {   /* this is the first time the function is called */
53     sd_global->link_list = xbt_new(SD_link_t, sd_global->link_count);
54
55     i = 0;
56     xbt_dict_foreach(sd_global->links, cursor, key, data) {
57       sd_global->link_list[i++] = (SD_link_t) data;
58     }
59   }
60   return sd_global->link_list;
61 }
62
63 /**
64  * \brief Returns the number of links
65  *
66  * \return the number of existing links
67  * \see SD_link_get_list()
68  */
69 int SD_link_get_number(void)
70 {
71   SD_CHECK_INIT_DONE();
72   return sd_global->link_count;
73 }
74
75 /**
76  * \brief Returns the user data of a link
77  *
78  * \param link a link
79  * \return the user data associated with this link (can be \c NULL)
80  * \see SD_link_set_data()
81  */
82 void *SD_link_get_data(SD_link_t link)
83 {
84   SD_CHECK_INIT_DONE();
85   xbt_assert0(link != NULL, "Invalid parameter");
86   return link->data;
87 }
88
89 /**
90  * \brief Sets the user data of a link
91  *
92  * The new data can be \c NULL. The old data should have been freed first
93  * if it was not \c NULL.
94  *
95  * \param link a link
96  * \param data the new data you want to associate with this link
97  * \see SD_link_get_data()
98  */
99 void SD_link_set_data(SD_link_t link, void *data)
100 {
101   SD_CHECK_INIT_DONE();
102   xbt_assert0(link != NULL, "Invalid parameter");
103   link->data = data;
104 }
105
106 /**
107  * \brief Returns the name of a link
108  *
109  * \param link a link
110  * \return the name of this link (cannot be \c NULL)
111  */
112 const char *SD_link_get_name(SD_link_t link)
113 {
114   SD_CHECK_INIT_DONE();
115   xbt_assert0(link != NULL, "Invalid parameter");
116   return surf_resource_name(link->surf_link);
117 }
118
119 /**
120  * \brief Returns the current bandwidth of a link
121  *
122  * \param link a link
123  * \return the current bandwidth of this link, in bytes per second
124  */
125 double SD_link_get_current_bandwidth(SD_link_t link)
126 {
127   SD_CHECK_INIT_DONE();
128   xbt_assert0(link != NULL, "Invalid parameter");
129   return surf_workstation_model->extension.workstation.
130     get_link_bandwidth(link->surf_link);
131 }
132
133 /**
134  * \brief Returns the value of a given link property
135  *
136  * \param link the inspected link
137  * \param name a property name
138  * \return value of a property (or NULL if property not set)
139  */
140 const char *SD_link_get_property_value(SD_link_t link, const char *name)
141 {
142   return xbt_dict_get_or_null(SD_link_get_properties(link), name);
143 }
144
145 /**
146  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to a link
147  *
148  * \param link a link
149  * \return the dictionary containing the properties associated with the link
150  */
151 xbt_dict_t SD_link_get_properties(SD_link_t link)
152 {
153   SD_CHECK_INIT_DONE();
154   xbt_assert0((link != NULL), "Invalid parameters");
155
156   return (surf_workstation_model->get_properties(link->surf_link));
157
158 }
159
160 /**
161  * \brief Returns the current latency of a link
162  *
163  * \param link a link
164  * \return the current latency of this link, in seconds
165  */
166 double SD_link_get_current_latency(SD_link_t link)
167 {
168   SD_CHECK_INIT_DONE();
169   xbt_assert0(link != NULL, "Invalid parameter");
170   return surf_workstation_model->extension.workstation.
171     get_link_latency(link->surf_link);
172 }
173
174 /**
175  * \brief Returns the sharing policy of this workstation.
176  *
177  * \param link a link
178  * \return the sharing policyfor the flows going through this link:
179  * SD_LINK_SHARED or SD_LINK_FATPIPE
180  *
181  */
182 e_SD_link_sharing_policy_t SD_link_get_sharing_policy(SD_link_t link)
183 {
184   SD_CHECK_INIT_DONE();
185   xbt_assert0(link != NULL, "Invalid parameter");
186   return link->sharing_policy;
187 }
188
189
190 /* Destroys a link.
191  */
192 void __SD_link_destroy(void *link)
193 {
194   SD_CHECK_INIT_DONE();
195   xbt_assert0(link != NULL, "Invalid parameter");
196   /* link->surf_link is freed by surf_exit and link->data is freed by the user */
197   xbt_free(link);
198 }