1 /* Copyright (c) 2007-2012. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "network_gtnets_private.h"
8 #include "gtnets/gtnets_interface.h"
10 #include "surf/surfxml_parse_values.h"
12 static double time_to_next_flow_completion = -1;
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
15 "Logging specific to the SURF network GTNetS module");
17 extern routing_platf_t routing_platf;
19 double sg_gtnets_jitter = 0.0;
20 int sg_gtnets_jitter_seed = 10;
22 static void link_new(const char *name, double bw, double lat, xbt_dict_t props)
24 static int link_count = -1;
25 network_link_GTNETS_t gtnets_link;
27 if (xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL)) {
31 XBT_DEBUG("Scanning link name %s", name);
34 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
35 gtnets_link->generic_resource.name = xbt_strdup(name);
36 gtnets_link->generic_resource.properties = props;
37 gtnets_link->bw_current = bw;
38 gtnets_link->lat_current = lat;
42 XBT_DEBUG("Adding new link, linkid %d, name %s, latency %g, bandwidth %g",
43 link_count, name, lat, bw);
45 if (gtnets_add_link(link_count, bw, lat)) {
46 xbt_die("Cannot create GTNetS link");
48 gtnets_link->id = link_count;
50 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, gtnets_link);
53 static void route_new(int src_id, int dst_id, xbt_dynar_t links,
56 network_link_GTNETS_t link;
61 XBT_IN("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
62 src_id, dst_id, links, nb_link);
64 /* Build the list of gtnets link IDs */
65 gtnets_links = xbt_new0(int, nb_link);
67 xbt_dynar_foreach(links, cursor, link) {
68 gtnets_links[i++] = link->id;
71 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
72 xbt_die("Cannot create GTNetS route");
77 static void route_onehop_new(int src_id, int dst_id,
78 network_link_GTNETS_t link)
80 if (gtnets_add_onehop_route(src_id, dst_id, link->id)) {
81 xbt_die("Cannot create GTNetS route");
85 /* Parse the XML for a network link */
86 static void parse_link_init(sg_platf_link_cbarg_t link)
88 XBT_DEBUG("link_gtnets");
90 if (link->bandwidth_trace)
92 ("The GTNetS network model doesn't support bandwidth state traces");
93 if (link->latency_trace)
94 XBT_INFO("The GTNetS network model doesn't support latency state traces");
95 if (link->state_trace)
96 XBT_INFO("The GTNetS network model doesn't support link state traces");
98 if (link->policy == SURF_LINK_FULLDUPLEX)
100 link_new(bprintf("%s_UP",link->id), link->bandwidth, link->latency, current_property_set);
101 link_new(bprintf("%s_DOWN",link->id), link->bandwidth, link->latency, current_property_set);
104 else link_new(link->id, link->bandwidth, link->latency, current_property_set);
105 current_property_set = NULL;
108 /* Create the gtnets topology based on routing strategy */
109 static void create_gtnets_topology(void)
111 XBT_DEBUG("Starting topology generation");
112 // FIXME: We should take the list of hosts/routers (in the routing module), number the elements of this list,
113 // and then you can use the id to reimplement properly the following GTNets calls
115 //get the onelinks from the parsed platform
116 xbt_dynar_t onelink_routes = routing_platf->get_onelink_routes();
120 //save them in trace file
123 xbt_dynar_foreach(onelink_routes, iter, onelink) {
124 void *link = onelink->link_ptr;
126 if(onelink->src->id != onelink->dst->id){
127 XBT_DEBUG("Link (#%p), src (#%s), dst (#%s), src_id = %d, dst_id = %d",
133 XBT_DEBUG("Calling one link route");
134 if(onelink->src->rc_type == SURF_NETWORK_ELEMENT_ROUTER){
135 gtnets_add_router(onelink->src->id);
137 if(onelink->dst->rc_type == SURF_NETWORK_ELEMENT_ROUTER){
138 gtnets_add_router(onelink->dst->id);
140 route_onehop_new(onelink->src->id, onelink->dst->id, (network_link_GTNETS_t)(link));
144 if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
145 gtnets_print_topology();
149 /* Main XML parsing */
150 static void define_callbacks(void)
152 /* Figuring out the network links */
153 sg_platf_link_add_cb (&parse_link_init);
154 sg_platf_postparse_add_cb(&create_gtnets_topology);
157 static int resource_used(void *resource_id)
159 xbt_die("The resource_used feature is not implemented in GTNets model");
162 static int action_unref(surf_action_t action)
165 if (!action->refcount) {
166 xbt_swag_remove(action, action->state_set);
168 xbt_free(action->category);
170 surf_action_free(&action);
176 static void action_cancel(surf_action_t action)
178 xbt_die("Cannot cancel GTNetS flow");
182 static void action_recycle(surf_action_t action)
184 xbt_die("Cannot recycle GTNetS flow");
188 static double action_get_remains(surf_action_t action)
190 return action->remains;
193 static void action_state_set(surf_action_t action,
194 e_surf_action_state_t state)
196 surf_action_state_set(action, state);
199 static double share_resources(double now)
201 xbt_swag_t running_actions =
202 surf_network_model->states.running_action_set;
204 //get the first relevant value from the running_actions list
205 if (!xbt_swag_size(running_actions))
208 xbt_assert(time_to_next_flow_completion,
209 "Time to next flow completion not initialized!\n");
211 XBT_DEBUG("Calling gtnets_get_time_to_next_flow_completion");
212 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
213 XBT_DEBUG("gtnets_get_time_to_next_flow_completion received %lg",
214 time_to_next_flow_completion);
216 return time_to_next_flow_completion;
219 static void update_actions_state(double now, double delta)
221 surf_action_network_GTNETS_t action = NULL;
222 xbt_swag_t running_actions =
223 surf_network_model->states.running_action_set;
225 /* If there are no running flows, just return */
226 if (time_to_next_flow_completion < 0.0) {
230 /* if delta == time_to_next_flow_completion, too. */
231 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
237 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
238 xbt_die("Cannot run GTNetS simulation until next flow completion");
241 xbt_die("GTNetS simulation couldn't find a flow that would complete");
244 xbt_swag_foreach(action, running_actions) {
245 XBT_DEBUG("Action (%p) remains old value: %f", action,
246 action->generic_action.remains);
247 double sent = gtnets_get_flow_rx(action);
249 XBT_DEBUG("Sent value returned by GTNetS : %f", sent);
252 action->last_remains = action->generic_action.remains;
255 //need to trust this remain value
257 action->generic_action.remains = 0;
259 action->generic_action.remains =
260 action->generic_action.cost - sent;
263 // verify that this action is a finishing action.
265 for (i = 0; i < num_flows; i++) {
266 if(action == (surf_action_network_GTNETS_t) (metadata[i])){
272 // indeed this action have not yet started
273 // because of that we need to fix the remaining to the
274 // original total cost
275 if(found != 1 && action->generic_action.remains == 0 ){
276 action->generic_action.remains = action->generic_action.cost;
279 XBT_DEBUG("Action (%p) remains new value: %f", action,
280 action->generic_action.remains);
283 if (TRACE_is_enabled()) {
284 double last_amount_sent = (action->generic_action.cost - action->last_remains);
285 double amount_sent = (action->generic_action.cost - action->generic_action.remains);
287 // tracing resource utilization
288 xbt_dynar_t route = NULL;
290 routing_get_route_and_latency (action->src, action->dst, &route, NULL);
293 for (i = 0; i < xbt_dynar_length (route); i++){
294 network_link_GTNETS_t *link = ((network_link_GTNETS_t*)xbt_dynar_get_ptr (route, i));
295 TRACE_surf_link_set_utilization ((*link)->generic_resource.name,
296 ((surf_action_t) action)->category,
297 (amount_sent - last_amount_sent)/(delta),
307 for (i = 0; i < num_flows; i++) {
308 action = (surf_action_network_GTNETS_t) (metadata[i]);
312 action->generic_action.finish = now + time_to_next_flow_completion;
313 action_state_set((surf_action_t) action, SURF_ACTION_DONE);
314 XBT_DEBUG("----> Action (%p) just terminated", action);
319 } else { /* run for a given number of seconds */
320 if (gtnets_run(delta)) {
321 xbt_die("Cannot run GTNetS simulation");
328 static void update_resource_state(void *id,
329 tmgr_trace_event_t event_type,
330 double value, double date)
332 xbt_die("Cannot update model state for GTNetS simulation");
335 /* Max durations are not supported */
336 static surf_action_t communicate(sg_routing_edge_t src_card,
337 sg_routing_edge_t dst_card,
338 double size, double rate)
340 surf_action_network_GTNETS_t action = NULL;
342 int src = src_card->id;
343 int dst = dst_card->id;
344 char *src_name = src_card->name;
345 char *dst_name = dst_card->name;
348 && dst >= 0), "Either src or dst have invalid id (id<0)");
350 XBT_DEBUG("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst,
353 xbt_dynar_t route = NULL;
355 routing_get_route_and_latency(src_card, dst_card, &route, NULL);
357 route_new(src, dst, route, xbt_dynar_length(route));
360 surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size,
361 surf_network_model, 0);
364 action->last_remains = 0;
367 /* Add a flow to the GTNets Simulation, associated to this action */
368 if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
369 xbt_die("Not route between host %s and host %s", src_name, dst_name);
372 TRACE_surf_gtnets_communicate(action, src_card, dst_card);
375 return (surf_action_t) action;
378 /* Suspend a flow() */
379 static void action_suspend(surf_action_t action)
384 /* Resume a flow() */
385 static void action_resume(surf_action_t action)
390 /* Test whether a flow is suspended */
391 static int action_is_suspended(surf_action_t action)
397 static void gtnets_action_set_category(surf_action_t action, const char *category)
399 action->category = xbt_strdup (category);
403 static void finalize(void)
408 static void surf_network_model_init_internal(void)
410 surf_network_model = surf_model_init();
412 surf_network_model->name = "network GTNetS";
413 surf_network_model->action_unref = action_unref;
414 surf_network_model->action_cancel = action_cancel;
415 surf_network_model->action_recycle = action_recycle;
416 surf_network_model->action_state_set = action_state_set;
417 surf_network_model->get_remains = action_get_remains;
419 surf_network_model->model_private->resource_used = resource_used;
420 surf_network_model->model_private->share_resources = share_resources;
421 surf_network_model->model_private->update_actions_state =
422 update_actions_state;
423 surf_network_model->model_private->update_resource_state =
424 update_resource_state;
425 surf_network_model->model_private->finalize = finalize;
427 surf_network_model->suspend = action_suspend;
428 surf_network_model->resume = action_resume;
429 surf_network_model->is_suspended = action_is_suspended;
431 surf_network_model->set_category = gtnets_action_set_category;
434 surf_network_model->extension.network.communicate = communicate;
436 /* Added the initialization for GTNetS interface */
437 if (gtnets_initialize(sg_tcp_gamma)) {
438 xbt_die("Impossible to initialize GTNetS interface");
441 routing_model_create(NULL);
444 #ifdef HAVE_LATENCY_BOUND_TRACKING
445 static int get_latency_limited(surf_action_t action)
451 void surf_network_model_init_GTNETS(void)
453 if (surf_network_model)
456 surf_network_model_init_internal();
458 xbt_dynar_push(model_list, &surf_network_model);
460 #ifdef HAVE_LATENCY_BOUND_TRACKING
461 surf_network_model->get_latency_limited = get_latency_limited;
464 if (sg_gtnets_jitter > 0.0) {
465 gtnets_set_jitter(sg_gtnets_jitter);
466 gtnets_set_jitter_seed(sg_gtnets_jitter_seed);