genericTracePktGen.cc

Go to the documentation of this file.
00001 #ifndef _genericTracePktGen_cc_INC
00002 #define _genericTracePktGen_cc_INC
00003 
00004 #include "genericTracePktGen.h"
00005 #include "../../../memctrl/mshr.h"
00006 
00007 
00008 
00009 extern unsigned int MC_ADDR_BITS;
00010 //unsigned int NUM_OF_CONTROLLERS = 8;
00011 GenericTracePktGen::GenericTracePktGen ()
00012 {
00013     name = "GenericTracePktGen";
00014     sending = false;
00015     /* Do this in setup with the address or node_ip appended */
00016     out_filename = "traceOut.tr";
00017     last_vc = 0;
00018 //    interface_connections.resize(1);  
00019     ni_recv = false;
00020 } /* ----- end of function GenericTracePktGen::GenericTracePktGen ----- */
00021 
00022 GenericTracePktGen::~GenericTracePktGen ()
00023 {
00024     out_file.close();
00025     mshrHandler->trace_filename.close();
00026     mshrHandler->mshr.clear();
00027     delete mshrHandler;
00028 
00029 } /* ----- end of function GenericTracePktGen::~GenericTracePktGen ----- */
00030 
00031 void
00032 GenericTracePktGen::set_trace_filename( string filename )
00033 {
00034     trace_name = filename;
00035     cout<<"TraceName: "<<trace_name<<endl;
00036 }
00037 
00038 void
00039 GenericTracePktGen::set_no_vcs ( uint v)
00040 {
00041     vcs = v;
00042 }
00043 
00044 void
00045 GenericTracePktGen::setup (uint n, uint v, uint time)
00046 {
00047     vcs =v;
00048     no_nodes = n;
00049     max_sim_time = time;
00050     address = myId();
00051     node_ip = address/3;
00052     last_vc = 0;
00053     
00054     packets = 0;
00055     packets_in = 0;
00056     min_pkt_latency = 999999999;
00057     last_packet_out_cycle = 0;
00058     fwd_path_delay = 0;
00059     roundTripLat = 0;
00060     stat_round_trip_hop_count = 0;
00061     stat_round_trip_network_latency = 0;
00062     stat_round_trip_memory_latency = 0;
00063     stat_waiting_in_ni = 0;
00064     TotalBLP = 0;       
00065 
00066     ready.resize( vcs );
00067     ready.insert( ready.begin(), ready.size(), false );
00068     for(unsigned int i = 0; i < ready.size(); i++)
00069         ready[i] = false;
00070     lastSentTime = 0;
00071     Addr_t addr;
00072     uint thread_id;
00073     Time time2;
00074     uint cmd;   
00075 
00076 //    InitMSHR();
00077 #ifdef USE_MSHR    
00078     mshrHandler = new MSHR_H(); 
00079     mshrHandler->id = node_ip;
00080     mshrHandler->parent = this; 
00081 #endif    
00082  //   mc->stats->doneOnce[i] = &mshrHandler[i].done; 
00083     trace_filename = &mshrHandler->trace_filename; 
00084     mshrHandler->filename = const_cast<char*>(trace_name.c_str());      
00085 
00086     trace_filename->open(trace_name.c_str(),ios::in);
00087     if(!trace_filename->is_open())
00088     {
00089         cout << "Err opening trace " << trace_name << endl;
00090         exit(1);
00091     }
00092 
00093 #ifdef USE_MSHR
00094     if (!trace_filename->eof())
00095     {
00096         (*trace_filename) >> hex >> addr;
00097         (*trace_filename) >> thread_id; 
00098         (*trace_filename) >> dec >> time2;
00099         (*trace_filename) >> dec >> cmd;
00100 
00101         addr= addr & 0xffffffc0;
00102         
00103         Request *req2 = new Request();
00104         req2->cmdType = (cache_command)cmd;
00105         req2->address = addr;
00106         req2->arrivalTime = time2;
00107         req2->threadId = node_ip;
00108         req2->address = mshrHandler->GlobalAddrMap(req2->address,req2->threadId);
00109 
00110         IrisEvent *event = new IrisEvent();
00111         event->src = this;
00112         event->dst = (Component*)(mshrHandler);
00113         event->type =CREDIT_EVENT; /*  NOTE: This event type has no meaning. Just dummy */
00114         event->event_data.push_back(req2);      
00115 #ifdef DEEP_DEBUG
00116         cout << dec << Simulator::Now() << ": " << hex << req2->address << ": First Request of each trace to be send to mshrs" << endl; 
00117 #endif
00118         Simulator::Schedule(time2, &MSHR_H::process_event, (MSHR_H*)event->dst, event);   
00119     }
00120     /* send ready events for each virtual channel*/
00121 #endif
00122 
00123     for( unsigned int i = 0; i < vcs; i++ )
00124     {
00125         IrisEvent* event = new IrisEvent();
00126         event->type = READY_EVENT;
00127         event->vc = i;
00128         Simulator::Schedule(Simulator::Now()+1, &NetworkComponent::process_event,this, event);
00129 // interface_connections[0], event);
00130     }
00131 
00132 
00133     return ;
00134 } /* ----- end of function GenericTracePktGen::setup ----- */
00135 
00136 void
00137 GenericTracePktGen::set_output_path( string name)
00138 {
00139     // open the output trace file
00140     stringstream str;
00141     str << name << "/tpg_" << node_ip << "_trace_out.tr";
00142     out_filename = str.str();
00143     out_file.open(out_filename.c_str());
00144 /*
00145     if( !out_file.is_open() )
00146     {
00147         stringstream stream;
00148         stream << "Could not open output trace file " << out_filename << ".\n";
00149         timed_cout(stream.str());
00150     }
00151 */
00152 }
00153 
00154 void
00155 GenericTracePktGen::finish ()
00156 {
00157     out_file.close();
00158     trace_filename->close();
00159     return ;
00160 } /* ----- end of function GenericTracePktGen::finish ----- */
00161 
00162 void
00163 GenericTracePktGen::process_event (IrisEvent* e)
00164 {
00165     switch(e->type)
00166     {
00167         case NEW_PACKET_EVENT:
00168             handle_new_packet_event(e);
00169             break;
00170         case OUT_PULL_EVENT:
00171             handle_out_pull_event(e);
00172             break;
00173         case READY_EVENT:
00174             handle_ready_event(e);
00175             break;
00176         default:
00177             cout << "\nTPG: " << address << "process_event: UNK event type" << endl;
00178             break;
00179     }
00180     return ;
00181 } /* ----- end of function GenericTracePktGen::process_event ----- */
00182 
00183 void
00184 GenericTracePktGen::handle_new_packet_event ( IrisEvent* e)
00185 {
00186     ni_recv = false;
00187     // get the packet data
00188     HighLevelPacket* hlp = static_cast< HighLevelPacket* >( e->event_data.at(0));
00189     double lat = Simulator::Now() - hlp->sent_time;
00190     last_packet_out_cycle = Simulator::Now();
00191     if( min_pkt_latency > lat)
00192         min_pkt_latency = lat;
00193     packets_in++;
00194     stat_round_trip_hop_count += hlp->hop_count;
00195     stat_round_trip_network_latency += (ullint)hlp->avg_network_latency;
00196     stat_round_trip_memory_latency += hlp->stat_memory_serviced_time;
00197     stat_waiting_in_ni += hlp->waiting_in_ni;
00198 #ifdef DEBUG
00199     _DBG( "-------------- TPG GOT NEW PACKET ---------------\n pkt_latency: %f", lat);
00200     // write out the packet data to the output trace file
00201     if( !out_file.is_open() )
00202         out_file.open( out_filename.c_str(), std::ios_base::app );
00203       
00204         out_file << hlp->toString();
00205         out_file << "\tPkt latency: " << lat << endl;
00206 #endif
00207 
00208 #ifdef USE_MSHR
00209     Request * req = new Request();
00210     convertFromBitStream(req,hlp);              
00211     req->startTime = hlp->req_start_time;
00212     IrisEvent *event = new IrisEvent();
00213     event->src = (Component*)this;
00214     event->dst = (Component*)mshrHandler;
00215     event->type = MSHR_DELETE;  
00216     event->event_data.push_back(req);   
00217 #ifdef DEBUG   
00218     _DBG( " Got at TPG newpkt: %llx",req->address);
00219 #endif
00220 
00221 #ifdef _DEEP_DEBUG
00222         cout << dec << Simulator::Now() << ": " << hex << req->address << ": Sending to MSHR for Delete" << endl;       
00223 #endif
00224     Simulator::Schedule(Simulator::Now()+1, &MSHR_H::process_event, (MSHR_H*)event->dst, event);        
00225 #endif
00226 
00227     delete hlp;
00228 
00229     // send back a ready event
00230     IrisEvent* event2 = new IrisEvent();
00231     event2->type = READY_EVENT;
00232     event2->vc = hlp->virtual_channel;
00233     Simulator::Schedule( Simulator::Now()+1, &NetworkComponent::process_event, interface_connections[0], event2);
00234 
00235 
00236     delete e;
00237     return ;
00238 } /* ----- end of function GenericTracePktGen::handle_new_packet_event ----- */
00239 
00240 void
00241 GenericTracePktGen::handle_out_pull_event ( IrisEvent* e )
00242 {
00243     sending =false;
00244     bool found = false;
00245     uint sending_vc = -1;
00246     uint old_last_vc = last_vc;
00247     for( uint i=last_vc+1; i<vcs; i++)
00248     {
00249         if(ready[i])
00250         {
00251             found = true;
00252             sending_vc = i;
00253             last_vc = i;
00254             break;
00255         }
00256     }
00257     if ( !found)
00258     {
00259         for ( uint i=0; i<=last_vc; i++)
00260         {
00261             if(ready[i])
00262             {
00263                 found = true;
00264                 sending_vc = i;
00265                 last_vc = i;
00266                 break;
00267             }
00268         }
00269     }
00270     if( found )
00271     {
00272 #ifdef USE_MSHR
00273         Request* next_req = GetRequest();
00274 #else
00275         Request* next_req = GetNextRequest();
00276 #endif
00277         if( next_req)
00278         {
00279             packets++;
00280             fwd_path_delay += (ullint)(Simulator::Now() - next_req->arrivalTime);
00281             HighLevelPacket* hlp = new HighLevelPacket();
00282             hlp->virtual_channel = sending_vc;
00283             hlp->source = node_ip;
00284             hlp->destination = next_req->mcNo;
00285             hlp->addr = next_req->address;
00286             hlp->transaction_id = 1000;
00287             if( hlp->destination == node_ip )
00288                 hlp->destination = 3; //(hlp->destination + 1) % max_nodes;
00289 
00290             convertToBitStream(next_req, hlp);
00291             if(hlp->sent_time < Simulator::Now())
00292                 hlp->sent_time = (ullint)Simulator::Now()+1;
00293             hlp->req_start_time = next_req->startTime;
00294 
00295 #ifdef _DEEP_DEBUG
00296             assert ( hlp->destination < no_nodes);
00297             _DBG( " Sending at TPG out pull: VC %d dest:%d ad:%llx mshr size: %d", sending_vc, hlp->destination
00298                   ,next_req->address, mshrHandler->mshr.size());
00299             cout << "HLP: " << hlp->toString() << endl;
00300             _DBG( " Sending pkt: no of packets %d %llx", packets, next_req->address );
00301 #endif
00302             ready[sending_vc] = false;
00303             IrisEvent* event = new IrisEvent();
00304             event->type = NEW_PACKET_EVENT;
00305             event->event_data.push_back(hlp);
00306             event->vc = sending_vc;
00307             Simulator::Schedule( hlp->sent_time, &NetworkComponent::process_event, interface_connections[0], event );
00308             sending = true;
00309             delete next_req;
00310             lastSentTime = (ullint)Simulator::Now();
00311 
00312         }
00313         else
00314         {
00315             last_vc = old_last_vc;
00316             sending = true;
00317         }
00318     }
00319 
00320 /* 
00321     if( Simulator::Now()-lastSentTime > 1000000 )
00322     {
00323         cout << " ******* Deaslock detected ****** " << endl;
00324         cout << " TPG " << node_ip << endl;
00325         cout << " last_packet_out_cycle: " << last_packet_out_cycle << endl;
00326         cout << " SimNow: " << Simulator::Now() << endl;
00327         cout << " mshrsize: " << mshrHandler->mshr.size() << endl;
00328         
00329         for (unsigned int i=0; i<mshrHandler->mshr.size(); i++)
00330             cout << mshrHandler->mshr[i].address << endl;
00331 //        for ( uint i=0; i<64; i++)
00332 //            static_cast<GenericRouterVcs*>(mesh->routers[i])->dump_ib_state();
00333         exit(1);
00334     }
00335  * */
00336     if( sending)
00337     {
00338         IrisEvent* event2 = new IrisEvent();
00339         event2->type = OUT_PULL_EVENT;
00340         Simulator::Schedule(Simulator::Now()+1, &NetworkComponent::process_event, this, event2);
00341         sending = true;
00342     }           
00343     delete e; 
00344 }
00345 
00346 void
00347 GenericTracePktGen::handle_ready_event ( IrisEvent* e)
00348 {
00349 
00350     if ( e->vc > vcs )
00351     {
00352         _DBG(" Got ready for vc %d no_vcs %d ", e->vc, vcs);
00353         exit(1);
00354     }
00355     // send the next packet if it is less than the current time
00356     ready[e->vc] = true;
00357     if( !sending && Simulator::Now() < max_sim_time )
00358     {
00359         IrisEvent* event = new IrisEvent();
00360         event->type = OUT_PULL_EVENT;
00361         event->vc = e->vc; 
00362         Simulator::Schedule(Simulator::Now()+1, &NetworkComponent::process_event, this, event);
00363         sending = true;
00364     }
00365 
00366     delete e;
00367     return ;
00368 } /* ----- end of function GenericTracePktGen::handle_ready_event ----- */
00369 Request*
00370 GenericTracePktGen::GetNextRequest()
00371 {
00372     Addr_t addr =0;
00373     uint thread_id = 0;
00374     Time time = 0;
00375     uint cmd;
00376 
00377     if (trace_filename->eof())
00378         return NULL;
00379 
00380     else
00381     {
00382         Request* tempRequest = new Request();
00383         (*trace_filename) >> hex >> addr;
00384         (*trace_filename) >> thread_id;
00385         (*trace_filename) >>dec>> time;
00386         (*trace_filename) >>dec>> cmd;
00387 
00388         tempRequest->cmdType = (cache_command)cmd;
00389         tempRequest->address = addr;
00390         tempRequest->arrivalTime = time;        
00391         tempRequest->threadId = node_ip;
00392         tempRequest->address = mshrHandler->GlobalAddrMap(tempRequest->address,tempRequest->threadId);
00393 #ifdef _DEEP_DEBUG
00394         cout << "\n Created Req: "
00395             << "\t cmd: " << cmd
00396             << "\t thr_id: " << thread_id
00397             << "\t time: " << time
00398             << "\t addr: " << addr
00399             << endl;
00400 #endif
00401 
00402         return tempRequest;
00403     }
00404 
00405 }
00406 
00407 bool
00408 GenericTracePktGen::GetNewRequest(Request *req)
00409 {
00410     if (!mshrHandler->writeQueue.empty())
00411     {
00412         if (mshrHandler->writeQueue[0].arrivalTime <= Simulator::Now())
00413         {
00414             vector<Request>::iterator bufferIndex = mshrHandler->writeQueue.begin();
00415             *req = mshrHandler->writeQueue[0];
00416             //req->startTime = Simulator::Now();        
00417             mshrHandler->writeQueue.erase(bufferIndex);
00418             return true;
00419         }
00420     }
00421     else  if (!mshrHandler->mshr.empty() && mshrHandler->lastScheduledIndex < mshrHandler->mshr.size())
00422     {
00423         if (mshrHandler->mshr[mshrHandler->lastScheduledIndex].arrivalTime <= Simulator::Now())
00424         {
00425             *req = mshrHandler->mshr[mshrHandler->lastScheduledIndex];
00426             mshrHandler->lastScheduledIndex++;
00427 //            TotalBLP += mshrHandler->countBLP(*req);
00428             return true;
00429         }
00430     }
00431     if (mshrHandler->lastScheduledIndex > mshrHandler->mshr.size())
00432     {
00433         cout << "\nERROR: lastIndex greater" << mshrHandler->lastScheduledIndex << " " << mshrHandler->mshr.size() << endl;
00434         exit(1);
00435     }
00436     return false;
00437 }
00438 
00439 Request*
00440 GenericTracePktGen::GetRequest()
00441 {
00442     Request* req = new Request();
00443     if (GetNewRequest(req))
00444     {
00445         return req;
00446     }
00447     delete req;
00448     return NULL;
00449 }
00450 
00451 void
00452 GenericTracePktGen::convertToBitStream(Request* req, HighLevelPacket* hlp)
00453 {
00454     for ( uint i=0 ; i < NETWORK_ADDRESS_BITS ; i++ )
00455     {
00456         bool bit = (bool)((req->address >> i) & 0x1);
00457         hlp->data.push_back(bit);
00458     }
00459     for ( uint i=0 ; i < NETWORK_COMMAND_BITS ; i++ )
00460     {
00461         bool bit = (bool)((req->cmdType >> i) & 0x1);
00462         hlp->data.push_back(bit);
00463     }
00464     for ( uint i=0 ; i < NETWORK_THREADID_BITS ; i++ )
00465     {
00466         bool bit = (bool)((req->threadId >> i) & 0x1);
00467         hlp->data.push_back(bit);
00468     }
00469     if (req->cmdType == CACHE_WRITEBACK)
00470     {   for ( uint i = hlp->data.size() ; i < (8*CACHE_BLOCK_SIZE - max_phy_link_bits); i++ )
00471         {
00472             hlp->data.push_back(true);
00473         }
00474         hlp->msg_class = WRITE_REQ;
00475     }
00476     else
00477         hlp->msg_class = ONE_FLIT_REQ;
00478 
00479 
00480     hlp->data_payload_length = uint(ceil(hlp->data.size() *1.0 / max_phy_link_bits));   
00481     hlp->data_payload_length = hlp->data_payload_length * max_phy_link_bits;
00482     hlp->sent_time = req->arrivalTime;
00483     for ( uint i=hlp->data.size() ; i < hlp->data_payload_length; i++ )
00484         hlp->data.push_back(false);
00485 
00486 }
00487 
00488 
00489 void 
00490 GenericTracePktGen::convertFromBitStream(Request* req, HighLevelPacket *hlp)
00491 {
00492     req->address = 0;   
00493     req->mcNo=hlp->source;
00494     for (unsigned int i=0; i < NETWORK_ADDRESS_BITS; i++)
00495     {
00496         req->address = req->address | (static_cast<ullint>(hlp->data[i]) << i);
00497     }
00498     /*    unsigned int temp = 0;
00499           for (unsigned int i = 0; i < NETWORK_COMMAND_BITS; i++)
00500           {
00501           temp = temp | (hlp->data[i+NETWORK_ADDRESS_BITS] << i);
00502           }
00503           req->cmdType = (Command_t)temp;
00504 
00505           req->threadId = 0;
00506           for (unsigned int i=0; i < NETWORK_THREADID_BITS; i++)
00507           {
00508           req->threadId = req->threadId | (hlp->data[i+NETWORK_ADDRESS_BITS+NETWORK_COMMAND_BITS] << i);
00509           }
00510      */
00511 
00512     req->data.value = 0;
00513     if (req->cmdType == CACHE_READ || req->cmdType == CACHE_WRITE || req->cmdType == CACHE_PREFETCH)
00514         for ( uint i = NETWORK_ADDRESS_BITS; i < (8*CACHE_BLOCK_SIZE - max_phy_link_bits); i++ )
00515         {
00516             req->data.value = req->data.value | (hlp->data[i+NETWORK_ADDRESS_BITS] << i);
00517             req->data.size = CACHE_BLOCK_SIZE;
00518         }
00519 }
00520 
00521 string
00522 GenericTracePktGen::toString () const
00523 {
00524     stringstream str;
00525     str << "\nGenericTracePktGen: "
00526         << "\t trace: " << trace_name
00527         << "\t vcs: " << ready.size()
00528         << "\t address: " <<address
00529         << "\t node_ip: " << node_ip
00530         ;
00531     return str.str();
00532 } /*  ----- end of function GenericTracePktGen::toString ----- */
00533 
00534 string
00535 GenericTracePktGen::print_stats() const
00536 {
00537     stringstream str;
00538     str << "\nTPG [" << node_ip << "] packets_out:\t" << packets
00539         << "\nTPG [" << node_ip << "] avg_fwd_path_delay:\t" << (fwd_path_delay+0.0)/packets
00540         << "\nTPG [" << node_ip << "] min_pkt_latency:\t" << min_pkt_latency
00541         << "\nTPG [" << node_ip << "] last_packet_out_cycle:\t" << last_packet_out_cycle
00542         << "\nTPG [" << node_ip << "] packet_in:\t" << packets_in
00543         << "\nTPG [" << node_ip << "] avg_round_trip_latency:\t" << (roundTripLat+0.0)/packets_in
00544         << "\nTPG [" << node_ip << "] avg_blp:\t" << (TotalBLP+0.0)/packets
00545         << "\nTPG [" << node_ip << "] total_unsink:\t" << mshrHandler->unsink
00546         << "\nTPG [" << node_ip << "] avg_round_trip_hop_count:\t" << (stat_round_trip_hop_count+0.0)/packets_in
00547         << "\nTPG [" << node_ip << "] avg_round_trip_network_latency:\t" << (stat_round_trip_network_latency+0.0)/packets_in
00548         << "\nTPG [" << node_ip << "] avg_round_trip_memory_latency:\t" << (stat_round_trip_memory_latency+0.0)/packets_in
00549         << "\nTPG [" << node_ip << "] avg_round_trip_waiting_in_ni:\t" << (stat_waiting_in_ni+0.0)/packets_in
00550         ;
00551     return str.str();
00552 } /* ----- end of function GenericTracePktGen::toString ----- */
00553 
00554 #endif /* ----- #ifndef _genericTracePktGen_cc_INC ----- */

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