Logo AND Algorithmique Numérique Distribuée

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