1 /* Copyright (c) 2006-2014. 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);
38 /* Creates a storage and registers it in SD.
40 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
43 SD_storage_priv_t storage;
46 storage = xbt_new(s_SD_storage_priv_t, 1);
47 storage->data = data; /* user data */
48 name = surf_resource_name(surf_storage);
49 storage->host = surf_storage_get_host(surf_storage_resource_by_name(name));
50 xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
51 return xbt_lib_get_elm_or_null(storage_lib, name);
54 /* Destroys a storage.
56 void __SD_storage_destroy(void *storage)
60 s = (SD_storage_priv_t) storage;
65 * \brief Returns a workstation given its name
67 * If there is no such workstation, the function returns \c NULL.
69 * \param name workstation name
70 * \return the workstation, or \c NULL if there is no such workstation
72 SD_workstation_t SD_workstation_get_by_name(const char *name)
74 return xbt_lib_get_elm_or_null(host_lib, name);
78 * \brief Returns the workstation list
80 * Use SD_workstation_get_number() to know the array size.
82 * \return an array of \ref SD_workstation_t containing all workstations
83 * \see SD_workstation_get_number()
85 const SD_workstation_t *SD_workstation_get_list(void)
88 xbt_lib_cursor_t cursor;
93 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
95 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
96 sd_global->workstation_list =
97 xbt_new(SD_workstation_t, xbt_lib_length(host_lib));
100 xbt_lib_foreach(host_lib, cursor, key, data) {
101 if(data[SD_HOST_LEVEL])
102 sd_global->workstation_list[i++] = xbt_dict_cursor_get_elm(cursor);
105 return sd_global->workstation_list;
109 * \brief Returns the number of workstations
111 * \return the number of existing workstations
112 * \see SD_workstation_get_list()
114 int SD_workstation_get_number(void)
116 return xbt_lib_length(host_lib);
120 * \brief Returns the user data of a workstation
122 * \param workstation a workstation
123 * \return the user data associated with this workstation (can be \c NULL)
124 * \see SD_workstation_set_data()
126 void *SD_workstation_get_data(SD_workstation_t workstation)
128 return SD_workstation_priv(workstation)->data;
132 * \brief Sets the user data of a workstation
134 * The new data can be \c NULL. The old data should have been freed first
135 * if it was not \c NULL.
137 * \param workstation a workstation
138 * \param data the new data you want to associate with this workstation
139 * \see SD_workstation_get_data()
141 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
143 SD_workstation_priv(workstation)->data = data;
147 * \brief Returns the name of a workstation
149 * \param workstation a workstation
150 * \return the name of this workstation (cannot be \c NULL)
152 const char *SD_workstation_get_name(SD_workstation_t workstation)
154 return sg_host_name(workstation);
158 * \brief Returns the value of a given workstation property
160 * \param ws a workstation
161 * \param name a property name
162 * \return value of a property (or NULL if property not set)
164 const char *SD_workstation_get_property_value(SD_workstation_t ws,
167 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
172 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
174 * \param workstation a workstation
175 * \return the dictionary containing the properties associated with the workstation
177 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
179 return surf_resource_get_properties(surf_workstation_resource_priv(workstation));
183 /** @brief Displays debugging informations about a workstation */
184 void SD_workstation_dump(SD_workstation_t ws)
187 xbt_dict_cursor_t cursor=NULL;
189 SD_task_t task = NULL;
191 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
192 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
193 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
194 switch (SD_workstation_priv(ws)->access_mode){
195 case SD_WORKSTATION_SHARED_ACCESS:
196 XBT_INFO(" - access mode: Space shared");
198 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
199 XBT_INFO(" - access mode: Exclusive");
200 task = SD_workstation_get_current_task(ws);
202 XBT_INFO(" current running task: %s",
203 SD_task_get_name(task));
205 XBT_INFO(" no task running");
209 props = SD_workstation_get_properties(ws);
211 if (!xbt_dict_is_empty(props)){
212 XBT_INFO(" - properties:");
214 xbt_dict_foreach(props,cursor,key,data) {
215 XBT_INFO(" %s->%s",key,data);
221 * \brief Returns the route between two workstations
223 * Use SD_route_get_size() to know the array size.
225 * \param src a workstation
226 * \param dst another workstation
227 * \return a new array of \ref SD_link_t representating the route between these two workstations
228 * \see SD_route_get_size(), SD_link_t
230 const SD_link_t *SD_route_get_list(SD_workstation_t src,
231 SD_workstation_t dst)
235 xbt_dynar_t surf_route;
236 const char *link_name;
240 if (sd_global->recyclable_route == NULL) {
242 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
248 surf_route = surf_workstation_model_get_route((surf_workstation_model_t)surf_workstation_model,
251 xbt_dynar_foreach(surf_route, cpt, surf_link) {
252 link_name = surf_resource_name(surf_link);
253 sd_global->recyclable_route[cpt] =
254 xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
256 return sd_global->recyclable_route;
260 * \brief Returns the number of links on the route between two workstations
262 * \param src a workstation
263 * \param dst another workstation
264 * \return the number of links on the route between these two workstations
265 * \see SD_route_get_list()
267 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
269 return xbt_dynar_length(surf_workstation_model_get_route(
270 (surf_workstation_model_t)surf_workstation_model, src, dst));
274 * \brief Returns the total power of a workstation
276 * \param workstation a workstation
277 * \return the total power of this workstation
278 * \see SD_workstation_get_available_power()
280 double SD_workstation_get_power(SD_workstation_t workstation)
282 return surf_workstation_get_speed(workstation, 1.0);
285 * \brief Returns the amount of cores of a workstation
287 * \param workstation a workstation
288 * \return the amount of cores of this workstation
290 int SD_workstation_get_cores(SD_workstation_t workstation) {
291 return surf_workstation_get_core(workstation);
295 * \brief Returns the proportion of available power in a workstation
297 * \param workstation a workstation
298 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
299 * \see SD_workstation_get_power()
301 double SD_workstation_get_available_power(SD_workstation_t workstation)
303 return surf_workstation_get_available_speed(workstation);
307 * \brief Returns an approximative estimated time for the given computation amount on a workstation
309 * \param workstation a workstation
310 * \param computation_amount the computation amount you want to evaluate (in flops)
311 * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
313 double SD_workstation_get_computation_time(SD_workstation_t workstation,
314 double computation_amount)
316 xbt_assert(computation_amount >= 0,
317 "computation_amount must be greater than or equal to zero");
318 return computation_amount / SD_workstation_get_power(workstation);
322 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
323 * between the workstations.
325 * \param src the first workstation
326 * \param dst the second workstation
327 * \return the latency of the route between the two workstations (in seconds)
328 * \see SD_route_get_current_bandwidth()
330 double SD_route_get_current_latency(SD_workstation_t src,
331 SD_workstation_t dst)
334 const SD_link_t *links;
339 links = SD_route_get_list(src, dst);
340 nb_links = SD_route_get_size(src, dst);
343 for (i = 0; i < nb_links; i++) {
344 latency += SD_link_get_current_latency(links[i]);
351 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
352 * between the workstations.
354 * \param src the first workstation
355 * \param dst the second workstation
356 * \return the bandwidth of the route between the two workstations (in bytes/second)
357 * \see SD_route_get_current_latency()
359 double SD_route_get_current_bandwidth(SD_workstation_t src,
360 SD_workstation_t dst)
363 const SD_link_t *links;
366 double min_bandwidth;
369 links = SD_route_get_list(src, dst);
370 nb_links = SD_route_get_size(src, dst);
371 min_bandwidth = -1.0;
373 for (i = 0; i < nb_links; i++) {
374 bandwidth = SD_link_get_current_bandwidth(links[i]);
375 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
376 min_bandwidth = bandwidth;
379 return min_bandwidth;
383 * \brief Returns an approximative estimated time for the given
384 * communication amount between two workstations
386 * \param src the first workstation
387 * \param dst the second workstation
388 * \param communication_amount the communication amount you want to evaluate (in bytes)
389 * \return an approximative estimated computation time for the given communication amount
390 * between the workstations (in seconds)
392 double SD_route_get_communication_time(SD_workstation_t src,
393 SD_workstation_t dst,
394 double communication_amount)
398 /* total time = latency + transmission time of the slowest link
399 transmission time of a link = communication amount / link bandwidth */
401 const SD_link_t *links;
403 double bandwidth, min_bandwidth;
407 xbt_assert(communication_amount >= 0,
408 "communication_amount must be greater than or equal to zero");
412 if (communication_amount == 0.0)
415 links = SD_route_get_list(src, dst);
416 nb_links = SD_route_get_size(src, dst);
417 min_bandwidth = -1.0;
420 for (i = 0; i < nb_links; i++) {
421 latency += SD_link_get_current_latency(links[i]);
422 bandwidth = SD_link_get_current_bandwidth(links[i]);
423 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
424 min_bandwidth = bandwidth;
427 return latency + (communication_amount / min_bandwidth);
431 * \brief Returns the access mode of this workstation.
433 * \param workstation a workstation
434 * \return the access mode for the tasks running on this workstation:
435 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
437 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
439 e_SD_workstation_access_mode_t
440 SD_workstation_get_access_mode(SD_workstation_t workstation)
442 return SD_workstation_priv(workstation)->access_mode;
446 * \brief Sets the access mode for the tasks that will be executed on a workstation
448 * By default, a workstation model is shared, i.e. several tasks
449 * can be executed at the same time on a workstation. The CPU power of
450 * the workstation is shared between the running tasks on the workstation.
451 * In sequential mode, only one task can use the workstation, and the other
452 * tasks wait in a FIFO.
454 * \param workstation a workstation
455 * \param access_mode the access mode you want to set to this workstation:
456 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
458 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
460 void SD_workstation_set_access_mode(SD_workstation_t workstation,
461 e_SD_workstation_access_mode_t
464 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
465 access_mode != SD_WORKSTATION_SHARED_ACCESS,
466 "Trying to set an invalid access mode");
468 if (access_mode == SD_workstation_priv(workstation)->access_mode) {
469 return; // nothing is changed
472 SD_workstation_priv(workstation)->access_mode = access_mode;
474 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
475 xbt_fifo_free(SD_workstation_priv(workstation)->task_fifo);
476 SD_workstation_priv(workstation)->task_fifo = NULL;
478 SD_workstation_priv(workstation)->task_fifo = xbt_fifo_new();
483 * \brief Return the list of mounted storages on a workstation.
485 * \param workstation a workstation
486 * \return a dynar containing all mounted storages on the workstation
488 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
489 return surf_workstation_get_mounted_storage_list(workstation);
493 * \brief Return the list of mounted storages on a workstation.
495 * \param workstation a workstation
496 * \return a dynar containing all mounted storages on the workstation
498 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
499 return surf_workstation_get_attached_storage_list(workstation);
503 * \brief Returns the host name the storage is attached to
505 * This functions checks whether a storage is a valid pointer or not and return its name.
507 const char *SD_storage_get_host(msg_storage_t storage) {
508 xbt_assert((storage != NULL), "Invalid parameters");
509 SD_storage_priv_t priv = SD_storage_priv(storage);
513 /* Returns whether a task can start now on a workstation*/
515 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
516 SD_CHECK_INIT_DONE();
517 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
519 return !__SD_workstation_is_busy(workstation) &&
520 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
524 /* Returns whether a workstation is busy. A workstation is busy is it is
525 * in sequential mode and a task is running on it or the fifo is not empty.
527 int __SD_workstation_is_busy(SD_workstation_t workstation)
530 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
531 SD_workstation_get_name(workstation),
532 (SD_workstation_priv(workstation)->access_mode ==
533 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
534 (SD_workstation_priv(workstation)->current_task ?
535 SD_task_get_name(SD_workstation_priv(workstation)->current_task)
537 (SD_workstation_priv(workstation)->task_fifo ? xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) :
540 return SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
541 (SD_workstation_priv(workstation)->current_task != NULL
542 || xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) > 0);
545 /* Destroys a workstation.
547 void __SD_workstation_destroy(void *workstation)
550 SD_workstation_priv_t w;
552 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
554 w = (SD_workstation_priv_t) workstation;
556 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
557 xbt_fifo_free(w->task_fifo);
563 * \brief Returns the kind of the task currently running on a workstation
564 * Only call this with sequential access mode set
565 * \param workstation a workstation */
566 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
568 xbt_assert(SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
569 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
570 " to use this function");
572 return (SD_workstation_priv(workstation)->current_task);
576 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
579 * \param AS, router name
580 * \return the xbt_dict_t properties of the AS
582 xbt_dict_t SD_as_router_get_properties(const char *asr)
584 return get_as_router_properties(asr);
587 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
590 * \param AS, router name
591 * \param The name of a properties
592 * \return value of the properties
594 const char* SD_as_router_get_property_value(const char *asr, const char *name)
596 xbt_dict_t dict = get_as_router_properties(asr);
597 if(!dict) return NULL;
598 return xbt_dict_get_or_null(dict,name);