Logo AND Algorithmique Numérique Distribuée

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