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"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
18 "Logging specific to SimDag (workstation)");
20 /* Creates a workstation and registers it in SD.
22 SD_workstation_t __SD_workstation_create(void *surf_workstation,
26 SD_workstation_t workstation;
29 workstation = xbt_new(s_SD_workstation_t, 1);
30 workstation->surf_workstation = surf_workstation;
31 workstation->data = data; /* user data */
32 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
33 workstation->task_fifo = NULL;
34 workstation->current_task = NULL;
36 name = SD_workstation_get_name(workstation);
37 xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
42 * \brief Returns a workstation given its name
44 * If there is no such workstation, the function returns \c NULL.
46 * \param name workstation name
47 * \return the workstation, or \c NULL if there is no such workstation
49 SD_workstation_t SD_workstation_get_by_name(const char *name)
51 return xbt_lib_get_or_null(host_lib, name, SD_HOST_LEVEL);
55 * \brief Returns the workstation list
57 * Use SD_workstation_get_number() to know the array size.
59 * \return an array of \ref SD_workstation_t containing all workstations
60 * \see SD_workstation_get_number()
62 const SD_workstation_t *SD_workstation_get_list(void)
65 xbt_lib_cursor_t cursor;
70 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
72 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
73 sd_global->workstation_list =
74 xbt_new(SD_workstation_t, xbt_lib_length(host_lib));
77 xbt_lib_foreach(host_lib, cursor, key, data) {
78 if(data[SD_HOST_LEVEL])
79 sd_global->workstation_list[i++] = (SD_workstation_t) data[SD_HOST_LEVEL];
82 return sd_global->workstation_list;
86 * \brief Returns the number of workstations
88 * \return the number of existing workstations
89 * \see SD_workstation_get_list()
91 int SD_workstation_get_number(void)
93 return xbt_lib_length(host_lib);
97 * \brief Returns the user data of a workstation
99 * \param workstation a workstation
100 * \return the user data associated with this workstation (can be \c NULL)
101 * \see SD_workstation_set_data()
103 void *SD_workstation_get_data(SD_workstation_t workstation)
105 return workstation->data;
109 * \brief Sets the user data of a workstation
111 * The new data can be \c NULL. The old data should have been freed first
112 * if it was not \c NULL.
114 * \param workstation a workstation
115 * \param data the new data you want to associate with this workstation
116 * \see SD_workstation_get_data()
118 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
120 workstation->data = data;
124 * \brief Returns the name of a workstation
126 * \param workstation a workstation
127 * \return the name of this workstation (cannot be \c NULL)
129 const char *SD_workstation_get_name(SD_workstation_t workstation)
131 return surf_resource_name(workstation->surf_workstation);
135 * \brief Returns the value of a given workstation property
137 * \param ws a workstation
138 * \param name a property name
139 * \return value of a property (or NULL if property not set)
141 const char *SD_workstation_get_property_value(SD_workstation_t ws,
144 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
149 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
151 * \param workstation a workstation
152 * \return the dictionary containing the properties associated with the workstation
154 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
156 return surf_workstation_model->extension.
157 workstation.get_properties(workstation->surf_workstation);
162 /** @brief Displays debugging informations about a workstation */
163 void SD_workstation_dump(SD_workstation_t ws)
166 xbt_dict_cursor_t cursor=NULL;
168 SD_task_t task = NULL;
170 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
171 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
172 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
173 switch (ws->access_mode){
174 case SD_WORKSTATION_SHARED_ACCESS:
175 XBT_INFO(" - access mode: Space shared");
177 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
178 XBT_INFO(" - access mode: Exclusive");
179 task = SD_workstation_get_current_task(ws);
181 XBT_INFO(" current running task: %s",
182 SD_task_get_name(task));
184 XBT_INFO(" no task running");
188 props = SD_workstation_get_properties(ws);
190 if (!xbt_dict_is_empty(props)){
191 XBT_INFO(" - properties:");
193 xbt_dict_foreach(props,cursor,key,data) {
194 XBT_INFO(" %s->%s",key,data);
200 * \brief Returns the route between two workstations
202 * Use SD_route_get_size() to know the array size.
204 * \param src a workstation
205 * \param dst another workstation
206 * \return a new array of \ref SD_link_t representating the route between these two workstations
207 * \see SD_route_get_size(), SD_link_t
209 const SD_link_t *SD_route_get_list(SD_workstation_t src,
210 SD_workstation_t dst)
214 xbt_dynar_t surf_route;
215 const char *link_name;
219 if (sd_global->recyclable_route == NULL) {
221 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
224 surf_src = src->surf_workstation;
225 surf_dst = dst->surf_workstation;
227 surf_workstation_model->extension.workstation.get_route(surf_src,
230 xbt_dynar_foreach(surf_route, cpt, surf_link) {
231 link_name = surf_resource_name(surf_link);
232 sd_global->recyclable_route[cpt] =
233 xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
235 return sd_global->recyclable_route;
239 * \brief Returns the number of links on the route between two workstations
241 * \param src a workstation
242 * \param dst another workstation
243 * \return the number of links on the route between these two workstations
244 * \see SD_route_get_list()
246 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
248 return xbt_dynar_length(surf_workstation_model->extension.
249 workstation.get_route(src->surf_workstation,
250 dst->surf_workstation));
254 * \brief Returns the total power of a workstation
256 * \param workstation a workstation
257 * \return the total power of this workstation
258 * \see SD_workstation_get_available_power()
260 double SD_workstation_get_power(SD_workstation_t workstation)
262 return surf_workstation_model->extension.workstation.
263 get_speed(workstation->surf_workstation, 1.0);
267 * \brief Returns the proportion of available power in a workstation
269 * \param workstation a workstation
270 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
271 * \see SD_workstation_get_power()
273 double SD_workstation_get_available_power(SD_workstation_t workstation)
275 return surf_workstation_model->extension.
276 workstation.get_available_speed(workstation->surf_workstation);
280 * \brief Returns an approximative estimated time for the given computation amount on a workstation
282 * \param workstation a workstation
283 * \param computation_amount the computation amount you want to evaluate (in flops)
284 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
286 double SD_workstation_get_computation_time(SD_workstation_t workstation,
287 double computation_amount)
289 xbt_assert(computation_amount >= 0,
290 "computation_amount must be greater than or equal to zero");
291 return computation_amount / SD_workstation_get_power(workstation);
295 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
296 * between the workstations.
298 * \param src the first workstation
299 * \param dst the second workstation
300 * \return the latency of the route between the two workstations (in seconds)
301 * \see SD_route_get_current_bandwidth()
303 double SD_route_get_current_latency(SD_workstation_t src,
304 SD_workstation_t dst)
307 const SD_link_t *links;
312 links = SD_route_get_list(src, dst);
313 nb_links = SD_route_get_size(src, dst);
316 for (i = 0; i < nb_links; i++) {
317 latency += SD_link_get_current_latency(links[i]);
324 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
325 * between the workstations.
327 * \param src the first workstation
328 * \param dst the second workstation
329 * \return the bandwidth of the route between the two workstations (in bytes/second)
330 * \see SD_route_get_current_latency()
332 double SD_route_get_current_bandwidth(SD_workstation_t src,
333 SD_workstation_t dst)
336 const SD_link_t *links;
339 double min_bandwidth;
342 links = SD_route_get_list(src, dst);
343 nb_links = SD_route_get_size(src, dst);
344 bandwidth = min_bandwidth = -1.0;
347 for (i = 0; i < nb_links; i++) {
348 bandwidth = SD_link_get_current_bandwidth(links[i]);
349 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
350 min_bandwidth = bandwidth;
353 return min_bandwidth;
357 * \brief Returns an approximative estimated time for the given
358 * communication amount between two workstations
360 * \param src the first workstation
361 * \param dst the second workstation
362 * \param communication_amount the communication amount you want to evaluate (in bytes)
363 * \return an approximative astimated computation time for the given communication amount
364 * between the workstations (in seconds)
366 double SD_route_get_communication_time(SD_workstation_t src,
367 SD_workstation_t dst,
368 double communication_amount)
372 /* total time = latency + transmission time of the slowest link
373 transmission time of a link = communication amount / link bandwidth */
375 const SD_link_t *links;
377 double bandwidth, min_bandwidth;
381 xbt_assert(communication_amount >= 0,
382 "communication_amount must be greater than or equal to zero");
386 if (communication_amount == 0.0)
389 links = SD_route_get_list(src, dst);
390 nb_links = SD_route_get_size(src, dst);
391 min_bandwidth = -1.0;
394 for (i = 0; i < nb_links; i++) {
395 latency += SD_link_get_current_latency(links[i]);
396 bandwidth = SD_link_get_current_bandwidth(links[i]);
397 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
398 min_bandwidth = bandwidth;
401 return latency + (communication_amount / min_bandwidth);
405 * \brief Returns the access mode of this workstation.
407 * \param workstation a workstation
408 * \return the access mode for the tasks running on this workstation:
409 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
411 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
413 e_SD_workstation_access_mode_t
414 SD_workstation_get_access_mode(SD_workstation_t workstation)
416 return workstation->access_mode;
420 * \brief Sets the access mode for the tasks that will be executed on a workstation
422 * By default, a workstation model is shared, i.e. several tasks
423 * can be executed at the same time on a workstation. The CPU power of
424 * the workstation is shared between the running tasks on the workstation.
425 * In sequential mode, only one task can use the workstation, and the other
426 * tasks wait in a FIFO.
428 * \param workstation a workstation
429 * \param access_mode the access mode you want to set to this workstation:
430 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
432 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
434 void SD_workstation_set_access_mode(SD_workstation_t workstation,
435 e_SD_workstation_access_mode_t
438 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
439 access_mode != SD_WORKSTATION_SHARED_ACCESS,
440 "Trying to set an invalid access mode");
442 if (access_mode == workstation->access_mode) {
443 return; // nothing is changed
446 workstation->access_mode = access_mode;
448 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
449 xbt_fifo_free(workstation->task_fifo);
450 workstation->task_fifo = NULL;
452 workstation->task_fifo = xbt_fifo_new();
456 /* Returns whether a task can start now on a workstation*/
458 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
459 SD_CHECK_INIT_DONE();
460 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
462 return !__SD_workstation_is_busy(workstation) &&
463 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
467 /* Returns whether a workstation is busy. A workstation is busy is it is
468 * in sequential mode and a task is running on it or the fifo is not empty.
470 int __SD_workstation_is_busy(SD_workstation_t workstation)
473 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
474 SD_workstation_get_name(workstation),
475 (workstation->access_mode ==
476 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
477 (workstation->current_task ?
478 SD_task_get_name(workstation->current_task)
480 (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) :
483 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
484 (workstation->current_task != NULL
485 || xbt_fifo_size(workstation->task_fifo) > 0);
488 /* Destroys a workstation.
490 void __SD_workstation_destroy(void *workstation)
495 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
497 w = (SD_workstation_t) workstation;
499 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
500 xbt_fifo_free(w->task_fifo);
506 * \brief Returns the kind of the task currently running on a workstation
507 * Only call this with sequential access mode set
508 * \param workstation a workstation */
509 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
511 xbt_assert(workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
512 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
513 " to use this function");
515 return (workstation->current_task);
519 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
522 * \param AS, router name
523 * \return the xbt_dict_t properties of the AS
525 xbt_dict_t SD_as_router_get_properties(const char *asr)
527 return get_as_router_properties(asr);
530 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
533 * \param AS, router name
534 * \param The name of a properties
535 * \return value of the properties
537 const char* SD_as_router_get_property_value(const char *asr, const char *name)
539 xbt_dict_t dict = get_as_router_properties(asr);
540 if(!dict) return NULL;
541 return xbt_dict_get_or_null(dict,name);