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 static int src_id = -1;
26 static int dst_id = -1;
28 /* Free memory for a network link */
29 static void link_free(void *nw_link)
31 free(((network_link_GTNETS_t) nw_link)->name);
32 xbt_dict_free(&(((network_link_GTNETS_t)nw_link)->properties));
36 /* Instantiate a new network link */
37 /* name: some name for the link, from the XML */
38 /* bw: The bandwidth value */
39 /* lat: The latency value */
40 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
42 static int link_count = -1;
43 network_link_GTNETS_t gtnets_link;
45 /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
46 if (xbt_dict_get_or_null(link_set, name)) {
50 /* KF: Increment the link counter for GTNetS */
54 nw_link->model = (surf_model_t) surf_network_model;
56 nw_link->bw_current = bw_initial;
59 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
60 nw_link->lat_current = lat_initial;
63 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
64 nw_link->state_current = state_initial;
66 nw_link->state_event =
67 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
70 /* KF: Add the link to the GTNetS simulation */
71 if (gtnets_add_link(link_count, bw, lat)) {
72 xbt_assert0(0, "Cannot create GTNetS link");
75 /* KF: Insert entry in the dictionary */
76 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
77 gtnets_link->name = name;
78 gtnets_link->bw_current = bw;
79 gtnets_link->lat_current = lat;
80 gtnets_link->id = link_count;
81 /* Add the properties */
82 gtnets_link->properties = props;
84 xbt_dict_set(link_set, name, gtnets_link, link_free);
89 /* free the network card */
90 static void network_card_free(void *nw_card)
92 free(((network_card_GTNETS_t) nw_card)->name);
96 /* Instantiate a new network card: MODIFYED BY KF */
97 static int network_card_new(const char *name)
99 static int card_count = -1;
101 /* KF: Check that we haven't seen the network card before */
102 network_card_GTNETS_t card =
103 xbt_dict_get_or_null(network_card_set, 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(network_card_set, name, card, network_card_free);
116 /* KF: just return the GTNetS ID as the SURF ID */
120 /* Instantiate a new route: MODIFY BY KF */
121 static void route_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
126 /* KF: Build the list of gtnets link IDs */
127 gtnets_links = (int *) calloc(nb_link, sizeof(int));
128 for (i = 0; i < nb_link; i++) {
129 gtnets_links[i] = links[i]->id;
132 /* KF: Create the GTNets route */
133 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
134 xbt_assert0(0, "Cannot create GTNetS route");
138 /* Instantiate a new route: MODIFY BY KF */
139 static void route_onehop_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
144 xbt_assert0(0, "In onehop_new, nb_link should be 1");
147 /* KF: Build the linbst of gtnets link IDs */
148 linkid = links[0]->id;
150 /* KF: Create the GTNets route */
151 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
152 xbt_assert0(0, "Cannot create GTNetS route");
158 /* Parse the XML for a network link */
159 static void parse_link_init(void)
164 e_surf_link_state_t state;
166 name = xbt_strdup(A_surfxml_link_id);
167 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
168 surf_parse_get_double(&lat, A_surfxml_link_latency);
169 state = SURF_LINK_ON;
171 tmgr_trace_t bw_trace;
172 tmgr_trace_t state_trace;
173 tmgr_trace_t lat_trace;
175 surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
176 surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
177 surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
180 INFO0("The GTNetS network model doesn't support bandwidth state traces");
182 INFO0("The GTNetS network model doesn't support latency state traces");
184 INFO0("The GTNetS network model doesn't support link state traces");
186 current_property_set = xbt_dict_new();
187 link_new(name, bw, lat, current_property_set);
190 /* Parses a route from the XML: UNMODIFIED BY HC */
191 static void parse_route_set_endpoints(void)
193 src_id = network_card_new(A_surfxml_route_src);
194 dst_id = network_card_new(A_surfxml_route_dst);
195 route_action = A_surfxml_route_action;
199 static void parse_route_set_routers(void)
201 int id = network_card_new(A_surfxml_router_id);
203 /* KF: Create the GTNets router */
204 if (gtnets_add_router(id)) {
205 xbt_assert0(0, "Cannot add GTNetS router");
209 /* Create the route (more than one hops): MODIFIED BY KF */
210 static void parse_route_set_route(void)
213 if (src_id != -1 && dst_id != -1) {
214 name = bprintf("%x#%x",src_id, dst_id);
215 manage_route(route_table, name, route_action, 0);
220 static void add_route()
223 unsigned int cpt = 0;
224 int link_list_capacity = 0;
226 xbt_dict_cursor_t cursor = NULL;
227 char *key,*data, *end;
228 const char *sep = "#";
229 xbt_dynar_t links, keys;
230 static network_link_GTNETS_t *link_list = NULL;
233 DEBUG0("Entering add_route()");
234 xbt_dict_foreach(route_table, cursor, key, data) {
237 links = (xbt_dynar_t)data;
238 keys = xbt_str_split_str(key, sep);
240 link_list_capacity = xbt_dynar_length(links);
241 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
243 src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
244 dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
245 xbt_dynar_free(&keys);
247 xbt_dynar_foreach (links, cpt, link) {
249 link_list[nb_link++] = xbt_dict_get(link_set, link);
252 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
256 route_new(src_id, dst_id, link_list, nb_link);
258 route_onehop_new(src_id, dst_id, link_list, nb_link);
261 xbt_dict_free(&route_table);
262 DEBUG0("Bailling add_route()");
265 /* Main XML parsing */
266 static void define_callbacks(const char *file)
268 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
269 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
270 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
271 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
272 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
275 static void *name_service(const char *name)
277 return xbt_dict_get_or_null(network_card_set, name);
280 static const char *get_resource_name(void *resource_id)
282 return ((network_card_GTNETS_t) resource_id)->name;
285 static xbt_dict_t get_properties(void *link)
287 return ((network_card_GTNETS_t) link)->properties;
291 /* We do not care about this: only used for traces */
292 static int resource_used(void *resource_id)
294 return 0; /* We don't care */
297 static int action_free(surf_action_t action)
300 if (!action->using) {
301 xbt_swag_remove(action, action->state_set);
302 /* KF: No explicit freeing needed for GTNeTS here */
309 static void action_use(surf_action_t action)
314 static void action_cancel(surf_action_t action)
316 xbt_assert0(0, "Cannot cancel GTNetS flow");
320 static void action_recycle(surf_action_t action)
322 xbt_assert0(0, "Cannot recycle GTNetS flow");
326 static void action_change_state(surf_action_t action,
327 e_surf_action_state_t state)
329 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
330 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
331 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
332 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
335 surf_action_change_state(action, state);
340 /* share_resources() */
341 static double share_resources(double now)
343 xbt_swag_t running_actions = surf_network_model->common_public->states.running_action_set;
345 //get the first relevant value from the running_actions list
346 if (!xbt_swag_size(running_actions))
349 return gtnets_get_time_to_next_flow_completion();
352 /* delta: by how many time units the simulation must advance */
353 /* In this function: change the state of actions that terminate */
354 /* The delta may not come from the network, and thus may be different (smaller)
355 than the one returned by the function above */
356 /* If the delta is a network-caused min, then do not emulate any timer in the
357 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
359 static void update_actions_state(double now, double delta)
361 surf_action_network_GTNETS_t action = NULL;
362 // surf_action_network_GTNETS_t next_action = NULL;
363 xbt_swag_t running_actions =
364 surf_network_model->common_public->states.running_action_set;
366 double time_to_next_flow_completion =
367 gtnets_get_time_to_next_flow_completion();
369 /* If there are no renning flows, just return */
370 if (time_to_next_flow_completion < 0.0) {
374 /*KF: if delta == time_to_next_flow_completion, too. */
375 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
381 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
383 "Cannot run GTNetS simulation until next flow completion");
387 "GTNetS simulation couldn't find a flow that would complete");
392 xbt_swag_foreach(action, running_actions) {
393 DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
394 double remain = gtnets_get_flow_rx(action);
395 //need to trust this remain value
397 action->generic_action.remains=0;
399 action->generic_action.remains-=remain;
401 DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
404 for (i = 0; i < num_flows; i++) {
405 action = (surf_action_network_GTNETS_t) (metadata[i]);
407 action->generic_action.finish = now + time_to_next_flow_completion;
408 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
409 /* TODO: Anything else here? */
411 //need to map this action to the gtnets engine
412 DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
413 action->generic_action.remains -= gtnets_get_flow_rx(metadata[i]);
414 DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
418 } else { /* run for a given number of seconds */
419 if (gtnets_run(delta)) {
420 xbt_assert0(0, "Cannot run GTNetS simulation");
427 /* UNUSED HERE: no traces */
428 static void update_resource_state(void *id,
429 tmgr_trace_event_t event_type,
430 double value, double date)
432 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
436 /* KF: Rate not supported */
437 static surf_action_t communicate(void *src, void *dst, double size,
440 surf_action_network_GTNETS_t action = NULL;
441 network_card_GTNETS_t card_src = src;
442 network_card_GTNETS_t card_dst = dst;
444 int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
445 network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
449 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);
452 action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
454 action->generic_action.using = 1;
455 action->generic_action.cost = size;
456 action->generic_action.remains = size;
457 /* Max durations are not supported */
458 action->generic_action.max_duration = NO_MAX_DURATION;
459 action->generic_action.start = surf_get_clock();
460 action->generic_action.finish = -1.0;
461 action->generic_action.model_type =
462 (surf_model_t) surf_network_model;
464 action->generic_action.state_set =
465 surf_network_model->common_public->states.running_action_set;
467 xbt_swag_insert(action, action->generic_action.state_set);
469 /* KF: Add a flow to the GTNets Simulation, associated to this action */
470 if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *) action)
472 xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
476 return (surf_action_t) action;
479 /* Suspend a flow() */
480 static void action_suspend(surf_action_t action)
483 "action_suspend() not supported for the GTNets network model");
486 /* Resume a flow() */
487 static void action_resume(surf_action_t action)
490 "action_resume() not supported for the GTNets network model");
493 /* Test whether a flow is suspended */
494 static int action_is_suspended(surf_action_t action)
499 static void finalize(void)
504 xbt_dict_free(&network_card_set);
505 xbt_dict_free(&link_set);
506 xbt_swag_free(surf_network_model->common_public->states.
508 xbt_swag_free(surf_network_model->common_public->states.
510 xbt_swag_free(surf_network_model->common_public->states.
512 xbt_swag_free(surf_network_model->common_public->states.
514 free(surf_network_model->common_public);
515 free(surf_network_model->common_private);
516 free(surf_network_model->extension_public);
518 free(surf_network_model);
519 surf_network_model = NULL;
524 static void surf_network_model_init_internal(void)
526 s_surf_action_t action;
528 surf_network_model = xbt_new0(s_surf_network_model_t, 1);
530 surf_network_model->common_private =
531 xbt_new0(s_surf_model_private_t, 1);
532 surf_network_model->common_public =
533 xbt_new0(s_surf_model_public_t, 1);
534 surf_network_model->extension_public =
535 xbt_new0(s_surf_network_model_extension_public_t, 1);
537 surf_network_model->common_public->states.ready_action_set =
538 xbt_swag_new(xbt_swag_offset(action, state_hookup));
539 surf_network_model->common_public->states.running_action_set =
540 xbt_swag_new(xbt_swag_offset(action, state_hookup));
541 surf_network_model->common_public->states.failed_action_set =
542 xbt_swag_new(xbt_swag_offset(action, state_hookup));
543 surf_network_model->common_public->states.done_action_set =
544 xbt_swag_new(xbt_swag_offset(action, state_hookup));
546 surf_network_model->common_public->name_service = name_service;
547 surf_network_model->common_public->get_resource_name =
549 surf_network_model->common_public->action_get_state =
550 surf_action_get_state;
551 surf_network_model->common_public->action_use = action_use;
552 surf_network_model->common_public->action_free = action_free;
553 surf_network_model->common_public->action_cancel = action_cancel;
554 surf_network_model->common_public->action_recycle = action_recycle;
555 surf_network_model->common_public->action_change_state =
557 surf_network_model->common_public->action_set_data =
558 surf_action_set_data;
559 surf_network_model->common_public->name = "network";
561 surf_network_model->common_private->resource_used = resource_used;
562 surf_network_model->common_private->share_resources = share_resources;
563 surf_network_model->common_private->update_actions_state =
564 update_actions_state;
565 surf_network_model->common_private->update_resource_state = update_resource_state;
566 surf_network_model->common_private->finalize = finalize;
568 surf_network_model->common_public->suspend = action_suspend;
569 surf_network_model->common_public->resume = action_resume;
570 surf_network_model->common_public->is_suspended = action_is_suspended;
572 surf_network_model->extension_public->communicate = communicate;
574 /*for the props of the link*/
575 surf_network_model->common_public->get_properties = get_properties;
577 link_set = xbt_dict_new();
578 network_card_set = xbt_dict_new();
580 /* KF: Added the initialization for GTNetS interface */
581 if (gtnets_initialize()) {
582 xbt_assert0(0, "impossible to initialize GTNetS interface");
587 void surf_network_model_init_GTNETS(const char *filename)
589 if (surf_network_model)
591 surf_network_model_init_internal();
592 define_callbacks(filename);
593 xbt_dynar_push(model_list, &surf_network_model);
595 update_model_description(surf_network_model_description,
597 (surf_model_t) surf_network_model);