2 #include "simdag/simdag.h"
4 #include "xbt/sysdep.h"
7 /* Creates a workstation and registers it in SD.
9 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
11 xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
13 SD_workstation_t workstation = xbt_new0(s_SD_workstation_t, 1);
14 workstation->surf_workstation = surf_workstation;
15 workstation->data = data; /* user data */
17 const char *name = SD_workstation_get_name(workstation);
18 xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
19 sd_global->workstation_count++;
25 * \brief Returns a workstation given its name
27 * If there is no such workstation, the function returns \c NULL.
29 * \param name workstation name
30 * \return the workstation, or \c NULL if there is no such workstation
32 SD_workstation_t SD_workstation_get_by_name(const char *name) {
35 xbt_assert0(name != NULL, "Invalid parameter");
37 return xbt_dict_get_or_null(sd_global->workstations, name);
41 * \brief Returns the workstation list
43 * Use SD_workstation_get_number() to know the array size.
45 * \return an array of \ref SD_workstation_t containing all workstations
46 * \see SD_workstation_get_number()
48 const SD_workstation_t* SD_workstation_get_list(void) {
50 xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
52 SD_workstation_t *array = sd_global->workstation_list;
53 xbt_dict_cursor_t cursor;
58 if (array == NULL) { /* this is the first time the function is called */
59 array = xbt_new0(SD_workstation_t, sd_global->workstation_count);
62 xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
63 array[i++] = (SD_workstation_t) data;
70 * \brief Returns the number of workstations
72 * \return the number of existing workstations
73 * \see SD_workstation_get_list()
75 int SD_workstation_get_number(void) {
77 return sd_global->workstation_count;
81 * \brief Returns the user data of a workstation
83 * \param workstation a workstation
84 * \return the user data associated with this workstation (can be \c NULL)
85 * \see SD_workstation_set_data()
87 void* SD_workstation_get_data(SD_workstation_t workstation) {
89 xbt_assert0(workstation != NULL, "Invalid parameter");
90 return workstation->data;
94 * \brief Sets the user data of a workstation
96 * The new data can be \c NULL. The old data should have been freed first
97 * if it was not \c NULL.
99 * \param workstation a workstation
100 * \param data the new data you want to associate with this workstation
101 * \see SD_workstation_get_data()
103 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
104 SD_CHECK_INIT_DONE();
105 xbt_assert0(workstation != NULL, "Invalid parameter");
106 workstation->data = data;
110 * \brief Returns the name of a workstation
112 * \param workstation a workstation
113 * \return the name of this workstation (cannot be \c NULL)
115 const char* SD_workstation_get_name(SD_workstation_t workstation) {
116 SD_CHECK_INIT_DONE();
117 xbt_assert0(workstation != NULL, "Invalid parameter");
118 return surf_workstation_resource->common_public->get_resource_name(workstation->surf_workstation);
122 * \brief Returns the route between two workstations
124 * Use SD_workstation_route_get_size() to know the array size.
126 * \param src a workstation
127 * \param dst another workstation
128 * \return a new array of \ref SD_link_t representating the route between these two workstations
129 * \see SD_workstation_route_get_size(), SD_link_t
131 SD_link_t* SD_workstation_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
132 SD_CHECK_INIT_DONE();
134 void *surf_src = src->surf_workstation;
135 void *surf_dst = dst->surf_workstation;
137 const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
138 int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
140 SD_link_t* route = xbt_new0(SD_link_t, route_size);
141 const char *link_name;
143 for (i = 0; i < route_size; i++) {
144 link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
145 route[i] = xbt_dict_get(sd_global->links, link_name);
152 * \brief Returns the number of links on the route between two workstations
154 * \param src a workstation
155 * \param dst another workstation
156 * \return the number of links on the route between these two workstations
157 * \see SD_workstation_route_get_list()
159 int SD_workstation_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
160 SD_CHECK_INIT_DONE();
161 return surf_workstation_resource->extension_public->
162 get_route_size(src->surf_workstation, dst->surf_workstation);
166 * \brief Returns the total power of a workstation
168 * \param workstation a workstation
169 * \return the total power of this workstation
170 * \see SD_workstation_get_available_power()
172 double SD_workstation_get_power(SD_workstation_t workstation) {
173 SD_CHECK_INIT_DONE();
174 xbt_assert0(workstation != NULL, "Invalid parameter");
175 return surf_workstation_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
179 * \brief Returns the proportion of available power in a workstation
181 * \param workstation a workstation
182 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
183 * \see SD_workstation_get_power()
185 double SD_workstation_get_available_power(SD_workstation_t workstation) {
186 SD_CHECK_INIT_DONE();
187 xbt_assert0(workstation != NULL, "Invalid parameter");
188 return surf_workstation_resource->extension_public->get_available_speed(workstation->surf_workstation);
191 /* Destroys a workstation.
193 void __SD_workstation_destroy(void *workstation) {
194 SD_CHECK_INIT_DONE();
195 xbt_assert0(workstation != NULL, "Invalid parameter");
196 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
197 xbt_free(workstation);