1 /* Copyright (c) 2005 Henri Casanova. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "network_gtnets_private.h"
7 #include "gtnets/gtnets_interface.h"
9 #include "surf_private.h"
10 #include "surf/datatypes.h"
12 /* ************************************************************************** */
13 /* *************************** FULL ROUTING ********************************* */
15 s_routing_t generic_routing;
16 xbt_dynar_t *routing_table;
19 } s_routing_full_t,*routing_full_t;
22 static double time_to_next_flow_completion = -1;
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
25 "Logging specific to the SURF network GTNetS module");
27 extern routing_t used_routing;
29 /** QUESTIONS for GTNetS integration
30 ** 1. Check that we did the right thing with name_service and get_resource_name
31 ** 2. Right now there is no "kill flow" in our GTNetS implementation. Do we
32 ** need to do something about this?
33 ** 3. We ignore the fact there is some max_duration on flows (see #2 above)
34 ** 4. share_resources() returns a duration, not a date, right?
35 ** 5. We don't suppoer "rates"
36 ** 6. We don't update "remaining" for ongoing flows. Is it bad?
39 static int src_id = -1;
40 static int dst_id = -1;
42 /* Instantiate a new network link */
43 /* name: some name for the link, from the XML */
44 /* bw: The bandwidth value */
45 /* lat: The latency value */
46 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
48 static int link_count = -1;
49 network_link_GTNETS_t gtnets_link;
51 /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
52 if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
56 /* KF: Increment the link counter for GTNetS */
60 nw_link->model = surf_network_model;
62 nw_link->bw_current = bw_initial;
65 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
66 nw_link->lat_current = lat_initial;
69 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
70 nw_link->state_current = state_initial;
72 nw_link->state_event =
73 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
76 /* KF: Add the link to the GTNetS simulation */
77 if (gtnets_add_link(link_count, bw, lat)) {
78 xbt_assert0(0, "Cannot create GTNetS link");
81 /* KF: Insert entry in the dictionary */
82 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
83 gtnets_link->generic_resource.name = name;
84 gtnets_link->generic_resource.properties = props;
85 gtnets_link->bw_current = bw;
86 gtnets_link->lat_current = lat;
87 gtnets_link->id = link_count;
89 xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
95 /* Instantiate a new network card: MODIFYED BY KF */
96 static int network_card_new(const char *name)
98 static int card_count = -1;
100 XBT_IN1("(%s)", name);
101 /* KF: Check that we haven't seen the network card before */
102 network_card_GTNETS_t card =
103 surf_model_resource_by_name(surf_network_model, name);
106 /* KF: Increment the card counter for GTNetS */
109 /* KF: just use the dictionary to map link names to link indices */
110 card = xbt_new0(s_network_card_GTNETS_t, 1);
111 card->name = xbt_strdup(name);
112 card->id = card_count;
113 xbt_dict_set(surf_model_resource_set(surf_network_model), name, card,
117 LOG1(xbt_log_priority_trace, " return %d", card->id);
119 /* KF: just return the GTNetS ID as the SURF ID */
123 /* Instantiate a new route: MODIFY BY KF */
124 static void route_new(int src_id, int dst_id, network_link_GTNETS_t * links,
129 XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
130 src_id, dst_id, links, nb_link);
132 /* KF: Build the list of gtnets link IDs */
133 gtnets_links = (int *) calloc(nb_link, sizeof(int));
134 for (i = 0; i < nb_link; i++) {
135 gtnets_links[i] = links[i]->id;
138 /* KF: Create the GTNets route */
139 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
140 xbt_assert0(0, "Cannot create GTNetS route");
145 /* Instantiate a new route: MODIFY BY KF */
146 static void route_onehop_new(int src_id, int dst_id,
147 network_link_GTNETS_t * links, int nb_link)
152 xbt_assert0(0, "In onehop_new, nb_link should be 1");
155 /* KF: Build the linbst of gtnets link IDs */
156 linkid = links[0]->id;
158 /* KF: Create the GTNets route */
159 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
160 xbt_assert0(0, "Cannot create GTNetS route");
164 /* Read hosts using a full-fledged topollogy */
165 static void routing_full_parse_Shost(void) {
166 int *val = xbt_malloc(sizeof(int));
167 DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
168 *val = used_routing->host_count++;
169 xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
173 /* Parse the XML for a network link */
174 static void parse_link_init(void)
179 e_surf_resource_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_RESOURCE_ON;
186 tmgr_trace_t bw_trace;
187 tmgr_trace_t state_trace;
188 tmgr_trace_t lat_trace;
190 bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
191 lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
192 state_trace = tmgr_trace_new(A_surfxml_link_state_file);
195 INFO0("The GTNetS network model doesn't support bandwidth state traces");
197 INFO0("The GTNetS network model doesn't support latency state traces");
199 INFO0("The GTNetS network model doesn't support link state traces");
201 current_property_set = xbt_dict_new();
202 link_new(name, bw, lat, current_property_set);
205 /* Parses a route from the XML: UNMODIFIED BY HC */
206 static void parse_route_set_endpoints(void)
208 src_id = network_card_new(A_surfxml_route_src);
209 dst_id = network_card_new(A_surfxml_route_dst);
210 route_action = A_surfxml_route_action;
214 static void parse_route_set_routers(void)
216 int id = network_card_new(A_surfxml_router_id);
218 /* KF: Create the GTNets router */
219 if (gtnets_add_router(id)) {
220 xbt_assert0(0, "Cannot add GTNetS router");
224 /* Create the route (more than one hops): MODIFIED BY KF */
225 static void parse_route_set_route(void)
228 if (src_id != -1 && dst_id != -1) {
229 name = bprintf("%x#%x", src_id, dst_id);
230 DEBUG0("Calling manage route with route_action=");
231 switch (route_action) {
232 case A_surfxml_route_action_PREPEND:
235 case A_surfxml_route_action_POSTPEND:
238 case A_surfxml_route_action_OVERRIDE:
243 manage_route(route_table, name, route_action, 0);
245 DEBUG2("Setting a route from NIC %d to NIC %d", src_id, dst_id);
250 static void add_route()
253 unsigned int cpt = 0;
254 int link_list_capacity = 0;
256 xbt_dict_cursor_t cursor = NULL;
257 char *key, *data, *end;
258 const char *sep = "#";
259 xbt_dynar_t links, keys;
260 static network_link_GTNETS_t *link_list = NULL;
264 xbt_dict_foreach(route_table, cursor, key, data) {
267 links = (xbt_dynar_t) data;
268 keys = xbt_str_split_str(key, sep);
270 link_list_capacity = xbt_dynar_length(links);
271 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
273 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
274 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
275 xbt_dynar_free(&keys);
277 xbt_dynar_foreach(links, cpt, link) {
279 link_list[nb_link++] =
280 xbt_dict_get(surf_network_model->resource_set, link);
283 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
287 DEBUG0("Calling a one link route");
288 route_onehop_new(src_id, dst_id, link_list, nb_link);
292 xbt_dict_foreach(route_table, cursor, key, data) {
295 links = (xbt_dynar_t) data;
296 keys = xbt_str_split_str(key, sep);
298 link_list_capacity = xbt_dynar_length(links);
299 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
301 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
302 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
303 xbt_dynar_free(&keys);
305 xbt_dynar_foreach(links, cpt, link) {
307 link_list[nb_link++] =
308 xbt_dict_get(surf_network_model->resource_set, link);
311 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
315 route_new(src_id, dst_id, link_list, nb_link);
318 xbt_dict_free(&route_table);
319 if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
320 gtnets_print_topology();
325 /* Main XML parsing */
326 static void define_callbacks(const char *file)
328 routing_full_t routing = xbt_new0(s_routing_full_t,1);
329 routing->generic_routing.name = "Full";
330 routing->generic_routing.host_count = 0;
331 routing->generic_routing.host_id = xbt_dict_new();
332 used_routing = (routing_t) routing;
333 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
335 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
336 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
337 surfxml_add_callback(STag_surfxml_route_cb_list,
338 &parse_route_set_endpoints);
339 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
340 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
344 /* We do not care about this: only used for traces */
345 static int resource_used(void *resource_id)
347 return 0; /* We don't care */
350 static int action_unref(surf_action_t action)
353 if (!action->refcount) {
354 xbt_swag_remove(action, action->state_set);
355 /* KF: No explicit freeing needed for GTNeTS here */
362 static void action_cancel(surf_action_t action)
364 xbt_die("Cannot cancel GTNetS flow");
368 static void action_recycle(surf_action_t action)
370 xbt_die("Cannot recycle GTNetS flow");
374 static double action_get_remains(surf_action_t action)
376 return action->remains;
379 static void action_state_set(surf_action_t action,
380 e_surf_action_state_t state)
382 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
383 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
384 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
385 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
388 surf_action_state_set(action, state);
393 /* share_resources() */
394 static double share_resources(double now)
396 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
398 //get the first relevant value from the running_actions list
399 if (!xbt_swag_size(running_actions))
402 xbt_assert0(time_to_next_flow_completion,
403 "Time to next flow completion not initialized!\n");
405 DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
406 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
407 DEBUG1("gtnets_get_time_to_next_flow_completion received %lg", time_to_next_flow_completion);
409 return time_to_next_flow_completion;
412 /* delta: by how many time units the simulation must advance */
413 /* In this function: change the state of actions that terminate */
414 /* The delta may not come from the network, and thus may be different (smaller)
415 than the one returned by the function above */
416 /* If the delta is a network-caused min, then do not emulate any timer in the
417 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
419 static void update_actions_state(double now, double delta)
421 surf_action_network_GTNETS_t action = NULL;
422 // surf_action_network_GTNETS_t next_action = NULL;
423 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
425 /* If there are no renning flows, just return */
426 if (time_to_next_flow_completion < 0.0) {
430 /*KF: if delta == time_to_next_flow_completion, too. */
431 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
437 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
439 "Cannot run GTNetS simulation until next flow completion");
443 "GTNetS simulation couldn't find a flow that would complete");
446 xbt_swag_foreach(action, running_actions) {
447 DEBUG2("Action (%p) remains old value: %f", action,
448 action->generic_action.remains);
449 double remain = gtnets_get_flow_rx(action);
450 DEBUG1("Remain value returned by GTNetS : %f", remain);
451 //need to trust this remain value
453 action->generic_action.remains = 0;
455 action->generic_action.remains = action->generic_action.cost - remain;
457 DEBUG2("Action (%p) remains new value: %f", action,
458 action->generic_action.remains);
461 for (i = 0; i < num_flows; i++) {
462 action = (surf_action_network_GTNETS_t) (metadata[i]);
464 action->generic_action.finish = now + time_to_next_flow_completion;
465 action_state_set((surf_action_t) action, SURF_ACTION_DONE);
466 DEBUG1("----> Action (%p) just terminated", action);
470 } else { /* run for a given number of seconds */
471 if (gtnets_run(delta)) {
472 xbt_assert0(0, "Cannot run GTNetS simulation");
479 /* UNUSED HERE: no traces */
480 static void update_resource_state(void *id,
481 tmgr_trace_event_t event_type,
482 double value, double date)
484 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
488 /* KF: Rate not supported */
489 /* Max durations are not supported */
490 static surf_action_t communicate(const char *src_name, const char *dst_name,
491 int src, int dst, double size, double rate)
493 surf_action_network_GTNETS_t action = NULL;
495 DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst, dst_name);
498 surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
499 surf_network_model, 0);
501 /* KF: Add a flow to the GTNets Simulation, associated to this action */
502 if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
503 xbt_assert2(0, "Not route between host %s and host %s", src_name,
507 return (surf_action_t) action;
510 /* Suspend a flow() */
511 static void action_suspend(surf_action_t action)
516 /* Resume a flow() */
517 static void action_resume(surf_action_t action)
522 /* Test whether a flow is suspended */
523 static int action_is_suspended(surf_action_t action)
528 static void finalize(void)
530 xbt_dict_free(&surf_network_model->resource_set);
532 surf_model_exit(surf_network_model);
534 free(surf_network_model);
535 surf_network_model = NULL;
540 static void surf_network_model_init_internal(void)
542 surf_network_model = surf_model_init();
544 surf_network_model->name = "network GTNetS";
545 surf_network_model->action_unref = action_unref;
546 surf_network_model->action_cancel = action_cancel;
547 surf_network_model->action_recycle = action_recycle;
548 surf_network_model->action_state_set = action_state_set;
549 surf_network_model->get_remains = action_get_remains;
551 surf_network_model->model_private->resource_used = resource_used;
552 surf_network_model->model_private->share_resources = share_resources;
553 surf_network_model->model_private->update_actions_state =
554 update_actions_state;
555 surf_network_model->model_private->update_resource_state =
556 update_resource_state;
557 surf_network_model->model_private->finalize = finalize;
559 surf_network_model->suspend = action_suspend;
560 surf_network_model->resume = action_resume;
561 surf_network_model->is_suspended = action_is_suspended;
563 surf_network_model->extension.network.communicate = communicate;
565 /* KF: Added the initialization for GTNetS interface */
566 if (gtnets_initialize()) {
567 xbt_assert0(0, "impossible to initialize GTNetS interface");
573 void surf_network_model_init_GTNETS(const char *filename)
575 if (surf_network_model)
577 surf_network_model_init_internal();
578 define_callbacks(filename);
579 xbt_dynar_push(model_list, &surf_network_model);
581 update_model_description(surf_network_model_description,
582 "GTNets", surf_network_model);