Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5fffbe0edf738ac2f19a8a5d489d2f669c43ff0d
[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]=
148       ((network_link_GTNETS_t)(xbt_dict_get(network_link_set, links[i])))->id;
149   }
150
151   /* KF: Create the GTNets route */
152   if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
153     xbt_assert0(0,"Cannot create GTNetS route");
154   }
155 }
156
157 /* Instantiate a new route: MODIFY BY KF */
158 static void route_onehop_new(int src_id, int dst_id, char **links, int nb_link)
159 {
160   int linkid;
161
162   if (nb_link != 1){
163     xbt_assert0(0, "In onehop_new, nb_link should be 1");
164   }
165
166   /* KF: Build the list of gtnets link IDs */
167   linkid = 
168       ((network_link_GTNETS_t)(xbt_dict_get(network_link_set, links[0])))->id;
169
170   /* KF: Create the GTNets route */
171   if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
172     xbt_assert0(0,"Cannot create GTNetS route");
173   }
174 }
175
176 /* Parse the XML for a network link */
177 static void parse_network_link(void)
178 {
179   char *name;
180   double bw;
181   double lat;
182   e_surf_network_link_state_t state;
183
184   name = xbt_strdup(A_surfxml_network_link_name);
185   surf_parse_get_double(&bw,A_surfxml_network_link_bandwidth);
186   surf_parse_get_double(&lat,A_surfxml_network_link_latency);
187   state = SURF_NETWORK_LINK_ON;
188
189   /* Print values when no traces are specified */
190   {
191     tmgr_trace_t bw_trace;
192     tmgr_trace_t state_trace;
193     tmgr_trace_t lat_trace;
194
195     surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
196     surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
197     surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
198
199     /*TODO Where is WARNING0 defined???*/
200 #if 0  
201     if (bw_trace) 
202       WARNING0("The GTNetS network model doesn't support bandwidth state traces");
203     if (lat_trace)
204       WARNING0("The GTNetS network model doesn't support latency state traces");
205     if (state_trace)
206       WARNING0("The GTNetS network model doesn't support link state traces");
207 #endif
208   }
209
210
211   /* KF: remove several arguments to network_link_new */
212   network_link_new(name, bw, lat);
213 }
214
215 static int nb_link = 0;
216 static char **link_name = NULL;
217 static int src_id = -1;
218 static int dst_id = -1;
219
220 /* Parses a route from the XML: UNMODIFIED BY HC */
221 static void parse_route_set_endpoints(void)
222 {
223   src_id = network_card_new(A_surfxml_route_src);
224   dst_id = network_card_new(A_surfxml_route_dst);
225   nb_link = 0;
226   link_name = NULL;
227 }
228
229 /* KF*/
230 static void parse_route_set_routers(void)
231 {
232   int id = network_card_new(A_surfxml_router_name);
233
234   /* KF: Create the GTNets router */
235   if (gtnets_add_router(id)) {
236     xbt_assert0(0,"Cannot add GTNetS router");
237   }
238 }
239
240 /* Parses a route element from the XML: UNMODIFIED BY HC */
241 static void parse_route_elem(void)
242 {
243   nb_link++;
244   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
245   link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
246 }
247
248 /* Create the route (more than one hops): MODIFIED BY KF */
249 static void parse_route_set_route(void)
250 {
251   if (nb_link > 1)
252     route_new(src_id, dst_id, link_name, nb_link);
253 }
254
255 /* Create the one-hope route: BY KF */
256 static void parse_route_set_onehop_route(void)
257 {
258   if (nb_link == 1)
259     route_onehop_new(src_id, dst_id, link_name, nb_link);
260 }
261
262 /* Main XML parsing */
263 static void parse_file(const char *file)
264 {
265   /* Figuring out the network links */
266   surf_parse_reset_parser();
267   ETag_surfxml_network_link_fun=parse_network_link;
268   surf_parse_open(file);
269   xbt_assert1((!surf_parse()),"Parse error in %s",file);
270   surf_parse_close();
271
272   /* Figuring out the network cards used */
273   /* KF
274   surf_parse_reset_parser();
275   STag_surfxml_route_fun=parse_route_set_endpoints;
276   surf_parse_open(file);
277   xbt_assert1((!surf_parse()),"Parse error in %s",file);
278   surf_parse_close();
279   */
280
281   /* KF: Figuring out the router (considered as part of
282      network cards) used.*/
283   surf_parse_reset_parser();
284   STag_surfxml_router_fun=parse_route_set_routers;
285   surf_parse_open(file);
286   xbt_assert1((!surf_parse()),"Parse error in %s",file);
287   surf_parse_close();  
288
289   /* Building the one-hop routes */
290   surf_parse_reset_parser();
291   STag_surfxml_route_fun=parse_route_set_endpoints;
292   ETag_surfxml_route_element_fun=parse_route_elem;
293   ETag_surfxml_route_fun=parse_route_set_onehop_route;
294   surf_parse_open(file);
295   xbt_assert1((!surf_parse()),"Parse error in %s",file);
296   surf_parse_close();
297
298   /* Building the routes */
299   surf_parse_reset_parser();
300   STag_surfxml_route_fun=parse_route_set_endpoints;
301   ETag_surfxml_route_element_fun=parse_route_elem;
302   ETag_surfxml_route_fun=parse_route_set_route;
303   surf_parse_open(file);
304   xbt_assert1((!surf_parse()),"Parse error in %s",file);
305   surf_parse_close();
306 }
307
308 static void *name_service(const char *name)
309 {
310   return xbt_dict_get_or_null(network_card_set, name);
311 }
312
313 static const char *get_resource_name(void *resource_id)
314 {
315   return ((network_card_GTNETS_t) resource_id)->name;
316 }
317
318 /* We do not care about this: only used for traces */
319 static int resource_used(void *resource_id)
320 {
321   return 0; /* We don't care */
322 }
323
324 static int action_free(surf_action_t action)
325 {
326   action->using--;
327   if(!action->using) {
328     xbt_swag_remove(action, action->state_set);
329     /* KF: No explicit freeing needed for GTNeTS here */
330     free(action);
331     return 1;
332   }
333   return 0;
334 }
335
336 static void action_use(surf_action_t action)
337 {
338   action->using++;
339 }
340
341 static void action_cancel(surf_action_t action)
342 {
343   xbt_assert0(0,"Cannot cancel GTNetS flow");
344   return;
345 }
346
347 static void action_recycle(surf_action_t action)
348 {
349   xbt_assert0(0,"Cannot recycle GTNetS flow");
350   return;
351 }
352
353 static void action_change_state(surf_action_t action,
354                                 e_surf_action_state_t state)
355 {
356 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
357 /*     if(((surf_action_network_GTNETS_t)action)->variable) { */
358 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
359 /*       ((surf_action_network_GTNETS_t)action)->variable = NULL; */
360 /*     } */
361
362   surf_action_change_state(action, state);
363   return;
364 }
365
366
367 /* share_resources() */
368 static double share_resources(double now)
369 {
370 #if 0
371   s_surf_action_network_GTNETS_t s_action;
372   surf_action_network_GTNETS_t action = NULL;
373   xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
374 #endif
375
376   return gtnets_get_time_to_next_flow_completion();
377 }
378
379 /* delta: by how many time units the simulation must advance */
380 /* In this function: change the state of actions that terminate */
381 /* The delta may not come from the network, and thus may be different (smaller) 
382    than the one returned by the function above */
383 /* If the delta is a network-caused min, then do not emulate any timer in the
384    network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
385
386 static void update_actions_state(double now, double delta)
387 {
388 #if 0
389   surf_action_network_GTNETS_t action = NULL;
390   surf_action_network_GTNETS_t next_action = NULL;
391   xbt_swag_t running_actions =
392       surf_network_resource->common_public->states.running_action_set;
393 #endif
394
395   double time_to_next_flow_completion =  gtnets_get_time_to_next_flow_completion();
396
397   /* If there are no renning flows, just return */
398   if (time_to_next_flow_completion < 0.0) {
399     return;
400   }
401
402   /*KF: if delta == time_to_next_flow_completion, too.*/
403   if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
404     void **metadata;
405     int i,num_flows;
406
407     num_flows = 0;
408
409     if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
410       xbt_assert0(0,"Cannot run GTNetS simulation until next flow completion");
411     }
412     if (num_flows < 1) {
413       xbt_assert0(0,"GTNetS simulation couldn't find a flow that would complete");
414     }
415
416     for (i=0; i<num_flows; i++) {
417       surf_action_network_GTNETS_t action = 
418         (surf_action_network_GTNETS_t)(metadata[i]);
419
420       action->generic_action.remains = 0;
421       action->generic_action.finish =  now + time_to_next_flow_completion;
422       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
423       /* TODO: Anything else here? */
424     }
425   } else { /* run for a given number of seconds */
426     if (gtnets_run(delta)) {
427       xbt_assert0(0,"Cannot run GTNetS simulation");
428     }
429   }
430   
431   return;
432 }
433
434 /* UNUSED HERE: no traces */
435 static void update_resource_state(void *id,
436                                   tmgr_trace_event_t event_type,
437                                   double value)
438 {
439   xbt_assert0(0,"Cannot update resource state for GTNetS simulation");
440   return;
441 }
442
443 /* KF: Rate not supported */
444 static surf_action_t communicate(void *src, void *dst, double size, double rate)
445 {
446   surf_action_network_GTNETS_t action = NULL;
447   network_card_GTNETS_t card_src = src;
448   network_card_GTNETS_t card_dst = dst;
449 /*
450   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
451   network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
452 */
453
454 /*
455   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);
456 */
457
458   action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
459
460   action->generic_action.using = 1; 
461   action->generic_action.cost = size;
462   action->generic_action.remains = size;
463   /* Max durations are not supported */
464   action->generic_action.max_duration = NO_MAX_DURATION;
465   action->generic_action.start = surf_get_clock(); 
466   action->generic_action.finish = -1.0; 
467   action->generic_action.resource_type =
468       (surf_resource_t) surf_network_resource;
469
470   action->generic_action.state_set =
471       surf_network_resource->common_public->states.running_action_set;
472
473   xbt_swag_insert(action, action->generic_action.state_set);
474
475   /* KF: Add a flow to the GTNets Simulation, associated to this action */
476   if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *)action) < 0) {
477     xbt_assert2(0,"Not route between host %s and host %s", card_src->name, card_dst->name);
478   }
479
480   return (surf_action_t) action;
481 }
482
483 /* Suspend a flow() */
484 static void action_suspend(surf_action_t action)
485 {
486   xbt_assert0(0,"action_suspend() not supported for the GTNets network model");
487 }
488
489 /* Resume a flow() */
490 static void action_resume(surf_action_t action)
491 {
492   xbt_assert0(0,"action_resume() not supported for the GTNets network model");
493 }
494
495 /* Test whether a flow is suspended */
496 static int action_is_suspended(surf_action_t action)
497 {
498   return 0;
499 }
500
501 static void finalize(void)
502 {
503 #if 0
504   int i,j;
505 #endif
506   xbt_dict_free(&network_card_set);
507   xbt_dict_free(&network_link_set);
508   xbt_swag_free(surf_network_resource->common_public->states.
509                 ready_action_set);
510   xbt_swag_free(surf_network_resource->common_public->states.
511                 running_action_set);
512   xbt_swag_free(surf_network_resource->common_public->states.
513                 failed_action_set);
514   xbt_swag_free(surf_network_resource->common_public->states.
515                 done_action_set);
516   free(surf_network_resource->common_public);
517   free(surf_network_resource->common_private);
518   free(surf_network_resource->extension_public);
519
520   free(surf_network_resource);
521   surf_network_resource = NULL;
522
523 #if 0
524   for (i = 0; i < card_number; i++) 
525     for (j = 0; j < card_number; j++) 
526       free(ROUTE(i,j));
527   free(routing_table);
528   routing_table = NULL;
529   free(routing_table_size);
530   routing_table_size = NULL;
531   card_number = 0;
532 #endif
533
534   /* ADDED BY KF */
535   gtnets_finalize();
536   /* END ADDITION */
537 }
538
539 static void surf_network_resource_init_internal(void)
540 {
541   s_surf_action_t action;
542
543   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
544
545   surf_network_resource->common_private =
546       xbt_new0(s_surf_resource_private_t, 1);
547   surf_network_resource->common_public =
548       xbt_new0(s_surf_resource_public_t, 1);
549   surf_network_resource->extension_public =
550       xbt_new0(s_surf_network_resource_extension_public_t, 1);
551
552   surf_network_resource->common_public->states.ready_action_set =
553       xbt_swag_new(xbt_swag_offset(action, state_hookup));
554   surf_network_resource->common_public->states.running_action_set =
555       xbt_swag_new(xbt_swag_offset(action, state_hookup));
556   surf_network_resource->common_public->states.failed_action_set =
557       xbt_swag_new(xbt_swag_offset(action, state_hookup));
558   surf_network_resource->common_public->states.done_action_set =
559       xbt_swag_new(xbt_swag_offset(action, state_hookup));
560
561   surf_network_resource->common_public->name_service = name_service;
562   surf_network_resource->common_public->get_resource_name =
563       get_resource_name;
564   surf_network_resource->common_public->action_get_state =
565       surf_action_get_state;
566   surf_network_resource->common_public->action_use = action_use;
567   surf_network_resource->common_public->action_free = action_free;
568   surf_network_resource->common_public->action_cancel = action_cancel;
569   surf_network_resource->common_public->action_recycle = action_recycle;
570   surf_network_resource->common_public->action_change_state =
571       action_change_state;
572   surf_network_resource->common_public->action_set_data = surf_action_set_data;
573   surf_network_resource->common_public->name = "network";
574
575   surf_network_resource->common_private->resource_used = resource_used;
576   surf_network_resource->common_private->share_resources = share_resources;
577   surf_network_resource->common_private->update_actions_state =
578       update_actions_state;
579   surf_network_resource->common_private->update_resource_state =
580       update_resource_state;
581   surf_network_resource->common_private->finalize = finalize;
582
583   surf_network_resource->common_public->suspend = action_suspend;
584   surf_network_resource->common_public->resume = action_resume;
585   surf_network_resource->common_public->is_suspended = action_is_suspended;
586
587   surf_network_resource->extension_public->communicate = communicate;
588
589   network_link_set = xbt_dict_new();
590   network_card_set = xbt_dict_new();
591
592   /* HC: I am assuming that this stays in for simulation of compute tasks */
593   xbt_assert0(maxmin_system, "surf_init has to be called first!");
594
595   /* KF: Added the initialization for GTNetS interface */
596   if (gtnets_initialize()) {
597     xbt_assert0(0, "impossible to initialize GTNetS interface");
598   }
599 }
600
601
602
603 void surf_network_resource_init_GTNETS(const char *filename)
604 {
605   if (surf_network_resource)
606     return;
607   surf_network_resource_init_internal();
608   parse_file(filename);
609   xbt_dynar_push(resource_list, &surf_network_resource);
610 }