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, surf_resource_free);
83 /* Instantiate a new network card: MODIFYED BY KF */
84 static int network_card_new(const char *name)
86 static int card_count = -1;
88 XBT_IN1("(%s)", name);
89 /* KF: Check that we haven't seen the network card before */
90 network_card_GTNETS_t card =
91 surf_model_resource_by_name(surf_network_model, name);
94 /* KF: Increment the card counter for GTNetS */
97 /* KF: just use the dictionary to map link names to link indices */
98 card = xbt_new0(s_network_card_GTNETS_t, 1);
99 card->name = xbt_strdup(name);
100 card->id = card_count;
101 xbt_dict_set(surf_model_resource_set(surf_network_model), name, card,
105 LOG1(xbt_log_priority_trace, " return %d", card->id);
107 /* KF: just return the GTNetS ID as the SURF ID */
111 /* Instantiate a new route: MODIFY BY KF */
112 static void route_new(int src_id, int dst_id, network_link_GTNETS_t * links,
117 XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
118 src_id, dst_id, links, nb_link);
120 /* KF: Build the list of gtnets link IDs */
121 gtnets_links = (int *) calloc(nb_link, sizeof(int));
122 for (i = 0; i < nb_link; i++) {
123 gtnets_links[i] = links[i]->id;
126 /* KF: Create the GTNets route */
127 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
128 xbt_assert0(0, "Cannot create GTNetS route");
133 /* Instantiate a new route: MODIFY BY KF */
134 static void route_onehop_new(int src_id, int dst_id,
135 network_link_GTNETS_t * links, int nb_link)
140 xbt_assert0(0, "In onehop_new, nb_link should be 1");
143 /* KF: Build the linbst of gtnets link IDs */
144 linkid = links[0]->id;
146 /* KF: Create the GTNets route */
147 if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
148 xbt_assert0(0, "Cannot create GTNetS route");
154 /* Parse the XML for a network link */
155 static void parse_link_init(void)
160 e_surf_link_state_t state;
162 name = xbt_strdup(A_surfxml_link_id);
163 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
164 surf_parse_get_double(&lat, A_surfxml_link_latency);
165 state = SURF_LINK_ON;
167 tmgr_trace_t bw_trace;
168 tmgr_trace_t state_trace;
169 tmgr_trace_t lat_trace;
171 surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
172 surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
173 surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
176 INFO0("The GTNetS network model doesn't support bandwidth state traces");
178 INFO0("The GTNetS network model doesn't support latency state traces");
180 INFO0("The GTNetS network model doesn't support link state traces");
182 current_property_set = xbt_dict_new();
183 link_new(name, bw, lat, current_property_set);
186 /* Parses a route from the XML: UNMODIFIED BY HC */
187 static void parse_route_set_endpoints(void)
189 src_id = network_card_new(A_surfxml_route_src);
190 dst_id = network_card_new(A_surfxml_route_dst);
191 route_action = A_surfxml_route_action;
195 static void parse_route_set_routers(void)
197 int id = network_card_new(A_surfxml_router_id);
199 /* KF: Create the GTNets router */
200 if (gtnets_add_router(id)) {
201 xbt_assert0(0, "Cannot add GTNetS router");
205 /* Create the route (more than one hops): MODIFIED BY KF */
206 static void parse_route_set_route(void)
209 if (src_id != -1 && dst_id != -1) {
210 name = bprintf("%x#%x", src_id, dst_id);
211 manage_route(route_table, name, route_action, 0);
216 static void add_route()
219 unsigned int cpt = 0;
220 int link_list_capacity = 0;
222 xbt_dict_cursor_t cursor = NULL;
223 char *key, *data, *end;
224 const char *sep = "#";
225 xbt_dynar_t links, keys;
226 static network_link_GTNETS_t *link_list = NULL;
230 xbt_dict_foreach(route_table, cursor, key, data) {
233 links = (xbt_dynar_t) data;
234 keys = xbt_str_split_str(key, sep);
236 link_list_capacity = xbt_dynar_length(links);
237 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
239 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
240 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
241 xbt_dynar_free(&keys);
243 xbt_dynar_foreach(links, cpt, link) {
245 link_list[nb_link++] = xbt_dict_get(surf_network_model->resource_set, link);
248 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
252 route_onehop_new(src_id, dst_id, link_list, nb_link);
255 xbt_dict_foreach(route_table, cursor, key, data) {
258 links = (xbt_dynar_t) data;
259 keys = xbt_str_split_str(key, sep);
261 link_list_capacity = xbt_dynar_length(links);
262 link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
264 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
265 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
266 xbt_dynar_free(&keys);
268 xbt_dynar_foreach(links, cpt, link) {
270 link_list[nb_link++] = xbt_dict_get(surf_network_model->resource_set, link);
273 RETHROW1("Link %s not found (dict raised this exception: %s)", link);
277 route_new(src_id, dst_id, link_list, nb_link);
280 xbt_dict_free(&route_table);
281 gtnets_print_topology();
285 /* Main XML parsing */
286 static void define_callbacks(const char *file)
288 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
289 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
290 surfxml_add_callback(STag_surfxml_route_cb_list,
291 &parse_route_set_endpoints);
292 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
293 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
296 /* We do not care about this: only used for traces */
297 static int resource_used(void *resource_id)
299 return 0; /* We don't care */
302 static int action_unref(surf_action_t action)
305 if (!action->refcount) {
306 xbt_swag_remove(action, action->state_set);
307 /* KF: No explicit freeing needed for GTNeTS here */
314 static void action_cancel(surf_action_t action)
316 xbt_die("Cannot cancel GTNetS flow");
320 static void action_recycle(surf_action_t action)
322 xbt_die("Cannot recycle GTNetS flow");
326 static void action_state_set(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_state_set(action, state);
340 /* share_resources() */
341 static double share_resources(double now)
343 xbt_swag_t running_actions =
344 surf_network_model->states.running_action_set;
346 //get the first relevant value from the running_actions list
347 if (!xbt_swag_size(running_actions))
350 xbt_assert0(time_to_next_flow_completion,
351 "Time to next flow completion not initialized!\n");
353 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
355 return time_to_next_flow_completion;
358 /* delta: by how many time units the simulation must advance */
359 /* In this function: change the state of actions that terminate */
360 /* The delta may not come from the network, and thus may be different (smaller)
361 than the one returned by the function above */
362 /* If the delta is a network-caused min, then do not emulate any timer in the
363 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
365 static void update_actions_state(double now, double delta)
367 surf_action_network_GTNETS_t action = NULL;
368 // surf_action_network_GTNETS_t next_action = NULL;
369 xbt_swag_t running_actions =
370 surf_network_model->states.running_action_set;
372 /* If there are no renning flows, just return */
373 if (time_to_next_flow_completion < 0.0) {
377 /*KF: if delta == time_to_next_flow_completion, too. */
378 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
384 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
386 "Cannot run GTNetS simulation until next flow completion");
390 "GTNetS simulation couldn't find a flow that would complete");
393 xbt_swag_foreach(action, running_actions) {
394 DEBUG2("Action (%p) remains old value: %f", action,
395 action->generic_action.remains);
396 double remain = gtnets_get_flow_rx(action);
397 DEBUG1("Remain value returned by GTNetS : %f", remain);
398 //need to trust this remain value
400 action->generic_action.remains = 0;
402 action->generic_action.remains = action->generic_action.cost - remain;
404 DEBUG2("Action (%p) remains new value: %f", action,
405 action->generic_action.remains);
408 for (i = 0; i < num_flows; i++) {
409 action = (surf_action_network_GTNETS_t) (metadata[i]);
411 action->generic_action.finish = now + time_to_next_flow_completion;
412 action_state_set((surf_action_t) action, SURF_ACTION_DONE);
413 DEBUG1("----> Action (%p) just terminated", action);
417 } else { /* run for a given number of seconds */
418 if (gtnets_run(delta)) {
419 xbt_assert0(0, "Cannot run GTNetS simulation");
426 /* UNUSED HERE: no traces */
427 static void update_resource_state(void *id,
428 tmgr_trace_event_t event_type,
429 double value, double date)
431 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
435 /* KF: Rate not supported */
436 /* Max durations are not supported */
437 static surf_action_t communicate(const char *src_name, const char *dst_name,int src, int dst, double size,
440 surf_action_network_GTNETS_t action = NULL;
442 action = surf_action_new(sizeof(s_surf_action_network_GTNETS_t),size,surf_network_model,0);
444 /* KF: Add a flow to the GTNets Simulation, associated to this action */
445 if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
446 xbt_assert2(0, "Not route between host %s and host %s", src_name,
450 return (surf_action_t) action;
453 /* Suspend a flow() */
454 static void action_suspend(surf_action_t action)
459 /* Resume a flow() */
460 static void action_resume(surf_action_t action)
465 /* Test whether a flow is suspended */
466 static int action_is_suspended(surf_action_t action)
471 static void finalize(void)
473 xbt_dict_free(&surf_network_model->resource_set);
475 surf_model_exit(surf_network_model);
477 free(surf_network_model);
478 surf_network_model = NULL;
483 static void surf_network_model_init_internal(void)
485 surf_network_model = surf_model_init();
487 surf_network_model->name = "network GTNetS";
488 surf_network_model->action_unref = action_unref;
489 surf_network_model->action_cancel = action_cancel;
490 surf_network_model->action_recycle = action_recycle;
491 surf_network_model->action_state_set = action_state_set;
493 surf_network_model->model_private->resource_used = resource_used;
494 surf_network_model->model_private->share_resources = share_resources;
495 surf_network_model->model_private->update_actions_state =
496 update_actions_state;
497 surf_network_model->model_private->update_resource_state =
498 update_resource_state;
499 surf_network_model->model_private->finalize = finalize;
501 surf_network_model->suspend = action_suspend;
502 surf_network_model->resume = action_resume;
503 surf_network_model->is_suspended = action_is_suspended;
505 surf_network_model->extension.network.communicate = communicate;
507 /* KF: Added the initialization for GTNetS interface */
508 if (gtnets_initialize()) {
509 xbt_assert0(0, "impossible to initialize GTNetS interface");
514 void surf_network_model_init_GTNETS(const char *filename)
516 if (surf_network_model)
518 surf_network_model_init_internal();
519 define_callbacks(filename);
520 xbt_dynar_push(model_list, &surf_network_model);
522 update_model_description(surf_network_model_description,
523 "GTNets", surf_network_model);