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"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
13 "Logging specific to the SURF network module");
15 /** QUESTIONS for GTNetS integration
16 ** 1. Check that we did the right thing with name_service and get_resource_name
17 ** 2. Right now there is no "kill flow" in our GTNetS implementation. Do we
18 ** need to do something about this?
19 ** 3. We ignore the fact there is some max_duration on flows (see #2 above)
20 ** 4. share_resources() returns a duration, not a date, right?
21 ** 5. We don't suppoer "rates"
22 ** 6. We don't update "remaining" for ongoing flows. Is it bad?
25 /* Free memory for a network link */
26 static void link_free(void *nw_link)
28 free(((network_link_GTNETS_t) nw_link)->name);
29 xbt_dict_free(&(((network_link_GTNETS_t)nw_link)->properties));
33 /* Instantiate a new network link */
34 /* name: some name for the link, from the XML */
35 /* bw: The bandwidth value */
36 /* lat: The latency value */
37 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
39 static int link_count = -1;
40 network_link_GTNETS_t gtnets_link;
42 /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
43 if (xbt_dict_get_or_null(link_set, name)) {
47 /* KF: Increment the link counter for GTNetS */
51 nw_link->model = (surf_model_t) surf_network_model;
53 nw_link->bw_current = bw_initial;
56 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
57 nw_link->lat_current = lat_initial;
60 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
61 nw_link->state_current = state_initial;
63 nw_link->state_event =
64 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
67 /* KF: Add the link to the GTNetS simulation */
68 if (gtnets_add_link(link_count, bw, lat)) {
69 xbt_assert0(0, "Cannot create GTNetS link");
72 /* KF: Insert entry in the dictionary */
73 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
74 gtnets_link->name = name;
75 gtnets_link->bw_current = bw;
76 gtnets_link->lat_current = lat;
77 gtnets_link->id = link_count;
78 /* Add the properties */
79 gtnets_link->properties = props;
81 xbt_dict_set(link_set, name, gtnets_link, link_free);
86 /* free the network card */
87 static void network_card_free(void *nw_card)
89 free(((network_card_GTNETS_t) nw_card)->name);
93 /* Instantiate a new network card: MODIFYED BY KF */
94 static int network_card_new(const char *name)
96 static int card_count = -1;
98 /* KF: Check that we haven't seen the network card before */
99 network_card_GTNETS_t card =
100 xbt_dict_get_or_null(network_card_set, name);
103 /* KF: Increment the card counter for GTNetS */
106 /* KF: just use the dictionary to map link names to link indices */
107 card = xbt_new0(s_network_card_GTNETS_t, 1);
108 card->name = xbt_strdup(name);
109 card->id = card_count;
110 xbt_dict_set(network_card_set, name, card, network_card_free);
113 /* KF: just return the GTNetS ID as the SURF ID */
117 /* Instantiate a new route: MODIFY BY KF */
118 static void route_new(int src_id, int dst_id, char **links, int nb_link)
121 network_link_GTNETS_t *link_list = NULL;
124 ROUTE_SIZE(src_id, dst_id) = nb_link;
125 link_list = (ROUTE(src_id, dst_id) =
126 xbt_new0(network_link_GTNETS_t, nb_link));
127 for (i = 0; i < nb_link; i++) {
128 link_list[i] = xbt_dict_get_or_null(link_set, links[i]);
136 /* KF: Build the list of gtnets link IDs */
137 gtnets_links = (int *) calloc(nb_link, sizeof(int));
138 for (i = 0; i < nb_link; i++) {
140 ((network_link_GTNETS_t)
141 (xbt_dict_get(link_set, links[i])))->id;
144 /* KF: Create the GTNets route */
145 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
146 xbt_assert0(0, "Cannot create GTNetS route");
150 /* Instantiate a new route: MODIFY BY KF */
151 static void route_onehop_new(int src_id, int dst_id, char **links,
157 xbt_assert0(0, "In onehop_new, nb_link should be 1");
160 /* KF: Build the list of gtnets link IDs */
162 ((network_link_GTNETS_t)
163 (xbt_dict_get(link_set, links[0])))->id;
165 /* KF: Create the GTNets route */
166 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
167 xbt_assert0(0, "Cannot create GTNetS route");
173 /* Parse the XML for a network link */
174 static void parse_link_init(void)
179 e_surf_link_state_t state;
181 name = xbt_strdup(A_surfxml_link_id);
182 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
183 surf_parse_get_double(&lat, A_surfxml_link_latency);
184 state = SURF_LINK_ON;
186 /* Print values when no traces are specified */
188 tmgr_trace_t bw_trace;
189 tmgr_trace_t state_trace;
190 tmgr_trace_t lat_trace;
192 surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
193 surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
194 surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
196 /*TODO Where is WARNING0 defined??? */
200 ("The GTNetS network model doesn't support bandwidth state traces");
203 ("The GTNetS network model doesn't support latency state traces");
206 ("The GTNetS network model doesn't support link state traces");
209 /* KF: remove several arguments to link_new */
210 current_property_set = xbt_dict_new();
211 link_new(name, bw, lat, current_property_set);
214 static int nb_link = 0;
215 static char **link_name = NULL;
216 static int src_id = -1;
217 static int dst_id = -1;
219 /* Parses a route from the XML: UNMODIFIED BY HC */
220 static void parse_route_set_endpoints(void)
222 src_id = network_card_new(A_surfxml_route_src);
223 dst_id = network_card_new(A_surfxml_route_dst);
228 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
232 static void parse_route_set_routers(void)
234 int id = network_card_new(A_surfxml_router_id);
236 /* KF: Create the GTNets router */
237 if (gtnets_add_router(id)) {
238 xbt_assert0(0, "Cannot add GTNetS router");
242 //The following is common to all and has been relocated to surfxml_parse
243 /* Parses a route element from the XML: UNMODIFIED BY HC */
244 /*static void parse_route_elem(void)
247 link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
248 link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
252 /* Create the route (more than one hops): MODIFIED BY KF */
253 static void parse_route_set_route(void)
256 route_new(src_id, dst_id, link_name, nb_link);
258 char *name = bprintf("%x#%x",src_id, dst_id);
259 xbt_dict_set(route_table, name, route_link_list, NULL);
263 //This is not used anymore. one hop routes are created in add_route
264 /* Create the one-hope route: BY KF */
265 static void parse_route_set_onehop_route(void)
268 route_onehop_new(src_id, dst_id, link_name, nb_link);
271 static void add_route()
274 unsigned int cpt = 0;
276 xbt_dict_cursor_t cursor = NULL;
277 char *key,*data, *end;
278 const char *sep = "#";
279 xbt_dynar_t links, keys;
281 xbt_dict_foreach(route_table, cursor, key, data) {
283 links = (xbt_dynar_t)data;
284 keys = xbt_str_split_str(key, sep);
286 nb_link = xbt_dynar_length(links);
287 link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
289 src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
290 dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
294 xbt_dynar_foreach (links, cpt, link) {
296 link_name[i++] = xbt_dict_get(link_set, link);
299 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
303 route_new(src_id, dst_id, link_name, nb_link);
305 route_onehop_new(src_id, dst_id, link_name, nb_link);
308 xbt_dict_free(&route_table);
311 /* Main XML parsing */
312 static void define_callbacks(const char *file)
314 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
315 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
316 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
317 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
318 /* surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_onehop_route);*/
319 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
322 static void *name_service(const char *name)
324 return xbt_dict_get_or_null(network_card_set, name);
327 static const char *get_resource_name(void *resource_id)
329 return ((network_card_GTNETS_t) resource_id)->name;
332 static xbt_dict_t get_properties(void *link)
334 return ((network_card_GTNETS_t) link)->properties;
338 /* We do not care about this: only used for traces */
339 static int resource_used(void *resource_id)
341 return 0; /* We don't care */
344 static int action_free(surf_action_t action)
347 if (!action->using) {
348 xbt_swag_remove(action, action->state_set);
349 /* KF: No explicit freeing needed for GTNeTS here */
356 static void action_use(surf_action_t action)
361 static void action_cancel(surf_action_t action)
363 xbt_assert0(0, "Cannot cancel GTNetS flow");
367 static void action_recycle(surf_action_t action)
369 xbt_assert0(0, "Cannot recycle GTNetS flow");
373 static void action_change_state(surf_action_t action,
374 e_surf_action_state_t state)
376 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
377 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
378 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
379 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
382 surf_action_change_state(action, state);
387 /* share_resources() */
388 static double share_resources(double now)
390 xbt_swag_t running_actions = surf_network_model->common_public->states.running_action_set;
392 //get the first relevant value from the running_actions list
393 if (!xbt_swag_size(running_actions))
396 return gtnets_get_time_to_next_flow_completion();
399 /* delta: by how many time units the simulation must advance */
400 /* In this function: change the state of actions that terminate */
401 /* The delta may not come from the network, and thus may be different (smaller)
402 than the one returned by the function above */
403 /* If the delta is a network-caused min, then do not emulate any timer in the
404 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
406 static void update_actions_state(double now, double delta)
408 surf_action_network_GTNETS_t action = NULL;
409 // surf_action_network_GTNETS_t next_action = NULL;
410 xbt_swag_t running_actions =
411 surf_network_model->common_public->states.running_action_set;
413 double time_to_next_flow_completion =
414 gtnets_get_time_to_next_flow_completion();
416 /* If there are no renning flows, just return */
417 if (time_to_next_flow_completion < 0.0) {
421 /*KF: if delta == time_to_next_flow_completion, too. */
422 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
428 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
430 "Cannot run GTNetS simulation until next flow completion");
434 "GTNetS simulation couldn't find a flow that would complete");
439 xbt_swag_foreach(action, running_actions) {
440 DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
441 double remain = gtnets_get_flow_rx(action);
442 //need to trust this remain value
444 action->generic_action.remains=0;
446 action->generic_action.remains-=remain;
448 DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
451 for (i = 0; i < num_flows; i++) {
452 action = (surf_action_network_GTNETS_t) (metadata[i]);
454 action->generic_action.finish = now + time_to_next_flow_completion;
455 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
456 /* TODO: Anything else here? */
458 //need to map this action to the gtnets engine
459 DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
460 action->generic_action.remains -= gtnets_get_flow_rx(metadata[i]);
461 DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
465 } else { /* run for a given number of seconds */
466 if (gtnets_run(delta)) {
467 xbt_assert0(0, "Cannot run GTNetS simulation");
474 /* UNUSED HERE: no traces */
475 static void update_resource_state(void *id,
476 tmgr_trace_event_t event_type,
477 double value, double date)
479 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
483 /* KF: Rate not supported */
484 static surf_action_t communicate(void *src, void *dst, double size,
487 surf_action_network_GTNETS_t action = NULL;
488 network_card_GTNETS_t card_src = src;
489 network_card_GTNETS_t card_dst = dst;
491 int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
492 network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
496 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);
499 action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
501 action->generic_action.using = 1;
502 action->generic_action.cost = size;
503 action->generic_action.remains = size;
504 /* Max durations are not supported */
505 action->generic_action.max_duration = NO_MAX_DURATION;
506 action->generic_action.start = surf_get_clock();
507 action->generic_action.finish = -1.0;
508 action->generic_action.model_type =
509 (surf_model_t) surf_network_model;
511 action->generic_action.state_set =
512 surf_network_model->common_public->states.running_action_set;
514 xbt_swag_insert(action, action->generic_action.state_set);
516 /* KF: Add a flow to the GTNets Simulation, associated to this action */
517 if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *) action)
519 xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
523 return (surf_action_t) action;
526 /* Suspend a flow() */
527 static void action_suspend(surf_action_t action)
530 "action_suspend() not supported for the GTNets network model");
533 /* Resume a flow() */
534 static void action_resume(surf_action_t action)
537 "action_resume() not supported for the GTNets network model");
540 /* Test whether a flow is suspended */
541 static int action_is_suspended(surf_action_t action)
546 static void finalize(void)
551 xbt_dict_free(&network_card_set);
552 xbt_dict_free(&link_set);
553 xbt_swag_free(surf_network_model->common_public->states.
555 xbt_swag_free(surf_network_model->common_public->states.
557 xbt_swag_free(surf_network_model->common_public->states.
559 xbt_swag_free(surf_network_model->common_public->states.
561 free(surf_network_model->common_public);
562 free(surf_network_model->common_private);
563 free(surf_network_model->extension_public);
565 free(surf_network_model);
566 surf_network_model = NULL;
569 for (i = 0; i < card_number; i++)
570 for (j = 0; j < card_number; j++)
573 routing_table = NULL;
574 free(routing_table_size);
575 routing_table_size = NULL;
584 static void surf_network_model_init_internal(void)
586 s_surf_action_t action;
588 surf_network_model = xbt_new0(s_surf_network_model_t, 1);
590 surf_network_model->common_private =
591 xbt_new0(s_surf_model_private_t, 1);
592 surf_network_model->common_public =
593 xbt_new0(s_surf_model_public_t, 1);
594 surf_network_model->extension_public =
595 xbt_new0(s_surf_network_model_extension_public_t, 1);
597 surf_network_model->common_public->states.ready_action_set =
598 xbt_swag_new(xbt_swag_offset(action, state_hookup));
599 surf_network_model->common_public->states.running_action_set =
600 xbt_swag_new(xbt_swag_offset(action, state_hookup));
601 surf_network_model->common_public->states.failed_action_set =
602 xbt_swag_new(xbt_swag_offset(action, state_hookup));
603 surf_network_model->common_public->states.done_action_set =
604 xbt_swag_new(xbt_swag_offset(action, state_hookup));
606 surf_network_model->common_public->name_service = name_service;
607 surf_network_model->common_public->get_resource_name =
609 surf_network_model->common_public->action_get_state =
610 surf_action_get_state;
611 surf_network_model->common_public->action_use = action_use;
612 surf_network_model->common_public->action_free = action_free;
613 surf_network_model->common_public->action_cancel = action_cancel;
614 surf_network_model->common_public->action_recycle = action_recycle;
615 surf_network_model->common_public->action_change_state =
617 surf_network_model->common_public->action_set_data =
618 surf_action_set_data;
619 surf_network_model->common_public->name = "network";
621 surf_network_model->common_private->resource_used = resource_used;
622 surf_network_model->common_private->share_resources = share_resources;
623 surf_network_model->common_private->update_actions_state =
624 update_actions_state;
625 surf_network_model->common_private->update_resource_state = update_resource_state;
626 surf_network_model->common_private->finalize = finalize;
628 surf_network_model->common_public->suspend = action_suspend;
629 surf_network_model->common_public->resume = action_resume;
630 surf_network_model->common_public->is_suspended = action_is_suspended;
632 surf_network_model->extension_public->communicate = communicate;
634 /*for the props of the link*/
635 surf_network_model->common_public->get_properties = get_properties;
637 link_set = xbt_dict_new();
638 network_card_set = xbt_dict_new();
640 /* KF: Added the initialization for GTNetS interface */
641 if (gtnets_initialize()) {
642 xbt_assert0(0, "impossible to initialize GTNetS interface");