Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add missing files
[simgrid.git] / src / surf / vm_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 "surf/surf_resource.h"
12 #include "simgrid/sg_config.h"
13
14 typedef struct workstation_VM2013 {
15   s_surf_resource_t generic_resource;   /* Must remain first to add this to a trace */
16   surf_resource_t physical_workstation;  // Pointer to the host OS
17 } s_workstation_VM2013_t, *workstation_VM2013_t;
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
20                                 "Logging specific to the SURF workstation module");
21
22 surf_model_t surf_vm_workstation_model = NULL;
23
24 static void *vm_ws_create (const char *name, void *phys_workstation)
25 {
26   workstation_VM2013_t workstation = xbt_new0(s_workstation_VM2013_t, 1);
27 // TODO Implement the surf vm workstation model
28   workstation->generic_resource.model = surf_vm_workstation_model;
29   workstation->generic_resource.name = xbt_strdup(name);
30   workstation->physical_workstation = phys_workstation;
31   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, workstation);
32 }
33
34 static int ws_resource_used(void *resource_id)
35 {
36   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
37   return -1;
38 }
39
40 static void ws_parallel_action_cancel(surf_action_t action)
41 {
42   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
43 }
44
45 static int ws_parallel_action_free(surf_action_t action)
46 {
47   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
48   return -1;
49 }
50
51 static int ws_action_unref(surf_action_t action)
52 {
53   if (action->model_type == surf_network_model)
54     return surf_network_model->action_unref(action);
55   else if (action->model_type == surf_cpu_model)
56     return surf_cpu_model->action_unref(action);
57   else if (action->model_type == surf_workstation_model)
58     return ws_parallel_action_free(action);
59   else
60     DIE_IMPOSSIBLE;
61   return 0;
62 }
63
64 static void ws_action_cancel(surf_action_t action)
65 {
66   if (action->model_type == surf_network_model)
67     surf_network_model->action_cancel(action);
68   else if (action->model_type == surf_cpu_model)
69     surf_cpu_model->action_cancel(action);
70   else if (action->model_type == surf_workstation_model)
71     ws_parallel_action_cancel(action);
72   else
73     DIE_IMPOSSIBLE;
74   return;
75 }
76
77 static void ws_action_state_set(surf_action_t action,
78                                 e_surf_action_state_t state)
79 {
80   if (action->model_type == surf_network_model)
81     surf_network_model->action_state_set(action, state);
82   else if (action->model_type == surf_cpu_model)
83     surf_cpu_model->action_state_set(action, state);
84   else if (action->model_type == surf_workstation_model)
85     surf_action_state_set(action, state);
86   else
87     DIE_IMPOSSIBLE;
88   return;
89 }
90
91 static double ws_share_resources(double now)
92 {
93   return -1.0;
94 }
95
96 static void ws_update_actions_state(double now, double delta)
97 {
98   return;
99 }
100
101 static void ws_update_resource_state(void *id,
102                                      tmgr_trace_event_t event_type,
103                                      double value, double date)
104 {
105   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
106 }
107
108 static surf_action_t ws_execute(void *workstation, double size)
109 {
110   surf_resource_t cpu = ((surf_resource_t) surf_cpu_resource_priv(workstation));
111   return cpu->model->extension.cpu.execute(workstation, size);
112 }
113
114 static surf_action_t ws_action_sleep(void *workstation, double duration)
115 {
116   return surf_cpu_model->extension.cpu.
117       sleep(workstation, duration);
118 }
119
120 static void ws_action_suspend(surf_action_t action)
121 {
122   if (action->model_type == surf_network_model)
123     surf_network_model->suspend(action);
124   else if (action->model_type == surf_cpu_model)
125     surf_cpu_model->suspend(action);
126   else
127     DIE_IMPOSSIBLE;
128 }
129
130 static void ws_action_resume(surf_action_t action)
131 {
132   if (action->model_type == surf_network_model)
133     surf_network_model->resume(action);
134   else if (action->model_type == surf_cpu_model)
135     surf_cpu_model->resume(action);
136   else
137     DIE_IMPOSSIBLE;
138 }
139
140 static int ws_action_is_suspended(surf_action_t action)
141 {
142   if (action->model_type == surf_network_model)
143     return surf_network_model->is_suspended(action);
144   if (action->model_type == surf_cpu_model)
145     return surf_cpu_model->is_suspended(action);
146   DIE_IMPOSSIBLE;
147   return -1;
148 }
149
150 static void ws_action_set_max_duration(surf_action_t action,
151                                        double duration)
152 {
153   if (action->model_type == surf_network_model)
154     surf_network_model->set_max_duration(action, duration);
155   else if (action->model_type == surf_cpu_model)
156     surf_cpu_model->set_max_duration(action, duration);
157   else
158     DIE_IMPOSSIBLE;
159 }
160
161 static void ws_action_set_priority(surf_action_t action, double priority)
162 {
163   if (action->model_type == surf_network_model)
164     surf_network_model->set_priority(action, priority);
165   else if (action->model_type == surf_cpu_model)
166     surf_cpu_model->set_priority(action, priority);
167   else
168     DIE_IMPOSSIBLE;
169 }
170
171 #ifdef HAVE_TRACING
172 static void ws_action_set_category(surf_action_t action, const char *category)
173 {
174   if (action->model_type == surf_network_model)
175     surf_network_model->set_category(action, category);
176   else if (action->model_type == surf_cpu_model)
177     surf_cpu_model->set_category(action, category);
178   else
179     DIE_IMPOSSIBLE;
180 }
181 #endif
182
183 #ifdef HAVE_LATENCY_BOUND_TRACKING
184 static int ws_get_latency_limited(surf_action_t action)
185 {
186   if (action->model_type == surf_network_model)
187     return surf_network_model->get_latency_limited(action);
188   else
189     return 0;
190 }
191 #endif
192
193 static double ws_action_get_remains(surf_action_t action)
194 {
195   if (action->model_type == surf_network_model)
196     return surf_network_model->get_remains(action);
197   if (action->model_type == surf_cpu_model)
198     return surf_cpu_model->get_remains(action);
199   DIE_IMPOSSIBLE;
200   return -1.0;
201 }
202
203 static surf_action_t ws_communicate(void *workstation_src,
204                                     void *workstation_dst, double size,
205                                     double rate)
206 {
207   workstation_VM2013_t src = surf_workstation_resource_priv(workstation_src);
208   workstation_VM2013_t dst = surf_workstation_resource_priv(workstation_dst);
209   return surf_network_model->extension.network.
210       communicate(src->net_elm,
211                   dst->net_elm, size, rate);
212 }
213
214 static e_surf_resource_state_t ws_get_state(void *workstation)
215 {
216   return surf_cpu_model->extension.cpu.
217       get_state(workstation);
218 }
219
220 static double ws_get_speed(void *workstation, double load)
221 {
222   return surf_cpu_model->extension.cpu.
223       get_speed(workstation, load);
224 }
225
226 static double ws_get_available_speed(void *workstation)
227 {
228   return surf_cpu_model->extension.cpu.
229       get_available_speed(workstation);
230 }
231
232 static surf_action_t ws_execute_parallel_task(int workstation_nb,
233                                               void **workstation_list,
234                                               double *computation_amount,
235                                               double *communication_amount,
236                                               double rate)
237 {
238 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
239   if ((workstation_nb == 1)
240       && (cost_or_zero(communication_amount, 0) == 0.0))
241     return ws_execute(workstation_list[0], computation_amount[0]);
242   else if ((workstation_nb == 1)
243            && (cost_or_zero(computation_amount, 0) == 0.0))
244     return ws_communicate(workstation_list[0], workstation_list[0],communication_amount[0], rate);
245   else if ((workstation_nb == 2)
246              && (cost_or_zero(computation_amount, 0) == 0.0)
247              && (cost_or_zero(computation_amount, 1) == 0.0)) {
248     int i,nb = 0;
249     double value = 0.0;
250
251     for (i = 0; i < workstation_nb * workstation_nb; i++) {
252       if (cost_or_zero(communication_amount, i) > 0.0) {
253         nb++;
254         value = cost_or_zero(communication_amount, i);
255       }
256     }
257     if (nb == 1)
258       return ws_communicate(workstation_list[0], workstation_list[1],value, rate);
259   }
260 #undef cost_or_zero
261
262   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
263   return NULL;
264 }
265
266
267 /* returns an array of network_link_CM02_t */
268 static xbt_dynar_t ws_get_route(void *workstation_src, void *workstation_dst)
269 {
270   XBT_DEBUG("ws_get_route");
271   workstation_VM2013_t src = surf_workstation_resource_priv(workstation_src);
272   workstation_VM2013_t dst = surf_workstation_resource_priv(workstation_dst);
273   return surf_network_model->extension.
274       network.get_route(src->net_elm,
275                   dst->net_elm);
276 }
277
278 static double ws_get_link_bandwidth(const void *link)
279 {
280   return surf_network_model->extension.network.get_link_bandwidth(link);
281 }
282
283 static double ws_get_link_latency(const void *link)
284 {
285   return surf_network_model->extension.network.get_link_latency(link);
286 }
287
288 static int ws_link_shared(const void *link)
289 {
290   return surf_network_model->extension.network.link_shared(link);
291 }
292
293 static void ws_finalize(void)
294 {
295   surf_model_exit(surf_workstation_model);
296   surf_workstation_model = NULL;
297 }
298
299
300 static storage_t find_storage_on_mount_list(void *workstation,const char* storage)
301 {
302   storage_t st = NULL;
303   s_mount_t mnt;
304   unsigned int cursor;
305   workstation_VM2013_t ws = (workstation_VM2013_t) surf_workstation_resource_priv(workstation);
306   xbt_dynar_t storage_list = ws->storage;
307
308   XBT_DEBUG("Search for storage name '%s' on '%s'",storage,ws->generic_resource.name);
309   xbt_dynar_foreach(storage_list,cursor,mnt)
310   {
311     XBT_DEBUG("See '%s'",mnt.name);
312     if(!strcmp(storage,mnt.name)){
313       st = mnt.id;
314       break;
315     }
316   }
317   if(!st) xbt_die("Can't find mount '%s' for '%s'",storage,ws->generic_resource.name);
318   return st;
319 }
320
321 static surf_action_t ws_action_open(void *workstation, const char* mount, const char* path, const char* mode)
322 {
323   storage_t st = find_storage_on_mount_list(workstation, mount);
324   XBT_DEBUG("OPEN on disk '%s'",st->generic_resource.name);
325   surf_model_t model = st->generic_resource.model;
326   return model->extension.storage.open(st, mount, path, mode);
327 }
328
329 static surf_action_t ws_action_close(void *workstation, surf_file_t fp)
330 {
331   storage_t st = find_storage_on_mount_list(workstation, fp->storage);
332   XBT_DEBUG("CLOSE on disk '%s'",st->generic_resource.name);
333   surf_model_t model = st->generic_resource.model;
334   return model->extension.storage.close(st, fp);
335 }
336
337 static surf_action_t ws_action_read(void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
338 {
339   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
340   XBT_DEBUG("READ on disk '%s'",st->generic_resource.name);
341   surf_model_t model = st->generic_resource.model;
342   return model->extension.storage.read(st, ptr, (double)size, nmemb, stream);
343 }
344
345 static surf_action_t ws_action_write(void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
346 {
347   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
348   XBT_DEBUG("WRITE on disk '%s'",st->generic_resource.name);
349   surf_model_t model = st->generic_resource.model;
350   return model->extension.storage.write(st,  ptr, size, nmemb, stream);
351 }
352
353 static surf_action_t ws_action_stat(void *workstation, surf_file_t stream)
354 {
355   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
356   XBT_DEBUG("STAT on disk '%s'",st->generic_resource.name);
357   surf_model_t model = st->generic_resource.model;
358   return model->extension.storage.stat(st,  stream);
359 }
360
361 static surf_action_t ws_action_unlink(void *workstation, surf_file_t stream)
362 {
363   storage_t st = find_storage_on_mount_list(workstation, stream->storage);
364   XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name);
365   surf_model_t model = st->generic_resource.model;
366   return model->extension.storage.unlink(st,  stream);
367 }
368
369 static surf_action_t ws_action_ls(void *workstation, const char* mount, const char *path)
370 {
371   XBT_DEBUG("LS on mount '%s' and file '%s'",mount, path);
372   storage_t st = find_storage_on_mount_list(workstation, mount);
373   surf_model_t model = st->generic_resource.model;
374   return model->extension.storage.ls(st, path);
375 }
376
377 static void surf_vm_workstation_model_init_internal(void)
378 {
379   // TODO Adrien, who is calling sufr_model_init() and Why ?
380   surf_vm_workstation_model = surf_model_init();
381
382   surf_vm_workstation_model->name = "Virtual Workstation";
383   surf_vm_workstation_model->action_unref = ws_action_unref;
384   surf_vm_workstation_model->action_cancel = ws_action_cancel;
385   surf_vm_workstation_model->action_state_set = ws_action_state_set;
386
387   surf_vm_workstation_model->model_private->resource_used = ws_resource_used;
388   surf_vm_workstation_model->model_private->share_resources =
389       ws_share_resources;
390   surf_vm_workstation_model->model_private->update_actions_state =
391       ws_update_actions_state;
392   surf_vm_workstation_model->model_private->update_resource_state =
393       ws_update_resource_state;
394   surf_vm_workstation_model->model_private->finalize = ws_finalize;
395
396   surf_vm_workstation_model->suspend = ws_action_suspend;
397   surf_vm_workstation_model->resume = ws_action_resume;
398   surf_vm_workstation_model->is_suspended = ws_action_is_suspended;
399   surf_vm_workstation_model->set_max_duration = ws_action_set_max_duration;
400   surf_vm_workstation_model->set_priority = ws_action_set_priority;
401 #ifdef HAVE_TRACING
402   surf_vm_workstation_model->set_category = ws_action_set_category;
403 #endif
404   surf_vm_workstation_model->get_remains = ws_action_get_remains;
405 #ifdef HAVE_LATENCY_BOUND_TRACKING
406   surf_vm_workstation_model->get_latency_limited = ws_get_latency_limited;
407 #endif
408
409   surf_vm_workstation_model->extension.workstation.execute = ws_execute;
410   surf_vm_workstation_model->extension.workstation.sleep = ws_action_sleep;
411   surf_vm_workstation_model->extension.workstation.get_state = ws_get_state;
412   surf_vm_workstation_model->extension.workstation.get_speed = ws_get_speed;
413   surf_vm_workstation_model->extension.workstation.get_available_speed =
414       ws_get_available_speed;
415
416   surf_vm_workstation_model->extension.workstation.communicate =
417       ws_communicate;
418   surf_vm_workstation_model->extension.workstation.get_route = ws_get_route;
419   surf_vm_workstation_model->extension.workstation.execute_parallel_task =
420       ws_execute_parallel_task;
421   surf_vm_workstation_model->extension.workstation.get_link_bandwidth =
422       ws_get_link_bandwidth;
423   surf_vm_workstation_model->extension.workstation.get_link_latency =
424       ws_get_link_latency;
425   surf_vm_workstation_model->extension.workstation.link_shared =
426       ws_link_shared;
427   surf_vm_workstation_model->extension.workstation.get_properties =
428       surf_resource_properties; // Generic invocations
429
430   surf_vm_workstation_model->extension.workstation.open = ws_action_open;
431   surf_vm_workstation_model->extension.workstation.close = ws_action_close;
432   surf_vm_workstation_model->extension.workstation.read = ws_action_read;
433   surf_vm_workstation_model->extension.workstation.write = ws_action_write;
434   surf_vm_workstation_model->extension.workstation.stat = ws_action_stat;
435   surf_vm_workstation_model->extension.workstation.unlink = ws_action_unlink;
436   surf_vm_workstation_model->extension.workstation.ls = ws_action_ls;
437   surf_vm_workstation_model->extension.vm_workstation.create = vm_ws_create;
438
439 }
440
441 void surf_workstation_model_init_current_default(void)
442 {
443   surf_workstation_model_init_internal();
444   xbt_cfg_setdefault_int(_sg_cfg_set, "network/crosstraffic", 1);
445   surf_cpu_model_init_Cas01();
446   surf_network_model_init_LegrandVelho();
447
448   xbt_dynar_push(model_list, &surf_workstation_model);
449   sg_platf_host_add_cb(vm_ws_create);
450 //  sg_platf_postparse_add_cb(create_workstations);
451 }
452
453 void surf_vm_workstation_model_init()
454 {
455   surf_vm_workstation_model_init_internal();
456   xbt_dynar_push(model_list, &surf_vm_workstation_model);
457 }