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"
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_count() > 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_count(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(" - speed: %.0f", SD_workstation_get_speed(ws));
178 XBT_INFO(" - available speed: %.2f", SD_workstation_get_available_speed(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 representing 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, sg_link_count());
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 speed of a workstation
252 * \param workstation a workstation
253 * \return the total speed of this workstation
254 * \see SD_workstation_get_available_speed()
256 double SD_workstation_get_speed(SD_workstation_t workstation)
258 return workstation->speed();
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 workstation->core_count();
271 * \brief Returns the proportion of available speed in a workstation
273 * \param workstation a workstation
274 * \return the proportion of speed currently available in this workstation (normally a number between 0 and 1)
275 * \see SD_workstation_get_speed()
277 double SD_workstation_get_available_speed(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_speed(workstation);
298 * \brief Returns the latency of the route between two workstations.
300 * \param src the first workstation
301 * \param dst the second workstation
302 * \return the latency of the route between the two workstations (in seconds)
303 * \see SD_route_get_bandwidth()
305 double SD_route_get_latency(SD_workstation_t src, SD_workstation_t dst)
307 xbt_dynar_t route = NULL;
310 routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
317 * \brief Returns the bandwidth of the route between two workstations,
318 * i.e. the minimum link bandwidth of all between the workstations.
320 * \param src the first workstation
321 * \param dst the second workstation
322 * \return the bandwidth of the route between the two workstations
324 * \see SD_route_get_latency()
326 double SD_route_get_bandwidth(SD_workstation_t src, SD_workstation_t dst)
329 const SD_link_t *links;
332 double min_bandwidth;
335 links = SD_route_get_list(src, dst);
336 nb_links = SD_route_get_size(src, dst);
337 min_bandwidth = -1.0;
339 for (i = 0; i < nb_links; i++) {
340 bandwidth = sg_link_bandwidth(links[i]);
341 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
342 min_bandwidth = bandwidth;
345 return min_bandwidth;
349 * \brief Returns an approximative estimated time for the given
350 * communication amount between two workstations
352 * \param src the first workstation
353 * \param dst the second workstation
354 * \param bytes_amount the communication amount you want to evaluate (in bytes)
355 * \return an approximative estimated communication time for the given bytes amount
356 * between the workstations (in seconds)
358 double SD_route_get_communication_time(SD_workstation_t src,
359 SD_workstation_t dst,
364 /* total time = latency + transmission time of the slowest link
365 transmission time of a link = communication amount / link bandwidth */
367 const SD_link_t *links;
368 xbt_dynar_t route = NULL;
370 double bandwidth, min_bandwidth;
374 xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
377 if (bytes_amount == 0.0)
380 routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
383 links = SD_route_get_list(src, dst);
384 nb_links = SD_route_get_size(src, dst);
385 min_bandwidth = -1.0;
387 for (i = 0; i < nb_links; i++) {
388 bandwidth = sg_link_bandwidth(links[i]);
389 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
390 min_bandwidth = bandwidth;
393 return latency + (bytes_amount / min_bandwidth);
397 * \brief Returns the access mode of this workstation.
399 * \param workstation a workstation
400 * \return the access mode for the tasks running on this workstation:
401 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
403 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
405 e_SD_workstation_access_mode_t
406 SD_workstation_get_access_mode(SD_workstation_t workstation)
408 return sg_host_sd(workstation)->access_mode;
412 * \brief Sets the access mode for the tasks that will be executed on a workstation
414 * By default, a workstation model is shared, i.e. several tasks
415 * can be executed at the same time on a workstation. The CPU power of
416 * the workstation is shared between the running tasks on the workstation.
417 * In sequential mode, only one task can use the workstation, and the other
418 * tasks wait in a FIFO.
420 * \param workstation a workstation
421 * \param access_mode the access mode you want to set to this workstation:
422 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
424 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
426 void SD_workstation_set_access_mode(SD_workstation_t workstation,
427 e_SD_workstation_access_mode_t
430 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
431 access_mode != SD_WORKSTATION_SHARED_ACCESS,
432 "Trying to set an invalid access mode");
434 if (access_mode == sg_host_sd(workstation)->access_mode) {
435 return; // nothing is changed
438 sg_host_sd(workstation)->access_mode = access_mode;
440 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
441 xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
442 sg_host_sd(workstation)->task_fifo = NULL;
444 sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
449 * \brief Return the list of mounted storages on a workstation.
451 * \param workstation a workstation
452 * \return a dynar containing all mounted storages on the workstation
454 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
455 return workstation->extension<simgrid::surf::Host>()->getMountedStorageList();
459 * \brief Return the list of mounted storages on a workstation.
461 * \param workstation a workstation
462 * \return a dynar containing all mounted storages on the workstation
464 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
465 return surf_host_get_attached_storage_list(workstation);
469 * \brief Returns the host name the storage is attached to
471 * This functions checks whether a storage is a valid pointer or not and return its name.
473 const char *SD_storage_get_host(msg_storage_t storage) {
474 xbt_assert((storage != NULL), "Invalid parameters");
475 SD_storage_priv_t priv = SD_storage_priv(storage);
479 /* Returns whether a task can start now on a workstation*/
481 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
482 SD_CHECK_INIT_DONE();
483 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
485 return !__SD_workstation_is_busy(workstation) &&
486 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
490 /* Returns whether a workstation is busy. A workstation is busy is it is
491 * in sequential mode and a task is running on it or the fifo is not empty.
493 int __SD_workstation_is_busy(SD_workstation_t workstation)
496 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
497 SD_workstation_get_name(workstation),
498 (sg_host_sd(workstation)->access_mode ==
499 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
500 (sg_host_sd(workstation)->current_task ?
501 SD_task_get_name(sg_host_sd(workstation)->current_task)
503 (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
506 return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
507 (sg_host_sd(workstation)->current_task != NULL
508 || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
511 /* Destroys a workstation.
513 void __SD_workstation_destroy(void *workstation)
516 if (workstation==NULL)
518 SD_workstation_priv_t w;
520 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
522 w = (SD_workstation_priv_t) workstation;
524 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
525 xbt_fifo_free(w->task_fifo);
531 * \brief Returns the kind of the task currently running on a workstation
532 * Only call this with sequential access mode set
533 * \param workstation a workstation */
534 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
536 xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
537 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
538 " to use this function");
540 return (sg_host_sd(workstation)->current_task);