Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fe7087ddc358cfad200907f0db0bd2a498d6f450
[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_public->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_workstation_model->extension_public->get_link_name(link->
117                                                                  surf_link);
118 }
119
120 /**
121  * \brief Returns the current bandwidth of a link
122  *
123  * \param link a link
124  * \return the current bandwidth of this link, in bytes per second
125  */
126 double SD_link_get_current_bandwidth(SD_link_t link)
127 {
128   SD_CHECK_INIT_DONE();
129   xbt_assert0(link != NULL, "Invalid parameter");
130   return surf_workstation_model->extension_public->get_link_bandwidth(link->
131                                                                       surf_link);
132 }
133
134 /**
135  * \brief Returns the value of a given link property
136  *
137  * \param link the inspected link
138  * \param name a property name
139  * \return value of a property (or NULL if property not set)
140  */
141 const char *SD_link_get_property_value(SD_link_t link, const char *name)
142 {
143   return xbt_dict_get_or_null(SD_link_get_properties(link), name);
144 }
145
146 /**
147  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to a link
148  *
149  * \param link a link
150  * \return the dictionary containing the properties associated with the link
151  */
152 xbt_dict_t SD_link_get_properties(SD_link_t link)
153 {
154   SD_CHECK_INIT_DONE();
155   xbt_assert0((link != NULL), "Invalid parameters");
156
157   return (surf_workstation_model->common_public.
158           get_properties(link->surf_link));
159
160 }
161
162 /**
163  * \brief Returns the current latency of a link
164  *
165  * \param link a link
166  * \return the current latency of this link, in seconds
167  */
168 double SD_link_get_current_latency(SD_link_t link)
169 {
170   SD_CHECK_INIT_DONE();
171   xbt_assert0(link != NULL, "Invalid parameter");
172   return surf_workstation_model->extension_public->get_link_latency(link->
173                                                                     surf_link);
174 }
175
176 /**
177  * \brief Returns the sharing policy of this workstation.
178  *
179  * \param link a link
180  * \return the sharing policyfor the flows going through this link:
181  * SD_LINK_SHARED or SD_LINK_FATPIPE
182  *
183  */
184 e_SD_link_sharing_policy_t SD_link_get_sharing_policy(SD_link_t link)
185 {
186   SD_CHECK_INIT_DONE();
187   xbt_assert0(link != NULL, "Invalid parameter");
188   return link->sharing_policy;
189 }
190
191
192 /* Destroys a link.
193  */
194 void __SD_link_destroy(void *link)
195 {
196   SD_CHECK_INIT_DONE();
197   xbt_assert0(link != NULL, "Invalid parameter");
198   /* link->surf_link is freed by surf_exit and link->data is freed by the user */
199   xbt_free(link);
200 }