Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: sg_hosts_as_dynar
[simgrid.git] / src / simdag / sd_workstation.c
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "simgrid/simdag.h"
9 #include "xbt/dict.h"
10 #include "xbt/lib.h"
11 #include "xbt/sysdep.h"
12 #include "surf/surf.h"
13 #include "simgrid/msg.h" //FIXME: why?
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
16                                 "Logging specific to SimDag (workstation)");
17
18 /* Creates a workstation and registers it in SD.
19  */
20 SD_workstation_t __SD_workstation_create(void *surf_workstation,
21                                          void *data)
22 {
23
24   SD_workstation_priv_t workstation;
25   const char *name;
26
27   workstation = xbt_new(s_SD_workstation_priv_t, 1);
28   workstation->data = data;     /* user data */
29   workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS;      /* default mode is shared */
30   workstation->task_fifo = NULL;
31   workstation->current_task = NULL;
32
33   name = surf_resource_name(surf_workstation);
34   xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
35   return xbt_lib_get_elm_or_null(host_lib,name);
36 }
37
38 /* Creates a storage and registers it in SD.
39  */
40 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
41 {
42
43   SD_storage_priv_t storage;
44   const char *name;
45
46   storage = xbt_new(s_SD_storage_priv_t, 1);
47   storage->data = data;     /* user data */
48   name = surf_resource_name(surf_storage);
49   storage->host = surf_storage_get_host(surf_storage_resource_by_name(name));
50   xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
51   return xbt_lib_get_elm_or_null(storage_lib, name);
52 }
53
54 /* Destroys a storage.
55  */
56 void __SD_storage_destroy(void *storage)
57 {
58   SD_storage_priv_t s;
59
60   s = (SD_storage_priv_t) storage;
61   xbt_free(s);
62 }
63
64 /**
65  * \brief Returns a workstation given its name
66  *
67  * If there is no such workstation, the function returns \c NULL.
68  *
69  * \param name workstation name
70  * \return the workstation, or \c NULL if there is no such workstation
71  */
72 SD_workstation_t SD_workstation_get_by_name(const char *name)
73 {
74   return xbt_lib_get_elm_or_null(host_lib, name);
75 }
76
77 /**
78  * \brief Returns the workstation list
79  *
80  * Use SD_workstation_get_number() to know the array size.
81  * 
82  * \return an array of \ref SD_workstation_t containing all workstations
83  * \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).
84  * \see SD_workstation_get_number()
85  */
86 const SD_workstation_t *SD_workstation_get_list(void) {
87   xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
88
89   if (sd_global->workstation_list == NULL)     /* this is the first time the function is called */
90     sd_global->workstation_list = xbt_dynar_to_array(sg_hosts_as_dynar());
91
92   return sd_global->workstation_list;
93 }
94
95 /**
96  * \brief Returns the number of workstations
97  *
98  * \return the number of existing workstations
99  * \see SD_workstation_get_list()
100  */
101 int SD_workstation_get_number(void)
102 {
103   return xbt_lib_length(host_lib);
104 }
105
106 /**
107  * \brief Returns the user data of a workstation
108  *
109  * \param workstation a workstation
110  * \return the user data associated with this workstation (can be \c NULL)
111  * \see SD_workstation_set_data()
112  */
113 void *SD_workstation_get_data(SD_workstation_t workstation)
114 {
115   return SD_workstation_priv(workstation)->data;
116 }
117
118 /**
119  * \brief Sets the user data of a workstation
120  *
121  * The new data can be \c NULL. The old data should have been freed first
122  * if it was not \c NULL.
123  *
124  * \param workstation a workstation
125  * \param data the new data you want to associate with this workstation
126  * \see SD_workstation_get_data()
127  */
128 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
129 {
130   SD_workstation_priv(workstation)->data = data;
131 }
132
133 /**
134  * \brief Returns the name of a workstation
135  *
136  * \param workstation a workstation
137  * \return the name of this workstation (cannot be \c NULL)
138  */
139 const char *SD_workstation_get_name(SD_workstation_t workstation)
140 {
141   return sg_host_name(workstation);
142 }
143
144 /**
145  * \brief Returns the value of a given workstation property
146  *
147  * \param ws a workstation
148  * \param name a property name
149  * \return value of a property (or NULL if property not set)
150  */
151 const char *SD_workstation_get_property_value(SD_workstation_t ws,
152                                               const char *name)
153 {
154   return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
155 }
156
157
158 /**
159  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
160  *
161  * \param workstation a workstation
162  * \return the dictionary containing the properties associated with the workstation
163  */
164 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
165 {
166   return surf_host_get_properties(surf_host_resource_priv(workstation));
167 }
168
169
170 /** @brief Displays debugging informations about a workstation */
171 void SD_workstation_dump(SD_workstation_t ws)
172 {
173   xbt_dict_t props;
174   xbt_dict_cursor_t cursor=NULL;
175   char *key,*data;
176   SD_task_t task = NULL;
177   
178   XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
179   XBT_INFO("  - power: %.0f", SD_workstation_get_power(ws));
180   XBT_INFO("  - available power: %.2f", SD_workstation_get_available_power(ws));
181   switch (SD_workstation_priv(ws)->access_mode){
182   case SD_WORKSTATION_SHARED_ACCESS:
183       XBT_INFO("  - access mode: Space shared");
184       break;
185   case SD_WORKSTATION_SEQUENTIAL_ACCESS:
186       XBT_INFO("  - access mode: Exclusive");
187     task = SD_workstation_get_current_task(ws);
188     if(task)
189       XBT_INFO("    current running task: %s",
190                SD_task_get_name(task));
191     else
192       XBT_INFO("    no task running");
193       break;
194   default: break;
195   }
196   props = SD_workstation_get_properties(ws);
197   
198   if (!xbt_dict_is_empty(props)){
199     XBT_INFO("  - properties:");
200
201     xbt_dict_foreach(props,cursor,key,data) {
202       XBT_INFO("    %s->%s",key,data);
203     }
204   }
205 }
206
207 /**
208  * \brief Returns the route between two workstations
209  *
210  * Use SD_route_get_size() to know the array size.
211  *
212  * \param src a workstation
213  * \param dst another workstation
214  * \return a new array of \ref SD_link_t representating the route between these two workstations
215  * \see SD_route_get_size(), SD_link_t
216  */
217 const SD_link_t *SD_route_get_list(SD_workstation_t src,
218                                    SD_workstation_t dst)
219 {
220   void *surf_src;
221   void *surf_dst;
222   xbt_dynar_t surf_route;
223   const char *link_name;
224   void *surf_link;
225   unsigned int cpt;
226
227   if (sd_global->recyclable_route == NULL) {
228     /* first run */
229     sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
230   }
231
232   surf_src = src;
233   surf_dst = dst;
234
235   surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model,
236                                                         surf_src, surf_dst);
237
238   xbt_dynar_foreach(surf_route, cpt, surf_link) {
239     link_name = surf_resource_name(surf_link);
240     sd_global->recyclable_route[cpt] =
241         xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
242   }
243   return sd_global->recyclable_route;
244 }
245
246 /**
247  * \brief Returns the number of links on the route between two workstations
248  *
249  * \param src a workstation
250  * \param dst another workstation
251  * \return the number of links on the route between these two workstations
252  * \see SD_route_get_list()
253  */
254 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
255 {
256   return xbt_dynar_length(surf_host_model_get_route(
257                     (surf_host_model_t)surf_host_model, src, dst));
258 }
259
260 /**
261  * \brief Returns the total power of a workstation
262  *
263  * \param workstation a workstation
264  * \return the total power of this workstation
265  * \see SD_workstation_get_available_power()
266  */
267 double SD_workstation_get_power(SD_workstation_t workstation)
268 {
269   return surf_host_get_speed(workstation, 1.0);
270 }
271 /**
272  * \brief Returns the amount of cores of a workstation
273  *
274  * \param workstation a workstation
275  * \return the amount of cores of this workstation
276  */
277 int SD_workstation_get_cores(SD_workstation_t workstation) {
278   return surf_host_get_core(workstation);
279 }
280
281 /**
282  * \brief Returns the proportion of available power in a workstation
283  *
284  * \param workstation a workstation
285  * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
286  * \see SD_workstation_get_power()
287  */
288 double SD_workstation_get_available_power(SD_workstation_t workstation)
289 {
290   return surf_host_get_available_speed(workstation);
291 }
292
293 /**
294  * \brief Returns an approximative estimated time for the given computation amount on a workstation
295  *
296  * \param workstation a workstation
297  * \param flops_amount the computation amount you want to evaluate (in flops)
298  * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
299  */
300 double SD_workstation_get_computation_time(SD_workstation_t workstation,
301                                            double flops_amount)
302 {
303   xbt_assert(flops_amount >= 0,
304               "flops_amount must be greater than or equal to zero");
305   return flops_amount / SD_workstation_get_power(workstation);
306 }
307
308 /**
309  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
310  * between the workstations.
311  *
312  * \param src the first workstation
313  * \param dst the second workstation
314  * \return the latency of the route between the two workstations (in seconds)
315  * \see SD_route_get_current_bandwidth()
316  */
317 double SD_route_get_current_latency(SD_workstation_t src,
318                                     SD_workstation_t dst)
319 {
320
321   const SD_link_t *links;
322   int nb_links;
323   double latency;
324   int i;
325
326   links = SD_route_get_list(src, dst);
327   nb_links = SD_route_get_size(src, dst);
328   latency = 0.0;
329
330   for (i = 0; i < nb_links; i++) {
331     latency += SD_link_get_current_latency(links[i]);
332   }
333
334   return latency;
335 }
336
337 /**
338  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
339  * between the workstations.
340  *
341  * \param src the first workstation
342  * \param dst the second workstation
343  * \return the bandwidth of the route between the two workstations (in bytes/second)
344  * \see SD_route_get_current_latency()
345  */
346 double SD_route_get_current_bandwidth(SD_workstation_t src,
347                                       SD_workstation_t dst)
348 {
349
350   const SD_link_t *links;
351   int nb_links;
352   double bandwidth;
353   double min_bandwidth;
354   int i;
355
356   links = SD_route_get_list(src, dst);
357   nb_links = SD_route_get_size(src, dst);
358   min_bandwidth = -1.0;
359
360   for (i = 0; i < nb_links; i++) {
361     bandwidth = SD_link_get_current_bandwidth(links[i]);
362     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
363       min_bandwidth = bandwidth;
364   }
365
366   return min_bandwidth;
367 }
368
369 /**
370  * \brief Returns an approximative estimated time for the given
371  * communication amount between two workstations
372  *
373  * \param src the first workstation
374  * \param dst the second workstation
375  * \param bytes_amount the communication amount you want to evaluate (in bytes)
376  * \return an approximative estimated communication time for the given bytes amount
377  * between the workstations (in seconds)
378  */
379 double SD_route_get_communication_time(SD_workstation_t src,
380                                        SD_workstation_t dst,
381                                        double bytes_amount)
382 {
383
384
385   /* total time = latency + transmission time of the slowest link
386      transmission time of a link = communication amount / link bandwidth */
387
388   const SD_link_t *links;
389   int nb_links;
390   double bandwidth, min_bandwidth;
391   double latency;
392   int i;
393
394   xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
395
396
397   if (bytes_amount == 0.0)
398     return 0.0;
399
400   links = SD_route_get_list(src, dst);
401   nb_links = SD_route_get_size(src, dst);
402   min_bandwidth = -1.0;
403   latency = 0;
404
405   for (i = 0; i < nb_links; i++) {
406     latency += SD_link_get_current_latency(links[i]);
407     bandwidth = SD_link_get_current_bandwidth(links[i]);
408     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
409       min_bandwidth = bandwidth;
410   }
411
412   return latency + (bytes_amount / min_bandwidth);
413 }
414
415 /**
416  * \brief Returns the access mode of this workstation.
417  *
418  * \param workstation a workstation
419  * \return the access mode for the tasks running on this workstation:
420  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
421  *
422  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
423  */
424 e_SD_workstation_access_mode_t
425 SD_workstation_get_access_mode(SD_workstation_t workstation)
426 {
427   return SD_workstation_priv(workstation)->access_mode;
428 }
429
430 /**
431  * \brief Sets the access mode for the tasks that will be executed on a workstation
432  *
433  * By default, a workstation model is shared, i.e. several tasks
434  * can be executed at the same time on a workstation. The CPU power of
435  * the workstation is shared between the running tasks on the workstation.
436  * In sequential mode, only one task can use the workstation, and the other
437  * tasks wait in a FIFO.
438  *
439  * \param workstation a workstation
440  * \param access_mode the access mode you want to set to this workstation:
441  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
442  *
443  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
444  */
445 void SD_workstation_set_access_mode(SD_workstation_t workstation,
446                                     e_SD_workstation_access_mode_t
447                                     access_mode)
448 {
449   xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
450              access_mode != SD_WORKSTATION_SHARED_ACCESS,
451              "Trying to set an invalid access mode");
452
453   if (access_mode == SD_workstation_priv(workstation)->access_mode) {
454     return;                     // nothing is changed
455   }
456
457   SD_workstation_priv(workstation)->access_mode = access_mode;
458
459   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
460     xbt_fifo_free(SD_workstation_priv(workstation)->task_fifo);
461     SD_workstation_priv(workstation)->task_fifo = NULL;
462   } else {
463     SD_workstation_priv(workstation)->task_fifo = xbt_fifo_new();
464   }
465 }
466
467 /**
468  * \brief Return the list of mounted storages on a workstation.
469  *
470  * \param workstation a workstation
471  * \return a dynar containing all mounted storages on the workstation
472  */
473 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
474   return surf_host_get_mounted_storage_list(workstation);
475 }
476
477 /**
478  * \brief Return the list of mounted storages on a workstation.
479  *
480  * \param workstation a workstation
481  * \return a dynar containing all mounted storages on the workstation
482  */
483 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
484   return surf_host_get_attached_storage_list(workstation);
485 }
486
487 /**
488  * \brief Returns the host name the storage is attached to
489  *
490  * This functions checks whether a storage is a valid pointer or not and return its name.
491  */
492 const char *SD_storage_get_host(msg_storage_t storage) {
493   xbt_assert((storage != NULL), "Invalid parameters");
494   SD_storage_priv_t priv = SD_storage_priv(storage);
495   return priv->host;
496 }
497
498 /* Returns whether a task can start now on a workstation*/
499 /*
500   int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
501   SD_CHECK_INIT_DONE();
502   xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
503
504   return !__SD_workstation_is_busy(workstation) &&
505   (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
506   }
507 */
508
509 /* Returns whether a workstation is busy. A workstation is busy is it is
510  * in sequential mode and a task is running on it or the fifo is not empty.
511  */
512 int __SD_workstation_is_busy(SD_workstation_t workstation)
513 {
514   XBT_DEBUG
515       ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
516        SD_workstation_get_name(workstation),
517        (SD_workstation_priv(workstation)->access_mode ==
518         SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
519        (SD_workstation_priv(workstation)->current_task ?
520         SD_task_get_name(SD_workstation_priv(workstation)->current_task)
521         : "none"),
522        (SD_workstation_priv(workstation)->task_fifo ? xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) :
523         0));
524
525   return SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
526       (SD_workstation_priv(workstation)->current_task != NULL
527        || xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) > 0);
528 }
529
530 /* Destroys a workstation.
531  */
532 void __SD_workstation_destroy(void *workstation)
533 {
534
535   SD_workstation_priv_t w;
536
537   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
538
539   w = (SD_workstation_priv_t) workstation;
540
541   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
542     xbt_fifo_free(w->task_fifo);
543   }
544   xbt_free(w);
545 }
546
547 /** 
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)
552 {
553   xbt_assert(SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
554               "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
555               " to use this function");
556
557   return (SD_workstation_priv(workstation)->current_task);
558 }
559
560 /**
561  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
562  * or router
563  *
564  * \param AS, router name
565  * \return the xbt_dict_t properties of the AS
566  */
567 xbt_dict_t SD_as_router_get_properties(const char *asr)
568 {
569   return get_as_router_properties(asr);
570 }
571 /**
572  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
573  * or router
574  *
575  * \param AS, router name
576  * \param The name of a properties
577  * \return value of the properties
578  */
579 const char* SD_as_router_get_property_value(const char *asr, const char *name)
580 {
581   xbt_dict_t dict = get_as_router_properties(asr);
582   if(!dict) return NULL;
583   return xbt_dict_get_or_null(dict,name);
584 }