Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make hypervisor compile with compile_warnings=ON
[simgrid.git] / src / surf / workstation.c
1 /* Copyright (c) 2004, 2005, 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 "xbt/ex.h"
8 #include "xbt/dict.h"
9 #include "portable.h"
10 #include "surf_private.h"
11 #include "storage_private.h"
12 #include "surf/surf_resource.h"
13 #include "simgrid/sg_config.h"
14 #include "workstation_private.h"
15 #include "vm_workstation_private.h"
16 #include "cpu_cas01_private.h"
17 #include "maxmin_private.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
20                                 "Logging specific to the SURF workstation module");
21
22 surf_model_t surf_workstation_model = NULL;
23
24
25 static void workstation_new(sg_platf_host_cbarg_t host)
26 {
27   const char *name = host->id;
28
29   /* NOTE: The properties object is NULL, because the current code uses that of
30    * that of a cpu resource. */
31   workstation_CLM03_t ws = (workstation_CLM03_t) surf_resource_new(sizeof(s_workstation_CLM03_t), surf_workstation_model, name, NULL);
32
33   ws->storage = xbt_lib_get_or_null(storage_lib, name, ROUTING_STORAGE_HOST_LEVEL);
34   ws->net_elm = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
35
36   XBT_DEBUG("Create ws %s with %ld mounted disks", name, xbt_dynar_length(ws->storage));
37   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, ws);
38 }
39
40
41 static void ws_parallel_action_cancel(surf_action_t action)
42 {
43   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
44 }
45
46 static int ws_parallel_action_free(surf_action_t action)
47 {
48   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
49   return -1;
50 }
51
52 int ws_action_unref(surf_action_t action)
53 {
54   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
55     return surf_network_model->action_unref(action);
56   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
57     return action->model_obj->action_unref(action);
58       // previously was: Adrien/Arnaud 6 feb
59           // surf_cpu_model->action_unref(action);
60   else if (action->model_obj->type == SURF_MODEL_TYPE_WORKSTATION)
61     return ws_parallel_action_free(action);
62   else
63     DIE_IMPOSSIBLE;
64   return 0;
65 }
66
67 void ws_action_cancel(surf_action_t action)
68 {
69   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
70     surf_network_model->action_cancel(action);
71   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
72     action->model_obj->action_cancel(action);
73   else if (action->model_obj->type == SURF_MODEL_TYPE_WORKSTATION)
74     ws_parallel_action_cancel(action);
75   else
76     DIE_IMPOSSIBLE;
77   return;
78 }
79
80 static void ws_action_state_set(surf_action_t action,
81                                 e_surf_action_state_t state)
82 {
83   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
84     surf_network_model->action_state_set(action, state);
85   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
86     action->model_obj->action_state_set(action, state);
87   else if (action->model_obj->type == SURF_MODEL_TYPE_WORKSTATION)
88     surf_action_state_set(action, state);
89   else
90     DIE_IMPOSSIBLE;
91   return;
92 }
93
94
95 /* -- The callback functions at model_private -- */
96 /* These callbacks are also used for the vm workstation model. */
97 int ws_resource_used(void *resource_id)
98 {
99   /* This model does not implement parallel tasks */
100   THROW_IMPOSSIBLE;
101   return -1;
102 }
103
104
105 /* TODO: The current code would be slow due to the iteration. Later, we can
106  * make it faster. */
107 static int constraint_is_active(cpu_Cas01_t cpu_cas01)
108 {
109   surf_model_t cpu_model = cpu_cas01->generic_resource.model;
110   lmm_system_t sys = cpu_model->model_private->maxmin_system;
111   int found = 0;
112   lmm_constraint_t cnst_tmp;
113
114   xbt_swag_foreach(cnst_tmp, &sys->active_constraint_set) {
115     if (cnst_tmp == cpu_cas01->constraint) {
116       found = 1;
117       break;
118     }
119   }
120
121   return found;
122 }
123
124 static void adjust_weight_of_dummy_cpu_actions(void)
125 {
126   /* iterate for all hosts including virtual machines */
127   xbt_lib_cursor_t cursor;
128   char *key;
129   void **ind_host;
130
131   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
132     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
133     cpu_Cas01_t cpu_cas01 = ind_host[SURF_CPU_LEVEL];
134
135     if (!ws_clm03)
136       continue;
137     /* skip if it is not a virtual machine */
138     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
139       continue;
140     xbt_assert(cpu_cas01, "cpu-less workstation");
141
142     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
143     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
144
145     if (constraint_is_active(cpu_cas01)) {
146       /* some tasks exist on this VM */
147       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
148
149       /* FIXME: we shoud use lmm_update_variable_weight() ? */
150       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
151       surf_action_set_priority(ws_vm2013->cpu_action, 1);
152
153     } else {
154       /* no task exits on this VM */
155       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
156
157       surf_action_set_priority(ws_vm2013->cpu_action, 0);
158     }
159   }
160 }
161
162
163 double ws_share_resources(surf_model_t workstation_model, double now)
164 {
165   if (workstation_model->type == SURF_MODEL_TYPE_WORKSTATION)
166     adjust_weight_of_dummy_cpu_actions();
167
168   /* Invoke the share_resources() callback of the physical cpu model object and
169    * the network model objects. */
170   surf_model_t cpu_model = workstation_model->extension.workstation.cpu_model;
171   surf_model_t net_model = surf_network_model;
172
173   double min_by_cpu = cpu_model->model_private->share_resources(cpu_model, now);
174   double min_by_net = net_model->model_private->share_resources(net_model, now);
175
176   XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f",
177       workstation_model, cpu_model->name, min_by_cpu, net_model->name, min_by_net);
178
179   if (min_by_cpu >= 0.0 && min_by_net >= 0.0)
180     return min(min_by_cpu, min_by_net);
181   else if (min_by_cpu >= 0.0)
182     return min_by_cpu;
183   else if (min_by_net >= 0.0)
184     return min_by_net;
185   else
186     return min_by_cpu;  /* probably min_by_cpu == min_by_net == -1 */
187 }
188
189 void ws_update_actions_state(surf_model_t workstation_model, double now, double delta)
190 {
191   return;
192 }
193
194 void ws_update_resource_state(void *id, tmgr_trace_event_t event_type, double value, double date)
195 {
196   /* This model does not implement parallel tasks */
197   THROW_IMPOSSIBLE;
198 }
199
200 void ws_finalize(surf_model_t workstation_model)
201 {
202   surf_model_exit(workstation_model);
203   workstation_model = NULL;
204 }
205
206
207
208 surf_action_t ws_execute(void *workstation, double size)
209 {
210   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
211   return cpu->model->extension.cpu.execute(workstation, size);
212 }
213
214 surf_action_t ws_action_sleep(void *workstation, double duration)
215 {
216   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
217   return cpu->model->extension.cpu.sleep(workstation, duration);
218 }
219
220 void ws_action_suspend(surf_action_t action)
221 {
222   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
223     surf_network_model->suspend(action);
224   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
225     action->model_obj->suspend(action);
226   else
227     DIE_IMPOSSIBLE;
228 }
229
230 void ws_action_resume(surf_action_t action)
231 {
232   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
233     surf_network_model->resume(action);
234   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
235     action->model_obj->resume(action);
236   else
237     DIE_IMPOSSIBLE;
238 }
239
240 static int ws_action_is_suspended(surf_action_t action)
241 {
242   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
243     return surf_network_model->is_suspended(action);
244   if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
245     return action->model_obj->is_suspended(action);
246   DIE_IMPOSSIBLE;
247   return -1;
248 }
249
250 static void ws_action_set_max_duration(surf_action_t action,
251                                        double duration)
252 {
253   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
254     surf_network_model->set_max_duration(action, duration);
255   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
256     action->model_obj->set_max_duration(action, duration);
257   else
258     DIE_IMPOSSIBLE;
259 }
260
261 void ws_action_set_priority(surf_action_t action, double priority)
262 {
263   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
264     surf_network_model->set_priority(action, priority);
265   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
266     action->model_obj->set_priority(action, priority);
267   else
268     DIE_IMPOSSIBLE;
269 }
270
271 #ifdef HAVE_TRACING
272 static void ws_action_set_category(surf_action_t action, const char *category)
273 {
274   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
275     surf_network_model->set_category(action, category);
276   else if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
277     action->model_obj->set_category(action, category);
278   else
279     DIE_IMPOSSIBLE;
280 }
281 #endif
282
283 #ifdef HAVE_LATENCY_BOUND_TRACKING
284 static int ws_get_latency_limited(surf_action_t action)
285 {
286   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
287     return surf_network_model->get_latency_limited(action);
288   else
289     return 0;
290 }
291 #endif
292
293 double ws_action_get_remains(surf_action_t action)
294 {
295   if (action->model_obj->type == SURF_MODEL_TYPE_NETWORK)
296     return surf_network_model->get_remains(action);
297   if (action->model_obj->type == SURF_MODEL_TYPE_CPU)
298     return action->model_obj->get_remains(action);
299   DIE_IMPOSSIBLE;
300   return -1.0;
301 }
302
303 static surf_action_t ws_communicate(void *workstation_src,
304                                     void *workstation_dst, double size,
305                                     double rate)
306 {
307   workstation_CLM03_t src = surf_workstation_resource_priv(workstation_src);
308   workstation_CLM03_t dst = surf_workstation_resource_priv(workstation_dst);
309   return surf_network_model->extension.network.
310       communicate(src->net_elm,
311                   dst->net_elm, size, rate);
312 }
313
314 e_surf_resource_state_t ws_get_state(void *workstation)
315 {
316   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
317   return cpu->model->extension.cpu.get_state(workstation);
318 }
319
320 static double ws_get_speed(void *workstation, double load)
321 {
322   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
323   return cpu->model->extension.cpu.get_speed(workstation, load);
324 }
325
326 static double ws_get_available_speed(void *workstation)
327 {
328   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
329   return cpu->model->extension.cpu.get_available_speed(workstation);
330 }
331
332 static surf_action_t ws_execute_parallel_task(int workstation_nb,
333                                               void **workstation_list,
334                                               double *computation_amount,
335                                               double *communication_amount,
336                                               double rate)
337 {
338 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
339   if ((workstation_nb == 1)
340       && (cost_or_zero(communication_amount, 0) == 0.0))
341     return ws_execute(workstation_list[0], computation_amount[0]);
342   else if ((workstation_nb == 1)
343            && (cost_or_zero(computation_amount, 0) == 0.0))
344     return ws_communicate(workstation_list[0], workstation_list[0],communication_amount[0], rate);
345   else if ((workstation_nb == 2)
346              && (cost_or_zero(computation_amount, 0) == 0.0)
347              && (cost_or_zero(computation_amount, 1) == 0.0)) {
348     int i,nb = 0;
349     double value = 0.0;
350
351     for (i = 0; i < workstation_nb * workstation_nb; i++) {
352       if (cost_or_zero(communication_amount, i) > 0.0) {
353         nb++;
354         value = cost_or_zero(communication_amount, i);
355       }
356     }
357     if (nb == 1)
358       return ws_communicate(workstation_list[0], workstation_list[1],value, rate);
359   }
360 #undef cost_or_zero
361
362   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
363   return NULL;
364 }
365
366
367 /* returns an array of network_link_CM02_t */
368 static xbt_dynar_t ws_get_route(void *workstation_src, void *workstation_dst)
369 {
370   XBT_DEBUG("ws_get_route");
371   workstation_CLM03_t src = surf_workstation_resource_priv(workstation_src);
372   workstation_CLM03_t dst = surf_workstation_resource_priv(workstation_dst);
373   return surf_network_model->extension.
374       network.get_route(src->net_elm,
375                   dst->net_elm);
376 }
377
378 static double ws_get_link_bandwidth(const void *link)
379 {
380   return surf_network_model->extension.network.get_link_bandwidth(link);
381 }
382
383 static double ws_get_link_latency(const void *link)
384 {
385   return surf_network_model->extension.network.get_link_latency(link);
386 }
387
388 static int ws_link_shared(const void *link)
389 {
390   return surf_network_model->extension.network.link_shared(link);
391 }
392
393 static xbt_dict_t ws_get_properties(const void *ws)
394 {
395   return surf_resource_properties(surf_cpu_resource_priv(ws));
396 }
397
398 static storage_t find_storage_on_mount_list(void *workstation,const char* storage)
399 {
400   storage_t st = NULL;
401   s_mount_t mnt;
402   unsigned int cursor;
403   workstation_CLM03_t ws = (workstation_CLM03_t) surf_workstation_resource_priv(workstation);
404   xbt_dynar_t storage_list = ws->storage;
405
406   XBT_DEBUG("Search for storage name '%s' on '%s'",storage,ws->generic_resource.name);
407   xbt_dynar_foreach(storage_list,cursor,mnt)
408   {
409     XBT_DEBUG("See '%s'",mnt.name);
410     if(!strcmp(storage,mnt.name)){
411       st = mnt.id;
412       break;
413     }
414   }
415   if(!st) xbt_die("Can't find mount '%s' for '%s'",storage,ws->generic_resource.name);
416   return st;
417 }
418
419 static surf_action_t ws_action_open(void *workstation, const char* mount, const char* path, const char* mode)
420 {
421   storage_t st = find_storage_on_mount_list(workstation, mount);
422   XBT_DEBUG("OPEN on disk '%s'",st->generic_resource.name);
423   surf_model_t model = st->generic_resource.model;
424   return model->extension.storage.open(st, mount, path, mode);
425 }
426
427 static surf_action_t ws_action_close(void *workstation, surf_file_t fp)
428 {
429   storage_t st = find_storage_on_mount_list(workstation, fp->storage);
430   XBT_DEBUG("CLOSE on disk '%s'",st->generic_resource.name);
431   surf_model_t model = st->generic_resource.model;
432   return model->extension.storage.close(st, fp);
433 }
434
435 static surf_action_t ws_action_read(void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
436 {
437   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
438   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
439   surf_model_t model = st->generic_resource.model;
440   return model->extension.storage.read(st, ptr, (double)size, nmemb, stream);
441 }
442
443 static surf_action_t ws_action_write(void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
444 {
445   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
446   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
447   surf_model_t model = st->generic_resource.model;
448   return model->extension.storage.write(st,  ptr, size, nmemb, stream);
449 }
450
451 static surf_action_t ws_action_stat(void *workstation, surf_file_t stream)
452 {
453   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
454   XBT_DEBUG("STAT on disk '%s'",st->generic_resource.name);
455   surf_model_t model = st->generic_resource.model;
456   return model->extension.storage.stat(st,  stream);
457 }
458
459 static surf_action_t ws_action_unlink(void *workstation, surf_file_t stream)
460 {
461   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
462   XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
463   surf_model_t model = st->generic_resource.model;
464   return model->extension.storage.unlink(st,  stream);
465 }
466
467 static surf_action_t ws_action_ls(void *workstation, const char* mount, const char *path)
468 {
469   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
470   storage_t st = find_storage_on_mount_list(workstation, mount);
471   surf_model_t model = st->generic_resource.model;
472   return model->extension.storage.ls(st, path);
473 }
474
475 void ws_get_params(void *ws, ws_params_t params)
476 {
477   workstation_CLM03_t ws_clm03 = surf_workstation_resource_priv(ws);
478   memcpy(params, &ws_clm03->params, sizeof(s_ws_params_t));
479 }
480
481 void ws_set_params(void *ws, ws_params_t params)
482 {
483   workstation_CLM03_t ws_clm03 = surf_workstation_resource_priv(ws);
484   /* may check something here. */
485   memcpy(&ws_clm03->params, params, sizeof(s_ws_params_t));
486 }
487
488 static xbt_dynar_t ws_get_vms(void *pm)
489 {
490   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
491
492   /* iterate for all hosts including virtual machines */
493   xbt_lib_cursor_t cursor;
494   char *key;
495   void **ind_host;
496   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
497     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
498     if (!ws_clm03)
499       continue;
500     /* skip if it is not a virtual machine */
501     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
502       continue;
503
504     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
505     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
506     if (pm == ws_vm2013->sub_ws)
507       xbt_dynar_push(dyn, &ws_vm2013->sub_ws);
508   }
509
510   return dyn;
511 }
512
513
514 static void surf_workstation_model_init_internal(void)
515 {
516   surf_model_t model = surf_model_init();
517
518   model->name = "Workstation";
519   model->type = SURF_MODEL_TYPE_WORKSTATION;
520   model->action_unref     = ws_action_unref;
521   model->action_cancel    = ws_action_cancel;
522   model->action_state_set = ws_action_state_set;
523
524   model->model_private->resource_used         = ws_resource_used;
525   model->model_private->share_resources       = ws_share_resources;
526   model->model_private->update_actions_state  = ws_update_actions_state;
527   model->model_private->update_resource_state = ws_update_resource_state;
528   model->model_private->finalize              = ws_finalize;
529
530   model->suspend          = ws_action_suspend;
531   model->resume           = ws_action_resume;
532   model->is_suspended     = ws_action_is_suspended;
533   model->set_max_duration = ws_action_set_max_duration;
534   model->set_priority     = ws_action_set_priority;
535   #ifdef HAVE_TRACING
536   model->set_category     = ws_action_set_category;
537   #endif
538   model->get_remains      = ws_action_get_remains;
539   #ifdef HAVE_LATENCY_BOUND_TRACKING
540   model->get_latency_limited = ws_get_latency_limited;
541   #endif
542
543   /* For VM support, we have a surf cpu model object for each workstation model
544    * object. The physical workstation model object has the cpu model object of
545    * the physical machine layer. */
546   xbt_assert(surf_cpu_model_pm);
547   model->extension.workstation.cpu_model = surf_cpu_model_pm;
548
549   model->extension.workstation.execute   = ws_execute;
550   model->extension.workstation.sleep     = ws_action_sleep;
551   model->extension.workstation.get_state = ws_get_state;
552   model->extension.workstation.get_speed = ws_get_speed;
553   model->extension.workstation.get_available_speed = ws_get_available_speed;
554
555   model->extension.workstation.communicate           = ws_communicate;
556   model->extension.workstation.get_route             = ws_get_route;
557   model->extension.workstation.execute_parallel_task = ws_execute_parallel_task;
558   model->extension.workstation.get_link_bandwidth    = ws_get_link_bandwidth;
559   model->extension.workstation.get_link_latency      = ws_get_link_latency;
560   model->extension.workstation.link_shared           = ws_link_shared;
561   model->extension.workstation.get_properties        = ws_get_properties;
562
563   model->extension.workstation.open   = ws_action_open;
564   model->extension.workstation.close  = ws_action_close;
565   model->extension.workstation.read   = ws_action_read;
566   model->extension.workstation.write  = ws_action_write;
567   model->extension.workstation.stat   = ws_action_stat;
568   model->extension.workstation.unlink = ws_action_unlink;
569   model->extension.workstation.ls     = ws_action_ls;
570
571   model->extension.workstation.get_params = ws_get_params;
572   model->extension.workstation.set_params = ws_set_params;
573   model->extension.workstation.get_vms    = ws_get_vms;
574
575   surf_workstation_model = model;
576 }
577
578 void surf_workstation_model_init_current_default(void)
579 {
580   xbt_cfg_setdefault_int(_sg_cfg_set, "network/crosstraffic", 1);
581   surf_cpu_model_init_Cas01();
582   surf_network_model_init_LegrandVelho();
583
584   /* surf_cpu_mode_pm and surf_network_model must be initialized in advance. */
585   xbt_assert(surf_cpu_model_pm);
586   xbt_assert(surf_network_model);
587   surf_workstation_model_init_internal();
588
589   xbt_dynar_push(model_list, &surf_workstation_model);
590   xbt_dynar_push(model_list_invoke, &surf_workstation_model);
591   sg_platf_host_add_cb(workstation_new);
592 }
593
594 void surf_workstation_model_init_compound()
595 {
596   xbt_assert(surf_cpu_model_pm, "No CPU model defined yet!");
597   xbt_assert(surf_network_model, "No network model defined yet!");
598
599   surf_workstation_model_init_internal();
600   xbt_dynar_push(model_list, &surf_workstation_model);
601   xbt_dynar_push(model_list_invoke, &surf_workstation_model);
602   sg_platf_host_add_cb(workstation_new);
603 }