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"
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network_gtnets);
13 /* surf_network_resource_t surf_network_resource = NULL; */
14 /*static xbt_dict_t network_link_set = NULL;*/
16 /* xbt_dict_t network_card_set = NULL; */
19 static int card_number = 0;
20 static network_link_GTNETS_t **routing_table = NULL;
21 static int *routing_table_size = NULL;
23 #define ROUTE(i,j) routing_table[(i)+(j)*card_number]
24 #define ROUTE_SIZE(i,j) routing_table_size[(i)+(j)*card_number]
27 /** QUESTIONS for GTNetS integration
28 ** 1. Check that we did the right thing with name_service and get_resource_name
29 ** 2. Right now there is no "kill flow" in our GTNetS implementation. Do we
30 ** need to do something about this?
31 ** 3. We ignore the fact there is some max_duration on flows (see #2 above)
32 ** 4. share_resources() returns a duration, not a date, right?
33 ** 5. We don't suppoer "rates"
34 ** 6. We don't update "remaining" for ongoing flows. Is it bad?
37 /* Free memory for a network link */
38 static void network_link_free(void *nw_link)
40 free(((network_link_GTNETS_t)nw_link)->name);
44 /* Instantiate a new network link */
45 /* name: some name for the link, from the XML */
46 /* bw: The bandwidth value */
47 /* lat: The latency value */
48 static void network_link_new(char *name,
52 static int link_count=-1;
53 network_link_GTNETS_t gtnets_link;
55 /* KF: Check that the link wasn't added before */
56 if (xbt_dict_get_or_null(network_link_set, name)) {
60 /* KF: Increment the link counter for GTNetS */
64 nw_link->resource = (surf_resource_t) surf_network_resource;
66 nw_link->bw_current = bw_initial;
69 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
70 nw_link->lat_current = lat_initial;
73 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
74 nw_link->state_current = state_initial;
76 nw_link->state_event =
77 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
80 /* KF: Add the link to the GTNetS simulation */
81 if (gtnets_add_link(link_count, bw, lat)) {
82 xbt_assert0(0,"Cannot create GTNetS link");
85 /* KF: Insert entry in the dictionary */
86 gtnets_link = xbt_new0(s_network_link_GTNETS_t,1);
87 gtnets_link->name = name;
88 gtnets_link->bw_current = bw;
89 gtnets_link->lat_current = lat;
90 gtnets_link->id = link_count;
91 xbt_dict_set(network_link_set, name, gtnets_link, network_link_free);
96 /* free the network card */
97 static void network_card_free(void *nw_card)
99 free(((network_card_GTNETS_t)nw_card)->name);
103 /* Instantiate a new network card: MODIFYED BY KF */
104 static int network_card_new(const char *name)
106 static int card_count=-1;
108 /* KF: Check that we haven't seen the network card before */
109 network_card_GTNETS_t card = xbt_dict_get_or_null(network_card_set, name);
112 /* KF: Increment the card counter for GTNetS */
115 /* KF: just use the dictionary to map link names to link indices */
116 card = xbt_new0(s_network_card_GTNETS_t,1);
117 card->name = xbt_strdup(name);
118 card->id = card_count;
119 xbt_dict_set(network_card_set, name, card, network_card_free);
122 /* KF: just return the GTNetS ID as the SURF ID */
126 /* Instantiate a new route: MODIFY BY KF */
127 static void route_new(int src_id, int dst_id, char **links, int nb_link)
130 network_link_GTNETS_t *link_list = NULL;
133 ROUTE_SIZE(src_id, dst_id) = nb_link;
134 link_list = (ROUTE(src_id, dst_id) = xbt_new0(network_link_GTNETS_t, nb_link));
135 for (i = 0; i < nb_link; i++) {
136 link_list[i] = xbt_dict_get_or_null(network_link_set, links[i]);
144 /* KF: Build the list of gtnets link IDs */
145 gtnets_links = (int *)calloc(nb_link, sizeof(int));
146 for (i=0; i<nb_link; i++) {
147 gtnets_links[i]=(int)(xbt_dict_get(network_link_set, links[i]));
150 /* KF: Create the GTNets route */
151 if (gtnets_add_route(src_id, dst_id, gtnets_links, nb_link)) {
152 xbt_assert0(0,"Cannot create GTNetS route");
156 /* Parse the XML for a network link */
157 static void parse_network_link(void)
162 e_surf_network_link_state_t state;
164 name = xbt_strdup(A_surfxml_network_link_name);
165 surf_parse_get_double(&bw,A_surfxml_network_link_bandwidth);
166 surf_parse_get_double(&lat,A_surfxml_network_link_latency);
167 state = SURF_NETWORK_LINK_ON;
169 /* Print values when no traces are specified */
171 tmgr_trace_t bw_trace;
172 tmgr_trace_t state_trace;
173 tmgr_trace_t lat_trace;
175 surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
176 surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
177 surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
179 /*TODO Where is WARNING0 defined???*/
182 WARNING0("The GTNetS network model doesn't support bandwidth state traces");
184 WARNING0("The GTNetS network model doesn't support latency state traces");
186 WARNING0("The GTNetS network model doesn't support link state traces");
191 /* KF: remove several arguments to network_link_new */
192 network_link_new(name, bw, lat);
195 static int nb_link = 0;
196 static char **link_name = NULL;
197 static int src_id = -1;
198 static int dst_id = -1;
200 /* Parses a route from the XML: UNMODIFIED BY HC */
201 static void parse_route_set_endpoints(void)
203 src_id = network_card_new(A_surfxml_route_src);
204 dst_id = network_card_new(A_surfxml_route_dst);
209 /* Parses a route element from the XML: UNMODIFIED BY HC */
210 static void parse_route_elem(void)
213 link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
214 link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
217 /* Create the route: UNMODIFIED BY HC */
218 static void parse_route_set_route(void)
220 route_new(src_id, dst_id, link_name, nb_link);
223 /* Main XML parsing */
224 static void parse_file(const char *file)
226 /* Figuring out the network links */
227 surf_parse_reset_parser();
228 ETag_surfxml_network_link_fun=parse_network_link;
229 surf_parse_open(file);
230 xbt_assert1((!surf_parse()),"Parse error in %s",file);
233 /* Figuring out the network cards used */
234 surf_parse_reset_parser();
235 STag_surfxml_route_fun=parse_route_set_endpoints;
236 surf_parse_open(file);
237 xbt_assert1((!surf_parse()),"Parse error in %s",file);
240 /* Building the routes */
241 surf_parse_reset_parser();
242 STag_surfxml_route_fun=parse_route_set_endpoints;
243 ETag_surfxml_route_element_fun=parse_route_elem;
244 ETag_surfxml_route_fun=parse_route_set_route;
245 surf_parse_open(file);
246 xbt_assert1((!surf_parse()),"Parse error in %s",file);
250 static void *name_service(const char *name)
252 return xbt_dict_get_or_null(network_card_set, name);
255 static const char *get_resource_name(void *resource_id)
257 return ((network_card_GTNETS_t) resource_id)->name;
260 /* We do not care about this: only used for traces */
261 static int resource_used(void *resource_id)
263 return 0; /* We don't care */
266 static int action_free(surf_action_t action)
270 xbt_swag_remove(action, action->state_set);
271 /* KF: No explicit freeing needed for GTNeTS here */
278 static void action_use(surf_action_t action)
283 static void action_cancel(surf_action_t action)
285 xbt_assert0(0,"Cannot cancel GTNetS flow");
289 static void action_recycle(surf_action_t action)
291 xbt_assert0(0,"Cannot recycle GTNetS flow");
295 static void action_change_state(surf_action_t action,
296 e_surf_action_state_t state)
298 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
299 /* if(((surf_action_network_GTNETS_t)action)->variable) { */
300 /* lmm_variable_disable(maxmin_system, ((surf_action_network_GTNETS_t)action)->variable); */
301 /* ((surf_action_network_GTNETS_t)action)->variable = NULL; */
304 surf_action_change_state(action, state);
309 /* share_resources() */
310 static double share_resources(double now)
313 s_surf_action_network_GTNETS_t s_action;
314 surf_action_network_GTNETS_t action = NULL;
315 xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
318 return gtnets_get_time_to_next_flow_completion();
321 /* delta: by how many time units the simulation must advance */
322 /* In this function: change the state of actions that terminate */
323 /* The delta may not come from the network, and thus may be different (smaller)
324 than the one returned by the function above */
325 /* If the delta is a network-caused min, then do not emulate any timer in the
326 network simulation, otherwise fake a timer somehow to advance the simulation of min seconds */
328 static void update_actions_state(double now, double delta)
331 surf_action_network_GTNETS_t action = NULL;
332 surf_action_network_GTNETS_t next_action = NULL;
333 xbt_swag_t running_actions =
334 surf_network_resource->common_public->states.running_action_set;
337 double time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
339 /* If there are no renning flows, just return */
340 if (time_to_next_flow_completion < 0.0) {
344 /*KF: if delta == time_to_next_flow_completion, too.*/
345 if (time_to_next_flow_completion <= delta) { /* run until the first flow completes */
351 if (gtnets_run_until_next_flow_completion(&metadata, &num_flows)) {
352 xbt_assert0(0,"Cannot run GTNetS simulation until next flow completion");
355 xbt_assert0(0,"GTNetS simulation couldn't find a flow that would complete");
358 for (i=0; i<num_flows; i++) {
359 surf_action_network_GTNETS_t action =
360 (surf_action_network_GTNETS_t)(metadata[i]);
362 action->generic_action.remains = 0;
363 action->generic_action.finish = now + time_to_next_flow_completion;
364 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
365 /* TODO: Anything else here? */
367 } else { /* run for a given number of seconds */
368 if (gtnets_run(delta)) {
369 xbt_assert0(0,"Cannot run GTNetS simulation");
376 /* UNUSED HERE: no traces */
377 static void update_resource_state(void *id,
378 tmgr_trace_event_t event_type,
381 xbt_assert0(0,"Cannot update resource state for GTNetS simulation");
385 /* KF: Rate not supported */
386 static surf_action_t communicate(void *src, void *dst, double size, double rate)
388 surf_action_network_GTNETS_t action = NULL;
389 network_card_GTNETS_t card_src = src;
390 network_card_GTNETS_t card_dst = dst;
392 int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
393 network_link_GTNETS_t *route = ROUTE(card_src->id, card_dst->id);
397 xbt_assert2(route_size,"You're trying to send data from %s to %s but there is no connexion between these two cards.", card_src->name, card_dst->name);
400 action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
402 action->generic_action.using = 1;
403 action->generic_action.cost = size;
404 action->generic_action.remains = size;
405 /* Max durations are not supported */
406 action->generic_action.max_duration = NO_MAX_DURATION;
407 action->generic_action.start = surf_get_clock();
408 action->generic_action.finish = -1.0;
409 action->generic_action.resource_type =
410 (surf_resource_t) surf_network_resource;
412 action->generic_action.state_set =
413 surf_network_resource->common_public->states.running_action_set;
415 xbt_swag_insert(action, action->generic_action.state_set);
417 /* KF: Add a flow to the GTNets Simulation, associated to this action */
418 if (gtnets_create_flow(card_src->id, card_dst->id, size, (void *)action) < 0) {
419 xbt_assert2(0,"Not route between host %s and host %s", card_src->name, card_dst->name);
422 return (surf_action_t) action;
425 /* Suspend a flow() */
426 static void action_suspend(surf_action_t action)
428 xbt_assert0(0,"action_suspend() not supported for the GTNets network model");
431 /* Resume a flow() */
432 static void action_resume(surf_action_t action)
434 xbt_assert0(0,"action_resume() not supported for the GTNets network model");
437 /* Test whether a flow is suspended */
438 static int action_is_suspended(surf_action_t action)
443 static void finalize(void)
448 xbt_dict_free(&network_card_set);
449 xbt_dict_free(&network_link_set);
450 xbt_swag_free(surf_network_resource->common_public->states.
452 xbt_swag_free(surf_network_resource->common_public->states.
454 xbt_swag_free(surf_network_resource->common_public->states.
456 xbt_swag_free(surf_network_resource->common_public->states.
458 free(surf_network_resource->common_public);
459 free(surf_network_resource->common_private);
460 free(surf_network_resource->extension_public);
462 free(surf_network_resource);
463 surf_network_resource = NULL;
466 for (i = 0; i < card_number; i++)
467 for (j = 0; j < card_number; j++)
470 routing_table = NULL;
471 free(routing_table_size);
472 routing_table_size = NULL;
481 static void surf_network_resource_init_internal(void)
483 s_surf_action_t action;
485 surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
487 surf_network_resource->common_private =
488 xbt_new0(s_surf_resource_private_t, 1);
489 surf_network_resource->common_public =
490 xbt_new0(s_surf_resource_public_t, 1);
491 surf_network_resource->extension_public =
492 xbt_new0(s_surf_network_resource_extension_public_t, 1);
494 surf_network_resource->common_public->states.ready_action_set =
495 xbt_swag_new(xbt_swag_offset(action, state_hookup));
496 surf_network_resource->common_public->states.running_action_set =
497 xbt_swag_new(xbt_swag_offset(action, state_hookup));
498 surf_network_resource->common_public->states.failed_action_set =
499 xbt_swag_new(xbt_swag_offset(action, state_hookup));
500 surf_network_resource->common_public->states.done_action_set =
501 xbt_swag_new(xbt_swag_offset(action, state_hookup));
503 surf_network_resource->common_public->name_service = name_service;
504 surf_network_resource->common_public->get_resource_name =
506 surf_network_resource->common_public->action_get_state =
507 surf_action_get_state;
508 surf_network_resource->common_public->action_use = action_use;
509 surf_network_resource->common_public->action_free = action_free;
510 surf_network_resource->common_public->action_cancel = action_cancel;
511 surf_network_resource->common_public->action_recycle = action_recycle;
512 surf_network_resource->common_public->action_change_state =
514 surf_network_resource->common_public->action_set_data = surf_action_set_data;
515 surf_network_resource->common_public->name = "network";
517 surf_network_resource->common_private->resource_used = resource_used;
518 surf_network_resource->common_private->share_resources = share_resources;
519 surf_network_resource->common_private->update_actions_state =
520 update_actions_state;
521 surf_network_resource->common_private->update_resource_state =
522 update_resource_state;
523 surf_network_resource->common_private->finalize = finalize;
525 surf_network_resource->common_public->suspend = action_suspend;
526 surf_network_resource->common_public->resume = action_resume;
527 surf_network_resource->common_public->is_suspended = action_is_suspended;
529 surf_network_resource->extension_public->communicate = communicate;
531 network_link_set = xbt_dict_new();
532 network_card_set = xbt_dict_new();
534 /* HC: I am assuming that this stays in for simulation of compute tasks */
535 xbt_assert0(maxmin_system, "surf_init has to be called first!");
537 /* KF: Added the initialization for GTNetS interface */
538 if (gtnets_initialize()) {
539 xbt_assert0(0, "impossible to initialize GTNetS interface");
545 void surf_network_resource_init_GTNETS(const char *filename)
547 if (surf_network_resource)
549 surf_network_resource_init_internal();
550 parse_file(filename);
551 xbt_dynar_push(resource_list, &surf_network_resource);