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 /** QUESTIONS for GTNetS integration
15 ** 1. Check that we did the right thing with name_service and get_resource_name
16 ** 2. Right now there is no "kill flow" in our GTNetS implementation. Do we
17 ** need to do something about this?
18 ** 3. We ignore the fact there is some max_duration on flows (see #2 above)
19 ** 4. share_resources() returns a duration, not a date, right?
20 ** 5. We don't suppoer "rates"
21 ** 6. We don't update "remaining" for ongoing flows. Is it bad?
24 /* Free memory for a network link */
25 static void link_free(void *nw_link)
27 free(((link_GTNETS_t) nw_link)->name);
31 /* Instantiate a new network link */
32 /* name: some name for the link, from the XML */
33 /* bw: The bandwidth value */
34 /* lat: The latency value */
35 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
37 static int link_count = -1;
38 link_GTNETS_t gtnets_link;
40 /* If link already exists, nothing to do (FIXME: check that multiple definition match?) */
41 if (xbt_dict_get_or_null(link_set, name)) {
45 /* KF: Increment the link counter for GTNetS */
49 nw_link->model = (surf_model_t) surf_network_model;
51 nw_link->bw_current = bw_initial;
54 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
55 nw_link->lat_current = lat_initial;
58 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
59 nw_link->state_current = state_initial;
61 nw_link->state_event =
62 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
65 /* KF: Add the link to the GTNetS simulation */
66 if (gtnets_add_link(link_count, bw, lat)) {
67 xbt_assert0(0, "Cannot create GTNetS link");
70 /* KF: Insert entry in the dictionary */
71 gtnets_link = xbt_new0(s_link_GTNETS_t, 1);
72 gtnets_link->name = name;
73 gtnets_link->bw_current = bw;
74 gtnets_link->lat_current = lat;
75 gtnets_link->id = link_count;
76 /* Add the properties */
77 gtnets_link->properties = current_property_set;
79 xbt_dict_set(link_set, name, gtnets_link, link_free);
84 /* free the network card */
85 static void network_card_free(void *nw_card)
87 free(((network_card_GTNETS_t) nw_card)->name);
91 /* Instantiate a new network card: MODIFYED BY KF */
92 static int network_card_new(const char *name)
94 static int card_count = -1;
96 /* KF: Check that we haven't seen the network card before */
97 network_card_GTNETS_t card =
98 xbt_dict_get_or_null(network_card_set, name);
101 /* KF: Increment the card counter for GTNetS */
104 /* KF: just use the dictionary to map link names to link indices */
105 card = xbt_new0(s_network_card_GTNETS_t, 1);
106 card->name = xbt_strdup(name);
107 card->id = card_count;
108 xbt_dict_set(network_card_set, name, card, network_card_free);
111 /* KF: just return the GTNetS ID as the SURF ID */
115 /* Instantiate a new route: MODIFY BY KF */
116 static void route_new(int src_id, int dst_id, char **links, int nb_link)
119 link_GTNETS_t *link_list = NULL;
122 ROUTE_SIZE(src_id, dst_id) = nb_link;
123 link_list = (ROUTE(src_id, dst_id) =
124 xbt_new0(link_GTNETS_t, nb_link));
125 for (i = 0; i < nb_link; i++) {
126 link_list[i] = xbt_dict_get_or_null(link_set, links[i]);
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++) {
139 (xbt_dict_get(link_set, links[i])))->id;
142 /* KF: Create the GTNets route */
143 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
144 xbt_assert0(0, "Cannot create GTNetS route");
148 /* Instantiate a new route: MODIFY BY KF */
149 static void route_onehop_new(int src_id, int dst_id, char **links,
155 xbt_assert0(0, "In onehop_new, nb_link should be 1");
158 /* KF: Build the list of gtnets link IDs */
161 (xbt_dict_get(link_set, links[0])))->id;
163 /* KF: Create the GTNets route */
164 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
165 xbt_assert0(0, "Cannot create GTNetS route");
171 /* Parse the XML for a network link */
172 static void parse_link_init(void)
177 e_surf_link_state_t state;
179 name = xbt_strdup(A_surfxml_link_name);
180 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
181 surf_parse_get_double(&lat, A_surfxml_link_latency);
182 state = SURF_link_ON;
184 /* Print values when no traces are specified */
186 tmgr_trace_t bw_trace;
187 tmgr_trace_t state_trace;
188 tmgr_trace_t lat_trace;
190 surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
191 surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
192 surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
194 /*TODO Where is WARNING0 defined??? */
198 ("The GTNetS network model doesn't support bandwidth state traces");
201 ("The GTNetS network model doesn't support latency state traces");
204 ("The GTNetS network model doesn't support link state traces");
207 /* KF: remove several arguments to link_new */
208 current_property_set = xbt_dict_new();
209 link_new(name, bw, lat, current_property_set);
212 static int nb_link = 0;
213 static char **link_name = NULL;
214 static int src_id = -1;
215 static int dst_id = -1;
217 /* Parses a route from the XML: UNMODIFIED BY HC */
218 static void parse_route_set_endpoints(void)
220 src_id = network_card_new(A_surfxml_route_src);
221 dst_id = network_card_new(A_surfxml_route_dst);
227 static void parse_route_set_routers(void)
229 int id = network_card_new(A_surfxml_router_name);
231 /* KF: Create the GTNets router */
232 if (gtnets_add_router(id)) {
233 xbt_assert0(0, "Cannot add GTNetS router");
237 /* Parses a route element from the XML: UNMODIFIED BY HC */
238 static void parse_route_elem(void)
241 link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
242 link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
245 /* Create the route (more than one hops): MODIFIED BY KF */
246 static void parse_route_set_route(void)
249 route_new(src_id, dst_id, link_name, nb_link);
252 /* Create the one-hope route: BY KF */
253 static void parse_route_set_onehop_route(void)
256 route_onehop_new(src_id, dst_id, link_name, nb_link);
259 /* Main XML parsing */
260 static void parse_file(const char *file)
262 surf_parse_reset_parser();
264 surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
265 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
266 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
267 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
268 surfxml_add_callback(ETag_surfxml_route_element_cb_list, &parse_route_elem);
269 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_onehop_route);
270 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
272 surf_parse_open(file);
273 xbt_assert1((!surf_parse()), "Parse error in %s", file);
278 static void *name_service(const char *name)
280 return xbt_dict_get_or_null(network_card_set, name);
283 static const char *get_resource_name(void *resource_id)
285 return ((network_card_GTNETS_t) resource_id)->name;
288 static xbt_dict_t get_properties(void *link)
290 return ((network_card_GTNETS_t) link)->properties;
294 /* We do not care about this: only used for traces */
295 static int resource_used(void *resource_id)
297 return 0; /* We don't care */
300 static int action_free(surf_action_t action)
303 if (!action->using) {
304 xbt_swag_remove(action, action->state_set);
305 /* KF: No explicit freeing needed for GTNeTS here */
312 static void action_use(surf_action_t action)
317 static void action_cancel(surf_action_t action)
319 xbt_assert0(0, "Cannot cancel GTNetS flow");
323 static void action_recycle(surf_action_t action)
325 xbt_assert0(0, "Cannot recycle GTNetS flow");
329 static void action_change_state(surf_action_t action,
330 e_surf_action_state_t state)
332 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
333 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
334 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
335 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
338 surf_action_change_state(action, state);
343 /* share_resources() */
344 static double share_resources(double now)
347 s_surf_action_network_GTNETS_t s_action;
348 surf_action_network_GTNETS_t action = NULL;
349 xbt_swag_t running_actions =
350 surf_network_model->common_public->states.running_action_set;
353 return gtnets_get_time_to_next_flow_completion();
356 /* delta: by how many time units the simulation must advance */
357 /* In this function: change the state of actions that terminate */
358 /* The delta may not come from the network, and thus may be different (smaller)
359 than the one returned by the function above */
360 /* If the delta is a network-caused min, then do not emulate any timer in the
361 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
363 static void update_actions_state(double now, double delta)
366 surf_action_network_GTNETS_t action = NULL;
367 surf_action_network_GTNETS_t next_action = NULL;
368 xbt_swag_t running_actions =
369 surf_network_model->common_public->states.running_action_set;
372 double time_to_next_flow_completion =
373 gtnets_get_time_to_next_flow_completion();
375 /* If there are no renning flows, just return */
376 if (time_to_next_flow_completion < 0.0) {
380 /*KF: if delta == time_to_next_flow_completion, too. */
381 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
387 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
389 "Cannot run GTNetS simulation until next flow completion");
393 "GTNetS simulation couldn't find a flow that would complete");
396 for (i = 0; i < num_flows; i++) {
397 surf_action_network_GTNETS_t action =
398 (surf_action_network_GTNETS_t) (metadata[i]);
400 action->generic_action.remains = 0;
401 action->generic_action.finish = now + time_to_next_flow_completion;
402 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
403 /* TODO: Anything else here? */
405 } else { /* run for a given number of seconds */
406 if (gtnets_run(delta)) {
407 xbt_assert0(0, "Cannot run GTNetS simulation");
414 /* UNUSED HERE: no traces */
415 static void update_resource_state(void *id,
416 tmgr_trace_event_t event_type,
419 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
423 /* KF: Rate not supported */
424 static surf_action_t communicate(void *src, void *dst, double size,
427 surf_action_network_GTNETS_t action = NULL;
428 network_card_GTNETS_t card_src = src;
429 network_card_GTNETS_t card_dst = dst;
431 int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
432 link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
436 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);
439 action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
441 action->generic_action.using = 1;
442 action->generic_action.cost = size;
443 action->generic_action.remains = size;
444 /* Max durations are not supported */
445 action->generic_action.max_duration = NO_MAX_DURATION;
446 action->generic_action.start = surf_get_clock();
447 action->generic_action.finish = -1.0;
448 action->generic_action.model_type =
449 (surf_model_t) surf_network_model;
451 action->generic_action.state_set =
452 surf_network_model->common_public->states.running_action_set;
454 xbt_swag_insert(action, action->generic_action.state_set);
456 /* KF: Add a flow to the GTNets Simulation, associated to this action */
457 if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *) action)
459 xbt_assert2(0, "Not route between host %s and host %s", card_src->name,
463 return (surf_action_t) action;
466 /* Suspend a flow() */
467 static void action_suspend(surf_action_t action)
470 "action_suspend() not supported for the GTNets network model");
473 /* Resume a flow() */
474 static void action_resume(surf_action_t action)
477 "action_resume() not supported for the GTNets network model");
480 /* Test whether a flow is suspended */
481 static int action_is_suspended(surf_action_t action)
486 static void finalize(void)
491 xbt_dict_free(&network_card_set);
492 xbt_dict_free(&link_set);
493 xbt_swag_free(surf_network_model->common_public->states.
495 xbt_swag_free(surf_network_model->common_public->states.
497 xbt_swag_free(surf_network_model->common_public->states.
499 xbt_swag_free(surf_network_model->common_public->states.
501 free(surf_network_model->common_public);
502 free(surf_network_model->common_private);
503 free(surf_network_model->extension_public);
505 free(surf_network_model);
506 surf_network_model = NULL;
509 for (i = 0; i < card_number; i++)
510 for (j = 0; j < card_number; j++)
513 routing_table = NULL;
514 free(routing_table_size);
515 routing_table_size = 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 =
566 update_resource_state;
567 surf_network_model->common_private->finalize = finalize;
569 surf_network_model->common_public->suspend = action_suspend;
570 surf_network_model->common_public->resume = action_resume;
571 surf_network_model->common_public->is_suspended = action_is_suspended;
573 surf_network_model->extension_public->communicate = communicate;
575 /*for the props of the link*/
576 surf_network_model->common_public->get_properties = get_properties;
578 link_set = xbt_dict_new();
579 network_card_set = xbt_dict_new();
581 /* KF: Added the initialization for GTNetS interface */
582 if (gtnets_initialize()) {
583 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 parse_file(filename);
593 xbt_dynar_push(model_list, &surf_network_model);
595 update_model_description(surf_network_model_description,
596 surf_network_model_description_size,
598 (surf_model_t) surf_network_model);