Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't rely on assert for error handling.
[simgrid.git] / src / simdag / sd_workstation.c
1 /* Copyright (c) 2006-2013. 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 "simdag/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
15
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
18                                 "Logging specific to SimDag (workstation)");
19
20 /* Creates a workstation and registers it in SD.
21  */
22 SD_workstation_t __SD_workstation_create(void *surf_workstation,
23                                          void *data)
24 {
25
26   SD_workstation_priv_t workstation;
27   const char *name;
28
29   workstation = xbt_new(s_SD_workstation_priv_t, 1);
30   workstation->data = data;     /* user data */
31   workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS;      /* default mode is shared */
32   workstation->task_fifo = NULL;
33   workstation->current_task = NULL;
34
35   name = surf_resource_name(surf_workstation);
36   xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
37   return xbt_lib_get_elm_or_null(host_lib,name);
38 }
39
40 /* Creates a storage and registers it in SD.
41  */
42 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
43 {
44
45   SD_storage_priv_t storage;
46   const char *name;
47
48   storage = xbt_new(s_SD_storage_priv_t, 1);
49   storage->data = data;     /* user data */
50
51   name = surf_resource_name(surf_storage);
52   xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
53   return xbt_lib_get_elm_or_null(storage_lib, name);
54 }
55
56 /* Destroys a storage.
57  */
58 void __SD_storage_destroy(void *storage)
59 {
60   SD_storage_priv_t s;
61
62   s = (SD_storage_priv_t) storage;
63   xbt_free(s);
64 }
65
66 /**
67  * \brief Returns a workstation given its name
68  *
69  * If there is no such workstation, the function returns \c NULL.
70  *
71  * \param name workstation name
72  * \return the workstation, or \c NULL if there is no such workstation
73  */
74 SD_workstation_t SD_workstation_get_by_name(const char *name)
75 {
76   return xbt_lib_get_elm_or_null(host_lib, name);
77 }
78
79 /**
80  * \brief Returns the workstation list
81  *
82  * Use SD_workstation_get_number() to know the array size.
83  *
84  * \return an array of \ref SD_workstation_t containing all workstations
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_workstation_model->extension.
182       workstation.get_properties(surf_workstation_resource_priv(workstation));
183 }
184
185
186 /** @brief Displays debugging informations about a workstation */
187 void SD_workstation_dump(SD_workstation_t ws)
188 {
189   xbt_dict_t props;
190   xbt_dict_cursor_t cursor=NULL;
191   char *key,*data;
192   SD_task_t task = NULL;
193   
194   XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
195   XBT_INFO("  - power: %.0f", SD_workstation_get_power(ws));
196   XBT_INFO("  - available power: %.2f", SD_workstation_get_available_power(ws));
197   switch (SD_workstation_priv(ws)->access_mode){
198   case SD_WORKSTATION_SHARED_ACCESS:
199       XBT_INFO("  - access mode: Space shared");
200       break;
201   case SD_WORKSTATION_SEQUENTIAL_ACCESS:
202       XBT_INFO("  - access mode: Exclusive");
203     task = SD_workstation_get_current_task(ws);
204     if(task)
205       XBT_INFO("    current running task: %s",
206                SD_task_get_name(task));
207     else
208       XBT_INFO("    no task running");
209       break;
210   default: break;
211   }
212   props = SD_workstation_get_properties(ws);
213   
214   if (!xbt_dict_is_empty(props)){
215     XBT_INFO("  - properties:");
216
217     xbt_dict_foreach(props,cursor,key,data) {
218       XBT_INFO("    %s->%s",key,data);
219     }
220   }
221 }
222
223 /**
224  * \brief Returns the route between two workstations
225  *
226  * Use SD_route_get_size() to know the array size.
227  *
228  * \param src a workstation
229  * \param dst another workstation
230  * \return a new array of \ref SD_link_t representating the route between these two workstations
231  * \see SD_route_get_size(), SD_link_t
232  */
233 const SD_link_t *SD_route_get_list(SD_workstation_t src,
234                                    SD_workstation_t dst)
235 {
236   void *surf_src;
237   void *surf_dst;
238   xbt_dynar_t surf_route;
239   const char *link_name;
240   void *surf_link;
241   unsigned int cpt;
242
243   if (sd_global->recyclable_route == NULL) {
244     /* first run */
245     sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
246   }
247
248   surf_src = src;
249   surf_dst = dst;
250   surf_route =
251       surf_workstation_model->extension.workstation.get_route(surf_src,
252                                                               surf_dst);
253
254   xbt_dynar_foreach(surf_route, cpt, surf_link) {
255     link_name = surf_resource_name(surf_link);
256     sd_global->recyclable_route[cpt] =
257         xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
258   }
259   return sd_global->recyclable_route;
260 }
261
262 /**
263  * \brief Returns the number of links on the route between two workstations
264  *
265  * \param src a workstation
266  * \param dst another workstation
267  * \return the number of links on the route between these two workstations
268  * \see SD_route_get_list()
269  */
270 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
271 {
272   return xbt_dynar_length(surf_workstation_model->extension.
273                           workstation.get_route(src, dst));
274 }
275
276 /**
277  * \brief Returns the total power of a workstation
278  *
279  * \param workstation a workstation
280  * \return the total power of this workstation
281  * \see SD_workstation_get_available_power()
282  */
283 double SD_workstation_get_power(SD_workstation_t workstation)
284 {
285   return surf_workstation_model->extension.workstation.
286       get_speed(workstation, 1.0);
287 }
288
289 /**
290  * \brief Returns the proportion of available power in a workstation
291  *
292  * \param workstation a workstation
293  * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
294  * \see SD_workstation_get_power()
295  */
296 double SD_workstation_get_available_power(SD_workstation_t workstation)
297 {
298   return surf_workstation_model->extension.
299       workstation.get_available_speed(workstation);
300 }
301
302 /**
303  * \brief Returns an approximative estimated time for the given computation amount on a workstation
304  *
305  * \param workstation a workstation
306  * \param computation_amount the computation amount you want to evaluate (in flops)
307  * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
308  */
309 double SD_workstation_get_computation_time(SD_workstation_t workstation,
310                                            double computation_amount)
311 {
312   xbt_assert(computation_amount >= 0,
313               "computation_amount must be greater than or equal to zero");
314   return computation_amount / SD_workstation_get_power(workstation);
315 }
316
317 /**
318  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
319  * between the workstations.
320  *
321  * \param src the first workstation
322  * \param dst the second workstation
323  * \return the latency of the route between the two workstations (in seconds)
324  * \see SD_route_get_current_bandwidth()
325  */
326 double SD_route_get_current_latency(SD_workstation_t src,
327                                     SD_workstation_t dst)
328 {
329
330   const SD_link_t *links;
331   int nb_links;
332   double latency;
333   int i;
334
335   links = SD_route_get_list(src, dst);
336   nb_links = SD_route_get_size(src, dst);
337   latency = 0.0;
338
339   for (i = 0; i < nb_links; i++) {
340     latency += SD_link_get_current_latency(links[i]);
341   }
342
343   return latency;
344 }
345
346 /**
347  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
348  * between the workstations.
349  *
350  * \param src the first workstation
351  * \param dst the second workstation
352  * \return the bandwidth of the route between the two workstations (in bytes/second)
353  * \see SD_route_get_current_latency()
354  */
355 double SD_route_get_current_bandwidth(SD_workstation_t src,
356                                       SD_workstation_t dst)
357 {
358
359   const SD_link_t *links;
360   int nb_links;
361   double bandwidth;
362   double min_bandwidth;
363   int i;
364
365   links = SD_route_get_list(src, dst);
366   nb_links = SD_route_get_size(src, dst);
367   min_bandwidth = -1.0;
368
369   for (i = 0; i < nb_links; i++) {
370     bandwidth = SD_link_get_current_bandwidth(links[i]);
371     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
372       min_bandwidth = bandwidth;
373   }
374
375   return min_bandwidth;
376 }
377
378 /**
379  * \brief Returns an approximative estimated time for the given
380  * communication amount between two workstations
381  *
382  * \param src the first workstation
383  * \param dst the second workstation
384  * \param communication_amount the communication amount you want to evaluate (in bytes)
385  * \return an approximative estimated computation time for the given communication amount
386  * between the workstations (in seconds)
387  */
388 double SD_route_get_communication_time(SD_workstation_t src,
389                                        SD_workstation_t dst,
390                                        double communication_amount)
391 {
392
393
394   /* total time = latency + transmission time of the slowest link
395      transmission time of a link = communication amount / link bandwidth */
396
397   const SD_link_t *links;
398   int nb_links;
399   double bandwidth, min_bandwidth;
400   double latency;
401   int i;
402
403   xbt_assert(communication_amount >= 0,
404               "communication_amount must be greater than or equal to zero");
405
406
407
408   if (communication_amount == 0.0)
409     return 0.0;
410
411   links = SD_route_get_list(src, dst);
412   nb_links = SD_route_get_size(src, dst);
413   min_bandwidth = -1.0;
414   latency = 0;
415
416   for (i = 0; i < nb_links; i++) {
417     latency += SD_link_get_current_latency(links[i]);
418     bandwidth = SD_link_get_current_bandwidth(links[i]);
419     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
420       min_bandwidth = bandwidth;
421   }
422
423   return latency + (communication_amount / min_bandwidth);
424 }
425
426 /**
427  * \brief Returns the access mode of this workstation.
428  *
429  * \param workstation a workstation
430  * \return the access mode for the tasks running on this workstation:
431  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
432  *
433  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
434  */
435 e_SD_workstation_access_mode_t
436 SD_workstation_get_access_mode(SD_workstation_t workstation)
437 {
438   return SD_workstation_priv(workstation)->access_mode;
439 }
440
441 /**
442  * \brief Sets the access mode for the tasks that will be executed on a workstation
443  *
444  * By default, a workstation model is shared, i.e. several tasks
445  * can be executed at the same time on a workstation. The CPU power of
446  * the workstation is shared between the running tasks on the workstation.
447  * In sequential mode, only one task can use the workstation, and the other
448  * tasks wait in a FIFO.
449  *
450  * \param workstation a workstation
451  * \param access_mode the access mode you want to set to this workstation:
452  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
453  *
454  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
455  */
456 void SD_workstation_set_access_mode(SD_workstation_t workstation,
457                                     e_SD_workstation_access_mode_t
458                                     access_mode)
459 {
460   xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
461              access_mode != SD_WORKSTATION_SHARED_ACCESS,
462              "Trying to set an invalid access mode");
463
464   if (access_mode == SD_workstation_priv(workstation)->access_mode) {
465     return;                     // nothing is changed
466   }
467
468   SD_workstation_priv(workstation)->access_mode = access_mode;
469
470   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
471     xbt_fifo_free(SD_workstation_priv(workstation)->task_fifo);
472     SD_workstation_priv(workstation)->task_fifo = NULL;
473   } else {
474     SD_workstation_priv(workstation)->task_fifo = xbt_fifo_new();
475   }
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_dict_t SD_workstation_get_storage_list(SD_workstation_t workstation){
485   return surf_workstation_model->extension.workstation.get_storage_list(workstation);
486 }
487
488 /* Returns whether a task can start now on a workstation*/
489 /*
490   int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
491   SD_CHECK_INIT_DONE();
492   xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
493
494   return !__SD_workstation_is_busy(workstation) &&
495   (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
496   }
497 */
498
499 /* Returns whether a workstation is busy. A workstation is busy is it is
500  * in sequential mode and a task is running on it or the fifo is not empty.
501  */
502 int __SD_workstation_is_busy(SD_workstation_t workstation)
503 {
504   XBT_DEBUG
505       ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
506        SD_workstation_get_name(workstation),
507        (SD_workstation_priv(workstation)->access_mode ==
508         SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
509        (SD_workstation_priv(workstation)->current_task ?
510         SD_task_get_name(SD_workstation_priv(workstation)->current_task)
511         : "none"),
512        (SD_workstation_priv(workstation)->task_fifo ? xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) :
513         0));
514
515   return SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
516       (SD_workstation_priv(workstation)->current_task != NULL
517        || xbt_fifo_size(SD_workstation_priv(workstation)->task_fifo) > 0);
518 }
519
520 /* Destroys a workstation.
521  */
522 void __SD_workstation_destroy(void *workstation)
523 {
524
525   SD_workstation_priv_t w;
526
527   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
528
529   w = (SD_workstation_priv_t) workstation;
530
531   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
532     xbt_fifo_free(w->task_fifo);
533   }
534   xbt_free(w);
535 }
536
537 /** 
538  * \brief Returns the kind of the task currently running on a workstation
539  * Only call this with sequential access mode set
540  * \param workstation a workstation */
541 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
542 {
543   xbt_assert(SD_workstation_priv(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
544               "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
545               " to use this function");
546
547   return (SD_workstation_priv(workstation)->current_task);
548 }
549
550 /**
551  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
552  * or router
553  *
554  * \param AS, router name
555  * \return the xbt_dict_t properties of the AS
556  */
557 xbt_dict_t SD_as_router_get_properties(const char *asr)
558 {
559   return get_as_router_properties(asr);
560 }
561 /**
562  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
563  * or router
564  *
565  * \param AS, router name
566  * \param The name of a properties
567  * \return value of the properties
568  */
569 const char* SD_as_router_get_property_value(const char *asr, const char *name)
570 {
571   xbt_dict_t dict = get_as_router_properties(asr);
572   if(!dict) return NULL;
573   return xbt_dict_get_or_null(dict,name);
574 }