Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Works both in RL and SG. Those processes are as stupid as lemmings. They we blocking...
[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   SD_CHECK_INIT_DONE();
11   xbt_assert0(surf_link != NULL, "surf_link is NULL !");
12
13   SD_link_t link = xbt_new0(s_SD_link_t, 1);
14   link->surf_link = surf_link;
15   link->data = data; /* user data */
16
17   const char *name = SD_link_get_name(link);
18   xbt_dict_set(sd_global->links, name, link, __SD_link_destroy); /* add the link to the dictionary */
19   sd_global->link_count++;
20
21   return link;
22 }
23 /**
24  * \brief Returns the link list
25  *
26  * Use SD_link_get_number() to know the array size.
27  *
28  * \return an array of \ref SD_link_t containing all links
29  * \see SD_link_get_number()
30  */
31 const SD_link_t*  SD_link_get_list(void) {
32   SD_CHECK_INIT_DONE();
33   xbt_assert0(SD_link_get_number() > 0, "There is no link!");
34
35   SD_link_t *array = sd_global->link_list;
36   xbt_dict_cursor_t cursor;
37   char *key;
38   void *data;
39   int i;
40
41   if (array == NULL) { /* this is the first time the function is called */
42     array = xbt_new0(SD_link_t, sd_global->link_count);
43   
44     i = 0;
45     xbt_dict_foreach(sd_global->links, cursor, key, data) {
46       array[i++] = (SD_link_t) data;
47     }
48   }
49   return array;
50 }
51
52 /**
53  * \brief Returns the number of links
54  *
55  * \return the number of existing links
56  * \see SD_link_get_list()
57  */
58 int SD_link_get_number(void) {
59   SD_CHECK_INIT_DONE();
60   return sd_global->link_count;
61 }
62
63 /**
64  * \brief Returns the user data of a link
65  *
66  * \param link a link
67  * \return the user data associated with this link (can be \c NULL)
68  * \see SD_link_set_data()
69  */
70 void* SD_link_get_data(SD_link_t link) {
71   SD_CHECK_INIT_DONE();
72   xbt_assert0(link != NULL, "Invalid parameter");
73   return link->data;
74 }
75
76 /**
77  * \brief Sets the user data of a link
78  *
79  * The new data can be \c NULL. The old data should have been freed first
80  * if it was not \c NULL.
81  *
82  * \param link a link
83  * \param data the new data you want to associate with this link
84  * \see SD_link_get_data()
85  */
86 void SD_link_set_data(SD_link_t link, void *data) {
87   SD_CHECK_INIT_DONE();
88   xbt_assert0(link != NULL, "Invalid parameter");
89   link->data = data;
90 }
91
92 /**
93  * \brief Returns the name of a link
94  *
95  * \param link a link
96  * \return the name of this link (cannot be \c NULL)
97  */
98 const char* SD_link_get_name(SD_link_t link) {
99   SD_CHECK_INIT_DONE();
100   xbt_assert0(link != NULL, "Invalid parameter");
101   return surf_workstation_resource->extension_public->get_link_name(link->surf_link);
102 }
103
104 /**
105  * \brief Returns the current bandwidth of a link
106  *
107  * \param link a link
108  * \return the current bandwidth of this link, in Flops
109  */
110 double SD_link_get_current_bandwidth(SD_link_t link) {
111   SD_CHECK_INIT_DONE();
112   xbt_assert0(link != NULL, "Invalid parameter");
113   return surf_workstation_resource->extension_public->get_link_bandwidth(link->surf_link);
114 }
115
116 /**
117  * \brief Returns the current latency of a link
118  *
119  * \param link a link
120  * \return the current latency of this link, in seconds
121  */
122 double SD_link_get_current_latency(SD_link_t link) {
123   SD_CHECK_INIT_DONE();
124   xbt_assert0(link != NULL, "Invalid parameter");
125   return surf_workstation_resource->extension_public->get_link_latency(link->surf_link);
126 }
127
128 /* Destroys a link.
129  */
130 void __SD_link_destroy(void *link) {
131   SD_CHECK_INIT_DONE();
132   xbt_assert0(link != NULL, "Invalid parameter");
133   /* link->surf_link is freed by surf_exit and link->data is freed by the user */
134   xbt_free(link);
135 }
136