genericPktGen.cc

Go to the documentation of this file.
00001 
00019 #ifndef _genericPktGen_cc_INC
00020 #define _genericPktGen_cc_INC
00021 
00022 #include "genericPktGen.h"
00023 
00024 
00025 
00026 GenericPktGen::GenericPktGen ()
00027 {
00028     name = "GenericPktGen";
00029     sending = false;
00030     out_filename = "traceOut.tr";
00031     last_vc = 0;
00032     ni_recv = false;
00033     srand( time(NULL) );
00034 } /* ----- end of function GenericPktGen::GenericPktGen ----- */
00035 
00036 GenericPktGen::~GenericPktGen ()
00037 {
00038     out_file.close();
00039     gsl_rng_free(dest_gen);
00040     gsl_rng_free(arate_gen);
00041     gsl_rng_free(plen_gen);
00042 } /* ----- end of function GenericPktGen::~GenericPktGen ----- */
00043 
00044 void
00045 GenericPktGen::set_trace_filename( string filename )
00046 {
00047     trace_name = filename;
00048     cout<<"TraceName: "<<trace_name<<endl;
00049 }
00050 
00051 void
00052 GenericPktGen::set_no_vcs ( uint v)
00053 {
00054     vcs = v;
00055 }
00056 
00057 void
00058 GenericPktGen::setup (uint n, uint v, uint time)
00059 {
00060     vcs =v;
00061     no_nodes = n;
00062     max_sim_time = time;
00063     address = myId();
00064     node_ip = address/3;
00065     last_vc = 0;
00066     
00067     stat_packets_in = 0;
00068     stat_packets_out = 0;
00069     stat_min_pkt_latency = 999999999;
00070     stat_last_packet_out_cycle = 0;
00071     stat_last_packet_in_cycle = 0;
00072     stat_total_lat = 0;
00073     stat_hop_count= 0;
00074 
00075     gsl_rng_env_setup();
00076     gsl_rng_default_seed = 2^25;
00077     T = gsl_rng_default;
00078     dest_gen = gsl_rng_alloc (T);
00079     plen_gen = gsl_rng_alloc (T);
00080     arate_gen = gsl_rng_alloc (T);
00081 
00082     ready.resize( vcs );
00083     ready.insert( ready.begin(), ready.size(), false );
00084     for(unsigned int i = 0; i < ready.size(); i++)
00085         ready[i] = false;
00086     lastSentTime = 0;
00087     irt = 0;
00088 
00089 
00090     for( unsigned int i = 0; i < vcs; i++ )
00091     {
00092         IrisEvent* event = new IrisEvent();
00093         event->type = READY_EVENT;
00094         event->vc = i;
00095         Simulator::Schedule(Simulator::Now()+1, &NetworkComponent::process_event,this, event);
00096     }
00097 
00098 
00099     return ;
00100 } /* ----- end of function GenericPktGen::setup ----- */
00101 
00102 void
00103 GenericPktGen::set_output_path( string name)
00104 {
00105     // open the output trace file
00106     stringstream str;
00107     str << name << "/tpg_" << node_ip << "_trace_out.tr";
00108     out_filename = str.str();
00109     out_file.open(out_filename.c_str());
00110     if( !out_file.is_open() )
00111     {
00112         _DBG_NOARG("Could not open output trace file ");
00113         cout << out_filename << endl;
00114     }
00115 }
00116 
00117 void
00118 GenericPktGen::finish ()
00119 {
00120     out_file.close();
00121     trace_filename->close();
00122     return ;
00123 } /* ----- end of function GenericPktGen::finish ----- */
00124 
00125 void
00126 GenericPktGen::process_event (IrisEvent* e)
00127 {
00128     switch(e->type)
00129     {
00130         case NEW_PACKET_EVENT:
00131             handle_new_packet_event(e);
00132             break;
00133         case OUT_PULL_EVENT:
00134             handle_out_pull_event(e);
00135             break;
00136         case READY_EVENT:
00137             handle_ready_event(e);
00138             break;
00139         default:
00140             cout << "\nTPG: " << address << "process_event: UNK event type" << endl;
00141             break;
00142     }
00143     return ;
00144 } /* ----- end of function GenericPktGen::process_event ----- */
00145 
00146 void
00147 GenericPktGen::handle_new_packet_event ( IrisEvent* e)
00148 {
00149     ni_recv = false;
00150     // get the packet data
00151     HighLevelPacket* hlp = static_cast< HighLevelPacket* >( e->event_data.at(0));
00152     double lat = Simulator::Now() - hlp->sent_time;
00153     if( stat_min_pkt_latency > lat)
00154         stat_min_pkt_latency = lat;
00155 
00156     stat_last_packet_in_cycle = Simulator::Now();
00157     stat_packets_in++;
00158     stat_hop_count += hlp->hop_count;
00159     stat_total_lat += lat;
00160 
00161 #ifdef DEBUG
00162     _DBG( "-------------- TPG GOT NEW PACKET ---------------\n pkt_latency: %f", lat);
00163     // write out the packet data to the output trace file
00164     if( !out_file.is_open() )
00165         out_file.open( out_filename.c_str(), std::ios_base::app );
00166 
00167     out_file << hlp->toString();
00168     out_file << "\tPkt latency: " << lat << endl;
00169 #endif
00170 
00171     delete hlp;
00172 
00173     // send back a ready event
00174     IrisEvent* event2 = new IrisEvent();
00175     event2->type = READY_EVENT;
00176     event2->vc = hlp->virtual_channel;
00177     Simulator::Schedule( Simulator::Now()+1, &NetworkComponent::process_event, interface_connections[0], event2);
00178 
00179 
00180     delete e;
00181     return ;
00182 } /* ----- end of function GenericPktGen::handle_new_packet_event ----- */
00183 
00184 void
00185 GenericPktGen::handle_out_pull_event ( IrisEvent* e )
00186 {
00187     sending =false;
00188     bool found = false;
00189     uint sending_vc = -1;
00190     uint max_vc = vcs;
00191     if(do_request_reply_network)
00192         max_vc = vcs/2;
00193 
00194     for( uint i=last_vc+1; i<max_vc; i++)
00195     {
00196         if(ready[i])
00197         {
00198             found = true;
00199             sending_vc = i;
00200             last_vc = i;
00201             break;
00202         }
00203     }
00204     if ( !found)
00205     {
00206         for ( uint i=0; i<=last_vc; i++)
00207         {
00208             if(ready[i])
00209             {
00210                 found = true;
00211                 sending_vc = i;
00212                 last_vc = i;
00213                 break;
00214             }
00215         }
00216     }
00217     /* Example of how to allow some nodes to inject 
00218        if( node_ip != (no_nodes-1))
00219        found = false;
00220      */
00221     if( found )
00222     {
00223         if( lastSentTime+irt <= Simulator::Now() )
00224             //        if( rand()%5 == 4 )
00225         {
00226             stat_packets_out++;
00227             HighLevelPacket* hlp = new HighLevelPacket();
00228             hlp->virtual_channel = sending_vc;
00229             hlp->source = node_ip;
00230             hlp->destination = mc_positions[gsl_rng_uniform_int( dest_gen , no_mcs) % ( no_mcs)];
00231             hlp->addr = node_ip*10000 + stat_packets_out;
00232             hlp->transaction_id = 1000;
00233             if( hlp->destination == node_ip )
00234                 hlp->destination = (hlp->destination + 1) % no_nodes ;
00235 
00236             hlp->sent_time = (ullint)Simulator::Now();
00237             uint pkt_len = pkt_payload_length; //1*max_phy_link_bits; //Static for now but can use gsl_ran
00238             hlp->data_payload_length = pkt_len; 
00239             hlp->msg_class = terminal_msg_class;        
00240             /* 
00241                for ( uint i=0 ; i < pkt_len ; i++ )
00242                {
00243                hlp->data.push_back(true);
00244                }
00245              * */
00246             ready[sending_vc] = false;
00247 
00248             assert ( hlp->destination < no_nodes);
00249 #ifdef _DEEP_DEBUG
00250             _DBG( " Sending at TPG out pull: VC %d dest:%d ", sending_vc, hlp->destination );
00251             cout << "HLP: " << hlp->toString() << endl;
00252             _DBG( " Sending pkt: no of packets %d ", stat_packets_out);
00253 #endif
00254             out_file << Simulator::Now() << " " << hlp->destination << endl;
00255 
00256             IrisEvent* event = new IrisEvent();
00257             event->type = NEW_PACKET_EVENT;
00258             event->event_data.push_back(hlp);
00259             event->vc = sending_vc;
00260             Simulator::Schedule( hlp->sent_time, &NetworkComponent::process_event, interface_connections[0], event );
00261 
00262             lastSentTime = (ullint)Simulator::Now();
00263             irt = gsl_ran_gaussian_tail( arate_gen ,0,mean_irt) ;
00264             //            irt = gsl_ran_poisson( arate_gen ,mean_irt) ;
00265             stat_last_packet_out_cycle = Simulator::Now();
00266             //            if( stat_packets_out >= 10000 )
00267             //                irt = max_sim_time;
00268             /* 
00269                for( uint i=0; i<vcs; i++)
00270                if(ready[i])
00271                {
00272                irt = 10;
00273                break;
00274                }
00275                else
00276                irt=10;
00277              * */
00278             sending = true;
00279         }
00280 
00281     }
00282     else
00283     {
00284         sending = true;
00285         irt = gsl_ran_poisson( arate_gen ,mean_irt) ;
00286     }
00287 
00288     /* Example of how to allow some nodes to inject 
00289        if( node_ip != (no_nodes-1))
00290        sending = false;
00291      * */
00292 
00293     if( sending)
00294     {
00295         IrisEvent* event2 = new IrisEvent();
00296         event2->type = OUT_PULL_EVENT;
00297         Simulator::Schedule(Simulator::Now()+irt, &NetworkComponent::process_event, this, event2);
00298         sending = true;
00299     }           
00300     delete e; 
00301 }
00302 
00303 void
00304 GenericPktGen::handle_ready_event ( IrisEvent* e)
00305 {
00306 
00307     if ( e->vc > vcs )
00308     {
00309         _DBG(" Got ready for vc %d no_vcs %d ", e->vc, vcs);
00310         exit(1);
00311     }
00312     // send the first packet and schedule a out pull if ur ready
00313     ready[e->vc] = true;
00314     if( !sending && Simulator::Now()==1)// < max_sim_time )
00315     {
00316         IrisEvent* event = new IrisEvent();
00317         event->type = OUT_PULL_EVENT;
00318         event->vc = e->vc; 
00319         Simulator::Schedule(Simulator::Now()+1, &NetworkComponent::process_event, this, event);
00320         sending = true;
00321     }
00322 
00323     delete e;
00324     return ;
00325 } /* ----- end of function GenericPktGen::handle_ready_event ----- */
00326 
00327 string
00328 GenericPktGen::toString () const
00329 {
00330     stringstream str;
00331     str << "\nGenericPktGen: "
00332         << "\t vcs: " << ready.size()
00333         << "\t address: " <<address
00334         << "\t node_ip: " << node_ip
00335         ;
00336     return str.str();
00337 } /*  ----- end of function GenericPktGen::toString ----- */
00338 
00339 string
00340 GenericPktGen::print_stats() const
00341 {
00342     stringstream str;
00343     str << "\n PktGen[" << node_ip << "] packets_out: " << stat_packets_out
00344         << "\n PktGen[" << node_ip << "] packets_in: " << stat_packets_in
00345         << "\n PktGen[" << node_ip << "] min_pkt_latency: " << stat_min_pkt_latency
00346         << "\n PktGen[" << node_ip << "] last_packet_out_cycle: " << stat_last_packet_out_cycle
00347         << "\n PktGen[" << node_ip << "] last_packet_in_cycle: " << stat_last_packet_in_cycle
00348         << "\n PktGen[" << node_ip << "] avg_latency: " << (stat_total_lat+0.0)/stat_packets_in
00349         << "\n PktGen[" << node_ip << "] avg_hop_count: " << (stat_hop_count+0.0)/stat_packets_in
00350         << endl;
00351     return str.str();
00352 } /* ----- end of function GenericPktGen::toString ----- */
00353 
00354 #endif /* ----- #ifndef _genericPktGen_cc_INC ----- */

Generated on Tue Oct 19 17:22:00 2010 for IRIS by  doxygen 1.5.8