Logo AND Algorithmique Numérique Distribuée

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