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;
17 double sg_gtnets_jitter=0.0;
18 int sg_gtnets_jitter_seed=10;
20 static void link_new(char *name, double bw, double lat, xbt_dict_t props)
22 static int link_count = -1;
23 network_link_GTNETS_t gtnets_link;
25 if (xbt_dict_get_or_null(surf_network_model->resource_set, name)) {
31 if (gtnets_add_link(link_count, bw, lat)) {
32 xbt_assert0(0, "Cannot create GTNetS link");
35 /* KF: Insert entry in the dictionary */
36 gtnets_link = xbt_new0(s_network_link_GTNETS_t, 1);
37 gtnets_link->generic_resource.name = name;
38 gtnets_link->generic_resource.properties = props;
39 gtnets_link->bw_current = bw;
40 gtnets_link->lat_current = lat;
41 gtnets_link->id = link_count;
43 TRACE_surf_link_declaration (name, bw, lat);
45 xbt_dict_set(surf_network_model->resource_set, name, gtnets_link,
49 static void route_new(int src_id, int dst_id, xbt_dynar_t links,int nb_link)
51 network_link_GTNETS_t link;
56 XBT_IN4("(src_id=%d, dst_id=%d, links=%p, nb_link=%d)",
57 src_id, dst_id, links, nb_link);
59 /* Build the list of gtnets link IDs */
60 gtnets_links = (int *) calloc(nb_link, sizeof(int));
62 xbt_dynar_foreach(links, cursor, link) {
63 gtnets_links[i++] = link->id;
66 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
67 xbt_assert0(0, "Cannot create GTNetS route");
72 static void route_onehop_new(int src_id, int dst_id,
73 network_link_GTNETS_t link)
75 if (gtnets_add_onehop_route(src_id, dst_id, link->id)) {
76 xbt_assert0(0, "Cannot create GTNetS route");
80 /* Parse the XML for a network link */
81 static void parse_link_init(void)
86 e_surf_resource_state_t state;
88 name = xbt_strdup(A_surfxml_link_id);
89 surf_parse_get_double(&bw, A_surfxml_link_bandwidth);
90 surf_parse_get_double(&lat, A_surfxml_link_latency);
91 state = SURF_RESOURCE_ON;
93 tmgr_trace_t bw_trace;
94 tmgr_trace_t state_trace;
95 tmgr_trace_t lat_trace;
97 bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
98 lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
99 state_trace = tmgr_trace_new(A_surfxml_link_state_file);
102 INFO0("The GTNetS network model doesn't support bandwidth state traces");
104 INFO0("The GTNetS network model doesn't support latency state traces");
106 INFO0("The GTNetS network model doesn't support link state traces");
108 current_property_set = xbt_dict_new();
109 link_new(name, bw, lat, current_property_set);
112 /* Create the gtnets topology based on routing strategy */
113 static void create_gtnets_topology()
115 xbt_dict_cursor_t cursor = NULL;
118 xbt_dict_t onelink_routes = used_routing->get_onelink_routes();
119 xbt_assert0(onelink_routes, "Error onelink_routes was not initialized");
121 DEBUG0("Starting topology generation");
123 xbt_dict_foreach(onelink_routes, cursor, key, data){
124 s_onelink_t link = (s_onelink_t) data;
125 DEBUG3("Link (#%d), src (#%d), dst (#%d)", ((network_link_GTNETS_t)(link->link_ptr))->id , link->src_id, link->dst_id);
126 DEBUG0("Calling one link route");
127 if(used_routing->is_router(link->src_id)){
128 gtnets_add_router(link->src_id);
130 if(used_routing->is_router(link->dst_id)){
131 gtnets_add_router(link->dst_id);
133 route_onehop_new(link->src_id, link->dst_id, (network_link_GTNETS_t)(link->link_ptr));
136 xbt_dict_free(&route_table);
137 if (XBT_LOG_ISENABLED(surf_network_gtnets, xbt_log_priority_debug)) {
138 gtnets_print_topology();
142 /* Main XML parsing */
143 static void define_callbacks(const char *file)
145 /* Figuring out the network links */
146 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
147 surfxml_add_callback(ETag_surfxml_platform_cb_list, &create_gtnets_topology);
150 static int resource_used(void *resource_id)
152 xbt_assert0(0, "The resource_used feature is not implemented in GTNets model");
155 static int action_unref(surf_action_t action)
158 if (!action->refcount) {
159 xbt_swag_remove(action, action->state_set);
166 static void action_cancel(surf_action_t action)
168 xbt_assert0(0,"Cannot cancel GTNetS flow");
172 static void action_recycle(surf_action_t action)
174 xbt_assert0(0,"Cannot recycle GTNetS flow");
178 static double action_get_remains(surf_action_t action)
180 return action->remains;
183 static void action_state_set(surf_action_t action,
184 e_surf_action_state_t state)
186 surf_action_state_set(action, state);
189 static double share_resources(double now)
191 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
193 //get the first relevant value from the running_actions list
194 if (!xbt_swag_size(running_actions))
197 xbt_assert0(time_to_next_flow_completion,
198 "Time to next flow completion not initialized!\n");
200 DEBUG0("Calling gtnets_get_time_to_next_flow_completion");
201 time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
202 DEBUG1("gtnets_get_time_to_next_flow_completion received %lg", time_to_next_flow_completion);
204 return time_to_next_flow_completion;
207 static void update_actions_state(double now, double delta)
209 surf_action_network_GTNETS_t action = NULL;
210 xbt_swag_t running_actions = surf_network_model->states.running_action_set;
212 /* If there are no renning flows, just return */
213 if (time_to_next_flow_completion < 0.0) {
217 /* if delta == time_to_next_flow_completion, too. */
218 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
224 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
226 "Cannot run GTNetS simulation until next flow completion");
230 "GTNetS simulation couldn't find a flow that would complete");
233 xbt_swag_foreach(action, running_actions) {
234 DEBUG2("Action (%p) remains old value: %f", action,
235 action->generic_action.remains);
236 double remain = gtnets_get_flow_rx(action);
239 // tracing resource utilization
240 int src = TRACE_surf_gtnets_get_src (action);
241 int dst = TRACE_surf_gtnets_get_dst (action);
242 if (src != -1 && dst != -1){
243 xbt_dynar_t route = used_routing->get_route(src, dst);
244 network_link_GTNETS_t link;
246 xbt_dynar_foreach(route, i, link) {
248 TRACE_surf_link_set_utilization (link->generic_resource.name,
249 action->generic_action.data, (action->generic_action.remains-remain)/delta, now-delta, delta);
254 DEBUG1("Remain value returned by GTNetS : %f", remain);
255 //need to trust this remain value
257 action->generic_action.remains = 0;
259 action->generic_action.remains = action->generic_action.cost - remain;
261 DEBUG2("Action (%p) remains new value: %f", action,
262 action->generic_action.remains);
265 for (i = 0; i < num_flows; i++) {
266 action = (surf_action_network_GTNETS_t) (metadata[i]);
268 action->generic_action.finish = now + time_to_next_flow_completion;
270 TRACE_surf_gtnets_destroy (action);
272 action_state_set((surf_action_t) action, SURF_ACTION_DONE);
273 DEBUG1("----> Action (%p) just terminated", action);
277 } else { /* run for a given number of seconds */
278 if (gtnets_run(delta)) {
279 xbt_assert0(0, "Cannot run GTNetS simulation");
286 static void update_resource_state(void *id,
287 tmgr_trace_event_t event_type,
288 double value, double date)
290 xbt_assert0(0, "Cannot update model state for GTNetS simulation");
293 /* Max durations are not supported */
294 static surf_action_t communicate(const char *src_name, const char *dst_name,
295 int src, int dst, double size, double rate)
297 surf_action_network_GTNETS_t action = NULL;
299 xbt_assert0((src >= 0 && dst >= 0), "Either src or dst have invalid id (id<0)");
301 DEBUG4("Setting flow src %d \"%s\", dst %d \"%s\"", src, src_name, dst, dst_name);
303 xbt_dynar_t links = used_routing->get_route(src, dst);
304 route_new(src, dst, links, xbt_dynar_length(links));
306 action = surf_action_new(sizeof(s_surf_action_network_GTNETS_t), size, surf_network_model, 0);
308 /* Add a flow to the GTNets Simulation, associated to this action */
309 if (gtnets_create_flow(src, dst, size, (void *) action) < 0) {
310 xbt_assert2(0, "Not route between host %s and host %s", src_name,
314 TRACE_surf_gtnets_communicate (action, src, dst);
317 return (surf_action_t) action;
320 /* Suspend a flow() */
321 static void action_suspend(surf_action_t action)
326 /* Resume a flow() */
327 static void action_resume(surf_action_t action)
332 /* Test whether a flow is suspended */
333 static int action_is_suspended(surf_action_t action)
338 static void finalize(void)
340 xbt_dict_free(&surf_network_model->resource_set);
342 surf_model_exit(surf_network_model);
343 surf_network_model = NULL;
348 static void surf_network_model_init_internal(void)
350 surf_network_model = surf_model_init();
352 surf_network_model->name = "network GTNetS";
353 surf_network_model->action_unref = action_unref;
354 surf_network_model->action_cancel = action_cancel;
355 surf_network_model->action_recycle = action_recycle;
356 surf_network_model->action_state_set = action_state_set;
357 surf_network_model->get_remains = action_get_remains;
359 surf_network_model->model_private->resource_used = resource_used;
360 surf_network_model->model_private->share_resources = share_resources;
361 surf_network_model->model_private->update_actions_state =
362 update_actions_state;
363 surf_network_model->model_private->update_resource_state =
364 update_resource_state;
365 surf_network_model->model_private->finalize = finalize;
367 surf_network_model->suspend = action_suspend;
368 surf_network_model->resume = action_resume;
369 surf_network_model->is_suspended = action_is_suspended;
371 surf_network_model->extension.network.communicate = communicate;
373 /* Added the initialization for GTNetS interface */
374 if (gtnets_initialize()) {
375 xbt_assert0(0, "Impossible to initialize GTNetS interface");
378 routing_model_create(sizeof(network_link_GTNETS_t), NULL);
382 void surf_network_model_init_GTNETS(const char *filename)
384 if (surf_network_model)
386 surf_network_model_init_internal();
387 define_callbacks(filename);
388 xbt_dynar_push(model_list, &surf_network_model);
390 if(sg_gtnets_jitter > 0.0){
391 gtnets_set_jitter(sg_gtnets_jitter);
392 gtnets_set_jitter_seed(sg_gtnets_jitter_seed);
395 update_model_description(surf_network_model_description,
396 "GTNets", surf_network_model);