1 /* Copyright (c) 2006-2013. 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_priv_t workstation;
29 workstation = xbt_new(s_SD_workstation_priv_t, 1);
30 workstation->data = data; /* user data */
31 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
32 workstation->task_fifo = NULL;
33 workstation->current_task = NULL;
35 name = surf_resource_name(surf_workstation);
36 xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
37 return xbt_lib_get_elm_or_null(host_lib,name);
40 /* Creates a storage and registers it in SD.
42 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
45 SD_storage_priv_t storage;
48 storage = xbt_new(s_SD_storage_priv_t, 1);
49 storage->data = data; /* user data */
51 name = surf_resource_name(surf_storage);
52 xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
53 return xbt_lib_get_elm_or_null(storage_lib, name);
56 /* Destroys a storage.
58 void __SD_storage_destroy(void *storage)
62 s = (SD_storage_priv_t) storage;
67 * \brief Returns a workstation given its name
69 * If there is no such workstation, the function returns \c NULL.
71 * \param name workstation name
72 * \return the workstation, or \c NULL if there is no such workstation
74 SD_workstation_t SD_workstation_get_by_name(const char *name)
76 return xbt_lib_get_elm_or_null(host_lib, name);
80 * \brief Returns the workstation list
82 * Use SD_workstation_get_number() to know the array size.
84 * \return an array of \ref SD_workstation_t containing all workstations
85 * \see SD_workstation_get_number()
87 const SD_workstation_t *SD_workstation_get_list(void)
90 xbt_lib_cursor_t cursor;
95 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
97 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
98 sd_global->workstation_list =
99 xbt_new(SD_workstation_t, xbt_lib_length(host_lib));
102 xbt_lib_foreach(host_lib, cursor, key, data) {
103 if(data[SD_HOST_LEVEL])
104 sd_global->workstation_list[i++] = xbt_dict_cursor_get_elm(cursor);
107 return sd_global->workstation_list;
111 * \brief Returns the number of workstations
113 * \return the number of existing workstations
114 * \see SD_workstation_get_list()
116 int SD_workstation_get_number(void)
118 return xbt_lib_length(host_lib);
122 * \brief Returns the user data of a workstation
124 * \param workstation a workstation
125 * \return the user data associated with this workstation (can be \c NULL)
126 * \see SD_workstation_set_data()
128 void *SD_workstation_get_data(SD_workstation_t workstation)
130 return SD_workstation_priv(workstation)->data;
134 * \brief Sets the user data of a workstation
136 * The new data can be \c NULL. The old data should have been freed first
137 * if it was not \c NULL.
139 * \param workstation a workstation
140 * \param data the new data you want to associate with this workstation
141 * \see SD_workstation_get_data()
143 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
145 SD_workstation_priv(workstation)->data = data;
149 * \brief Returns the name of a workstation
151 * \param workstation a workstation
152 * \return the name of this workstation (cannot be \c NULL)
154 const char *SD_workstation_get_name(SD_workstation_t workstation)
156 return sg_host_name(workstation);
160 * \brief Returns the value of a given workstation property
162 * \param ws a workstation
163 * \param name a property name
164 * \return value of a property (or NULL if property not set)
166 const char *SD_workstation_get_property_value(SD_workstation_t ws,
169 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
174 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
176 * \param workstation a workstation
177 * \return the dictionary containing the properties associated with the workstation
179 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
181 return surf_workstation_model->extension.
182 workstation.get_properties(surf_workstation_resource_priv(workstation));
186 /** @brief Displays debugging informations about a workstation */
187 void SD_workstation_dump(SD_workstation_t ws)
190 xbt_dict_cursor_t cursor=NULL;
192 SD_task_t task = NULL;
194 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
195 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
196 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
197 switch (SD_workstation_priv(ws)->access_mode){
198 case SD_WORKSTATION_SHARED_ACCESS:
199 XBT_INFO(" - access mode: Space shared");
201 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
202 XBT_INFO(" - access mode: Exclusive");
203 task = SD_workstation_get_current_task(ws);
205 XBT_INFO(" current running task: %s",
206 SD_task_get_name(task));
208 XBT_INFO(" no task running");
212 props = SD_workstation_get_properties(ws);
214 if (!xbt_dict_is_empty(props)){
215 XBT_INFO(" - properties:");
217 xbt_dict_foreach(props,cursor,key,data) {
218 XBT_INFO(" %s->%s",key,data);
224 * \brief Returns the route between two workstations
226 * Use SD_route_get_size() to know the array size.
228 * \param src a workstation
229 * \param dst another workstation
230 * \return a new array of \ref SD_link_t representating the route between these two workstations
231 * \see SD_route_get_size(), SD_link_t
233 const SD_link_t *SD_route_get_list(SD_workstation_t src,
234 SD_workstation_t dst)
238 xbt_dynar_t surf_route;
239 const char *link_name;
243 if (sd_global->recyclable_route == NULL) {
245 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
251 surf_workstation_model->extension.workstation.get_route(surf_src,
254 xbt_dynar_foreach(surf_route, cpt, surf_link) {
255 link_name = surf_resource_name(surf_link);
256 sd_global->recyclable_route[cpt] =
257 xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
259 return sd_global->recyclable_route;
263 * \brief Returns the number of links on the route between two workstations
265 * \param src a workstation
266 * \param dst another workstation
267 * \return the number of links on the route between these two workstations
268 * \see SD_route_get_list()
270 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
272 return xbt_dynar_length(surf_workstation_model->extension.
273 workstation.get_route(src, dst));
277 * \brief Returns the total power of a workstation
279 * \param workstation a workstation
280 * \return the total power of this workstation
281 * \see SD_workstation_get_available_power()
283 double SD_workstation_get_power(SD_workstation_t workstation)
285 return surf_workstation_model->extension.workstation.
286 get_speed(workstation, 1.0);
290 * \brief Returns the proportion of available power in a workstation
292 * \param workstation a workstation
293 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
294 * \see SD_workstation_get_power()
296 double SD_workstation_get_available_power(SD_workstation_t workstation)
298 return surf_workstation_model->extension.
299 workstation.get_available_speed(workstation);
303 * \brief Returns an approximative estimated time for the given computation amount on a workstation
305 * \param workstation a workstation
306 * \param computation_amount the computation amount you want to evaluate (in flops)
307 * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
309 double SD_workstation_get_computation_time(SD_workstation_t workstation,
310 double computation_amount)
312 xbt_assert(computation_amount >= 0,
313 "computation_amount must be greater than or equal to zero");
314 return computation_amount / SD_workstation_get_power(workstation);
318 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
319 * between the workstations.
321 * \param src the first workstation
322 * \param dst the second workstation
323 * \return the latency of the route between the two workstations (in seconds)
324 * \see SD_route_get_current_bandwidth()
326 double SD_route_get_current_latency(SD_workstation_t src,
327 SD_workstation_t dst)
330 const SD_link_t *links;
335 links = SD_route_get_list(src, dst);
336 nb_links = SD_route_get_size(src, dst);
339 for (i = 0; i < nb_links; i++) {
340 latency += SD_link_get_current_latency(links[i]);
347 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
348 * between the workstations.
350 * \param src the first workstation
351 * \param dst the second workstation
352 * \return the bandwidth of the route between the two workstations (in bytes/second)
353 * \see SD_route_get_current_latency()
355 double SD_route_get_current_bandwidth(SD_workstation_t src,
356 SD_workstation_t dst)
359 const SD_link_t *links;
362 double min_bandwidth;
365 links = SD_route_get_list(src, dst);
366 nb_links = SD_route_get_size(src, dst);
367 min_bandwidth = -1.0;
369 for (i = 0; i < nb_links; i++) {
370 bandwidth = SD_link_get_current_bandwidth(links[i]);
371 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
372 min_bandwidth = bandwidth;
375 return min_bandwidth;
379 * \brief Returns an approximative estimated time for the given
380 * communication amount between two workstations
382 * \param src the first workstation
383 * \param dst the second workstation
384 * \param communication_amount the communication amount you want to evaluate (in bytes)
385 * \return an approximative estimated computation time for the given communication amount
386 * between the workstations (in seconds)
388 double SD_route_get_communication_time(SD_workstation_t src,
389 SD_workstation_t dst,
390 double communication_amount)
394 /* total time = latency + transmission time of the slowest link
395 transmission time of a link = communication amount / link bandwidth */
397 const SD_link_t *links;
399 double bandwidth, min_bandwidth;
403 xbt_assert(communication_amount >= 0,
404 "communication_amount must be greater than or equal to zero");
408 if (communication_amount == 0.0)
411 links = SD_route_get_list(src, dst);
412 nb_links = SD_route_get_size(src, dst);
413 min_bandwidth = -1.0;
416 for (i = 0; i < nb_links; i++) {
417 latency += SD_link_get_current_latency(links[i]);
418 bandwidth = SD_link_get_current_bandwidth(links[i]);
419 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
420 min_bandwidth = bandwidth;
423 return latency + (communication_amount / min_bandwidth);
427 * \brief Returns the access mode of this workstation.
429 * \param workstation a workstation
430 * \return the access mode for the tasks running on this workstation:
431 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
433 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
435 e_SD_workstation_access_mode_t
436 SD_workstation_get_access_mode(SD_workstation_t workstation)
438 return SD_workstation_priv(workstation)->access_mode;
442 * \brief Sets the access mode for the tasks that will be executed on a workstation
444 * By default, a workstation model is shared, i.e. several tasks
445 * can be executed at the same time on a workstation. The CPU power of
446 * the workstation is shared between the running tasks on the workstation.
447 * In sequential mode, only one task can use the workstation, and the other
448 * tasks wait in a FIFO.
450 * \param workstation a workstation
451 * \param access_mode the access mode you want to set to this workstation:
452 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
454 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
456 void SD_workstation_set_access_mode(SD_workstation_t workstation,
457 e_SD_workstation_access_mode_t
460 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
461 access_mode != SD_WORKSTATION_SHARED_ACCESS,
462 "Trying to set an invalid access mode");
464 if (access_mode == SD_workstation_priv(workstation)->access_mode) {
465 return; // nothing is changed
468 SD_workstation_priv(workstation)->access_mode = access_mode;
470 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
471 xbt_fifo_free(SD_workstation_priv(workstation)->task_fifo);
472 SD_workstation_priv(workstation)->task_fifo = NULL;
474 SD_workstation_priv(workstation)->task_fifo = xbt_fifo_new();
479 * \brief Return the list of mounted storages on a workstation.
481 * \param workstation a workstation
482 * \return a dynar containing all mounted storages on the workstation
484 xbt_dict_t SD_workstation_get_storage_list(SD_workstation_t workstation){
485 return surf_workstation_model->extension.workstation.get_storage_list(workstation);
488 /* Returns whether a task can start now on a workstation*/
490 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
491 SD_CHECK_INIT_DONE();
492 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
494 return !__SD_workstation_is_busy(workstation) &&
495 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
499 /* Returns whether a workstation is busy. A workstation is busy is it is
500 * in sequential mode and a task is running on it or the fifo is not empty.
502 int __SD_workstation_is_busy(SD_workstation_t workstation)
505 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
506 SD_workstation_get_name(workstation),
507 (SD_workstation_priv(workstation)->access_mode ==
508 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
509 (SD_workstation_priv(workstation)->current_task ?
510 SD_task_get_name(SD_workstation_priv(workstation)->current_task)
512 (SD_workstation_priv(workstation)->task_fifo ? xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) :
515 return SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
516 (SD_workstation_priv(workstation)->current_task != NULL
517 || xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) > 0);
520 /* Destroys a workstation.
522 void __SD_workstation_destroy(void *workstation)
525 SD_workstation_priv_t w;
527 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
529 w = (SD_workstation_priv_t) workstation;
531 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
532 xbt_fifo_free(w->task_fifo);
538 * \brief Returns the kind of the task currently running on a workstation
539 * Only call this with sequential access mode set
540 * \param workstation a workstation */
541 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
543 xbt_assert(SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
544 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
545 " to use this function");
547 return (SD_workstation_priv(workstation)->current_task);
551 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
554 * \param AS, router name
555 * \return the xbt_dict_t properties of the AS
557 xbt_dict_t SD_as_router_get_properties(const char *asr)
559 return get_as_router_properties(asr);
562 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
565 * \param AS, router name
566 * \param The name of a properties
567 * \return value of the properties
569 const char* SD_as_router_get_property_value(const char *asr, const char *name)
571 xbt_dict_t dict = get_as_router_properties(asr);
572 if(!dict) return NULL;
573 return xbt_dict_get_or_null(dict,name);