main.cpp

 1 /*main.cpp
 2  *
 3  *The starting point of the network simulator
 4  *-Include all network header files
 5  *-initilize the network
 6  *-initialize the traffic manager and set it to run
 7  *
 8  *
 9  */
10 #include <sys/time.h>
11 
12 #include <string>
13 #include <cstdlib>
14 #include <iostream>
15 #include <fstream>
16 
17 
18 
19 #include <sstream>
20 #include "booksim.hpp"
21 #include "routefunc.hpp"
22 #include "traffic.hpp"
23 #include "booksim_config.hpp"
24 #include "trafficmanager.hpp"
25 #include "random_utils.hpp"
26 #include "network.hpp"
27 #include "injection.hpp"
28 #include "power_module.hpp"
 1 ///////////////////////////////////////////////////////////////////////////////
 2 //Global declarations
 3 //////////////////////
 4 
 5  /* the current traffic manager instance */
 6 TrafficManager * trafficManager = NULL;
 7 
 8 int GetSimTime() {
 9   return trafficManager->getTime();
10 }
11 
12 class Stats; //数据实体
13 Stats * GetStats(const std::string & name) {
14   Stats* test =  trafficManager->getStats(name);
15   if(test == 0){
16     cout<<"warning statistics "<<name<<" not found"<<endl;
17   }
18   return test;
19 }
20 
21 /* printing activity factor*/
22 bool gPrintActivity;
23 
24 int gK;//radix, the number of routers per dimension
25 int gN;//dimension, such as 1D, 2D
26 int gC;//concentration, the number of nodes sharing a single router
27 
28 int gNodes;
29 
30 //generate nocviewer trace
31 bool gTrace;
32 
33 ostream * gWatchOut;
 1 bool Simulate( BookSimConfig const & config )
 2 {
 3   vector<Network *> net;
 4 
 5   int subnets = config.GetInt("subnets");
 6   /*To include a new network, must register the network here
 7    *add an else if statement with the name of the network
 8    */
 9   //resize改变当前使用数据的大小,如果它比当前使用的大,填充默认值
10   net.resize(subnets);
11   for (int i = 0; i < subnets; ++i) {
12       /*ostringstream
13       *通常用于执行C风格的串流的输出操作,
14       *格式化字符串,避免申请大量的缓冲区,替代sprintf
15       *该类能够根据内容自动分配内存,对内存的管理也是相当的到位
16       *取得ostringstream里的内容可以通过str()和str(string&)成员函数
17       */
18     ostringstream name;
19     name << "network_" << i;
20     net[i] = Network::New( config, name.str() );
21   }
22 
23   /*tcc and characterize are legacy
24    *not sure how to use them 
25    */
26   //assert如果它的条件返回错误,则终止程序执行
27   assert(trafficManager == NULL);
28   trafficManager = TrafficManager::New( config, net ) ;
29 
30   /*Start the simulation run
31    */
32 
33   double total_time; /* Amount of time we've run */
34   struct timeval start_time, end_time; /* Time before/after user code */
35   total_time = 0.0;
36   //获得当前精确时间
37   gettimeofday(&start_time, NULL);
38 
39   bool result = trafficManager->Run() ;
40 
41 
42   gettimeofday(&end_time, NULL);
43   total_time = ((double)(end_time.tv_sec) + (double)(end_time.tv_usec)/1000000.0)
44             - ((double)(start_time.tv_sec) + (double)(start_time.tv_usec)/1000000.0);
45 
46   cout<<"Total run time "<<total_time<<endl;
47 
48   for (int i=0; i<subnets; ++i) {
49 
50     ///Power analysis
51     if(config.GetInt("sim_power") > 0){
52       Power_Module pnet(net[i], config);
53       pnet.run();
54     }
55 
56     delete net[i];
57   }
58 
59   delete trafficManager;
60   trafficManager = NULL;
61 
62   return result;
63 }
 1 int main( int argc, char **argv )
 2 {
 3 
 4   BookSimConfig config;
 5 
 6 
 7   if ( !ParseArgs( &config, argc, argv ) ) {
 8     cerr << "Usage: " << argv[0] << " configfile... [param=value...]" << endl;
 9     return 0;
10  } 
11 
12   
13   /*initialize routing, traffic, injection functions
14    */
15   InitializeRoutingMap( config );
16 
17   gPrintActivity = (config.GetInt("print_activity") > 0);
18   gTrace = (config.GetInt("viewer_trace") > 0);
19   
20   string watch_out_file = config.GetStr( "watch_out" );
21   if(watch_out_file == "") {
22     gWatchOut = NULL;
23   } else if(watch_out_file == "-") {
24     gWatchOut = &cout;
25   } else {
26     gWatchOut = new ofstream(watch_out_file.c_str());
27   }
28   
29 
30   /*configure and run the simulator
31    */
32   bool result = Simulate( config );
33   return result ? -1 : 0;
34 }

猜你喜欢

转载自www.cnblogs.com/cpsmile/p/9069177.html