Logo AND Algorithmique Numérique Distribuée

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