Logo AND Algorithmique Numérique Distribuée

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