Logo AND Algorithmique Numérique Distribuée

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