3 /* Copyright (c) 2005 Henri Casanova. All rights reserved. */
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. */
8 #include "network_gtnets_private.h"
9 #include "gtnets/gtnets_interface.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
12 "Logging specific to the SURF network module");
14 /* surf_network_model_t surf_network_model = NULL; */
15 /*static xbt_dict_t network_link_set = NULL;*/
17 /* xbt_dict_t network_card_set = NULL; */
20 static int card_number = 0;
21 static network_link_GTNETS_t **routing_table = NULL;
22 static int *routing_table_size = NULL;
24 #define ROUTE(i,j) routing_table[(i)+(j)*card_number]
25 #define ROUTE_SIZE(i,j) routing_table_size[(i)+(j)*card_number]
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?
38 /* Free memory for a network link */
39 static void network_link_free(void *nw_link)
41 free(((network_link_GTNETS_t) nw_link)->name);
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)
51 static int link_count = -1;
52 network_link_GTNETS_t gtnets_link;
54 /* KF: Check that the link wasn't added before */
55 if (xbt_dict_get_or_null(network_link_set, name)) {
59 /* KF: Increment the link counter for GTNetS */
63 nw_link->model = (surf_model_t) surf_network_model;
65 nw_link->bw_current = bw_initial;
68 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
69 nw_link->lat_current = lat_initial;
72 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
73 nw_link->state_current = state_initial;
75 nw_link->state_event =
76 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
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");
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;
93 xbt_dict_set(network_link_set, name, gtnets_link, network_link_free);
98 /* free the network card */
99 static void network_card_free(void *nw_card)
101 free(((network_card_GTNETS_t) nw_card)->name);
105 /* Instantiate a new network card: MODIFYED BY KF */
106 static int network_card_new(const char *name)
108 static int card_count = -1;
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);
115 /* KF: Increment the card counter for GTNetS */
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);
125 /* KF: just return the GTNetS ID as the SURF ID */
129 /* Instantiate a new route: MODIFY BY KF */
130 static void route_new(int src_id, int dst_id, char **links, int nb_link)
133 network_link_GTNETS_t *link_list = NULL;
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]);
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++) {
152 ((network_link_GTNETS_t)
153 (xbt_dict_get(network_link_set, links[i])))->id;
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");
162 /* Instantiate a new route: MODIFY BY KF */
163 static void route_onehop_new(int src_id, int dst_id, char **links,
169 xbt_assert0(0, "In onehop_new, nb_link should be 1");
172 /* KF: Build the list of gtnets link IDs */
174 ((network_link_GTNETS_t)
175 (xbt_dict_get(network_link_set, links[0])))->id;
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");
185 /* Parse the XML for a network link */
186 static void parse_network_link_init(void)
191 e_surf_network_link_state_t state;
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;
198 /* Print values when no traces are specified */
200 tmgr_trace_t bw_trace;
201 tmgr_trace_t state_trace;
202 tmgr_trace_t lat_trace;
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);
208 /*TODO Where is WARNING0 defined??? */
212 ("The GTNetS network model doesn't support bandwidth state traces");
215 ("The GTNetS network model doesn't support latency state traces");
218 ("The GTNetS network model doesn't support link state traces");
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);
226 static int nb_link = 0;
227 static char **link_name = NULL;
228 static int src_id = -1;
229 static int dst_id = -1;
231 /* Parses a route from the XML: UNMODIFIED BY HC */
232 static void parse_route_set_endpoints(void)
234 src_id = network_card_new(A_surfxml_route_src);
235 dst_id = network_card_new(A_surfxml_route_dst);
241 static void parse_route_set_routers(void)
243 int id = network_card_new(A_surfxml_router_name);
245 /* KF: Create the GTNets router */
246 if (gtnets_add_router(id)) {
247 xbt_assert0(0, "Cannot add GTNetS router");
251 /* Parses a route element from the XML: UNMODIFIED BY HC */
252 static void parse_route_elem(void)
255 link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
256 link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
259 /* Create the route (more than one hops): MODIFIED BY KF */
260 static void parse_route_set_route(void)
263 route_new(src_id, dst_id, link_name, nb_link);
266 /* Create the one-hope route: BY KF */
267 static void parse_route_set_onehop_route(void)
270 route_onehop_new(src_id, dst_id, link_name, nb_link);
273 /* Main XML parsing */
274 static void parse_file(const char *file)
276 surf_parse_reset_parser();
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);
286 surf_parse_open(file);
287 xbt_assert1((!surf_parse()), "Parse error in %s", file);
292 static void *name_service(const char *name)
294 return xbt_dict_get_or_null(network_card_set, name);
297 static const char *get_resource_name(void *resource_id)
299 return ((network_card_GTNETS_t) resource_id)->name;
302 static xbt_dict_t get_link_property_list(void *link)
304 return ((network_card_GTNETS_t) link)->properties;
308 /* We do not care about this: only used for traces */
309 static int resource_used(void *resource_id)
311 return 0; /* We don't care */
314 static int action_free(surf_action_t action)
317 if (!action->using) {
318 xbt_swag_remove(action, action->state_set);
319 /* KF: No explicit freeing needed for GTNeTS here */
326 static void action_use(surf_action_t action)
331 static void action_cancel(surf_action_t action)
333 xbt_assert0(0, "Cannot cancel GTNetS flow");
337 static void action_recycle(surf_action_t action)
339 xbt_assert0(0, "Cannot recycle GTNetS flow");
343 static void action_change_state(surf_action_t action,
344 e_surf_action_state_t state)
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; */
352 surf_action_change_state(action, state);
357 /* share_resources() */
358 static double share_resources(double now)
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;
367 return gtnets_get_time_to_next_flow_completion();
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 */
377 static void update_actions_state(double now, double delta)
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;
386 double time_to_next_flow_completion =
387 gtnets_get_time_to_next_flow_completion();
389 /* If there are no renning flows, just return */
390 if (time_to_next_flow_completion < 0.0) {
394 /*KF: if delta == time_to_next_flow_completion, too. */
395 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
401 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
403 "Cannot run GTNetS simulation until next flow completion");
407 "GTNetS simulation couldn't find a flow that would complete");
410 for (i = 0; i < num_flows; i++) {
411 surf_action_network_GTNETS_t action =
412 (surf_action_network_GTNETS_t) (metadata[i]);
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? */
419 } else { /* run for a given number of seconds */
420 if (gtnets_run(delta)) {
421 xbt_assert0(0, "Cannot run GTNetS simulation");
428 /* UNUSED HERE: no traces */
429 static void update_resource_state(void *id,
430 tmgr_trace_event_t event_type,
433 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
437 /* KF: Rate not supported */
438 static surf_action_t communicate(void *src, void *dst, double size,
441 surf_action_network_GTNETS_t action = NULL;
442 network_card_GTNETS_t card_src = src;
443 network_card_GTNETS_t card_dst = dst;
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);
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);
453 action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
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;
465 action->generic_action.state_set =
466 surf_network_model->common_public->states.running_action_set;
468 xbt_swag_insert(action, action->generic_action.state_set);
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)
473 xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
477 return (surf_action_t) action;
480 /* Suspend a flow() */
481 static void action_suspend(surf_action_t action)
484 "action_suspend() not supported for the GTNets network model");
487 /* Resume a flow() */
488 static void action_resume(surf_action_t action)
491 "action_resume() not supported for the GTNets network model");
494 /* Test whether a flow is suspended */
495 static int action_is_suspended(surf_action_t action)
500 static void finalize(void)
505 xbt_dict_free(&network_card_set);
506 xbt_dict_free(&network_link_set);
507 xbt_swag_free(surf_network_model->common_public->states.
509 xbt_swag_free(surf_network_model->common_public->states.
511 xbt_swag_free(surf_network_model->common_public->states.
513 xbt_swag_free(surf_network_model->common_public->states.
515 free(surf_network_model->common_public);
516 free(surf_network_model->common_private);
517 free(surf_network_model->extension_public);
519 free(surf_network_model);
520 surf_network_model = NULL;
523 for (i = 0; i < card_number; i++)
524 for (j = 0; j < card_number; j++)
527 routing_table = NULL;
528 free(routing_table_size);
529 routing_table_size = NULL;
538 static void surf_network_model_init_internal(void)
540 s_surf_action_t action;
542 surf_network_model = xbt_new0(s_surf_network_model_t, 1);
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);
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));
560 surf_network_model->common_public->name_service = name_service;
561 surf_network_model->common_public->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 =
571 surf_network_model->common_public->action_set_data =
572 surf_action_set_data;
573 surf_network_model->common_public->name = "network";
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;
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;
587 surf_network_model->extension_public->communicate = communicate;
589 /*for the props of the link*/
590 surf_network_model->common_public->get_link_properties = get_link_property_list;
592 network_link_set = xbt_dict_new();
593 network_card_set = xbt_dict_new();
595 /* KF: Added the initialization for GTNetS interface */
596 if (gtnets_initialize()) {
597 xbt_assert0(0, "impossible to initialize GTNetS interface");
601 void surf_network_model_init_GTNETS(const char *filename)
603 if (surf_network_model)
605 surf_network_model_init_internal();
606 parse_file(filename);
607 xbt_dynar_push(model_list, &surf_network_model);
609 update_model_description(surf_network_model_description,
610 surf_network_model_description_size,
612 (surf_model_t) surf_network_model);