Logo AND Algorithmique Numérique Distribuée

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