Logo AND Algorithmique Numérique Distribuée

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