Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f3a3750fa74b267138e21c2b805f99fd05f50548
[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   link = xbt_new(s_SD_link_t, 1);
22   link->surf_link = surf_link;
23   link->data = data;            /* user data */
24   if (surf_workstation_model->extension.workstation.link_shared(surf_link))
25     link->sharing_policy = SD_LINK_SHARED;
26   else
27     link->sharing_policy = SD_LINK_FATPIPE;
28
29   name = SD_link_get_name(link);
30   xbt_lib_set(link_lib,name,SD_LINK_LEVEL,link);
31
32   return link;
33 }
34
35 /**
36  * \brief Returns the link list
37  *
38  * Use SD_link_get_number() to know the array size.
39  *
40  * \return an array of \ref SD_link_t containing all links
41  * \see SD_link_get_number()
42  */
43 const SD_link_t *SD_link_get_list(void)
44 {
45
46   xbt_lib_cursor_t cursor;
47   char *key;
48   void **data;
49   int i;
50
51   if (sd_global->link_list == NULL) {   /* this is the first time the function is called */
52     sd_global->link_list = xbt_new(SD_link_t, link_lib->count);
53
54     i = 0;
55     xbt_lib_foreach(link_lib, cursor, key, data) {
56                 sd_global->link_list[i++] = (SD_link_t) data[SD_LINK_LEVEL];
57     }
58   }
59   return sd_global->link_list;
60 }
61
62 /**
63  * \brief Returns the number of links
64  *
65  * \return the number of existing links
66  * \see SD_link_get_list()
67  */
68 int SD_link_get_number(void)
69 {
70   return link_lib->count;
71 }
72
73 /**
74  * \brief Returns the user data of a link
75  *
76  * \param link a link
77  * \return the user data associated with this link (can be \c NULL)
78  * \see SD_link_set_data()
79  */
80 void *SD_link_get_data(SD_link_t link)
81 {
82   return link->data;
83 }
84
85 /**
86  * \brief Sets the user data of a link
87  *
88  * The new data can be \c NULL. The old data should have been freed first
89  * if it was not \c NULL.
90  *
91  * \param link a link
92  * \param data the new data you want to associate with this link
93  * \see SD_link_get_data()
94  */
95 void SD_link_set_data(SD_link_t link, void *data)
96 {
97   link->data = data;
98 }
99
100 /**
101  * \brief Returns the name of a link
102  *
103  * \param link a link
104  * \return the name of this link (cannot be \c NULL)
105  */
106 const char *SD_link_get_name(SD_link_t link)
107 {
108   return surf_resource_name(link->surf_link);
109 }
110
111 /**
112  * \brief Returns the current bandwidth of a link
113  *
114  * \param link a link
115  * \return the current bandwidth of this link, in bytes per second
116  */
117 double SD_link_get_current_bandwidth(SD_link_t link)
118 {
119   return surf_workstation_model->extension.workstation.
120       get_link_bandwidth(link->surf_link);
121 }
122
123 /**
124  * \brief Returns the current latency of a link
125  *
126  * \param link a link
127  * \return the current latency of this link, in seconds
128  */
129 double SD_link_get_current_latency(SD_link_t link)
130 {
131   return surf_workstation_model->extension.workstation.
132       get_link_latency(link->surf_link);
133 }
134
135 /**
136  * \brief Returns the sharing policy of this workstation.
137  *
138  * \param link a link
139  * \return the sharing policyfor the flows going through this link:
140  * SD_LINK_SHARED or SD_LINK_FATPIPE
141  *
142  */
143 e_SD_link_sharing_policy_t SD_link_get_sharing_policy(SD_link_t link)
144 {
145   return link->sharing_policy;
146 }
147
148
149 /* Destroys a link.
150  */
151 void __SD_link_destroy(void *link)
152 {
153   /* link->surf_link is freed by surf_exit and link->data is freed by the user */
154   xbt_free(link);
155 }