1 /* Copyright (c) 2006-2011. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "simdag/simdag.h"
11 #include "xbt/sysdep.h"
12 #include "surf/surf.h"
13 #include "surf/surf_resource.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
16 "Logging specific to SimDag (workstation)");
18 /* Creates a workstation and registers it in SD.
20 SD_workstation_t __SD_workstation_create(void *surf_workstation,
24 SD_workstation_priv_t workstation;
27 workstation = xbt_new(s_SD_workstation_priv_t, 1);
28 workstation->data = data; /* user data */
29 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
30 workstation->task_fifo = NULL;
31 workstation->current_task = NULL;
33 name = surf_resource_name(surf_workstation);
34 xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
35 return xbt_lib_get_elm_or_null(host_lib,name);
39 * \brief Returns a workstation given its name
41 * If there is no such workstation, the function returns \c NULL.
43 * \param name workstation name
44 * \return the workstation, or \c NULL if there is no such workstation
46 SD_workstation_t SD_workstation_get_by_name(const char *name)
48 return xbt_lib_get_elm_or_null(host_lib, name);
52 * \brief Returns the workstation list
54 * Use SD_workstation_get_number() to know the array size.
56 * \return an array of \ref SD_workstation_t containing all workstations
57 * \see SD_workstation_get_number()
59 const SD_workstation_t *SD_workstation_get_list(void)
62 xbt_lib_cursor_t cursor;
67 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
69 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
70 sd_global->workstation_list =
71 xbt_new(SD_workstation_t, xbt_lib_length(host_lib));
74 xbt_lib_foreach(host_lib, cursor, key, data) {
75 if(data[SD_HOST_LEVEL])
76 sd_global->workstation_list[i++] = xbt_dict_cursor_get_elm(cursor);
79 return sd_global->workstation_list;
83 * \brief Returns the number of workstations
85 * \return the number of existing workstations
86 * \see SD_workstation_get_list()
88 int SD_workstation_get_number(void)
90 return xbt_lib_length(host_lib);
94 * \brief Returns the user data of a workstation
96 * \param workstation a workstation
97 * \return the user data associated with this workstation (can be \c NULL)
98 * \see SD_workstation_set_data()
100 void *SD_workstation_get_data(SD_workstation_t workstation)
102 return SD_workstation_priv(workstation)->data;
106 * \brief Sets the user data of a workstation
108 * The new data can be \c NULL. The old data should have been freed first
109 * if it was not \c NULL.
111 * \param workstation a workstation
112 * \param data the new data you want to associate with this workstation
113 * \see SD_workstation_get_data()
115 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
117 SD_workstation_priv(workstation)->data = data;
121 * \brief Returns the name of a workstation
123 * \param workstation a workstation
124 * \return the name of this workstation (cannot be \c NULL)
126 const char *SD_workstation_get_name(SD_workstation_t workstation)
128 return sg_host_name(workstation);
132 * \brief Returns the value of a given workstation property
134 * \param ws a workstation
135 * \param name a property name
136 * \return value of a property (or NULL if property not set)
138 const char *SD_workstation_get_property_value(SD_workstation_t ws,
141 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
146 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
148 * \param workstation a workstation
149 * \return the dictionary containing the properties associated with the workstation
151 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
153 return surf_resource_get_properties(surf_workstation_resource_priv(workstation));
157 /** @brief Displays debugging informations about a workstation */
158 void SD_workstation_dump(SD_workstation_t ws)
161 xbt_dict_cursor_t cursor=NULL;
163 SD_task_t task = NULL;
165 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
166 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
167 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
168 switch (SD_workstation_priv(ws)->access_mode){
169 case SD_WORKSTATION_SHARED_ACCESS:
170 XBT_INFO(" - access mode: Space shared");
172 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
173 XBT_INFO(" - access mode: Exclusive");
174 task = SD_workstation_get_current_task(ws);
176 XBT_INFO(" current running task: %s",
177 SD_task_get_name(task));
179 XBT_INFO(" no task running");
183 props = SD_workstation_get_properties(ws);
185 if (!xbt_dict_is_empty(props)){
186 XBT_INFO(" - properties:");
188 xbt_dict_foreach(props,cursor,key,data) {
189 XBT_INFO(" %s->%s",key,data);
195 * \brief Returns the route between two workstations
197 * Use SD_route_get_size() to know the array size.
199 * \param src a workstation
200 * \param dst another workstation
201 * \return a new array of \ref SD_link_t representating the route between these two workstations
202 * \see SD_route_get_size(), SD_link_t
204 const SD_link_t *SD_route_get_list(SD_workstation_t src,
205 SD_workstation_t dst)
209 xbt_dynar_t surf_route;
210 const char *link_name;
214 if (sd_global->recyclable_route == NULL) {
216 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
222 surf_route = surf_workstation_model_get_route((surf_workstation_model_t)surf_workstation_model,
225 xbt_dynar_foreach(surf_route, cpt, surf_link) {
226 link_name = surf_resource_name(surf_link);
227 sd_global->recyclable_route[cpt] =
228 xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
230 return sd_global->recyclable_route;
234 * \brief Returns the number of links on the route between two workstations
236 * \param src a workstation
237 * \param dst another workstation
238 * \return the number of links on the route between these two workstations
239 * \see SD_route_get_list()
241 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
243 return xbt_dynar_length(surf_workstation_model_get_route(
244 (surf_workstation_model_t)surf_workstation_model, src, dst));
248 * \brief Returns the total power of a workstation
250 * \param workstation a workstation
251 * \return the total power of this workstation
252 * \see SD_workstation_get_available_power()
254 double SD_workstation_get_power(SD_workstation_t workstation)
256 return surf_workstation_get_speed(workstation, 1.0);
260 * \brief Returns the proportion of available power in a workstation
262 * \param workstation a workstation
263 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
264 * \see SD_workstation_get_power()
266 double SD_workstation_get_available_power(SD_workstation_t workstation)
268 return surf_workstation_get_available_speed(workstation);
272 * \brief Returns an approximative estimated time for the given computation amount on a workstation
274 * \param workstation a workstation
275 * \param computation_amount the computation amount you want to evaluate (in flops)
276 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
278 double SD_workstation_get_computation_time(SD_workstation_t workstation,
279 double computation_amount)
281 xbt_assert(computation_amount >= 0,
282 "computation_amount must be greater than or equal to zero");
283 return computation_amount / SD_workstation_get_power(workstation);
287 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
288 * between the workstations.
290 * \param src the first workstation
291 * \param dst the second workstation
292 * \return the latency of the route between the two workstations (in seconds)
293 * \see SD_route_get_current_bandwidth()
295 double SD_route_get_current_latency(SD_workstation_t src,
296 SD_workstation_t dst)
299 const SD_link_t *links;
304 links = SD_route_get_list(src, dst);
305 nb_links = SD_route_get_size(src, dst);
308 for (i = 0; i < nb_links; i++) {
309 latency += SD_link_get_current_latency(links[i]);
316 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
317 * between the workstations.
319 * \param src the first workstation
320 * \param dst the second workstation
321 * \return the bandwidth of the route between the two workstations (in bytes/second)
322 * \see SD_route_get_current_latency()
324 double SD_route_get_current_bandwidth(SD_workstation_t src,
325 SD_workstation_t dst)
328 const SD_link_t *links;
331 double min_bandwidth;
334 links = SD_route_get_list(src, dst);
335 nb_links = SD_route_get_size(src, dst);
336 bandwidth = min_bandwidth = -1.0;
339 for (i = 0; i < nb_links; i++) {
340 bandwidth = SD_link_get_current_bandwidth(links[i]);
341 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
342 min_bandwidth = bandwidth;
345 return min_bandwidth;
349 * \brief Returns an approximative estimated time for the given
350 * communication amount between two workstations
352 * \param src the first workstation
353 * \param dst the second workstation
354 * \param communication_amount the communication amount you want to evaluate (in bytes)
355 * \return an approximative astimated computation time for the given communication amount
356 * between the workstations (in seconds)
358 double SD_route_get_communication_time(SD_workstation_t src,
359 SD_workstation_t dst,
360 double communication_amount)
364 /* total time = latency + transmission time of the slowest link
365 transmission time of a link = communication amount / link bandwidth */
367 const SD_link_t *links;
369 double bandwidth, min_bandwidth;
373 xbt_assert(communication_amount >= 0,
374 "communication_amount must be greater than or equal to zero");
378 if (communication_amount == 0.0)
381 links = SD_route_get_list(src, dst);
382 nb_links = SD_route_get_size(src, dst);
383 min_bandwidth = -1.0;
386 for (i = 0; i < nb_links; i++) {
387 latency += SD_link_get_current_latency(links[i]);
388 bandwidth = SD_link_get_current_bandwidth(links[i]);
389 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
390 min_bandwidth = bandwidth;
393 return latency + (communication_amount / min_bandwidth);
397 * \brief Returns the access mode of this workstation.
399 * \param workstation a workstation
400 * \return the access mode for the tasks running on this workstation:
401 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
403 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
405 e_SD_workstation_access_mode_t
406 SD_workstation_get_access_mode(SD_workstation_t workstation)
408 return SD_workstation_priv(workstation)->access_mode;
412 * \brief Sets the access mode for the tasks that will be executed on a workstation
414 * By default, a workstation model is shared, i.e. several tasks
415 * can be executed at the same time on a workstation. The CPU power of
416 * the workstation is shared between the running tasks on the workstation.
417 * In sequential mode, only one task can use the workstation, and the other
418 * tasks wait in a FIFO.
420 * \param workstation a workstation
421 * \param access_mode the access mode you want to set to this workstation:
422 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
424 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
426 void SD_workstation_set_access_mode(SD_workstation_t workstation,
427 e_SD_workstation_access_mode_t
430 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
431 access_mode != SD_WORKSTATION_SHARED_ACCESS,
432 "Trying to set an invalid access mode");
434 if (access_mode == SD_workstation_priv(workstation)->access_mode) {
435 return; // nothing is changed
438 SD_workstation_priv(workstation)->access_mode = access_mode;
440 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
441 xbt_fifo_free(SD_workstation_priv(workstation)->task_fifo);
442 SD_workstation_priv(workstation)->task_fifo = NULL;
444 SD_workstation_priv(workstation)->task_fifo = xbt_fifo_new();
448 /* Returns whether a task can start now on a workstation*/
450 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
451 SD_CHECK_INIT_DONE();
452 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
454 return !__SD_workstation_is_busy(workstation) &&
455 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
459 /* Returns whether a workstation is busy. A workstation is busy is it is
460 * in sequential mode and a task is running on it or the fifo is not empty.
462 int __SD_workstation_is_busy(SD_workstation_t workstation)
465 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
466 SD_workstation_get_name(workstation),
467 (SD_workstation_priv(workstation)->access_mode ==
468 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
469 (SD_workstation_priv(workstation)->current_task ?
470 SD_task_get_name(SD_workstation_priv(workstation)->current_task)
472 (SD_workstation_priv(workstation)->task_fifo ? xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) :
475 return SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
476 (SD_workstation_priv(workstation)->current_task != NULL
477 || xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) > 0);
480 /* Destroys a workstation.
482 void __SD_workstation_destroy(void *workstation)
485 SD_workstation_priv_t w;
487 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
489 w = (SD_workstation_priv_t) workstation;
491 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
492 xbt_fifo_free(w->task_fifo);
498 * \brief Returns the kind of the task currently running on a workstation
499 * Only call this with sequential access mode set
500 * \param workstation a workstation */
501 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
503 xbt_assert(SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
504 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
505 " to use this function");
507 return (SD_workstation_priv(workstation)->current_task);
511 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
514 * \param AS, router name
515 * \return the xbt_dict_t properties of the AS
517 xbt_dict_t SD_as_router_get_properties(const char *asr)
519 return get_as_router_properties(asr);
522 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
525 * \param AS, router name
526 * \param The name of a properties
527 * \return value of the properties
529 const char* SD_as_router_get_property_value(const char *asr, const char *name)
531 xbt_dict_t dict = get_as_router_properties(asr);
532 if(!dict) return NULL;
533 return xbt_dict_get_or_null(dict,name);