Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed name inconsistency, etc.
[simgrid.git] / src / surf / network_gtnets.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 Henri Casanova. 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 "network_gtnets_private.h"
9 #include "gtnets/gtnets_interface.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network_gtnets);
12
13 /* surf_network_resource_t surf_network_resource = NULL; */
14 /*static xbt_dict_t network_link_set = NULL;*/
15
16 /* xbt_dict_t network_card_set = NULL; */
17
18 #if 0
19 static int card_number = 0;
20 static network_link_GTNETS_t **routing_table = NULL;
21 static int *routing_table_size = NULL;
22
23 #define ROUTE(i,j) routing_table[(i)+(j)*card_number]
24 #define ROUTE_SIZE(i,j) routing_table_size[(i)+(j)*card_number]
25 #endif
26
27 /** QUESTIONS for GTNetS integration
28  **   1. Check that we did the right thing with name_service and get_resource_name
29  **   2. Right now there is no "kill flow" in our GTNetS implementation. Do we
30  **      need to do something about this?
31  **   3. We ignore the fact there is some max_duration on flows (see #2 above)
32  **   4. share_resources() returns a duration, not a date, right?
33  **   5. We don't suppoer "rates"
34  **   6. We don't update "remaining" for ongoing flows. Is it bad?
35  **/
36
37 /* Free memory for a network link */
38 static void network_link_free(void *nw_link)
39 {
40   free(((network_link_GTNETS_t)nw_link)->name);
41   free(nw_link);
42 }
43
44 /* Instantiate a new network link */
45 /* name: some name for the link, from the XML */
46 /* bw: The bandwidth value            */
47 /* lat: The latency value             */
48 static void network_link_new(char *name,
49                              double bw,
50                              double lat)
51 {
52   static int link_count=-1;
53   network_link_GTNETS_t gtnets_link;
54
55   /* KF: Check that the link wasn't added before */
56   if (xbt_dict_get_or_null(network_link_set, name)) {
57     return;
58   }
59
60   /* KF: Increment the link counter for GTNetS */
61   link_count++;
62
63 /*
64   nw_link->resource = (surf_resource_t) surf_network_resource;
65   nw_link->name = name;
66   nw_link->bw_current = bw_initial;
67   if (bw_trace)
68     nw_link->bw_event =
69         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
70   nw_link->lat_current = lat_initial;
71   if (lat_trace)
72     nw_link->lat_event =
73         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
74   nw_link->state_current = state_initial;
75   if (state_trace)
76     nw_link->state_event =
77         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
78 */
79
80   /* KF: Add the link to the GTNetS simulation */
81   if (gtnets_add_link(link_count, bw, lat)) {
82     xbt_assert0(0,"Cannot create GTNetS link");
83   }
84
85   /* KF: Insert entry in the dictionary */
86   gtnets_link = xbt_new0(s_network_link_GTNETS_t,1);
87   gtnets_link->name = name;
88   gtnets_link->bw_current = bw;
89   gtnets_link->lat_current = lat;
90   gtnets_link->id = link_count;
91   xbt_dict_set(network_link_set, name, gtnets_link, network_link_free);
92
93   return;
94 }
95
96 /* free the network card */
97 static void network_card_free(void *nw_card)
98 {
99   free(((network_card_GTNETS_t)nw_card)->name);
100   free(nw_card);
101 }
102
103 /* Instantiate a new network card: MODIFYED BY KF */
104 static int network_card_new(const char *name)
105 {
106   static int card_count=-1;
107
108   /* KF: Check that we haven't seen the network card before */ 
109   network_card_GTNETS_t card = xbt_dict_get_or_null(network_card_set, name);
110
111   if (!card){
112     /* KF: Increment the card counter for GTNetS */
113     card_count++;
114
115     /* KF: just use the dictionary to map link names to link indices */
116     card = xbt_new0(s_network_card_GTNETS_t,1);
117     card->name = xbt_strdup(name);
118     card->id = card_count;
119     xbt_dict_set(network_card_set, name, card, network_card_free);
120   }
121
122   /* KF: just return the GTNetS ID as the SURF ID */
123   return card->id;
124 }
125
126 /* Instantiate a new route: MODIFY BY KF */
127 static void route_new(int src_id, int dst_id, char **links, int nb_link)
128 {
129 #if 0
130   network_link_GTNETS_t *link_list = NULL;
131   int i;
132
133   ROUTE_SIZE(src_id, dst_id) = nb_link;
134   link_list = (ROUTE(src_id, dst_id) = xbt_new0(network_link_GTNETS_t, nb_link));
135   for (i = 0; i < nb_link; i++) {
136     link_list[i] = xbt_dict_get_or_null(network_link_set, links[i]);
137     free(links[i]);
138   }
139   free(links);
140 #endif
141   int i;
142   int *gtnets_links;
143  
144   /* KF: Build the list of gtnets link IDs */
145   gtnets_links = (int *)calloc(nb_link, sizeof(int));
146   for (i=0; i<nb_link; i++) {
147     gtnets_links[i]=(int)(xbt_dict_get(network_link_set, links[i]));
148   }
149
150   /* KF: Create the GTNets route */
151   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
152     xbt_assert0(0,"Cannot create GTNetS route");
153   }
154 }
155
156 /* Parse the XML for a network link */
157 static void parse_network_link(void)
158 {
159   char *name;
160   double bw;
161   double lat;
162   e_surf_network_link_state_t state;
163
164   name = xbt_strdup(A_surfxml_network_link_name);
165   surf_parse_get_double(&bw,A_surfxml_network_link_bandwidth);
166   surf_parse_get_double(&lat,A_surfxml_network_link_latency);
167   state = SURF_NETWORK_LINK_ON;
168
169   /* Print values when no traces are specified */
170   {
171     tmgr_trace_t bw_trace;
172     tmgr_trace_t state_trace;
173     tmgr_trace_t lat_trace;
174
175     surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
176     surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
177     surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
178
179     /*TODO Where is WARNING0 defined???*/
180 #if 0  
181     if (bw_trace) 
182       WARNING0("The GTNetS network model doesn't support bandwidth state traces");
183     if (lat_trace)
184       WARNING0("The GTNetS network model doesn't support latency state traces");
185     if (state_trace)
186       WARNING0("The GTNetS network model doesn't support link state traces");
187 #endif
188   }
189
190
191   /* KF: remove several arguments to network_link_new */
192   network_link_new(name, bw, lat);
193 }
194
195 static int nb_link = 0;
196 static char **link_name = NULL;
197 static int src_id = -1;
198 static int dst_id = -1;
199
200 /* Parses a route from the XML: UNMODIFIED BY HC */
201 static void parse_route_set_endpoints(void)
202 {
203   src_id = network_card_new(A_surfxml_route_src);
204   dst_id = network_card_new(A_surfxml_route_dst);
205   nb_link = 0;
206   link_name = NULL;
207 }
208
209 /* Parses a route element from the XML: UNMODIFIED BY HC */
210 static void parse_route_elem(void)
211 {
212   nb_link++;
213   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
214   link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
215 }
216
217 /* Create the route: UNMODIFIED BY HC */
218 static void parse_route_set_route(void)
219 {
220   route_new(src_id, dst_id, link_name, nb_link);
221 }
222
223 /* Main XML parsing */
224 static void parse_file(const char *file)
225 {
226   /* Figuring out the network links */
227   surf_parse_reset_parser();
228   ETag_surfxml_network_link_fun=parse_network_link;
229   surf_parse_open(file);
230   xbt_assert1((!surf_parse()),"Parse error in %s",file);
231   surf_parse_close();
232
233   /* Figuring out the network cards used */
234   surf_parse_reset_parser();
235   STag_surfxml_route_fun=parse_route_set_endpoints;
236   surf_parse_open(file);
237   xbt_assert1((!surf_parse()),"Parse error in %s",file);
238   surf_parse_close();
239
240   /* Building the routes */
241   surf_parse_reset_parser();
242   STag_surfxml_route_fun=parse_route_set_endpoints;
243   ETag_surfxml_route_element_fun=parse_route_elem;
244   ETag_surfxml_route_fun=parse_route_set_route;
245   surf_parse_open(file);
246   xbt_assert1((!surf_parse()),"Parse error in %s",file);
247   surf_parse_close();
248 }
249
250 static void *name_service(const char *name)
251 {
252   return xbt_dict_get_or_null(network_card_set, name);
253 }
254
255 static const char *get_resource_name(void *resource_id)
256 {
257   return ((network_card_GTNETS_t) resource_id)->name;
258 }
259
260 /* We do not care about this: only used for traces */
261 static int resource_used(void *resource_id)
262 {
263   return 0; /* We don't care */
264 }
265
266 static int action_free(surf_action_t action)
267 {
268   action->using--;
269   if(!action->using) {
270     xbt_swag_remove(action, action->state_set);
271     /* KF: No explicit freeing needed for GTNeTS here */
272     free(action);
273   }
274   return 1;
275 }
276
277 static void action_use(surf_action_t action)
278 {
279   action->using++;
280 }
281
282 static void action_cancel(surf_action_t action)
283 {
284   xbt_assert0(0,"Cannot cancel GTNetS flow");
285   return;
286 }
287
288 static void action_recycle(surf_action_t action)
289 {
290   xbt_assert0(0,"Cannot recycle GTNetS flow");
291   return;
292 }
293
294 static void action_change_state(surf_action_t action,
295                                 e_surf_action_state_t state)
296 {
297 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
298 /*     if(((surf_action_network_GTNETS_t)action)->variable) { */
299 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
300 /*       ((surf_action_network_GTNETS_t)action)->variable = NULL; */
301 /*     } */
302
303   surf_action_change_state(action, state);
304   return;
305 }
306
307
308 /* share_resources() */
309 static double share_resources(double now)
310 {
311 #if 0
312   s_surf_action_network_GTNETS_t s_action;
313   surf_action_network_GTNETS_t action = NULL;
314   xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
315 #endif
316
317   return gtnets_get_time_to_next_flow_completion();
318 }
319
320 /* delta: by how many time units the simulation must advance */
321 /* In this function: change the state of actions that terminate */
322 /* The delta may not come from the network, and thus may be different (smaller) 
323    than the one returned by the function above */
324 /* If the delta is a network-caused min, then do not emulate any timer in the
325    network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
326
327 static void update_actions_state(double now, double delta)
328 {
329 #if 0
330   surf_action_network_GTNETS_t action = NULL;
331   surf_action_network_GTNETS_t next_action = NULL;
332   xbt_swag_t running_actions =
333       surf_network_resource->common_public->states.running_action_set;
334 #endif
335
336   double time_to_next_flow_completion =  gtnets_get_time_to_next_flow_completion();
337   
338   /* If there are no renning flows, just return */
339   if (time_to_next_flow_completion < 0.0) {
340     return;
341   }
342
343   if (time_to_next_flow_completion < delta) { /* run until the first flow completes */
344     void **metadata; 
345     int i,num_flows;
346
347     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
348       xbt_assert0(0,"Cannot run GTNetS simulation until next flow completion");
349     }
350     if (num_flows < 1) {
351       xbt_assert0(0,"GTNetS simulation couldn't find a flow that would complete");
352     }
353
354     for (i=0; i<num_flows; i++) {
355       surf_action_network_GTNETS_t action = 
356         (surf_action_network_GTNETS_t)(metadata[i]);
357
358       action->generic_action.remains = 0;
359       action->generic_action.finish =  now + time_to_next_flow_completion;
360       action->generic_action.finish =  SURF_ACTION_DONE;
361       /* TODO: Anything else here? */
362     }
363   } else { /* run for a given number of seconds */
364     if (gtnets_run(delta)) {
365       xbt_assert0(0,"Cannot run GTNetS simulation");
366     }
367   }
368   
369   return;
370 }
371
372 /* UNUSED HERE: no traces */
373 static void update_resource_state(void *id,
374                                   tmgr_trace_event_t event_type,
375                                   double value)
376 {
377   xbt_assert0(0,"Cannot update resource state for GTNetS simulation");
378   return;
379 }
380
381 /* KF: Rate not supported */
382 static surf_action_t communicate(void *src, void *dst, double size, double rate)
383 {
384   surf_action_network_GTNETS_t action = NULL;
385   network_card_GTNETS_t card_src = src;
386   network_card_GTNETS_t card_dst = dst;
387 /*
388   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
389   network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
390 */
391
392 /*
393   xbt_assert2(route_size,"You're trying to send data from %s to %s but there is no connexion between these two cards.", card_src->name, card_dst->name);
394 */
395
396   action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
397
398   action->generic_action.using = 1; 
399   action->generic_action.cost = size;
400   action->generic_action.remains = size;
401   /* Max durations are not supported */
402   action->generic_action.max_duration = NO_MAX_DURATION;
403   action->generic_action.start = surf_get_clock(); 
404   action->generic_action.finish = -1.0; 
405   action->generic_action.resource_type =
406       (surf_resource_t) surf_network_resource;
407
408   action->generic_action.state_set =
409       surf_network_resource->common_public->states.running_action_set;
410
411   xbt_swag_insert(action, action->generic_action.state_set);
412
413   /* KF: Add a flow to the GTNets Simulation, associated to this action */
414   if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *)action) < 0) {
415     xbt_assert2(0,"Not route between host %s and host %s", card_src->name, card_dst->name);
416   }
417
418   return (surf_action_t) action;
419 }
420
421 /* Suspend a flow() */
422 static void action_suspend(surf_action_t action)
423 {
424   xbt_assert0(0,"action_suspend() not supported for the GTNets network model");
425 }
426
427 /* Resume a flow() */
428 static void action_resume(surf_action_t action)
429 {
430   xbt_assert0(0,"action_resume() not supported for the GTNets network model");
431 }
432
433 /* Test whether a flow is suspended */
434 static int action_is_suspended(surf_action_t action)
435 {
436   return 0;
437 }
438
439 static void finalize(void)
440 {
441 #if 0
442   int i,j;
443 #endif
444   xbt_dict_free(&network_card_set);
445   xbt_dict_free(&network_link_set);
446   xbt_swag_free(surf_network_resource->common_public->states.
447                 ready_action_set);
448   xbt_swag_free(surf_network_resource->common_public->states.
449                 running_action_set);
450   xbt_swag_free(surf_network_resource->common_public->states.
451                 failed_action_set);
452   xbt_swag_free(surf_network_resource->common_public->states.
453                 done_action_set);
454   free(surf_network_resource->common_public);
455   free(surf_network_resource->common_private);
456   free(surf_network_resource->extension_public);
457
458   free(surf_network_resource);
459   surf_network_resource = NULL;
460
461 #if 0
462   for (i = 0; i < card_number; i++) 
463     for (j = 0; j < card_number; j++) 
464       free(ROUTE(i,j));
465   free(routing_table);
466   routing_table = NULL;
467   free(routing_table_size);
468   routing_table_size = NULL;
469   card_number = 0;
470 #endif
471
472   /* ADDED BY KF */
473   gtnets_finalize();
474   /* END ADDITION */
475 }
476
477 static void surf_network_resource_init_internal(void)
478 {
479   s_surf_action_t action;
480
481   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
482
483   surf_network_resource->common_private =
484       xbt_new0(s_surf_resource_private_t, 1);
485   surf_network_resource->common_public =
486       xbt_new0(s_surf_resource_public_t, 1);
487   surf_network_resource->extension_public =
488       xbt_new0(s_surf_network_resource_extension_public_t, 1);
489
490   surf_network_resource->common_public->states.ready_action_set =
491       xbt_swag_new(xbt_swag_offset(action, state_hookup));
492   surf_network_resource->common_public->states.running_action_set =
493       xbt_swag_new(xbt_swag_offset(action, state_hookup));
494   surf_network_resource->common_public->states.failed_action_set =
495       xbt_swag_new(xbt_swag_offset(action, state_hookup));
496   surf_network_resource->common_public->states.done_action_set =
497       xbt_swag_new(xbt_swag_offset(action, state_hookup));
498
499   surf_network_resource->common_public->name_service = name_service;
500   surf_network_resource->common_public->get_resource_name =
501       get_resource_name;
502   surf_network_resource->common_public->action_get_state =
503       surf_action_get_state;
504   surf_network_resource->common_public->action_use = action_use;
505   surf_network_resource->common_public->action_free = action_free;
506   surf_network_resource->common_public->action_cancel = action_cancel;
507   surf_network_resource->common_public->action_recycle = action_recycle;
508   surf_network_resource->common_public->action_change_state =
509       action_change_state;
510   surf_network_resource->common_public->action_set_data = surf_action_set_data;
511   surf_network_resource->common_public->name = "network";
512
513   surf_network_resource->common_private->resource_used = resource_used;
514   surf_network_resource->common_private->share_resources = share_resources;
515   surf_network_resource->common_private->update_actions_state =
516       update_actions_state;
517   surf_network_resource->common_private->update_resource_state =
518       update_resource_state;
519   surf_network_resource->common_private->finalize = finalize;
520
521   surf_network_resource->common_public->suspend = action_suspend;
522   surf_network_resource->common_public->resume = action_resume;
523   surf_network_resource->common_public->is_suspended = action_is_suspended;
524
525   surf_network_resource->extension_public->communicate = communicate;
526
527   network_link_set = xbt_dict_new();
528   network_card_set = xbt_dict_new();
529
530   /* HC: I am assuming that this stays in for simulation of compute tasks */
531   xbt_assert0(maxmin_system, "surf_init has to be called first!");
532
533   /* KF: Added the initialization for GTNetS interface */
534   if (gtnets_initialize()) {
535     xbt_assert0(0, "impossible to initialize GTNetS interface");
536   }
537 }
538
539 /* HC: I put this prototype here for now but it will have to go in 
540    src/include/surf.h when it is functionnal. */
541 void surf_network_resource_init_GTNETS(const char *filename);
542
543 void surf_network_resource_init_GTNETS(const char *filename)
544 {
545   if (surf_network_resource)
546     return;
547   surf_network_resource_init_internal();
548   parse_file(filename);
549   xbt_dynar_push(resource_list, &surf_network_resource);
550 }