Logo AND Algorithmique Numérique Distribuée

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