Logo AND Algorithmique Numérique Distribuée

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