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 /* 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 network_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(surf_network_model->resource_set, name)) {
45 /* KF: Increment the link counter for GTNetS */
49 nw_link->model = 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_network_link_GTNETS_t, 1);
72 gtnets_link->generic_resource.name = name;
73 gtnets_link->generic_resource.properties = props;
74 gtnets_link->bw_current = bw;
75 gtnets_link->lat_current = lat;
76 gtnets_link->id = link_count;
78 xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
84 /* Instantiate a new network card: MODIFYED BY KF */
85 static int network_card_new(const char *name)
87 static int card_count = -1;
89 XBT_IN1("(%s)", name);
90 /* KF: Check that we haven't seen the network card before */
91 network_card_GTNETS_t card =
92 surf_model_resource_by_name(surf_network_model, name);
95 /* KF: Increment the card counter for GTNetS */
98 /* KF: just use the dictionary to map link names to link indices */
99 card = xbt_new0(s_network_card_GTNETS_t, 1);
100 card->name = xbt_strdup(name);
101 card->id = card_count;
102 xbt_dict_set(surf_model_resource_set(surf_network_model), name, card,
106 LOG1(xbt_log_priority_trace, " return %d", card->id);
108 /* KF: just return the GTNetS ID as the SURF ID */
112 /* Instantiate a new route: MODIFY BY KF */
113 static void route_new(int src_id, int dst_id, network_link_GTNETS_t * links,
118 XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
119 src_id, dst_id, links, nb_link);
121 /* KF: Build the list of gtnets link IDs */
122 gtnets_links = (int *) calloc(nb_link, sizeof(int));
123 for (i = 0; i < nb_link; i++) {
124 gtnets_links[i] = links[i]->id;
127 /* KF: Create the GTNets route */
128 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
129 xbt_assert0(0, "Cannot create GTNetS route");
134 /* Instantiate a new route: MODIFY BY KF */
135 static void route_onehop_new(int src_id, int dst_id,
136 network_link_GTNETS_t * links, int nb_link)
141 xbt_assert0(0, "In onehop_new, nb_link should be 1");
144 /* KF: Build the linbst of gtnets link IDs */
145 linkid = links[0]->id;
147 /* KF: Create the GTNets route */
148 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
149 xbt_assert0(0, "Cannot create GTNetS route");
155 /* Parse the XML for a network link */
156 static void parse_link_init(void)
161 e_surf_resource_state_t state;
163 name = xbt_strdup(A_surfxml_link_id);
164 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
165 surf_parse_get_double(&lat, A_surfxml_link_latency);
166 state = SURF_RESOURCE_ON;
168 tmgr_trace_t bw_trace;
169 tmgr_trace_t state_trace;
170 tmgr_trace_t lat_trace;
172 bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
173 lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
174 state_trace = tmgr_trace_new(A_surfxml_link_state_file);
177 INFO0("The GTNetS network model doesn't support bandwidth state traces");
179 INFO0("The GTNetS network model doesn't support latency state traces");
181 INFO0("The GTNetS network model doesn't support link state traces");
183 current_property_set = xbt_dict_new();
184 link_new(name, bw, lat, current_property_set);
187 /* Parses a route from the XML: UNMODIFIED BY HC */
188 static void parse_route_set_endpoints(void)
190 src_id = network_card_new(A_surfxml_route_src);
191 dst_id = network_card_new(A_surfxml_route_dst);
192 route_action = A_surfxml_route_action;
196 static void parse_route_set_routers(void)
198 int id = network_card_new(A_surfxml_router_id);
200 /* KF: Create the GTNets router */
201 if (gtnets_add_router(id)) {
202 xbt_assert0(0, "Cannot add GTNetS router");
206 /* Create the route (more than one hops): MODIFIED BY KF */
207 static void parse_route_set_route(void)
210 if (src_id != -1 && dst_id != -1) {
211 name = bprintf("%x#%x", src_id, dst_id);
212 manage_route(route_table, name, route_action, 0);
217 static void add_route()
220 unsigned int cpt = 0;
221 int link_list_capacity = 0;
223 xbt_dict_cursor_t cursor = NULL;
224 char *key, *data, *end;
225 const char *sep = "#";
226 xbt_dynar_t links, keys;
227 static network_link_GTNETS_t *link_list = NULL;
231 xbt_dict_foreach(route_table, cursor, key, data) {
234 links = (xbt_dynar_t) data;
235 keys = xbt_str_split_str(key, sep);
237 link_list_capacity = xbt_dynar_length(links);
238 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
240 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
241 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
242 xbt_dynar_free(&keys);
244 xbt_dynar_foreach(links, cpt, link) {
246 link_list[nb_link++] =
247 xbt_dict_get(surf_network_model->resource_set, link);
250 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
254 route_onehop_new(src_id, dst_id, link_list, nb_link);
257 xbt_dict_foreach(route_table, cursor, key, data) {
260 links = (xbt_dynar_t) data;
261 keys = xbt_str_split_str(key, sep);
263 link_list_capacity = xbt_dynar_length(links);
264 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
266 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
267 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
268 xbt_dynar_free(&keys);
270 xbt_dynar_foreach(links, cpt, link) {
272 link_list[nb_link++] =
273 xbt_dict_get(surf_network_model->resource_set, link);
276 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
280 route_new(src_id, dst_id, link_list, nb_link);
283 xbt_dict_free(&route_table);
284 gtnets_print_topology();
288 /* Main XML parsing */
289 static void define_callbacks(const char *file)
291 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
292 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
293 surfxml_add_callback(STag_surfxml_route_cb_list,
294 &parse_route_set_endpoints);
295 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
296 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
299 /* We do not care about this: only used for traces */
300 static int resource_used(void *resource_id)
302 return 0; /* We don't care */
305 static int action_unref(surf_action_t action)
308 if (!action->refcount) {
309 xbt_swag_remove(action, action->state_set);
310 /* KF: No explicit freeing needed for GTNeTS here */
317 static void action_cancel(surf_action_t action)
319 xbt_die("Cannot cancel GTNetS flow");
323 static void action_recycle(surf_action_t action)
325 xbt_die("Cannot recycle GTNetS flow");
329 static double action_get_remains(surf_action_t action)
331 return action->remains;
334 static void action_state_set(surf_action_t action,
335 e_surf_action_state_t state)
337 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
338 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
339 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
340 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
343 surf_action_state_set(action, state);
348 /* share_resources() */
349 static double share_resources(double now)
351 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
353 //get the first relevant value from the running_actions list
354 if (!xbt_swag_size(running_actions))
357 xbt_assert0(time_to_next_flow_completion,
358 "Time to next flow completion not initialized!\n");
360 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
362 return time_to_next_flow_completion;
365 /* delta: by how many time units the simulation must advance */
366 /* In this function: change the state of actions that terminate */
367 /* The delta may not come from the network, and thus may be different (smaller)
368 than the one returned by the function above */
369 /* If the delta is a network-caused min, then do not emulate any timer in the
370 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
372 static void update_actions_state(double now, double delta)
374 surf_action_network_GTNETS_t action = NULL;
375 // surf_action_network_GTNETS_t next_action = NULL;
376 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
378 /* If there are no renning flows, just return */
379 if (time_to_next_flow_completion < 0.0) {
383 /*KF: if delta == time_to_next_flow_completion, too. */
384 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
390 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
392 "Cannot run GTNetS simulation until next flow completion");
396 "GTNetS simulation couldn't find a flow that would complete");
399 xbt_swag_foreach(action, running_actions) {
400 DEBUG2("Action (%p) remains old value: %f", action,
401 action->generic_action.remains);
402 double remain = gtnets_get_flow_rx(action);
403 DEBUG1("Remain value returned by GTNetS : %f", remain);
404 //need to trust this remain value
406 action->generic_action.remains = 0;
408 action->generic_action.remains = action->generic_action.cost - remain;
410 DEBUG2("Action (%p) remains new value: %f", action,
411 action->generic_action.remains);
414 for (i = 0; i < num_flows; i++) {
415 action = (surf_action_network_GTNETS_t) (metadata[i]);
417 action->generic_action.finish = now + time_to_next_flow_completion;
418 action_state_set((surf_action_t) action, SURF_ACTION_DONE);
419 DEBUG1("----> Action (%p) just terminated", action);
423 } else { /* run for a given number of seconds */
424 if (gtnets_run(delta)) {
425 xbt_assert0(0, "Cannot run GTNetS simulation");
432 /* UNUSED HERE: no traces */
433 static void update_resource_state(void *id,
434 tmgr_trace_event_t event_type,
435 double value, double date)
437 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
441 /* KF: Rate not supported */
442 /* Max durations are not supported */
443 static surf_action_t communicate(const char *src_name, const char *dst_name,
444 int src, int dst, double size, double rate)
446 surf_action_network_GTNETS_t action = NULL;
449 surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
450 surf_network_model, 0);
452 /* KF: Add a flow to the GTNets Simulation, associated to this action */
453 if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
454 xbt_assert2(0, "Not route between host %s and host %s", src_name,
458 return (surf_action_t) action;
461 /* Suspend a flow() */
462 static void action_suspend(surf_action_t action)
467 /* Resume a flow() */
468 static void action_resume(surf_action_t action)
473 /* Test whether a flow is suspended */
474 static int action_is_suspended(surf_action_t action)
479 static void finalize(void)
481 xbt_dict_free(&surf_network_model->resource_set);
483 surf_model_exit(surf_network_model);
485 free(surf_network_model);
486 surf_network_model = NULL;
491 static void surf_network_model_init_internal(void)
493 surf_network_model = surf_model_init();
495 surf_network_model->name = "network GTNetS";
496 surf_network_model->action_unref = action_unref;
497 surf_network_model->action_cancel = action_cancel;
498 surf_network_model->action_recycle = action_recycle;
499 surf_network_model->action_state_set = action_state_set;
500 surf_network_model->get_remains = action_get_remains;
502 surf_network_model->model_private->resource_used = resource_used;
503 surf_network_model->model_private->share_resources = share_resources;
504 surf_network_model->model_private->update_actions_state =
505 update_actions_state;
506 surf_network_model->model_private->update_resource_state =
507 update_resource_state;
508 surf_network_model->model_private->finalize = finalize;
510 surf_network_model->suspend = action_suspend;
511 surf_network_model->resume = action_resume;
512 surf_network_model->is_suspended = action_is_suspended;
514 surf_network_model->extension.network.communicate = communicate;
516 /* KF: Added the initialization for GTNetS interface */
517 if (gtnets_initialize()) {
518 xbt_assert0(0, "impossible to initialize GTNetS interface");
523 void surf_network_model_init_GTNETS(const char *filename)
525 if (surf_network_model)
527 surf_network_model_init_internal();
528 define_callbacks(filename);
529 xbt_dynar_push(model_list, &surf_network_model);
531 update_model_description(surf_network_model_description,
532 "GTNets", surf_network_model);