Logo AND Algorithmique Numérique Distribuée

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