Logo AND Algorithmique Numérique Distribuée

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