Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
It worries me however, to realize how tough an ass-hole I have had to be, in
[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_t workstation_new(const char *name,
21                                      void *cpu, void *card)
22 {
23   workstation_t workstation = xbt_new0(s_workstation_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   xbt_free(((workstation_t)workstation)->name);
36   xbt_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_assert0(nw_card, "No corresponding card found");
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_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 void action_free(surf_action_t action)
77 {
78   return;
79 }
80
81 static void action_cancel(surf_action_t action)
82 {
83   return;
84 }
85
86 static void action_recycle(surf_action_t action)
87 {
88   return;
89 }
90
91 static void action_change_state(surf_action_t action,
92                                 e_surf_action_state_t state)
93 {
94   surf_action_change_state(action, state);
95   return;
96 }
97
98 static double share_resources(double now)
99 {
100   return -1.0;
101 }
102
103
104 static void update_actions_state(double now, double delta)
105 {
106   return;
107 }
108
109 static void update_resource_state(void *id,
110                                   tmgr_trace_event_t event_type,
111                                   double value)
112 {
113   return;
114 }
115
116 static surf_action_t execute(void *workstation, double size)
117 {
118   return surf_cpu_resource->extension_public->
119       execute(((workstation_t) workstation)->cpu, size);
120 }
121
122 static surf_action_t action_sleep(void *workstation, double duration)
123 {
124   return surf_cpu_resource->extension_public->
125       sleep(((workstation_t) workstation)->cpu, duration);
126 }
127
128 static void action_suspend(surf_action_t action)
129 {
130   xbt_assert0(action->resource_type ==
131               ((surf_resource_t) surf_cpu_resource),
132               "Resource type mismatch");
133   surf_cpu_resource->extension_public->suspend(action);
134 }
135
136 static void action_resume(surf_action_t action)
137 {
138   xbt_assert0(action->resource_type ==
139               ((surf_resource_t) surf_cpu_resource),
140               "Resource type mismatch");
141   surf_cpu_resource->extension_public->resume(action);
142 }
143
144 static int action_is_suspended(surf_action_t action)
145 {
146   if(action->resource_type==(surf_resource_t)surf_network_resource) 
147     return 0;
148   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
149     return surf_cpu_resource->extension_public->is_suspended(action);
150   DIE_IMPOSSIBLE;
151 }
152
153 static surf_action_t communicate(void *workstation_src,
154                                  void *workstation_dst, double size)
155 {
156   return surf_network_resource->extension_public->
157       communicate(((workstation_t) workstation_src)->network_card,
158                   ((workstation_t) workstation_dst)->network_card, size);
159 }
160
161 static e_surf_cpu_state_t get_state(void *workstation)
162 {
163   return surf_cpu_resource->extension_public->
164       get_state(((workstation_t) workstation)->cpu);
165 }
166
167 static void finalize(void)
168 {
169   xbt_dict_free(&workstation_set);
170   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
171   xbt_swag_free(surf_workstation_resource->common_public->states.
172                 running_action_set);
173   xbt_swag_free(surf_workstation_resource->common_public->states.
174                 failed_action_set);
175   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
176
177   xbt_free(surf_workstation_resource->common_public);
178   xbt_free(surf_workstation_resource->common_private);
179   xbt_free(surf_workstation_resource->extension_public);
180
181   xbt_free(surf_workstation_resource);
182   surf_workstation_resource = NULL;
183 }
184
185 static void surf_workstation_resource_init_internal(void)
186 {
187   s_surf_action_t action;
188
189   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
190
191   surf_workstation_resource->common_private =
192       xbt_new0(s_surf_resource_private_t, 1);
193   surf_workstation_resource->common_public =
194       xbt_new0(s_surf_resource_public_t, 1);
195 /*   surf_workstation_resource->extension_private = xbt_new0(s_surf_workstation_resource_extension_private_t,1); */
196   surf_workstation_resource->extension_public =
197       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
198
199   surf_workstation_resource->common_public->states.ready_action_set =
200       xbt_swag_new(xbt_swag_offset(action, state_hookup));
201   surf_workstation_resource->common_public->states.running_action_set =
202       xbt_swag_new(xbt_swag_offset(action, state_hookup));
203   surf_workstation_resource->common_public->states.failed_action_set =
204       xbt_swag_new(xbt_swag_offset(action, state_hookup));
205   surf_workstation_resource->common_public->states.done_action_set =
206       xbt_swag_new(xbt_swag_offset(action, state_hookup));
207
208   surf_workstation_resource->common_public->name_service = name_service;
209   surf_workstation_resource->common_public->get_resource_name =
210       get_resource_name;
211   surf_workstation_resource->common_public->action_get_state =
212       surf_action_get_state;
213   surf_workstation_resource->common_public->action_free = action_free;
214   surf_workstation_resource->common_public->action_cancel = action_cancel;
215   surf_workstation_resource->common_public->action_recycle =
216       action_recycle;
217   surf_workstation_resource->common_public->action_change_state =
218       action_change_state;
219   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
220   surf_workstation_resource->common_public->name = "Workstation";
221
222   surf_workstation_resource->common_private->resource_used = resource_used;
223   surf_workstation_resource->common_private->share_resources =
224       share_resources;
225   surf_workstation_resource->common_private->update_actions_state =
226       update_actions_state;
227   surf_workstation_resource->common_private->update_resource_state =
228       update_resource_state;
229   surf_workstation_resource->common_private->finalize = finalize;
230
231   surf_workstation_resource->extension_public->execute = execute;
232   surf_workstation_resource->extension_public->sleep = action_sleep;
233   surf_workstation_resource->extension_public->suspend = action_suspend;
234   surf_workstation_resource->extension_public->resume = action_resume;
235   surf_workstation_resource->extension_public->is_suspended = action_is_suspended;
236   surf_workstation_resource->extension_public->get_state = get_state;
237   surf_workstation_resource->extension_public->communicate = communicate;
238
239   workstation_set = xbt_dict_new();
240
241   xbt_assert0(maxmin_system, "surf_init has to be called first!");
242 }
243
244 /********************************************************************/
245 /* The model used in MSG and presented at CCGrid03                  */
246 /********************************************************************/
247 /* @InProceedings{Casanova.CLM_03, */
248 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
249 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
250 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
251 /*   publisher = {"IEEE Computer Society Press"}, */
252 /*   month = {may}, */
253 /*   year = {2003} */
254 /* } */
255 void surf_workstation_resource_init_CLM03(const char *filename)
256 {
257 /*   int i ; */
258 /*   surf_resource_t resource =  NULL; */
259
260   surf_workstation_resource_init_internal();
261   surf_cpu_resource_init_Cas01(filename);
262   surf_network_resource_init_CM02(filename);
263   create_workstations();
264   xbt_dynar_push(resource_list, &surf_workstation_resource);
265 /*   xbt_dynar_foreach(resource_list, i, resource) { */
266 /*     if(resource==surf_cpu_resource) { */
267 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
268 /*       i--;  */
269 /*       continue; */
270 /*     } */
271 /*     if(resource==surf_network_resource) { */
272 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
273 /*       i--;  */
274 /*       continue; */
275 /*     } */
276 /*   } */
277 }