Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a5cb42af472117f1d021878d353d11d0702ca86
[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   if(action->resource_type==(surf_resource_t)surf_network_resource) 
131     surf_network_resource->extension_public->suspend(action);
132   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
133     surf_cpu_resource->extension_public->suspend(action);
134   else DIE_IMPOSSIBLE;
135 }
136
137 static void action_resume(surf_action_t action)
138 {
139   if(action->resource_type==(surf_resource_t)surf_network_resource)
140     surf_network_resource->extension_public->resume(action);
141   else if(action->resource_type==(surf_resource_t)surf_cpu_resource)
142     surf_cpu_resource->extension_public->resume(action);
143   else DIE_IMPOSSIBLE;
144 }
145
146 static int action_is_suspended(surf_action_t action)
147 {
148   if(action->resource_type==(surf_resource_t)surf_network_resource) 
149     return surf_network_resource->extension_public->is_suspended(action);
150   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
151     return surf_cpu_resource->extension_public->is_suspended(action);
152   DIE_IMPOSSIBLE;
153 }
154
155 static surf_action_t communicate(void *workstation_src,
156                                  void *workstation_dst, double size)
157 {
158   return surf_network_resource->extension_public->
159       communicate(((workstation_t) workstation_src)->network_card,
160                   ((workstation_t) workstation_dst)->network_card, size);
161 }
162
163 static e_surf_cpu_state_t get_state(void *workstation)
164 {
165   return surf_cpu_resource->extension_public->
166       get_state(((workstation_t) workstation)->cpu);
167 }
168
169 static void finalize(void)
170 {
171   xbt_dict_free(&workstation_set);
172   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
173   xbt_swag_free(surf_workstation_resource->common_public->states.
174                 running_action_set);
175   xbt_swag_free(surf_workstation_resource->common_public->states.
176                 failed_action_set);
177   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
178
179   xbt_free(surf_workstation_resource->common_public);
180   xbt_free(surf_workstation_resource->common_private);
181   xbt_free(surf_workstation_resource->extension_public);
182
183   xbt_free(surf_workstation_resource);
184   surf_workstation_resource = NULL;
185 }
186
187 static void surf_workstation_resource_init_internal(void)
188 {
189   s_surf_action_t action;
190
191   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
192
193   surf_workstation_resource->common_private =
194       xbt_new0(s_surf_resource_private_t, 1);
195   surf_workstation_resource->common_public =
196       xbt_new0(s_surf_resource_public_t, 1);
197 /*   surf_workstation_resource->extension_private = xbt_new0(s_surf_workstation_resource_extension_private_t,1); */
198   surf_workstation_resource->extension_public =
199       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
200
201   surf_workstation_resource->common_public->states.ready_action_set =
202       xbt_swag_new(xbt_swag_offset(action, state_hookup));
203   surf_workstation_resource->common_public->states.running_action_set =
204       xbt_swag_new(xbt_swag_offset(action, state_hookup));
205   surf_workstation_resource->common_public->states.failed_action_set =
206       xbt_swag_new(xbt_swag_offset(action, state_hookup));
207   surf_workstation_resource->common_public->states.done_action_set =
208       xbt_swag_new(xbt_swag_offset(action, state_hookup));
209
210   surf_workstation_resource->common_public->name_service = name_service;
211   surf_workstation_resource->common_public->get_resource_name =
212       get_resource_name;
213   surf_workstation_resource->common_public->action_get_state =
214       surf_action_get_state;
215   surf_workstation_resource->common_public->action_free = action_free;
216   surf_workstation_resource->common_public->action_cancel = action_cancel;
217   surf_workstation_resource->common_public->action_recycle =
218       action_recycle;
219   surf_workstation_resource->common_public->action_change_state =
220       action_change_state;
221   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
222   surf_workstation_resource->common_public->name = "Workstation";
223
224   surf_workstation_resource->common_private->resource_used = resource_used;
225   surf_workstation_resource->common_private->share_resources =
226       share_resources;
227   surf_workstation_resource->common_private->update_actions_state =
228       update_actions_state;
229   surf_workstation_resource->common_private->update_resource_state =
230       update_resource_state;
231   surf_workstation_resource->common_private->finalize = finalize;
232
233   surf_workstation_resource->extension_public->execute = execute;
234   surf_workstation_resource->extension_public->sleep = action_sleep;
235   surf_workstation_resource->extension_public->suspend = action_suspend;
236   surf_workstation_resource->extension_public->resume = action_resume;
237   surf_workstation_resource->extension_public->is_suspended = action_is_suspended;
238   surf_workstation_resource->extension_public->get_state = get_state;
239   surf_workstation_resource->extension_public->communicate = communicate;
240
241   workstation_set = xbt_dict_new();
242
243   xbt_assert0(maxmin_system, "surf_init has to be called first!");
244 }
245
246 /********************************************************************/
247 /* The model used in MSG and presented at CCGrid03                  */
248 /********************************************************************/
249 /* @InProceedings{Casanova.CLM_03, */
250 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
251 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
252 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
253 /*   publisher = {"IEEE Computer Society Press"}, */
254 /*   month = {may}, */
255 /*   year = {2003} */
256 /* } */
257 void surf_workstation_resource_init_CLM03(const char *filename)
258 {
259 /*   int i ; */
260 /*   surf_resource_t resource =  NULL; */
261
262   surf_workstation_resource_init_internal();
263   surf_cpu_resource_init_Cas01(filename);
264   surf_network_resource_init_CM02(filename);
265   create_workstations();
266   xbt_dynar_push(resource_list, &surf_workstation_resource);
267 /*   xbt_dynar_foreach(resource_list, i, resource) { */
268 /*     if(resource==surf_cpu_resource) { */
269 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
270 /*       i--;  */
271 /*       continue; */
272 /*     } */
273 /*     if(resource==surf_network_resource) { */
274 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
275 /*       i--;  */
276 /*       continue; */
277 /*     } */
278 /*   } */
279 }