1 /* Copyright (c) 2006-2016. 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. */
7 #include "src/simdag/simdag_private.h"
8 #include "simgrid/simdag.h"
9 #include "simgrid/host.h"
12 #include "xbt/sysdep.h"
13 #include "surf/surf.h"
14 #include "simgrid/msg.h" //FIXME: why?
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
17 "Logging specific to SimDag (workstation)");
19 /* Creates a workstation and registers it in SD.
21 SD_workstation_t __SD_workstation_create(const char *name)
24 SD_workstation_priv_t workstation;
26 workstation = xbt_new(s_SD_workstation_priv_t, 1);
27 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
28 workstation->task_fifo = NULL;
29 workstation->current_task = NULL;
31 sg_host_t sg_host = sg_host_by_name(name);
32 sg_host_sd_set(sg_host,workstation);
36 /* Creates a storage and registers it in SD.
38 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
41 SD_storage_priv_t storage;
44 storage = xbt_new(s_SD_storage_priv_t, 1);
45 storage->data = data; /* user data */
46 name = surf_resource_name((surf_cpp_resource_t)surf_storage);
47 storage->host = (const char*)surf_storage_get_host( (surf_resource_t )surf_storage_resource_by_name(name));
48 xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
49 return xbt_lib_get_elm_or_null(storage_lib, name);
52 /* Destroys a storage.
54 void __SD_storage_destroy(void *storage)
58 s = (SD_storage_priv_t) storage;
63 * \brief Returns a workstation given its name
65 * If there is no such workstation, the function returns \c NULL.
67 * \param name workstation name
68 * \return the workstation, or \c NULL if there is no such workstation
70 SD_workstation_t SD_workstation_get_by_name(const char *name)
72 return sg_host_by_name(name);
76 * \brief Returns the workstation list
78 * Use SD_workstation_get_number() to know the array size.
80 * \return an array of \ref SD_workstation_t containing all workstations
81 * \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).
82 * \see SD_workstation_get_number()
84 const SD_workstation_t *SD_workstation_get_list(void) {
85 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
87 if (sd_global->workstation_list == NULL) /* this is the first time the function is called */
88 sd_global->workstation_list = (SD_workstation_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
90 return sd_global->workstation_list;
94 * \brief Returns the number of workstations
96 * \return the number of existing workstations
97 * \see SD_workstation_get_list()
99 int SD_workstation_get_number(void)
101 return sg_host_count();
105 * \brief Returns the user data of a workstation
107 * \param workstation a workstation
108 * \return the user data associated with this workstation (can be \c NULL)
109 * \see SD_workstation_set_data()
111 void *SD_workstation_get_data(SD_workstation_t workstation)
113 return sg_host_user(workstation);
117 * \brief Sets the user data of a workstation
119 * The new data can be \c NULL. The old data should have been freed first
120 * if it was not \c NULL.
122 * \param workstation a workstation
123 * \param data the new data you want to associate with this workstation
124 * \see SD_workstation_get_data()
126 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
128 sg_host_user_set(workstation, data);
132 * \brief Returns the name of a workstation
134 * \param workstation a workstation
135 * \return the name of this workstation (cannot be \c NULL)
137 const char *SD_workstation_get_name(SD_workstation_t workstation)
139 return sg_host_get_name(workstation);
143 * \brief Returns the value of a given workstation property
145 * \param ws a workstation
146 * \param name a property name
147 * \return value of a property (or NULL if property not set)
149 const char *SD_workstation_get_property_value(SD_workstation_t ws,
152 return (const char*) xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
157 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
159 * \param workstation a workstation
160 * \return the dictionary containing the properties associated with the workstation
162 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
164 return sg_host_get_properties(workstation);
168 /** @brief Displays debugging informations about a workstation */
169 void SD_workstation_dump(SD_workstation_t ws)
172 xbt_dict_cursor_t cursor=NULL;
174 SD_task_t task = NULL;
176 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
177 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
178 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
179 switch (sg_host_sd(ws)->access_mode){
180 case SD_WORKSTATION_SHARED_ACCESS:
181 XBT_INFO(" - access mode: Space shared");
183 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
184 XBT_INFO(" - access mode: Exclusive");
185 task = SD_workstation_get_current_task(ws);
187 XBT_INFO(" current running task: %s",
188 SD_task_get_name(task));
190 XBT_INFO(" no task running");
194 props = SD_workstation_get_properties(ws);
196 if (!xbt_dict_is_empty(props)){
197 XBT_INFO(" - properties:");
199 xbt_dict_foreach(props,cursor,key,data) {
200 XBT_INFO(" %s->%s",key,data);
206 * \brief Returns the route between two workstations
208 * Use SD_route_get_size() to know the array size.
210 * \param src a workstation
211 * \param dst another workstation
212 * \return a new array of \ref SD_link_t representating the route between these two workstations
213 * \see SD_route_get_size(), SD_link_t
215 const SD_link_t *SD_route_get_list(SD_workstation_t src,
216 SD_workstation_t dst)
218 xbt_dynar_t surf_route;
222 if (sd_global->recyclable_route == NULL) {
224 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
227 surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model, src, dst);
229 xbt_dynar_foreach(surf_route, cpt, surf_link) {
230 sd_global->recyclable_route[cpt] = (SD_link_t)surf_link;
232 return sd_global->recyclable_route;
236 * \brief Returns the number of links on the route between two workstations
238 * \param src a workstation
239 * \param dst another workstation
240 * \return the number of links on the route between these two workstations
241 * \see SD_route_get_list()
243 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
245 return xbt_dynar_length(surf_host_model_get_route(
246 (surf_host_model_t)surf_host_model, src, dst));
250 * \brief Returns the total power of a workstation
252 * \param workstation a workstation
253 * \return the total power of this workstation
254 * \see SD_workstation_get_available_power()
256 double SD_workstation_get_power(SD_workstation_t workstation)
258 return sg_host_get_speed(workstation);
261 * \brief Returns the amount of cores of a workstation
263 * \param workstation a workstation
264 * \return the amount of cores of this workstation
266 int SD_workstation_get_cores(SD_workstation_t workstation) {
267 return sg_host_get_core(workstation);
271 * \brief Returns the proportion of available power in a workstation
273 * \param workstation a workstation
274 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
275 * \see SD_workstation_get_power()
277 double SD_workstation_get_available_power(SD_workstation_t workstation)
279 return surf_host_get_available_speed(workstation);
283 * \brief Returns an approximative estimated time for the given computation amount on a workstation
285 * \param workstation a workstation
286 * \param flops_amount the computation amount you want to evaluate (in flops)
287 * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
289 double SD_workstation_get_computation_time(SD_workstation_t workstation,
292 xbt_assert(flops_amount >= 0,
293 "flops_amount must be greater than or equal to zero");
294 return flops_amount / SD_workstation_get_power(workstation);
298 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
299 * between the workstations.
301 * \param src the first workstation
302 * \param dst the second workstation
303 * \return the latency of the route between the two workstations (in seconds)
304 * \see SD_route_get_current_bandwidth()
306 double SD_route_get_current_latency(SD_workstation_t src,
307 SD_workstation_t dst)
310 const SD_link_t *links;
315 links = SD_route_get_list(src, dst);
316 nb_links = SD_route_get_size(src, dst);
319 for (i = 0; i < nb_links; i++) {
320 latency += SD_link_get_current_latency(links[i]);
327 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
328 * between the workstations.
330 * \param src the first workstation
331 * \param dst the second workstation
332 * \return the bandwidth of the route between the two workstations (in bytes/second)
333 * \see SD_route_get_current_latency()
335 double SD_route_get_current_bandwidth(SD_workstation_t src,
336 SD_workstation_t dst)
339 const SD_link_t *links;
342 double min_bandwidth;
345 links = SD_route_get_list(src, dst);
346 nb_links = SD_route_get_size(src, dst);
347 min_bandwidth = -1.0;
349 for (i = 0; i < nb_links; i++) {
350 bandwidth = SD_link_get_current_bandwidth(links[i]);
351 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
352 min_bandwidth = bandwidth;
355 return min_bandwidth;
359 * \brief Returns an approximative estimated time for the given
360 * communication amount between two workstations
362 * \param src the first workstation
363 * \param dst the second workstation
364 * \param bytes_amount the communication amount you want to evaluate (in bytes)
365 * \return an approximative estimated communication time for the given bytes amount
366 * between the workstations (in seconds)
368 double SD_route_get_communication_time(SD_workstation_t src,
369 SD_workstation_t dst,
374 /* total time = latency + transmission time of the slowest link
375 transmission time of a link = communication amount / link bandwidth */
377 const SD_link_t *links;
379 double bandwidth, min_bandwidth;
383 xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
386 if (bytes_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 + (bytes_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 sg_host_sd(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 == sg_host_sd(workstation)->access_mode) {
443 return; // nothing is changed
446 sg_host_sd(workstation)->access_mode = access_mode;
448 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
449 xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
450 sg_host_sd(workstation)->task_fifo = NULL;
452 sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
457 * \brief Return the list of mounted storages on a workstation.
459 * \param workstation a workstation
460 * \return a dynar containing all mounted storages on the workstation
462 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
463 return surf_host_get_mounted_storage_list(workstation);
467 * \brief Return the list of mounted storages on a workstation.
469 * \param workstation a workstation
470 * \return a dynar containing all mounted storages on the workstation
472 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
473 return surf_host_get_attached_storage_list(workstation);
477 * \brief Returns the host name the storage is attached to
479 * This functions checks whether a storage is a valid pointer or not and return its name.
481 const char *SD_storage_get_host(msg_storage_t storage) {
482 xbt_assert((storage != NULL), "Invalid parameters");
483 SD_storage_priv_t priv = SD_storage_priv(storage);
487 /* Returns whether a task can start now on a workstation*/
489 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
490 SD_CHECK_INIT_DONE();
491 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
493 return !__SD_workstation_is_busy(workstation) &&
494 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
498 /* Returns whether a workstation is busy. A workstation is busy is it is
499 * in sequential mode and a task is running on it or the fifo is not empty.
501 int __SD_workstation_is_busy(SD_workstation_t workstation)
504 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
505 SD_workstation_get_name(workstation),
506 (sg_host_sd(workstation)->access_mode ==
507 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
508 (sg_host_sd(workstation)->current_task ?
509 SD_task_get_name(sg_host_sd(workstation)->current_task)
511 (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
514 return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
515 (sg_host_sd(workstation)->current_task != NULL
516 || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
519 /* Destroys a workstation.
521 void __SD_workstation_destroy(void *workstation)
524 if (workstation==NULL)
526 SD_workstation_priv_t w;
528 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
530 w = (SD_workstation_priv_t) workstation;
532 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
533 xbt_fifo_free(w->task_fifo);
539 * \brief Returns the kind of the task currently running on a workstation
540 * Only call this with sequential access mode set
541 * \param workstation a workstation */
542 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
544 xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
545 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
546 " to use this function");
548 return (sg_host_sd(workstation)->current_task);
552 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
555 * \param AS, router name
556 * \return the xbt_dict_t properties of the AS
558 xbt_dict_t SD_as_router_get_properties(const char *asr)
560 return get_as_router_properties(asr);
563 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
566 * \param AS, router name
567 * \param The name of a properties
568 * \return value of the properties
570 const char* SD_as_router_get_property_value(const char *asr, const char *name)
572 xbt_dict_t dict = get_as_router_properties(asr);
573 if(!dict) return NULL;
574 return (const char*)xbt_dict_get_or_null(dict,name);