Logo AND Algorithmique Numérique Distribuée

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