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(const char *name)
23 SD_workstation_priv_t workstation;
25 workstation = xbt_new(s_SD_workstation_priv_t, 1);
26 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
27 workstation->task_fifo = NULL;
28 workstation->current_task = NULL;
30 sg_host_t sg_host = sg_host_by_name(name);
31 sg_host_sd_set(sg_host,workstation);
35 /* Creates a storage and registers it in SD.
37 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
40 SD_storage_priv_t storage;
43 storage = xbt_new(s_SD_storage_priv_t, 1);
44 storage->data = data; /* user data */
45 name = surf_resource_name(surf_storage);
46 storage->host = surf_storage_get_host(surf_storage_resource_by_name(name));
47 xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
48 return xbt_lib_get_elm_or_null(storage_lib, name);
51 /* Destroys a storage.
53 void __SD_storage_destroy(void *storage)
57 s = (SD_storage_priv_t) storage;
62 * \brief Returns a workstation given its name
64 * If there is no such workstation, the function returns \c NULL.
66 * \param name workstation name
67 * \return the workstation, or \c NULL if there is no such workstation
69 SD_workstation_t SD_workstation_get_by_name(const char *name)
71 return sg_host_by_name(name);
75 * \brief Returns the workstation list
77 * Use SD_workstation_get_number() to know the array size.
79 * \return an array of \ref SD_workstation_t containing all workstations
80 * \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).
81 * \see SD_workstation_get_number()
83 const SD_workstation_t *SD_workstation_get_list(void) {
84 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
86 if (sd_global->workstation_list == NULL) /* this is the first time the function is called */
87 sd_global->workstation_list = xbt_dynar_to_array(sg_hosts_as_dynar());
89 return sd_global->workstation_list;
93 * \brief Returns the number of workstations
95 * \return the number of existing workstations
96 * \see SD_workstation_get_list()
98 int SD_workstation_get_number(void)
100 return sg_host_count();
104 * \brief Returns the user data of a workstation
106 * \param workstation a workstation
107 * \return the user data associated with this workstation (can be \c NULL)
108 * \see SD_workstation_set_data()
110 void *SD_workstation_get_data(SD_workstation_t workstation)
112 return sg_host_user(workstation);
116 * \brief Sets the user data of a workstation
118 * The new data can be \c NULL. The old data should have been freed first
119 * if it was not \c NULL.
121 * \param workstation a workstation
122 * \param data the new data you want to associate with this workstation
123 * \see SD_workstation_get_data()
125 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
127 sg_host_user_set(workstation, data);
131 * \brief Returns the name of a workstation
133 * \param workstation a workstation
134 * \return the name of this workstation (cannot be \c NULL)
136 const char *SD_workstation_get_name(SD_workstation_t workstation)
138 return sg_host_get_name(workstation);
142 * \brief Returns the value of a given workstation property
144 * \param ws a workstation
145 * \param name a property name
146 * \return value of a property (or NULL if property not set)
148 const char *SD_workstation_get_property_value(SD_workstation_t ws,
151 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
156 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
158 * \param workstation a workstation
159 * \return the dictionary containing the properties associated with the workstation
161 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
163 return sg_host_get_properties(workstation);
167 /** @brief Displays debugging informations about a workstation */
168 void SD_workstation_dump(SD_workstation_t ws)
171 xbt_dict_cursor_t cursor=NULL;
173 SD_task_t task = NULL;
175 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
176 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
177 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
178 switch (sg_host_sd(ws)->access_mode){
179 case SD_WORKSTATION_SHARED_ACCESS:
180 XBT_INFO(" - access mode: Space shared");
182 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
183 XBT_INFO(" - access mode: Exclusive");
184 task = SD_workstation_get_current_task(ws);
186 XBT_INFO(" current running task: %s",
187 SD_task_get_name(task));
189 XBT_INFO(" no task running");
193 props = SD_workstation_get_properties(ws);
195 if (!xbt_dict_is_empty(props)){
196 XBT_INFO(" - properties:");
198 xbt_dict_foreach(props,cursor,key,data) {
199 XBT_INFO(" %s->%s",key,data);
205 * \brief Returns the route between two workstations
207 * Use SD_route_get_size() to know the array size.
209 * \param src a workstation
210 * \param dst another workstation
211 * \return a new array of \ref SD_link_t representating the route between these two workstations
212 * \see SD_route_get_size(), SD_link_t
214 const SD_link_t *SD_route_get_list(SD_workstation_t src,
215 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());
231 surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model,
234 xbt_dynar_foreach(surf_route, cpt, surf_link) {
235 sd_global->recyclable_route[cpt] = surf_link;
237 return sd_global->recyclable_route;
241 * \brief Returns the number of links on the route between two workstations
243 * \param src a workstation
244 * \param dst another workstation
245 * \return the number of links on the route between these two workstations
246 * \see SD_route_get_list()
248 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
250 return xbt_dynar_length(surf_host_model_get_route(
251 (surf_host_model_t)surf_host_model, src, dst));
255 * \brief Returns the total power of a workstation
257 * \param workstation a workstation
258 * \return the total power of this workstation
259 * \see SD_workstation_get_available_power()
261 double SD_workstation_get_power(SD_workstation_t workstation)
263 return surf_host_get_speed(workstation, 1.0);
266 * \brief Returns the amount of cores of a workstation
268 * \param workstation a workstation
269 * \return the amount of cores of this workstation
271 int SD_workstation_get_cores(SD_workstation_t workstation) {
272 return surf_host_get_core(workstation);
276 * \brief Returns the proportion of available power in a workstation
278 * \param workstation a workstation
279 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
280 * \see SD_workstation_get_power()
282 double SD_workstation_get_available_power(SD_workstation_t workstation)
284 return surf_host_get_available_speed(workstation);
288 * \brief Returns an approximative estimated time for the given computation amount on a workstation
290 * \param workstation a workstation
291 * \param flops_amount the computation amount you want to evaluate (in flops)
292 * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
294 double SD_workstation_get_computation_time(SD_workstation_t workstation,
297 xbt_assert(flops_amount >= 0,
298 "flops_amount must be greater than or equal to zero");
299 return flops_amount / SD_workstation_get_power(workstation);
303 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
304 * between the workstations.
306 * \param src the first workstation
307 * \param dst the second workstation
308 * \return the latency of the route between the two workstations (in seconds)
309 * \see SD_route_get_current_bandwidth()
311 double SD_route_get_current_latency(SD_workstation_t src,
312 SD_workstation_t dst)
315 const SD_link_t *links;
320 links = SD_route_get_list(src, dst);
321 nb_links = SD_route_get_size(src, dst);
324 for (i = 0; i < nb_links; i++) {
325 latency += SD_link_get_current_latency(links[i]);
332 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
333 * between the workstations.
335 * \param src the first workstation
336 * \param dst the second workstation
337 * \return the bandwidth of the route between the two workstations (in bytes/second)
338 * \see SD_route_get_current_latency()
340 double SD_route_get_current_bandwidth(SD_workstation_t src,
341 SD_workstation_t dst)
344 const SD_link_t *links;
347 double min_bandwidth;
350 links = SD_route_get_list(src, dst);
351 nb_links = SD_route_get_size(src, dst);
352 min_bandwidth = -1.0;
354 for (i = 0; i < nb_links; i++) {
355 bandwidth = SD_link_get_current_bandwidth(links[i]);
356 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
357 min_bandwidth = bandwidth;
360 return min_bandwidth;
364 * \brief Returns an approximative estimated time for the given
365 * communication amount between two workstations
367 * \param src the first workstation
368 * \param dst the second workstation
369 * \param bytes_amount the communication amount you want to evaluate (in bytes)
370 * \return an approximative estimated communication time for the given bytes amount
371 * between the workstations (in seconds)
373 double SD_route_get_communication_time(SD_workstation_t src,
374 SD_workstation_t dst,
379 /* total time = latency + transmission time of the slowest link
380 transmission time of a link = communication amount / link bandwidth */
382 const SD_link_t *links;
384 double bandwidth, min_bandwidth;
388 xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
391 if (bytes_amount == 0.0)
394 links = SD_route_get_list(src, dst);
395 nb_links = SD_route_get_size(src, dst);
396 min_bandwidth = -1.0;
399 for (i = 0; i < nb_links; i++) {
400 latency += SD_link_get_current_latency(links[i]);
401 bandwidth = SD_link_get_current_bandwidth(links[i]);
402 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
403 min_bandwidth = bandwidth;
406 return latency + (bytes_amount / min_bandwidth);
410 * \brief Returns the access mode of this workstation.
412 * \param workstation a workstation
413 * \return the access mode for the tasks running on this workstation:
414 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
416 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
418 e_SD_workstation_access_mode_t
419 SD_workstation_get_access_mode(SD_workstation_t workstation)
421 return sg_host_sd(workstation)->access_mode;
425 * \brief Sets the access mode for the tasks that will be executed on a workstation
427 * By default, a workstation model is shared, i.e. several tasks
428 * can be executed at the same time on a workstation. The CPU power of
429 * the workstation is shared between the running tasks on the workstation.
430 * In sequential mode, only one task can use the workstation, and the other
431 * tasks wait in a FIFO.
433 * \param workstation a workstation
434 * \param access_mode the access mode you want to set to this workstation:
435 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
437 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
439 void SD_workstation_set_access_mode(SD_workstation_t workstation,
440 e_SD_workstation_access_mode_t
443 xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
444 access_mode != SD_WORKSTATION_SHARED_ACCESS,
445 "Trying to set an invalid access mode");
447 if (access_mode == sg_host_sd(workstation)->access_mode) {
448 return; // nothing is changed
451 sg_host_sd(workstation)->access_mode = access_mode;
453 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
454 xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
455 sg_host_sd(workstation)->task_fifo = NULL;
457 sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
462 * \brief Return the list of mounted storages on a workstation.
464 * \param workstation a workstation
465 * \return a dynar containing all mounted storages on the workstation
467 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
468 return surf_host_get_mounted_storage_list(workstation);
472 * \brief Return the list of mounted storages on a workstation.
474 * \param workstation a workstation
475 * \return a dynar containing all mounted storages on the workstation
477 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
478 return surf_host_get_attached_storage_list(workstation);
482 * \brief Returns the host name the storage is attached to
484 * This functions checks whether a storage is a valid pointer or not and return its name.
486 const char *SD_storage_get_host(msg_storage_t storage) {
487 xbt_assert((storage != NULL), "Invalid parameters");
488 SD_storage_priv_t priv = SD_storage_priv(storage);
492 /* Returns whether a task can start now on a workstation*/
494 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
495 SD_CHECK_INIT_DONE();
496 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
498 return !__SD_workstation_is_busy(workstation) &&
499 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
503 /* Returns whether a workstation is busy. A workstation is busy is it is
504 * in sequential mode and a task is running on it or the fifo is not empty.
506 int __SD_workstation_is_busy(SD_workstation_t workstation)
509 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
510 SD_workstation_get_name(workstation),
511 (sg_host_sd(workstation)->access_mode ==
512 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
513 (sg_host_sd(workstation)->current_task ?
514 SD_task_get_name(sg_host_sd(workstation)->current_task)
516 (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
519 return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
520 (sg_host_sd(workstation)->current_task != NULL
521 || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
524 /* Destroys a workstation.
526 void __SD_workstation_destroy(void *workstation)
529 if (workstation==NULL)
531 SD_workstation_priv_t w;
533 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
535 w = (SD_workstation_priv_t) workstation;
537 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
538 xbt_fifo_free(w->task_fifo);
544 * \brief Returns the kind of the task currently running on a workstation
545 * Only call this with sequential access mode set
546 * \param workstation a workstation */
547 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
549 xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
550 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
551 " to use this function");
553 return (sg_host_sd(workstation)->current_task);
557 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
560 * \param AS, router name
561 * \return the xbt_dict_t properties of the AS
563 xbt_dict_t SD_as_router_get_properties(const char *asr)
565 return get_as_router_properties(asr);
568 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
571 * \param AS, router name
572 * \param The name of a properties
573 * \return value of the properties
575 const char* SD_as_router_get_property_value(const char *asr, const char *name)
577 xbt_dict_t dict = get_as_router_properties(asr);
578 if(!dict) return NULL;
579 return xbt_dict_get_or_null(dict,name);