Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0d366a4e2fd653620fd8c57fcf9686a9c0ef7c98
[simgrid.git] / src / surf / gtnets / gtnets_interface.cc
1 /*      $Id$     */
2 /* Copyright (c) 2007 Kayo Fujiwara. All rights reserved.                  */
3
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. */
6
7
8 #include "gtnets_simulator.h"
9 #include "gtnets_interface.h"
10 #ifdef XBT_DEBUG
11         #undef XBT_DEBUG
12 #endif
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15
16 static GTSim* gtnets_sim = 0;
17
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets_interface, surf_network_gtnets,
20                                 "Logging specific to the SURF network GTNetS interface");
21
22
23
24 // initialize the GTNetS interface and environment
25 int gtnets_initialize(int wsize){
26   XBT_DEBUG("Using logging.");
27   xbt_assert(!gtnets_sim, "gtnets already initialized");
28
29   if(wsize > 0){
30         XBT_INFO("TCP window maximum size : %d", wsize);
31         gtnets_sim = new GTSim(wsize);
32   }else{
33         gtnets_sim = new GTSim(wsize);
34   }
35
36   return 0;
37 }
38
39 // add a link (argument link is just an index starting at 0... 
40 // add link 0, add link 1, etc.)
41 int gtnets_add_link(int id, double bandwidth, double latency){
42   return gtnets_sim->add_link(id, bandwidth, latency);
43 }
44
45 // add a route between a source and a destination as an array of link indices
46 // (note that there is no gtnets_add_network_card(), as we discover them
47 // on the fly via calls to gtnets_add_route()
48 int gtnets_add_route(int src, int dst, int* links, int nlink){
49   return gtnets_sim->add_route(src, dst, links, nlink);
50 }
51
52 // add a router
53 int gtnets_add_router(int id){
54   return gtnets_sim->add_router(id);
55 }
56
57 // add a route between a source and a destination as an array of link indices
58 // (note that there is no gtnets_add_network_card(), as we discover them
59 // on the fly via calls to gtnets_add_route()
60 int gtnets_add_onehop_route(int src, int dst, int link){
61   return gtnets_sim->add_onehop_route(src, dst, link);
62 }
63
64 // create a new flow on a route
65 // one can attach arbitrary metadata to a flow
66 int gtnets_create_flow(int src, int dst, long datasize, void* metadata){
67   return gtnets_sim->create_flow(src, dst, datasize, metadata);
68 }
69
70 // get the time (double) until a flow completes (the first such flow)
71 // if no flows exist, return -1.0
72 double gtnets_get_time_to_next_flow_completion(){
73   ofstream file;
74   streambuf* sbuf;
75   double value;
76
77   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
78           file.open ("/dev/null");
79           sbuf = cout.rdbuf();
80           cout.rdbuf(file.rdbuf());
81           XBT_DEBUG("Enable GTNetS library quite mode");
82   }else {
83           XBT_DEBUG("Disable GTNetS library quite mode");
84   }
85
86   value = gtnets_sim->get_time_to_next_flow_completion();
87
88   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
89           cout.rdbuf(sbuf);
90           file.close();
91   }
92   return value;
93 }
94
95 // run until a flow completes (returns that flow's metadata)
96 double gtnets_run_until_next_flow_completion(void ***metadata, int *number_of_flows){
97   ofstream file;
98   streambuf* sbuf;
99   double value;
100
101   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
102           file.open ("/dev/null");
103           sbuf = cout.rdbuf();
104           cout.rdbuf(file.rdbuf());
105           XBT_DEBUG("Enable GTNetS library quite mode");
106   }else {
107           XBT_DEBUG("Disable GTNetS library quite mode");
108   }
109
110   value = gtnets_sim->run_until_next_flow_completion(metadata, number_of_flows);
111
112   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
113           cout.rdbuf(sbuf);
114           file.close();
115   }
116   return (double) value;
117 }
118
119 // get the total received in bytes using the TCPServer object totRx field
120 double gtnets_get_flow_rx(void *metadata){
121   return gtnets_sim->gtnets_get_flow_rx(metadata);
122 }
123
124
125 // run for a given time (double)
126 int gtnets_run(Time_t deltat){
127   ofstream file;
128   streambuf* sbuf;
129   double value;
130
131   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
132           file.open ("/dev/null");
133           sbuf = cout.rdbuf();
134           cout.rdbuf(file.rdbuf());
135           XBT_DEBUG("Enable GTNetS library quite mode");
136   }else {
137           XBT_DEBUG("Disable GTNetS library quite mode");
138   }
139
140   gtnets_sim->run(deltat);
141
142
143   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
144           cout.rdbuf(sbuf);
145           file.close();
146   }
147   return 0;
148 }
149
150 // clean up
151 int gtnets_finalize(){
152   if (!gtnets_sim) return -1;
153   delete gtnets_sim;
154   gtnets_sim = 0;
155   return 0;
156 }
157
158 // print topology
159 void gtnets_print_topology(void){
160   gtnets_sim->print_topology();
161 }
162
163 // set jitter
164 void gtnets_set_jitter(double d){
165   gtnets_sim->set_jitter(d);
166 }
167
168 // set jitter seed
169 void gtnets_set_jitter_seed(int seed){
170   gtnets_sim->set_jitter_seed(seed);
171 }
172
173