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/surf/host_interface.hpp"
8 #include "src/simdag/simdag_private.h"
9 #include "simgrid/simdag.h"
10 #include <simgrid/s4u/host.hpp>
13 #include "xbt/sysdep.h"
14 #include "surf/surf.h"
15 #include "simgrid/msg.h" //FIXME: why?
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(const char *name)
25 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 sg_host_t sg_host = sg_host_by_name(name);
33 sg_host_sd_set(sg_host,workstation);
37 /* Creates a storage and registers it in SD.
39 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
42 SD_storage_priv_t storage;
45 storage = xbt_new(s_SD_storage_priv_t, 1);
46 storage->data = data; /* user data */
47 name = surf_resource_name((surf_cpp_resource_t)surf_storage);
48 storage->host = (const char*)surf_storage_get_host( (surf_resource_t )surf_storage_resource_by_name(name));
49 xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
50 return xbt_lib_get_elm_or_null(storage_lib, name);
53 /* Destroys a storage.
55 void __SD_storage_destroy(void *storage)
59 s = (SD_storage_priv_t) storage;
64 * \brief Returns a workstation given its name
66 * If there is no such workstation, the function returns \c NULL.
68 * \param name workstation name
69 * \return the workstation, or \c NULL if there is no such workstation
71 SD_workstation_t SD_workstation_get_by_name(const char *name)
73 return sg_host_by_name(name);
77 * \brief Returns the workstation list
79 * Use SD_workstation_get_number() to know the array size.
81 * \return an array of \ref SD_workstation_t containing all workstations
82 * \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).
83 * \see SD_workstation_get_number()
85 const SD_workstation_t *SD_workstation_get_list(void) {
86 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
88 if (sd_global->workstation_list == NULL) /* this is the first time the function is called */
89 sd_global->workstation_list = (SD_workstation_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
91 return sd_global->workstation_list;
95 * \brief Returns the number of workstations
97 * \return the number of existing workstations
98 * \see SD_workstation_get_list()
100 int SD_workstation_get_number(void)
102 return sg_host_count();
106 * \brief Returns the user data of a workstation
108 * \param workstation a workstation
109 * \return the user data associated with this workstation (can be \c NULL)
110 * \see SD_workstation_set_data()
112 void *SD_workstation_get_data(SD_workstation_t workstation)
114 return sg_host_user(workstation);
118 * \brief Sets the user data of a workstation
120 * The new data can be \c NULL. The old data should have been freed first
121 * if it was not \c NULL.
123 * \param workstation a workstation
124 * \param data the new data you want to associate with this workstation
125 * \see SD_workstation_get_data()
127 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
129 sg_host_user_set(workstation, data);
133 * \brief Returns the name of a workstation
135 * \param workstation a workstation
136 * \return the name of this workstation (cannot be \c NULL)
138 const char *SD_workstation_get_name(SD_workstation_t workstation)
140 return sg_host_get_name(workstation);
144 * \brief Returns the value of a given workstation property
146 * \param ws a workstation
147 * \param name a property name
148 * \return value of a property (or NULL if property not set)
150 const char *SD_workstation_get_property_value(SD_workstation_t ws,
153 return (const char*) xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
158 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
160 * \param workstation a workstation
161 * \return the dictionary containing the properties associated with the workstation
163 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
165 return sg_host_get_properties(workstation);
169 /** @brief Displays debugging informations about a workstation */
170 void SD_workstation_dump(SD_workstation_t ws)
173 xbt_dict_cursor_t cursor=NULL;
175 SD_task_t task = NULL;
177 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
178 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
179 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
180 switch (sg_host_sd(ws)->access_mode){
181 case SD_WORKSTATION_SHARED_ACCESS:
182 XBT_INFO(" - access mode: Space shared");
184 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
185 XBT_INFO(" - access mode: Exclusive");
186 task = SD_workstation_get_current_task(ws);
188 XBT_INFO(" current running task: %s",
189 SD_task_get_name(task));
191 XBT_INFO(" no task running");
195 props = SD_workstation_get_properties(ws);
197 if (!xbt_dict_is_empty(props)){
198 XBT_INFO(" - properties:");
200 xbt_dict_foreach(props,cursor,key,data) {
201 XBT_INFO(" %s->%s",key,data);
207 * \brief Returns the route between two workstations
209 * Use SD_route_get_size() to know the array size.
211 * \param src a workstation
212 * \param dst another workstation
213 * \return a new array of \ref SD_link_t representating the route between these two workstations
214 * \see SD_route_get_size(), SD_link_t
216 const SD_link_t *SD_route_get_list(SD_workstation_t src,
217 SD_workstation_t dst)
219 xbt_dynar_t surf_route;
223 if (sd_global->recyclable_route == NULL) {
225 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
228 surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model, src, dst);
230 xbt_dynar_foreach(surf_route, cpt, surf_link) {
231 sd_global->recyclable_route[cpt] = (SD_link_t)surf_link;
233 return sd_global->recyclable_route;
237 * \brief Returns the number of links on the route between two workstations
239 * \param src a workstation
240 * \param dst another workstation
241 * \return the number of links on the route between these two workstations
242 * \see SD_route_get_list()
244 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
246 return xbt_dynar_length(surf_host_model_get_route(
247 (surf_host_model_t)surf_host_model, src, dst));
251 * \brief Returns the total power of a workstation
253 * \param workstation a workstation
254 * \return the total power of this workstation
255 * \see SD_workstation_get_available_power()
257 double SD_workstation_get_power(SD_workstation_t workstation)
259 return workstation->speed();
262 * \brief Returns the amount of cores of a workstation
264 * \param workstation a workstation
265 * \return the amount of cores of this workstation
267 int SD_workstation_get_cores(SD_workstation_t workstation) {
268 return workstation->core_count();
272 * \brief Returns the proportion of available power in a workstation
274 * \param workstation a workstation
275 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
276 * \see SD_workstation_get_power()
278 double SD_workstation_get_available_power(SD_workstation_t workstation)
280 return surf_host_get_available_speed(workstation);
284 * \brief Returns an approximative estimated time for the given computation amount on a workstation
286 * \param workstation a workstation
287 * \param flops_amount the computation amount you want to evaluate (in flops)
288 * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
290 double SD_workstation_get_computation_time(SD_workstation_t workstation,
293 xbt_assert(flops_amount >= 0,
294 "flops_amount must be greater than or equal to zero");
295 return flops_amount / SD_workstation_get_power(workstation);
299 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
300 * between the workstations.
302 * \param src the first workstation
303 * \param dst the second workstation
304 * \return the latency of the route between the two workstations (in seconds)
305 * \see SD_route_get_current_bandwidth()
307 double SD_route_get_current_latency(SD_workstation_t src,
308 SD_workstation_t dst)
311 const SD_link_t *links;
316 links = SD_route_get_list(src, dst);
317 nb_links = SD_route_get_size(src, dst);
320 for (i = 0; i < nb_links; i++) {
321 latency += SD_link_get_current_latency(links[i]);
328 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
329 * between the workstations.
331 * \param src the first workstation
332 * \param dst the second workstation
333 * \return the bandwidth of the route between the two workstations (in bytes/second)
334 * \see SD_route_get_current_latency()
336 double SD_route_get_current_bandwidth(SD_workstation_t src,
337 SD_workstation_t dst)
340 const SD_link_t *links;
343 double min_bandwidth;
346 links = SD_route_get_list(src, dst);
347 nb_links = SD_route_get_size(src, dst);
348 min_bandwidth = -1.0;
350 for (i = 0; i < nb_links; i++) {
351 bandwidth = SD_link_get_current_bandwidth(links[i]);
352 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
353 min_bandwidth = bandwidth;
356 return min_bandwidth;
360 * \brief Returns an approximative estimated time for the given
361 * communication amount between two workstations
363 * \param src the first workstation
364 * \param dst the second workstation
365 * \param bytes_amount the communication amount you want to evaluate (in bytes)
366 * \return an approximative estimated communication time for the given bytes amount
367 * between the workstations (in seconds)
369 double SD_route_get_communication_time(SD_workstation_t src,
370 SD_workstation_t dst,
375 /* total time = latency + transmission time of the slowest link
376 transmission time of a link = communication amount / link bandwidth */
378 const SD_link_t *links;
380 double bandwidth, min_bandwidth;
384 xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
387 if (bytes_amount == 0.0)
390 links = SD_route_get_list(src, dst);
391 nb_links = SD_route_get_size(src, dst);
392 min_bandwidth = -1.0;
395 for (i = 0; i < nb_links; i++) {
396 latency += SD_link_get_current_latency(links[i]);
397 bandwidth = SD_link_get_current_bandwidth(links[i]);
398 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
399 min_bandwidth = bandwidth;
402 return latency + (bytes_amount / min_bandwidth);
406 * \brief Returns the access mode of this workstation.
408 * \param workstation a workstation
409 * \return the access mode for the tasks running on this workstation:
410 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
412 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
414 e_SD_workstation_access_mode_t
415 SD_workstation_get_access_mode(SD_workstation_t workstation)
417 return sg_host_sd(workstation)->access_mode;
421 * \brief Sets the access mode for the tasks that will be executed on a workstation
423 * By default, a workstation model is shared, i.e. several tasks
424 * can be executed at the same time on a workstation. The CPU power of
425 * the workstation is shared between the running tasks on the workstation.
426 * In sequential mode, only one task can use the workstation, and the other
427 * tasks wait in a FIFO.
429 * \param workstation a workstation
430 * \param access_mode the access mode you want to set to this workstation:
431 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
433 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
435 void SD_workstation_set_access_mode(SD_workstation_t workstation,
436 e_SD_workstation_access_mode_t
439 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
440 access_mode != SD_WORKSTATION_SHARED_ACCESS,
441 "Trying to set an invalid access mode");
443 if (access_mode == sg_host_sd(workstation)->access_mode) {
444 return; // nothing is changed
447 sg_host_sd(workstation)->access_mode = access_mode;
449 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
450 xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
451 sg_host_sd(workstation)->task_fifo = NULL;
453 sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
458 * \brief Return the list of mounted storages on a workstation.
460 * \param workstation a workstation
461 * \return a dynar containing all mounted storages on the workstation
463 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
464 return workstation->extension<simgrid::surf::Host>()->getMountedStorageList();
468 * \brief Return the list of mounted storages on a workstation.
470 * \param workstation a workstation
471 * \return a dynar containing all mounted storages on the workstation
473 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
474 return surf_host_get_attached_storage_list(workstation);
478 * \brief Returns the host name the storage is attached to
480 * This functions checks whether a storage is a valid pointer or not and return its name.
482 const char *SD_storage_get_host(msg_storage_t storage) {
483 xbt_assert((storage != NULL), "Invalid parameters");
484 SD_storage_priv_t priv = SD_storage_priv(storage);
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 (sg_host_sd(workstation)->access_mode ==
508 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
509 (sg_host_sd(workstation)->current_task ?
510 SD_task_get_name(sg_host_sd(workstation)->current_task)
512 (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
515 return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
516 (sg_host_sd(workstation)->current_task != NULL
517 || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
520 /* Destroys a workstation.
522 void __SD_workstation_destroy(void *workstation)
525 if (workstation==NULL)
527 SD_workstation_priv_t w;
529 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
531 w = (SD_workstation_priv_t) workstation;
533 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
534 xbt_fifo_free(w->task_fifo);
540 * \brief Returns the kind of the task currently running on a workstation
541 * Only call this with sequential access mode set
542 * \param workstation a workstation */
543 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
545 xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
546 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
547 " to use this function");
549 return (sg_host_sd(workstation)->current_task);