Logo AND Algorithmique Numérique Distribuée

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