1 /* Copyright (c) 2006-2015. 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 "simgrid/simdag.h"
11 #include "xbt/sysdep.h"
12 #include "surf/surf.h"
13 #include "simgrid/msg.h" //FIXME: why?
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->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
29 workstation->task_fifo = NULL;
30 workstation->current_task = NULL;
32 name = surf_resource_name(surf_workstation);
33 sg_host_t sg_host = sg_host_by_name(name);
34 sg_host_sd_set(sg_host,workstation);
35 sg_host_user_set(sg_host,data);
39 /* Creates a storage and registers it in SD.
41 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
44 SD_storage_priv_t storage;
47 storage = xbt_new(s_SD_storage_priv_t, 1);
48 storage->data = data; /* user data */
49 name = surf_resource_name(surf_storage);
50 storage->host = surf_storage_get_host(surf_storage_resource_by_name(name));
51 xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
52 return xbt_lib_get_elm_or_null(storage_lib, name);
55 /* Destroys a storage.
57 void __SD_storage_destroy(void *storage)
61 s = (SD_storage_priv_t) storage;
66 * \brief Returns a workstation given its name
68 * If there is no such workstation, the function returns \c NULL.
70 * \param name workstation name
71 * \return the workstation, or \c NULL if there is no such workstation
73 SD_workstation_t SD_workstation_get_by_name(const char *name)
75 return xbt_lib_get_elm_or_null(host_lib, name);
79 * \brief Returns the workstation list
81 * Use SD_workstation_get_number() to know the array size.
83 * \return an array of \ref SD_workstation_t containing all workstations
84 * \remark The workstation order in the returned array is generally different from the workstation creation/declaration order in the XML platform (we use a hash table internally).
85 * \see SD_workstation_get_number()
87 const SD_workstation_t *SD_workstation_get_list(void) {
88 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
90 if (sd_global->workstation_list == NULL) /* this is the first time the function is called */
91 sd_global->workstation_list = xbt_dynar_to_array(sg_hosts_as_dynar());
93 return sd_global->workstation_list;
97 * \brief Returns the number of workstations
99 * \return the number of existing workstations
100 * \see SD_workstation_get_list()
102 int SD_workstation_get_number(void)
104 return xbt_lib_length(host_lib);
108 * \brief Returns the user data of a workstation
110 * \param workstation a workstation
111 * \return the user data associated with this workstation (can be \c NULL)
112 * \see SD_workstation_set_data()
114 void *SD_workstation_get_data(SD_workstation_t workstation)
116 return sg_host_user(workstation);
120 * \brief Sets the user data of a workstation
122 * The new data can be \c NULL. The old data should have been freed first
123 * if it was not \c NULL.
125 * \param workstation a workstation
126 * \param data the new data you want to associate with this workstation
127 * \see SD_workstation_get_data()
129 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
131 sg_host_user_set(workstation, data);
135 * \brief Returns the name of a workstation
137 * \param workstation a workstation
138 * \return the name of this workstation (cannot be \c NULL)
140 const char *SD_workstation_get_name(SD_workstation_t workstation)
142 return sg_host_get_name(workstation);
146 * \brief Returns the value of a given workstation property
148 * \param ws a workstation
149 * \param name a property name
150 * \return value of a property (or NULL if property not set)
152 const char *SD_workstation_get_property_value(SD_workstation_t ws,
155 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
160 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
162 * \param workstation a workstation
163 * \return the dictionary containing the properties associated with the workstation
165 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
167 return surf_host_get_properties(surf_host_resource_priv(workstation));
171 /** @brief Displays debugging informations about a workstation */
172 void SD_workstation_dump(SD_workstation_t ws)
175 xbt_dict_cursor_t cursor=NULL;
177 SD_task_t task = NULL;
179 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
180 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
181 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
182 switch (sg_host_sd(ws)->access_mode){
183 case SD_WORKSTATION_SHARED_ACCESS:
184 XBT_INFO(" - access mode: Space shared");
186 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
187 XBT_INFO(" - access mode: Exclusive");
188 task = SD_workstation_get_current_task(ws);
190 XBT_INFO(" current running task: %s",
191 SD_task_get_name(task));
193 XBT_INFO(" no task running");
197 props = SD_workstation_get_properties(ws);
199 if (!xbt_dict_is_empty(props)){
200 XBT_INFO(" - properties:");
202 xbt_dict_foreach(props,cursor,key,data) {
203 XBT_INFO(" %s->%s",key,data);
209 * \brief Returns the route between two workstations
211 * Use SD_route_get_size() to know the array size.
213 * \param src a workstation
214 * \param dst another workstation
215 * \return a new array of \ref SD_link_t representating the route between these two workstations
216 * \see SD_route_get_size(), SD_link_t
218 const SD_link_t *SD_route_get_list(SD_workstation_t src,
219 SD_workstation_t dst)
223 xbt_dynar_t surf_route;
227 if (sd_global->recyclable_route == NULL) {
229 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
235 surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model,
238 xbt_dynar_foreach(surf_route, cpt, surf_link) {
239 sd_global->recyclable_route[cpt] = surf_link;
241 return sd_global->recyclable_route;
245 * \brief Returns the number of links on the route between two workstations
247 * \param src a workstation
248 * \param dst another workstation
249 * \return the number of links on the route between these two workstations
250 * \see SD_route_get_list()
252 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
254 return xbt_dynar_length(surf_host_model_get_route(
255 (surf_host_model_t)surf_host_model, src, dst));
259 * \brief Returns the total power of a workstation
261 * \param workstation a workstation
262 * \return the total power of this workstation
263 * \see SD_workstation_get_available_power()
265 double SD_workstation_get_power(SD_workstation_t workstation)
267 return surf_host_get_speed(workstation, 1.0);
270 * \brief Returns the amount of cores of a workstation
272 * \param workstation a workstation
273 * \return the amount of cores of this workstation
275 int SD_workstation_get_cores(SD_workstation_t workstation) {
276 return surf_host_get_core(workstation);
280 * \brief Returns the proportion of available power in a workstation
282 * \param workstation a workstation
283 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
284 * \see SD_workstation_get_power()
286 double SD_workstation_get_available_power(SD_workstation_t workstation)
288 return surf_host_get_available_speed(workstation);
292 * \brief Returns an approximative estimated time for the given computation amount on a workstation
294 * \param workstation a workstation
295 * \param flops_amount the computation amount you want to evaluate (in flops)
296 * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
298 double SD_workstation_get_computation_time(SD_workstation_t workstation,
301 xbt_assert(flops_amount >= 0,
302 "flops_amount must be greater than or equal to zero");
303 return flops_amount / SD_workstation_get_power(workstation);
307 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
308 * between the workstations.
310 * \param src the first workstation
311 * \param dst the second workstation
312 * \return the latency of the route between the two workstations (in seconds)
313 * \see SD_route_get_current_bandwidth()
315 double SD_route_get_current_latency(SD_workstation_t src,
316 SD_workstation_t dst)
319 const SD_link_t *links;
324 links = SD_route_get_list(src, dst);
325 nb_links = SD_route_get_size(src, dst);
328 for (i = 0; i < nb_links; i++) {
329 latency += SD_link_get_current_latency(links[i]);
336 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
337 * between the workstations.
339 * \param src the first workstation
340 * \param dst the second workstation
341 * \return the bandwidth of the route between the two workstations (in bytes/second)
342 * \see SD_route_get_current_latency()
344 double SD_route_get_current_bandwidth(SD_workstation_t src,
345 SD_workstation_t dst)
348 const SD_link_t *links;
351 double min_bandwidth;
354 links = SD_route_get_list(src, dst);
355 nb_links = SD_route_get_size(src, dst);
356 min_bandwidth = -1.0;
358 for (i = 0; i < nb_links; i++) {
359 bandwidth = SD_link_get_current_bandwidth(links[i]);
360 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
361 min_bandwidth = bandwidth;
364 return min_bandwidth;
368 * \brief Returns an approximative estimated time for the given
369 * communication amount between two workstations
371 * \param src the first workstation
372 * \param dst the second workstation
373 * \param bytes_amount the communication amount you want to evaluate (in bytes)
374 * \return an approximative estimated communication time for the given bytes amount
375 * between the workstations (in seconds)
377 double SD_route_get_communication_time(SD_workstation_t src,
378 SD_workstation_t dst,
383 /* total time = latency + transmission time of the slowest link
384 transmission time of a link = communication amount / link bandwidth */
386 const SD_link_t *links;
388 double bandwidth, min_bandwidth;
392 xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
395 if (bytes_amount == 0.0)
398 links = SD_route_get_list(src, dst);
399 nb_links = SD_route_get_size(src, dst);
400 min_bandwidth = -1.0;
403 for (i = 0; i < nb_links; i++) {
404 latency += SD_link_get_current_latency(links[i]);
405 bandwidth = SD_link_get_current_bandwidth(links[i]);
406 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
407 min_bandwidth = bandwidth;
410 return latency + (bytes_amount / min_bandwidth);
414 * \brief Returns the access mode of this workstation.
416 * \param workstation a workstation
417 * \return the access mode for the tasks running on this workstation:
418 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
420 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
422 e_SD_workstation_access_mode_t
423 SD_workstation_get_access_mode(SD_workstation_t workstation)
425 return sg_host_sd(workstation)->access_mode;
429 * \brief Sets the access mode for the tasks that will be executed on a workstation
431 * By default, a workstation model is shared, i.e. several tasks
432 * can be executed at the same time on a workstation. The CPU power of
433 * the workstation is shared between the running tasks on the workstation.
434 * In sequential mode, only one task can use the workstation, and the other
435 * tasks wait in a FIFO.
437 * \param workstation a workstation
438 * \param access_mode the access mode you want to set to this workstation:
439 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
441 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
443 void SD_workstation_set_access_mode(SD_workstation_t workstation,
444 e_SD_workstation_access_mode_t
447 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
448 access_mode != SD_WORKSTATION_SHARED_ACCESS,
449 "Trying to set an invalid access mode");
451 if (access_mode == sg_host_sd(workstation)->access_mode) {
452 return; // nothing is changed
455 sg_host_sd(workstation)->access_mode = access_mode;
457 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
458 xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
459 sg_host_sd(workstation)->task_fifo = NULL;
461 sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
466 * \brief Return the list of mounted storages on a workstation.
468 * \param workstation a workstation
469 * \return a dynar containing all mounted storages on the workstation
471 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
472 return surf_host_get_mounted_storage_list(workstation);
476 * \brief Return the list of mounted storages on a workstation.
478 * \param workstation a workstation
479 * \return a dynar containing all mounted storages on the workstation
481 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
482 return surf_host_get_attached_storage_list(workstation);
486 * \brief Returns the host name the storage is attached to
488 * This functions checks whether a storage is a valid pointer or not and return its name.
490 const char *SD_storage_get_host(msg_storage_t storage) {
491 xbt_assert((storage != NULL), "Invalid parameters");
492 SD_storage_priv_t priv = SD_storage_priv(storage);
496 /* Returns whether a task can start now on a workstation*/
498 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
499 SD_CHECK_INIT_DONE();
500 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
502 return !__SD_workstation_is_busy(workstation) &&
503 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
507 /* Returns whether a workstation is busy. A workstation is busy is it is
508 * in sequential mode and a task is running on it or the fifo is not empty.
510 int __SD_workstation_is_busy(SD_workstation_t workstation)
513 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
514 SD_workstation_get_name(workstation),
515 (sg_host_sd(workstation)->access_mode ==
516 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
517 (sg_host_sd(workstation)->current_task ?
518 SD_task_get_name(sg_host_sd(workstation)->current_task)
520 (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
523 return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
524 (sg_host_sd(workstation)->current_task != NULL
525 || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
528 /* Destroys a workstation.
530 void __SD_workstation_destroy(void *workstation)
533 if (workstation==NULL)
535 SD_workstation_priv_t w;
537 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
539 w = (SD_workstation_priv_t) workstation;
541 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
542 xbt_fifo_free(w->task_fifo);
548 * \brief Returns the kind of the task currently running on a workstation
549 * Only call this with sequential access mode set
550 * \param workstation a workstation */
551 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
553 xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
554 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
555 " to use this function");
557 return (sg_host_sd(workstation)->current_task);
561 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
564 * \param AS, router name
565 * \return the xbt_dict_t properties of the AS
567 xbt_dict_t SD_as_router_get_properties(const char *asr)
569 return get_as_router_properties(asr);
572 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
575 * \param AS, router name
576 * \param The name of a properties
577 * \return value of the properties
579 const char* SD_as_router_get_property_value(const char *asr, const char *name)
581 xbt_dict_t dict = get_as_router_properties(asr);
582 if(!dict) return NULL;
583 return xbt_dict_get_or_null(dict,name);