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"
13 static double time_to_next_flow_completion=-1;
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
16 "Logging specific to the SURF network module");
18 /** QUESTIONS for GTNetS integration
19 ** 1. Check that we did the right thing with name_service and get_resource_name
20 ** 2. Right now there is no "kill flow" in our GTNetS implementation. Do we
21 ** need to do something about this?
22 ** 3. We ignore the fact there is some max_duration on flows (see #2 above)
23 ** 4. share_resources() returns a duration, not a date, right?
24 ** 5. We don't suppoer "rates"
25 ** 6. We don't update "remaining" for ongoing flows. Is it bad?
28 static int src_id = -1;
29 static int dst_id = -1;
31 /* Free memory for a network link */
32 static void link_free(void *nw_link)
34 free(((network_link_GTNETS_t) nw_link)->name);
35 xbt_dict_free(&(((network_link_GTNETS_t)nw_link)->properties));
39 /* Instantiate a new network link */
40 /* name: some name for the link, from the XML */
41 /* bw: The bandwidth value */
42 /* lat: The latency value */
43 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
45 static int link_count = -1;
46 network_link_GTNETS_t gtnets_link;
48 /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
49 if (xbt_dict_get_or_null(link_set, name)) {
53 /* KF: Increment the link counter for GTNetS */
57 nw_link->model = (surf_model_t) surf_network_model;
59 nw_link->bw_current = bw_initial;
62 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
63 nw_link->lat_current = lat_initial;
66 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
67 nw_link->state_current = state_initial;
69 nw_link->state_event =
70 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
73 /* KF: Add the link to the GTNetS simulation */
74 if (gtnets_add_link(link_count, bw, lat)) {
75 xbt_assert0(0, "Cannot create GTNetS link");
78 /* KF: Insert entry in the dictionary */
79 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
80 gtnets_link->name = name;
81 gtnets_link->bw_current = bw;
82 gtnets_link->lat_current = lat;
83 gtnets_link->id = link_count;
84 /* Add the properties */
85 gtnets_link->properties = props;
87 xbt_dict_set(link_set, name, gtnets_link, link_free);
92 /* free the network card */
93 static void network_card_free(void *nw_card)
95 free(((network_card_GTNETS_t) nw_card)->name);
99 /* Instantiate a new network card: MODIFYED BY KF */
100 static int network_card_new(const char *name)
102 static int card_count = -1;
104 XBT_IN1("(%s)",name);
105 /* KF: Check that we haven't seen the network card before */
106 network_card_GTNETS_t card =
107 xbt_dict_get_or_null(network_card_set, name);
110 /* KF: Increment the card counter for GTNetS */
113 /* KF: just use the dictionary to map link names to link indices */
114 card = xbt_new0(s_network_card_GTNETS_t, 1);
115 card->name = xbt_strdup(name);
116 card->id = card_count;
117 xbt_dict_set(network_card_set, name, card, network_card_free);
120 LOG1(xbt_log_priority_trace, " return %d",card->id);
122 /* KF: just return the GTNetS ID as the SURF ID */
126 /* Instantiate a new route: MODIFY BY KF */
127 static void route_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
131 XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
132 src_id,dst_id,links,nb_link);
134 /* KF: Build the list of gtnets link IDs */
135 gtnets_links = (int *) calloc(nb_link, sizeof(int));
136 for (i = 0; i < nb_link; i++) {
137 gtnets_links[i] = links[i]->id;
140 /* KF: Create the GTNets route */
141 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
142 xbt_assert0(0, "Cannot create GTNetS route");
147 /* Instantiate a new route: MODIFY BY KF */
148 static void route_onehop_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
153 xbt_assert0(0, "In onehop_new, nb_link should be 1");
156 /* KF: Build the linbst of gtnets link IDs */
157 linkid = links[0]->id;
159 /* KF: Create the GTNets route */
160 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
161 xbt_assert0(0, "Cannot create GTNetS route");
167 /* Parse the XML for a network link */
168 static void parse_link_init(void)
173 e_surf_link_state_t state;
175 name = xbt_strdup(A_surfxml_link_id);
176 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
177 surf_parse_get_double(&lat, A_surfxml_link_latency);
178 state = SURF_LINK_ON;
180 tmgr_trace_t bw_trace;
181 tmgr_trace_t state_trace;
182 tmgr_trace_t lat_trace;
184 surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
185 surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
186 surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
189 INFO0("The GTNetS network model doesn't support bandwidth state traces");
191 INFO0("The GTNetS network model doesn't support latency state traces");
193 INFO0("The GTNetS network model doesn't support link state traces");
195 current_property_set = xbt_dict_new();
196 link_new(name, bw, lat, current_property_set);
199 /* Parses a route from the XML: UNMODIFIED BY HC */
200 static void parse_route_set_endpoints(void)
202 src_id = network_card_new(A_surfxml_route_src);
203 dst_id = network_card_new(A_surfxml_route_dst);
204 route_action = A_surfxml_route_action;
208 static void parse_route_set_routers(void)
210 int id = network_card_new(A_surfxml_router_id);
212 /* KF: Create the GTNets router */
213 if (gtnets_add_router(id)) {
214 xbt_assert0(0, "Cannot add GTNetS router");
218 /* Create the route (more than one hops): MODIFIED BY KF */
219 static void parse_route_set_route(void)
222 if (src_id != -1 && dst_id != -1) {
223 name = bprintf("%x#%x",src_id, dst_id);
224 manage_route(route_table, name, route_action, 0);
229 static void add_route()
232 unsigned int cpt = 0;
233 int link_list_capacity = 0;
235 xbt_dict_cursor_t cursor = NULL;
236 char *key,*data, *end;
237 const char *sep = "#";
238 xbt_dynar_t links, keys;
239 static network_link_GTNETS_t *link_list = NULL;
243 xbt_dict_foreach(route_table, cursor, key, data) {
246 links = (xbt_dynar_t)data;
247 keys = xbt_str_split_str(key, sep);
249 link_list_capacity = xbt_dynar_length(links);
250 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
252 src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
253 dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
254 xbt_dynar_free(&keys);
256 xbt_dynar_foreach (links, cpt, link) {
258 link_list[nb_link++] = xbt_dict_get(link_set, link);
261 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
265 route_onehop_new(src_id, dst_id, link_list, nb_link);
268 xbt_dict_foreach(route_table, cursor, key, data) {
271 links = (xbt_dynar_t)data;
272 keys = xbt_str_split_str(key, sep);
274 link_list_capacity = xbt_dynar_length(links);
275 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
277 src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
278 dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
279 xbt_dynar_free(&keys);
281 xbt_dynar_foreach (links, cpt, link) {
283 link_list[nb_link++] = xbt_dict_get(link_set, link);
286 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
290 route_new(src_id, dst_id, link_list, nb_link);
293 xbt_dict_free(&route_table);
294 gtnets_print_topology();
298 /* Main XML parsing */
299 static void define_callbacks(const char *file)
301 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
302 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
303 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
304 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
305 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
308 static void *name_service(const char *name)
310 return xbt_dict_get_or_null(network_card_set, name);
313 static const char *get_resource_name(void *resource_id)
315 return ((network_card_GTNETS_t) resource_id)->name;
318 static xbt_dict_t get_properties(void *link)
320 return ((network_card_GTNETS_t) link)->properties;
324 /* We do not care about this: only used for traces */
325 static int resource_used(void *resource_id)
327 return 0; /* We don't care */
330 static int action_free(surf_action_t action)
333 if (!action->refcount) {
334 xbt_swag_remove(action, action->state_set);
335 /* KF: No explicit freeing needed for GTNeTS here */
342 static void action_use(surf_action_t action)
347 static void action_cancel(surf_action_t action)
349 xbt_assert0(0, "Cannot cancel GTNetS flow");
353 static void action_recycle(surf_action_t action)
355 xbt_assert0(0, "Cannot recycle GTNetS flow");
359 static void action_change_state(surf_action_t action,
360 e_surf_action_state_t state)
362 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
363 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
364 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
365 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
368 surf_action_change_state(action, state);
373 /* share_resources() */
374 static double share_resources(double now)
376 xbt_swag_t running_actions = surf_network_model->common_public->states.running_action_set;
378 //get the first relevant value from the running_actions list
379 if (!xbt_swag_size(running_actions))
382 xbt_assert0(time_to_next_flow_completion, "Time to next flow completion not initialized!\n");
384 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
386 return time_to_next_flow_completion;
389 /* delta: by how many time units the simulation must advance */
390 /* In this function: change the state of actions that terminate */
391 /* The delta may not come from the network, and thus may be different (smaller)
392 than the one returned by the function above */
393 /* If the delta is a network-caused min, then do not emulate any timer in the
394 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
396 static void update_actions_state(double now, double delta)
398 surf_action_network_GTNETS_t action = NULL;
399 // surf_action_network_GTNETS_t next_action = NULL;
400 xbt_swag_t running_actions =
401 surf_network_model->common_public->states.running_action_set;
403 /* If there are no renning flows, just return */
404 if (time_to_next_flow_completion < 0.0) {
408 /*KF: if delta == time_to_next_flow_completion, too. */
409 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
415 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
417 "Cannot run GTNetS simulation until next flow completion");
421 "GTNetS simulation couldn't find a flow that would complete");
424 xbt_swag_foreach(action, running_actions) {
425 DEBUG2("Action (%p) remains old value: %f", action, action->generic_action.remains);
426 double remain = gtnets_get_flow_rx(action);
427 DEBUG1("Remain value returned by GTNetS : %f", remain);
428 //need to trust this remain value
430 action->generic_action.remains=0;
432 action->generic_action.remains=action->generic_action.cost-remain;
434 DEBUG2("Action (%p) remains new value: %f", action, action->generic_action.remains);
437 for (i = 0; i < num_flows; i++) {
438 action = (surf_action_network_GTNETS_t) (metadata[i]);
440 action->generic_action.finish = now + time_to_next_flow_completion;
441 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
442 DEBUG1("----> Action (%p) just terminated",action);
446 } else { /* run for a given number of seconds */
447 if (gtnets_run(delta)) {
448 xbt_assert0(0, "Cannot run GTNetS simulation");
455 /* UNUSED HERE: no traces */
456 static void update_resource_state(void *id,
457 tmgr_trace_event_t event_type,
458 double value, double date)
460 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
464 /* KF: Rate not supported */
465 static surf_action_t communicate(void *src, void *dst, double size,
468 surf_action_network_GTNETS_t action = NULL;
469 network_card_GTNETS_t card_src = src;
470 network_card_GTNETS_t card_dst = dst;
472 int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
473 network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
477 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);
480 action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
482 action->generic_action.refcount = 1;
483 action->generic_action.cost = size;
484 action->generic_action.remains = size;
485 /* Max durations are not supported */
486 action->generic_action.max_duration = NO_MAX_DURATION;
487 action->generic_action.start = surf_get_clock();
488 action->generic_action.finish = -1.0;
489 action->generic_action.model_type =
490 (surf_model_t) surf_network_model;
492 action->generic_action.state_set =
493 surf_network_model->common_public->states.running_action_set;
495 xbt_swag_insert(action, action->generic_action.state_set);
497 /* KF: Add a flow to the GTNets Simulation, associated to this action */
498 if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *) action)
500 xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
504 return (surf_action_t) action;
507 /* Suspend a flow() */
508 static void action_suspend(surf_action_t action)
511 "action_suspend() not supported for the GTNets network model");
514 /* Resume a flow() */
515 static void action_resume(surf_action_t action)
518 "action_resume() not supported for the GTNets network model");
521 /* Test whether a flow is suspended */
522 static int action_is_suspended(surf_action_t action)
527 static void finalize(void)
532 xbt_dict_free(&network_card_set);
533 xbt_dict_free(&link_set);
534 xbt_swag_free(surf_network_model->common_public->states.
536 xbt_swag_free(surf_network_model->common_public->states.
538 xbt_swag_free(surf_network_model->common_public->states.
540 xbt_swag_free(surf_network_model->common_public->states.
542 free(surf_network_model->common_public);
543 free(surf_network_model->common_private);
544 free(surf_network_model->extension_public);
546 free(surf_network_model);
547 surf_network_model = NULL;
552 static void surf_network_model_init_internal(void)
554 s_surf_action_t action;
556 surf_network_model = xbt_new0(s_surf_network_model_t, 1);
558 surf_network_model->common_private =
559 xbt_new0(s_surf_model_private_t, 1);
560 surf_network_model->common_public =
561 xbt_new0(s_surf_model_public_t, 1);
562 surf_network_model->extension_public =
563 xbt_new0(s_surf_network_model_extension_public_t, 1);
565 surf_network_model->common_public->states.ready_action_set =
566 xbt_swag_new(xbt_swag_offset(action, state_hookup));
567 surf_network_model->common_public->states.running_action_set =
568 xbt_swag_new(xbt_swag_offset(action, state_hookup));
569 surf_network_model->common_public->states.failed_action_set =
570 xbt_swag_new(xbt_swag_offset(action, state_hookup));
571 surf_network_model->common_public->states.done_action_set =
572 xbt_swag_new(xbt_swag_offset(action, state_hookup));
574 surf_network_model->common_public->name_service = name_service;
575 surf_network_model->common_public->get_resource_name =
577 surf_network_model->common_public->action_get_state =
578 surf_action_get_state;
579 surf_network_model->common_public->action_use = action_use;
580 surf_network_model->common_public->action_free = action_free;
581 surf_network_model->common_public->action_cancel = action_cancel;
582 surf_network_model->common_public->action_recycle = action_recycle;
583 surf_network_model->common_public->action_change_state =
585 surf_network_model->common_public->action_set_data =
586 surf_action_set_data;
587 surf_network_model->common_public->name = "network";
589 surf_network_model->common_private->resource_used = resource_used;
590 surf_network_model->common_private->share_resources = share_resources;
591 surf_network_model->common_private->update_actions_state =
592 update_actions_state;
593 surf_network_model->common_private->update_resource_state = update_resource_state;
594 surf_network_model->common_private->finalize = finalize;
596 surf_network_model->common_public->suspend = action_suspend;
597 surf_network_model->common_public->resume = action_resume;
598 surf_network_model->common_public->is_suspended = action_is_suspended;
600 surf_network_model->extension_public->communicate = communicate;
602 /*for the props of the link*/
603 surf_network_model->common_public->get_properties = get_properties;
605 link_set = xbt_dict_new();
606 network_card_set = xbt_dict_new();
608 /* KF: Added the initialization for GTNetS interface */
609 if (gtnets_initialize()) {
610 xbt_assert0(0, "impossible to initialize GTNetS interface");
615 void surf_network_model_init_GTNETS(const char *filename)
617 if (surf_network_model)
619 surf_network_model_init_internal();
620 define_callbacks(filename);
621 xbt_dynar_push(model_list, &surf_network_model);
623 update_model_description(surf_network_model_description,
625 (surf_model_t) surf_network_model);