Logo AND Algorithmique Numérique Distribuée

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