1 /* Copyright (c) 2007, 2008, 2009, 2010. 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"
11 static double time_to_next_flow_completion = -1;
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
14 "Logging specific to the SURF network GTNetS module");
16 //extern routing_t used_routing; // COMMENTED BY DAVID
17 extern routing_global_t global_routing; // ADDED BY DAVID
19 double sg_gtnets_jitter=0.0;
20 int sg_gtnets_jitter_seed=10;
22 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
24 static int link_count = -1;
25 network_link_GTNETS_t gtnets_link;
26 network_link_GTNETS_t gtnets_link_friend;
31 if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
36 TRACE_surf_link_declaration (name, bw, lat);
39 DEBUG1("Scanning link name %s", name);
40 sscanf(name, "%d_%d", &tmp_idsrc, &tmp_iddst);
41 DEBUG2("Link name split into %d and %d", tmp_idsrc, tmp_iddst);
43 xbt_assert0( (tmp_idsrc!=-1)&&(tmp_idsrc!=-1), "You need to respect fullduplex convention x_y for xml link id.");
45 name_friend = (char *)calloc(strlen(name), sizeof(char));
46 sprintf(name_friend, "%d_%d", tmp_iddst, tmp_idsrc);
48 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
49 gtnets_link->generic_resource.name = name;
50 gtnets_link->generic_resource.properties = props;
51 gtnets_link->bw_current = bw;
52 gtnets_link->lat_current = lat;
54 if((gtnets_link_friend=xbt_dict_get_or_null(surf_network_model->resource_set, name_friend))) {
55 gtnets_link->id = gtnets_link_friend->id;
59 DEBUG4("Adding new link, linkid %d, name %s, latency %g, bandwidth %g", link_count, name, lat, bw);
60 if (gtnets_add_link(link_count, bw, lat)) {
61 xbt_assert0(0, "Cannot create GTNetS link");
63 gtnets_link->id = link_count;
65 xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
69 static void route_new(int src_id, int dst_id, xbt_dynar_t links,int nb_link)
71 network_link_GTNETS_t link;
76 XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
77 src_id, dst_id, links, nb_link);
79 /* Build the list of gtnets link IDs */
80 gtnets_links = (int *) calloc(nb_link, sizeof(int));
82 xbt_dynar_foreach(links, cursor, link) {
83 gtnets_links[i++] = link->id;
86 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
87 xbt_assert0(0, "Cannot create GTNetS route");
92 static void route_onehop_new(int src_id, int dst_id,
93 network_link_GTNETS_t link)
95 if (gtnets_add_onehop_route(src_id, dst_id, link->id)) {
96 xbt_assert0(0, "Cannot create GTNetS route");
100 /* Parse the XML for a network link */
101 static void parse_link_init(void)
106 e_surf_resource_state_t state;
108 name = xbt_strdup(A_surfxml_link_id);
109 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
110 surf_parse_get_double(&lat, A_surfxml_link_latency);
111 state = SURF_RESOURCE_ON;
113 tmgr_trace_t bw_trace;
114 tmgr_trace_t state_trace;
115 tmgr_trace_t lat_trace;
117 bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
118 lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
119 state_trace = tmgr_trace_new(A_surfxml_link_state_file);
122 INFO0("The GTNetS network model doesn't support bandwidth state traces");
124 INFO0("The GTNetS network model doesn't support latency state traces");
126 INFO0("The GTNetS network model doesn't support link state traces");
128 current_property_set = xbt_dict_new();
129 link_new(name, bw, lat, current_property_set);
132 /* Create the gtnets topology based on routing strategy */
133 static void create_gtnets_topology()
135 xbt_dict_cursor_t cursor = NULL;
138 // COMMENTED BY DAVID
139 // xbt_dict_t onelink_routes = used_routing->get_onelink_routes();
140 // xbt_assert0(onelink_routes, "Error onelink_routes was not initialized");
142 // DEBUG0("Starting topology generation");
144 // xbt_dict_foreach(onelink_routes, cursor, key, data){
145 // s_onelink_t link = (s_onelink_t) data;
147 // DEBUG3("Link (#%d), src (#%d), dst (#%d)", ((network_link_GTNETS_t)(link->link_ptr))->id , link->src_id, link->dst_id);
148 // DEBUG0("Calling one link route");
149 // if(used_routing->is_router(link->src_id)){
150 // gtnets_add_router(link->src_id);
152 // if(used_routing->is_router(link->dst_id)){
153 // gtnets_add_router(link->dst_id);
155 // route_onehop_new(link->src_id, link->dst_id, (network_link_GTNETS_t)(link->link_ptr));
158 // xbt_dict_free(&route_table);
159 // if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
160 // gtnets_print_topology();
165 /* Main XML parsing */
166 static void define_callbacks(const char *file)
168 /* Figuring out the network links */
169 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
170 surfxml_add_callback(ETag_surfxml_platform_cb_list, &create_gtnets_topology);
173 static int resource_used(void *resource_id)
175 xbt_assert0(0, "The resource_used feature is not implemented in GTNets model");
178 static int action_unref(surf_action_t action)
181 if (!action->refcount) {
182 xbt_swag_remove(action, action->state_set);
189 static void action_cancel(surf_action_t action)
191 xbt_assert0(0,"Cannot cancel GTNetS flow");
195 static void action_recycle(surf_action_t action)
197 xbt_assert0(0,"Cannot recycle GTNetS flow");
201 static double action_get_remains(surf_action_t action)
203 return action->remains;
206 static void action_state_set(surf_action_t action,
207 e_surf_action_state_t state)
209 surf_action_state_set(action, state);
212 static double share_resources(double now)
214 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
216 //get the first relevant value from the running_actions list
217 if (!xbt_swag_size(running_actions))
220 xbt_assert0(time_to_next_flow_completion,
221 "Time to next flow completion not initialized!\n");
223 DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
224 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
225 DEBUG1("gtnets_get_time_to_next_flow_completion received %lg", time_to_next_flow_completion);
227 return time_to_next_flow_completion;
230 static void update_actions_state(double now, double delta)
232 surf_action_network_GTNETS_t action = NULL;
233 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
235 /* If there are no renning flows, just return */
236 if (time_to_next_flow_completion < 0.0) {
240 /* if delta == time_to_next_flow_completion, too. */
241 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
247 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
249 "Cannot run GTNetS simulation until next flow completion");
253 "GTNetS simulation couldn't find a flow that would complete");
256 xbt_swag_foreach(action, running_actions) {
257 DEBUG2("Action (%p) remains old value: %f", action,
258 action->generic_action.remains);
259 double sent = gtnets_get_flow_rx(action);
262 double trace_sent = sent;
263 if (trace_sent == 0){
264 //if sent is equals to 0, means that gtnets sent all the bytes
265 trace_sent = action->generic_action.cost;
267 // tracing resource utilization
269 int src = TRACE_surf_gtnets_get_src (action);
270 int dst = TRACE_surf_gtnets_get_dst (action);
271 if (src != -1 && dst != -1){
272 xbt_dynar_t route = used_routing->get_route(src, dst);
273 network_link_GTNETS_t link;
275 xbt_dynar_foreach(route, i, link) {
276 TRACE_surf_link_set_utilization (link->generic_resource.name,
277 action->generic_action.data, trace_sent/delta, now-delta, delta);
282 // COMMENTED BY DAVID
283 // int src = TRACE_surf_gtnets_get_src (action);
284 // int dst = TRACE_surf_gtnets_get_dst (action);
285 // if (src != -1 && dst != -1){
286 // xbt_dynar_t route = used_routing->get_route(src, dst);
287 // network_link_GTNETS_t link;
289 // xbt_dynar_foreach(route, i, link) {
291 // TRACE_surf_link_set_utilization (link->generic_resource.name,
292 // action->generic_action.data, (action->generic_action.remains-remain)/delta, now-delta, delta);
295 >>>>>>> I comment all the calls to used_routing, in order to use global routing.
298 DEBUG1("Sent value returned by GTNetS : %f", sent);
299 //need to trust this remain value
301 action->generic_action.remains = 0;
303 action->generic_action.remains = action->generic_action.cost - sent;
305 DEBUG2("Action (%p) remains new value: %f", action,
306 action->generic_action.remains);
309 for (i = 0; i < num_flows; i++) {
310 action = (surf_action_network_GTNETS_t) (metadata[i]);
312 action->generic_action.finish = now + time_to_next_flow_completion;
314 TRACE_surf_gtnets_destroy (action);
316 action_state_set((surf_action_t) action, SURF_ACTION_DONE);
317 DEBUG1("----> Action (%p) just terminated", action);
321 } else { /* run for a given number of seconds */
322 if (gtnets_run(delta)) {
323 xbt_assert0(0, "Cannot run GTNetS simulation");
330 static void update_resource_state(void *id,
331 tmgr_trace_event_t event_type,
332 double value, double date)
334 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
337 /* Max durations are not supported */
338 static surf_action_t communicate(const char *src_name, const char *dst_name,
339 int src, int dst, double size, double rate)
341 surf_action_network_GTNETS_t action = NULL;
343 xbt_assert0((src >= 0 && dst >= 0), "Either src or dst have invalid id (id<0)");
345 DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst, dst_name);
347 //xbt_dynar_t links = used_routing->get_route(src, dst); // COMMENTED BY DAVID
348 xbt_dynar_t links = global_routing->get_route(src_name, dst_name); // ADDED BY DAVID
349 route_new(src, dst, links, xbt_dynar_length(links));
351 action = surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size, surf_network_model, 0);
353 /* Add a flow to the GTNets Simulation, associated to this action */
354 if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
355 xbt_assert2(0, "Not route between host %s and host %s", src_name,
359 TRACE_surf_gtnets_communicate (action, src, dst);
362 return (surf_action_t) action;
365 /* Suspend a flow() */
366 static void action_suspend(surf_action_t action)
371 /* Resume a flow() */
372 static void action_resume(surf_action_t action)
377 /* Test whether a flow is suspended */
378 static int action_is_suspended(surf_action_t action)
383 static void finalize(void)
388 static void surf_network_model_init_internal(void)
390 surf_network_model = surf_model_init();
392 surf_network_model->name = "network GTNetS";
393 surf_network_model->action_unref = action_unref;
394 surf_network_model->action_cancel = action_cancel;
395 surf_network_model->action_recycle = action_recycle;
396 surf_network_model->action_state_set = action_state_set;
397 surf_network_model->get_remains = action_get_remains;
399 surf_network_model->model_private->resource_used = resource_used;
400 surf_network_model->model_private->share_resources = share_resources;
401 surf_network_model->model_private->update_actions_state =
402 update_actions_state;
403 surf_network_model->model_private->update_resource_state =
404 update_resource_state;
405 surf_network_model->model_private->finalize = finalize;
407 surf_network_model->suspend = action_suspend;
408 surf_network_model->resume = action_resume;
409 surf_network_model->is_suspended = action_is_suspended;
411 surf_network_model->extension.network.communicate = communicate;
413 /* Added the initialization for GTNetS interface */
414 if (gtnets_initialize(sg_tcp_gamma)) {
415 xbt_assert0(0, "Impossible to initialize GTNetS interface");
418 routing_model_create(sizeof(network_link_GTNETS_t), NULL);
421 static int get_latency_limited(surf_action_t action){
426 void surf_network_model_init_GTNETS(const char *filename)
428 if (surf_network_model)
430 surf_network_model_init_internal();
431 define_callbacks(filename);
432 xbt_dynar_push(model_list, &surf_network_model);
434 surf_network_model->get_latency_limited = get_latency_limited;
436 if(sg_gtnets_jitter > 0.0){
437 gtnets_set_jitter(sg_gtnets_jitter);
438 gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
441 update_model_description(surf_network_model_description,
442 "GTNets", surf_network_model);