Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
386b548bdd2ad5d48aae0bb274766233df30aafb
[simgrid.git] / src / surf / workstation.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/dict.h"
9 #include "workstation_private.h"
10 #include "cpu_private.h"
11 #include "network_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(workstation, surf,
14                                 "Logging specific to the SURF workstation module");
15
16 surf_workstation_resource_t surf_workstation_resource = NULL;
17
18 xbt_dict_t workstation_set = NULL;
19
20 static workstation_CLM03_t workstation_new(const char *name,
21                                      void *cpu, void *card)
22 {
23   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
24
25   workstation->resource = (surf_resource_t) surf_workstation_resource;
26   workstation->name = xbt_strdup(name);
27   workstation->cpu = cpu;
28   workstation->network_card = card;
29
30   return workstation;
31 }
32
33 static void workstation_free(void *workstation)
34 {
35   free(((workstation_CLM03_t)workstation)->name);
36   free(workstation);
37 }
38
39 static void create_workstations(void)
40 {
41   xbt_dict_cursor_t cursor = NULL;
42   char *name = NULL;
43   void *cpu = NULL;
44   void *nw_card = NULL;
45
46   xbt_dict_foreach(cpu_set, cursor, name, cpu) {
47     nw_card = NULL;
48     xbt_dict_get(network_card_set, name, (void *) &nw_card);
49     xbt_assert1(nw_card, "No corresponding card found for %s",name);
50     xbt_dict_set(workstation_set, name,
51                  workstation_new(name, cpu, nw_card), workstation_free);
52   }
53 }
54
55 static void *name_service(const char *name)
56 {
57   void *workstation = NULL;
58
59   xbt_dict_get(workstation_set, name, &workstation);
60
61   return workstation;
62 }
63
64 static const char *get_resource_name(void *resource_id)
65 {
66   return ((workstation_CLM03_t) resource_id)->name;
67 }
68
69 static int resource_used(void *resource_id)
70 {
71   xbt_assert0(0,
72               "Workstation is a virtual resource. I should not be there!");
73   return 0;
74 }
75
76 static int action_free(surf_action_t action)
77 {
78   if(action->resource_type==(surf_resource_t)surf_network_resource) 
79     return surf_network_resource->common_public->action_free(action);
80   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
81     return surf_cpu_resource->common_public->action_free(action);
82   else DIE_IMPOSSIBLE;
83   return 0;
84 }
85
86
87 static void action_use(surf_action_t action)
88 {
89   if(action->resource_type==(surf_resource_t)surf_network_resource) 
90     surf_network_resource->common_public->action_use(action);
91   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
92     surf_cpu_resource->common_public->action_use(action);
93   else DIE_IMPOSSIBLE;
94   return;
95 }
96
97 static void action_cancel(surf_action_t action)
98 {
99   return;
100 }
101
102 static void action_recycle(surf_action_t action)
103 {
104   return;
105 }
106
107 static void action_change_state(surf_action_t action,
108                                 e_surf_action_state_t state)
109 {
110   if(action->resource_type==(surf_resource_t)surf_network_resource) 
111     surf_network_resource->common_public->action_change_state(action,state);
112   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
113     surf_cpu_resource->common_public->action_change_state(action,state);
114   else DIE_IMPOSSIBLE;
115   return;
116 }
117
118 static double share_resources(double now)
119 {
120   return -1.0;
121 }
122
123
124 static void update_actions_state(double now, double delta)
125 {
126   return;
127 }
128
129 static void update_resource_state(void *id,
130                                   tmgr_trace_event_t event_type,
131                                   double value)
132 {
133   return;
134 }
135
136 static surf_action_t execute(void *workstation, double size)
137 {
138   return surf_cpu_resource->extension_public->
139       execute(((workstation_CLM03_t) workstation)->cpu, size);
140 }
141
142 static surf_action_t action_sleep(void *workstation, double duration)
143 {
144   return surf_cpu_resource->extension_public->
145       sleep(((workstation_CLM03_t) workstation)->cpu, duration);
146 }
147
148 static void action_suspend(surf_action_t action)
149 {
150   if(action->resource_type==(surf_resource_t)surf_network_resource) 
151     surf_network_resource->common_public->suspend(action);
152   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
153     surf_cpu_resource->common_public->suspend(action);
154   else DIE_IMPOSSIBLE;
155 }
156
157 static void action_resume(surf_action_t action)
158 {
159   if(action->resource_type==(surf_resource_t)surf_network_resource)
160     surf_network_resource->common_public->resume(action);
161   else if(action->resource_type==(surf_resource_t)surf_cpu_resource)
162     surf_cpu_resource->common_public->resume(action);
163   else DIE_IMPOSSIBLE;
164 }
165
166 static int action_is_suspended(surf_action_t action)
167 {
168   if(action->resource_type==(surf_resource_t)surf_network_resource) 
169     return surf_network_resource->common_public->is_suspended(action);
170   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
171     return surf_cpu_resource->common_public->is_suspended(action);
172   DIE_IMPOSSIBLE;
173 }
174
175 static surf_action_t communicate(void *workstation_src,
176                                  void *workstation_dst, double size,
177                                  double rate)
178 {
179   return surf_network_resource->extension_public->
180       communicate(((workstation_CLM03_t) workstation_src)->network_card,
181                   ((workstation_CLM03_t) workstation_dst)->network_card, size, rate);
182 }
183
184 static e_surf_cpu_state_t get_state(void *workstation)
185 {
186   return surf_cpu_resource->extension_public->
187       get_state(((workstation_CLM03_t) workstation)->cpu);
188 }
189
190 static void finalize(void)
191 {
192   xbt_dict_free(&workstation_set);
193   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
194   xbt_swag_free(surf_workstation_resource->common_public->states.
195                 running_action_set);
196   xbt_swag_free(surf_workstation_resource->common_public->states.
197                 failed_action_set);
198   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
199
200   free(surf_workstation_resource->common_public);
201   free(surf_workstation_resource->common_private);
202   free(surf_workstation_resource->extension_public);
203
204   free(surf_workstation_resource);
205   surf_workstation_resource = NULL;
206 }
207
208 static void surf_workstation_resource_init_internal(void)
209 {
210   s_surf_action_t action;
211
212   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
213
214   surf_workstation_resource->common_private =
215       xbt_new0(s_surf_resource_private_t, 1);
216   surf_workstation_resource->common_public =
217       xbt_new0(s_surf_resource_public_t, 1);
218 /*   surf_workstation_resource->extension_private = xbt_new0(s_surf_workstation_resource_extension_private_t,1); */
219   surf_workstation_resource->extension_public =
220       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
221
222   surf_workstation_resource->common_public->states.ready_action_set =
223       xbt_swag_new(xbt_swag_offset(action, state_hookup));
224   surf_workstation_resource->common_public->states.running_action_set =
225       xbt_swag_new(xbt_swag_offset(action, state_hookup));
226   surf_workstation_resource->common_public->states.failed_action_set =
227       xbt_swag_new(xbt_swag_offset(action, state_hookup));
228   surf_workstation_resource->common_public->states.done_action_set =
229       xbt_swag_new(xbt_swag_offset(action, state_hookup));
230
231   surf_workstation_resource->common_public->name_service = name_service;
232   surf_workstation_resource->common_public->get_resource_name =
233       get_resource_name;
234   surf_workstation_resource->common_public->action_get_state =
235       surf_action_get_state;
236   surf_workstation_resource->common_public->action_free = action_free;
237   surf_workstation_resource->common_public->action_use = action_use;
238   surf_workstation_resource->common_public->action_cancel = action_cancel;
239   surf_workstation_resource->common_public->action_recycle =
240       action_recycle;
241   surf_workstation_resource->common_public->action_change_state =
242       action_change_state;
243   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
244   surf_workstation_resource->common_public->name = "Workstation";
245
246   surf_workstation_resource->common_private->resource_used = resource_used;
247   surf_workstation_resource->common_private->share_resources =
248       share_resources;
249   surf_workstation_resource->common_private->update_actions_state =
250       update_actions_state;
251   surf_workstation_resource->common_private->update_resource_state =
252       update_resource_state;
253   surf_workstation_resource->common_private->finalize = finalize;
254
255   surf_workstation_resource->common_public->suspend = action_suspend;
256   surf_workstation_resource->common_public->resume = action_resume;
257   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
258
259   surf_workstation_resource->extension_public->execute = execute;
260   surf_workstation_resource->extension_public->sleep = action_sleep;
261   surf_workstation_resource->extension_public->get_state = get_state;
262   surf_workstation_resource->extension_public->communicate = communicate;
263
264   workstation_set = xbt_dict_new();
265
266   xbt_assert0(maxmin_system, "surf_init has to be called first!");
267 }
268
269 /********************************************************************/
270 /* The model used in MSG and presented at CCGrid03                  */
271 /********************************************************************/
272 /* @InProceedings{Casanova.CLM_03, */
273 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
274 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
275 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
276 /*   publisher = {"IEEE Computer Society Press"}, */
277 /*   month = {may}, */
278 /*   year = {2003} */
279 /* } */
280 void surf_workstation_resource_init_CLM03(const char *filename)
281 {
282 /*   int i ; */
283 /*   surf_resource_t resource =  NULL; */
284
285   surf_workstation_resource_init_internal();
286   surf_cpu_resource_init_Cas01(filename);
287   surf_network_resource_init_CM02(filename);
288   create_workstations();
289   xbt_dynar_push(resource_list, &surf_workstation_resource);
290 /*   xbt_dynar_foreach(resource_list, i, resource) { */
291 /*     if(resource==surf_cpu_resource) { */
292 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
293 /*       i--;  */
294 /*       continue; */
295 /*     } */
296 /*     if(resource==surf_network_resource) { */
297 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
298 /*       i--;  */
299 /*       continue; */
300 /*     } */
301 /*   } */
302 }