Sequencer.cc revision 6922
1 2/* 3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include "mem/ruby/libruby.hh" 31#include "mem/ruby/common/Global.hh" 32#include "mem/ruby/system/Sequencer.hh" 33#include "mem/ruby/system/System.hh" 34#include "mem/protocol/Protocol.hh" 35#include "mem/ruby/profiler/Profiler.hh" 36#include "mem/ruby/system/CacheMemory.hh" 37#include "mem/protocol/CacheMsg.hh" 38#include "mem/ruby/recorder/Tracer.hh" 39#include "mem/ruby/common/SubBlock.hh" 40#include "mem/protocol/Protocol.hh" 41#include "mem/gems_common/Map.hh" 42#include "mem/ruby/buffers/MessageBuffer.hh" 43#include "mem/ruby/slicc_interface/AbstractController.hh" 44#include "cpu/rubytest/RubyTester.hh" 45 46#include "params/RubySequencer.hh" 47 48Sequencer * 49RubySequencerParams::create() 50{ 51 return new Sequencer(this); 52} 53 54Sequencer::Sequencer(const Params *p) 55 : RubyPort(p), deadlockCheckEvent(this) 56{ 57 m_store_waiting_on_load_cycles = 0; 58 m_store_waiting_on_store_cycles = 0; 59 m_load_waiting_on_store_cycles = 0; 60 m_load_waiting_on_load_cycles = 0; 61 62 m_outstanding_count = 0; 63 64 m_max_outstanding_requests = 0; 65 m_deadlock_threshold = 0; 66 m_instCache_ptr = NULL; 67 m_dataCache_ptr = NULL; 68 69 m_instCache_ptr = p->icache; 70 m_dataCache_ptr = p->dcache; 71 m_max_outstanding_requests = p->max_outstanding_requests; 72 m_deadlock_threshold = p->deadlock_threshold; 73 m_usingRubyTester = p->using_ruby_tester; 74 75 assert(m_max_outstanding_requests > 0); 76 assert(m_deadlock_threshold > 0); 77 assert(m_instCache_ptr != NULL); 78 assert(m_dataCache_ptr != NULL); 79} 80 81Sequencer::~Sequencer() { 82 83} 84 85void Sequencer::wakeup() { 86 // Check for deadlock of any of the requests 87 Time current_time = g_eventQueue_ptr->getTime(); 88 89 // Check across all outstanding requests 90 int total_outstanding = 0; 91 92 Vector<Address> keys = m_readRequestTable.keys(); 93 for (int i=0; i<keys.size(); i++) { 94 SequencerRequest* request = m_readRequestTable.lookup(keys[i]); 95 if (current_time - request->issue_time >= m_deadlock_threshold) { 96 WARN_MSG("Possible Deadlock detected"); 97 WARN_EXPR(request); 98 WARN_EXPR(m_version); 99 WARN_EXPR(request->ruby_request.paddr); 100 WARN_EXPR(keys.size()); 101 WARN_EXPR(current_time); 102 WARN_EXPR(request->issue_time); 103 WARN_EXPR(current_time - request->issue_time); 104 ERROR_MSG("Aborting"); 105 } 106 } 107 108 keys = m_writeRequestTable.keys(); 109 for (int i=0; i<keys.size(); i++) { 110 SequencerRequest* request = m_writeRequestTable.lookup(keys[i]); 111 if (current_time - request->issue_time >= m_deadlock_threshold) { 112 WARN_MSG("Possible Deadlock detected"); 113 WARN_EXPR(request); 114 WARN_EXPR(m_version); 115 WARN_EXPR(current_time); 116 WARN_EXPR(request->issue_time); 117 WARN_EXPR(current_time - request->issue_time); 118 WARN_EXPR(keys.size()); 119 ERROR_MSG("Aborting"); 120 } 121 } 122 total_outstanding += m_writeRequestTable.size() + m_readRequestTable.size(); 123 124 assert(m_outstanding_count == total_outstanding); 125 126 if (m_outstanding_count > 0) { // If there are still outstanding requests, keep checking 127 schedule(deadlockCheckEvent, 128 (m_deadlock_threshold * g_eventQueue_ptr->getClock()) + curTick); 129 } 130} 131 132void Sequencer::printStats(ostream & out) const { 133 out << "Sequencer: " << m_name << endl; 134 out << " store_waiting_on_load_cycles: " << m_store_waiting_on_load_cycles << endl; 135 out << " store_waiting_on_store_cycles: " << m_store_waiting_on_store_cycles << endl; 136 out << " load_waiting_on_load_cycles: " << m_load_waiting_on_load_cycles << endl; 137 out << " load_waiting_on_store_cycles: " << m_load_waiting_on_store_cycles << endl; 138} 139 140void Sequencer::printProgress(ostream& out) const{ 141 /* 142 int total_demand = 0; 143 out << "Sequencer Stats Version " << m_version << endl; 144 out << "Current time = " << g_eventQueue_ptr->getTime() << endl; 145 out << "---------------" << endl; 146 out << "outstanding requests" << endl; 147 148 Vector<Address> rkeys = m_readRequestTable.keys(); 149 int read_size = rkeys.size(); 150 out << "proc " << m_version << " Read Requests = " << read_size << endl; 151 // print the request table 152 for(int i=0; i < read_size; ++i){ 153 SequencerRequest * request = m_readRequestTable.lookup(rkeys[i]); 154 out << "\tRequest[ " << i << " ] = " << request->type << " Address " << rkeys[i] << " Posted " << request->issue_time << " PF " << PrefetchBit_No << endl; 155 total_demand++; 156 } 157 158 Vector<Address> wkeys = m_writeRequestTable.keys(); 159 int write_size = wkeys.size(); 160 out << "proc " << m_version << " Write Requests = " << write_size << endl; 161 // print the request table 162 for(int i=0; i < write_size; ++i){ 163 CacheMsg & request = m_writeRequestTable.lookup(wkeys[i]); 164 out << "\tRequest[ " << i << " ] = " << request.getType() << " Address " << wkeys[i] << " Posted " << request.getTime() << " PF " << request.getPrefetch() << endl; 165 if( request.getPrefetch() == PrefetchBit_No ){ 166 total_demand++; 167 } 168 } 169 170 out << endl; 171 172 out << "Total Number Outstanding: " << m_outstanding_count << endl; 173 out << "Total Number Demand : " << total_demand << endl; 174 out << "Total Number Prefetches : " << m_outstanding_count - total_demand << endl; 175 out << endl; 176 out << endl; 177 */ 178} 179 180void Sequencer::printConfig(ostream& out) const { 181 out << "Seqeuncer config: " << m_name << endl; 182 out << " controller: " << m_controller->getName() << endl; 183 out << " version: " << m_version << endl; 184 out << " max_outstanding_requests: " << m_max_outstanding_requests << endl; 185 out << " deadlock_threshold: " << m_deadlock_threshold << endl; 186} 187 188// Insert the request on the correct request table. Return true if 189// the entry was already present. 190bool Sequencer::insertRequest(SequencerRequest* request) { 191 int total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size(); 192 193 assert(m_outstanding_count == total_outstanding); 194 195 // See if we should schedule a deadlock check 196 if (deadlockCheckEvent.scheduled() == false) { 197 schedule(deadlockCheckEvent, m_deadlock_threshold + curTick); 198 } 199 200 Address line_addr(request->ruby_request.paddr); 201 line_addr.makeLineAddress(); 202 if ((request->ruby_request.type == RubyRequestType_ST) || 203 (request->ruby_request.type == RubyRequestType_RMW_Read) || 204 (request->ruby_request.type == RubyRequestType_RMW_Write) || 205 (request->ruby_request.type == RubyRequestType_Locked_Read) || 206 (request->ruby_request.type == RubyRequestType_Locked_Write)) { 207 if (m_writeRequestTable.exist(line_addr)) { 208 m_writeRequestTable.lookup(line_addr) = request; 209 // return true; 210 assert(0); // drh5: isn't this an error? do you lose the initial request? 211 } 212 m_writeRequestTable.allocate(line_addr); 213 m_writeRequestTable.lookup(line_addr) = request; 214 m_outstanding_count++; 215 } else { 216 if (m_readRequestTable.exist(line_addr)) { 217 m_readRequestTable.lookup(line_addr) = request; 218 // return true; 219 assert(0); // drh5: isn't this an error? do you lose the initial request? 220 } 221 m_readRequestTable.allocate(line_addr); 222 m_readRequestTable.lookup(line_addr) = request; 223 m_outstanding_count++; 224 } 225 226 g_system_ptr->getProfiler()->sequencerRequests(m_outstanding_count); 227 228 total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size(); 229 assert(m_outstanding_count == total_outstanding); 230 231 return false; 232} 233 234void Sequencer::removeRequest(SequencerRequest* srequest) { 235 236 assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size()); 237 238 const RubyRequest & ruby_request = srequest->ruby_request; 239 Address line_addr(ruby_request.paddr); 240 line_addr.makeLineAddress(); 241 if ((ruby_request.type == RubyRequestType_ST) || 242 (ruby_request.type == RubyRequestType_RMW_Read) || 243 (ruby_request.type == RubyRequestType_RMW_Write) || 244 (ruby_request.type == RubyRequestType_Locked_Read) || 245 (ruby_request.type == RubyRequestType_Locked_Write)) { 246 m_writeRequestTable.deallocate(line_addr); 247 } else { 248 m_readRequestTable.deallocate(line_addr); 249 } 250 m_outstanding_count--; 251 252 assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size()); 253} 254 255void Sequencer::writeCallback(const Address& address, DataBlock& data) { 256 257 assert(address == line_address(address)); 258 assert(m_writeRequestTable.exist(line_address(address))); 259 260 SequencerRequest* request = m_writeRequestTable.lookup(address); 261 262 removeRequest(request); 263 264 assert((request->ruby_request.type == RubyRequestType_ST) || 265 (request->ruby_request.type == RubyRequestType_RMW_Read) || 266 (request->ruby_request.type == RubyRequestType_RMW_Write) || 267 (request->ruby_request.type == RubyRequestType_Locked_Read) || 268 (request->ruby_request.type == RubyRequestType_Locked_Write)); 269 270 if (request->ruby_request.type == RubyRequestType_Locked_Read) { 271 m_dataCache_ptr->setLocked(address, m_version); 272 } 273 else if (request->ruby_request.type == RubyRequestType_RMW_Read) { 274 m_controller->blockOnQueue(address, m_mandatory_q_ptr); 275 } 276 else if (request->ruby_request.type == RubyRequestType_RMW_Write) { 277 m_controller->unblock(address); 278 } 279 280 hitCallback(request, data); 281} 282 283void Sequencer::readCallback(const Address& address, DataBlock& data) { 284 285 assert(address == line_address(address)); 286 assert(m_readRequestTable.exist(line_address(address))); 287 288 SequencerRequest* request = m_readRequestTable.lookup(address); 289 removeRequest(request); 290 291 assert((request->ruby_request.type == RubyRequestType_LD) || 292 (request->ruby_request.type == RubyRequestType_RMW_Read) || 293 (request->ruby_request.type == RubyRequestType_IFETCH)); 294 295 hitCallback(request, data); 296} 297 298void Sequencer::hitCallback(SequencerRequest* srequest, DataBlock& data) { 299 const RubyRequest & ruby_request = srequest->ruby_request; 300 Address request_address(ruby_request.paddr); 301 Address request_line_address(ruby_request.paddr); 302 request_line_address.makeLineAddress(); 303 RubyRequestType type = ruby_request.type; 304 Time issued_time = srequest->issue_time; 305 306 // Set this cache entry to the most recently used 307 if (type == RubyRequestType_IFETCH) { 308 if (m_instCache_ptr->isTagPresent(request_line_address) ) 309 m_instCache_ptr->setMRU(request_line_address); 310 } else { 311 if (m_dataCache_ptr->isTagPresent(request_line_address) ) 312 m_dataCache_ptr->setMRU(request_line_address); 313 } 314 315 assert(g_eventQueue_ptr->getTime() >= issued_time); 316 Time miss_latency = g_eventQueue_ptr->getTime() - issued_time; 317 318 // Profile the miss latency for all non-zero demand misses 319 if (miss_latency != 0) { 320 g_system_ptr->getProfiler()->missLatency(miss_latency, type); 321 322 if (Debug::getProtocolTrace()) { 323 g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(ruby_request.paddr), 324 "", "Done", "", int_to_string(miss_latency)+" cycles"); 325 } 326 } 327 /* 328 if (request.getPrefetch() == PrefetchBit_Yes) { 329 return; // Ignore the prefetch 330 } 331 */ 332 333 // update the data 334 if (ruby_request.data != NULL) { 335 if ((type == RubyRequestType_LD) || 336 (type == RubyRequestType_IFETCH) || 337 (type == RubyRequestType_RMW_Read)) { 338 memcpy(ruby_request.data, data.getData(request_address.getOffset(), ruby_request.len), ruby_request.len); 339 } else { 340 data.setData(ruby_request.data, request_address.getOffset(), ruby_request.len); 341 } 342 } 343 344 // 345 // If using the RubyTester, update the RubyTester sender state's subBlock 346 // with the recieved data. The tester will later access this state. 347 // Note: RubyPort will access it's sender state before the RubyTester. 348 // 349 if (m_usingRubyTester) { 350 RubyTester::SenderState* testerSenderState; 351 testerSenderState = safe_cast<RubyTester::SenderState*>( \ 352 safe_cast<RubyPort::SenderState*>(ruby_request.pkt->senderState)->saved); 353 testerSenderState->subBlock->mergeFrom(data); 354 } 355 356 ruby_hit_callback(ruby_request.pkt); 357 delete srequest; 358} 359 360// Returns true if the sequencer already has a load or store outstanding 361RequestStatus Sequencer::getRequestStatus(const RubyRequest& request) { 362 bool is_outstanding_store = m_writeRequestTable.exist(line_address(Address(request.paddr))); 363 bool is_outstanding_load = m_readRequestTable.exist(line_address(Address(request.paddr))); 364 if ( is_outstanding_store ) { 365 if ((request.type == RubyRequestType_LD) || 366 (request.type == RubyRequestType_IFETCH) || 367 (request.type == RubyRequestType_RMW_Read)) { 368 m_store_waiting_on_load_cycles++; 369 } else { 370 m_store_waiting_on_store_cycles++; 371 } 372 return RequestStatus_Aliased; 373 } else if ( is_outstanding_load ) { 374 if ((request.type == RubyRequestType_ST) || 375 (request.type == RubyRequestType_RMW_Write) ) { 376 m_load_waiting_on_store_cycles++; 377 } else { 378 m_load_waiting_on_load_cycles++; 379 } 380 return RequestStatus_Aliased; 381 } 382 383 if (m_outstanding_count >= m_max_outstanding_requests) { 384 return RequestStatus_BufferFull; 385 } 386 387 return RequestStatus_Ready; 388} 389 390bool Sequencer::empty() const { 391 return (m_writeRequestTable.size() == 0) && (m_readRequestTable.size() == 0); 392} 393 394 395RequestStatus Sequencer::makeRequest(const RubyRequest & request) 396{ 397 assert(Address(request.paddr).getOffset() + request.len <= 398 RubySystem::getBlockSizeBytes()); 399 RequestStatus status = getRequestStatus(request); 400 if (status == RequestStatus_Ready) { 401 SequencerRequest *srequest = new SequencerRequest(request, 402 g_eventQueue_ptr->getTime()); 403 bool found = insertRequest(srequest); 404 if (!found) { 405 if (request.type == RubyRequestType_Locked_Write) { 406 // NOTE: it is OK to check the locked flag here as the mandatory queue will be checked first 407 // ensuring that nothing comes between checking the flag and servicing the store 408 if (!m_dataCache_ptr->isLocked(line_address(Address(request.paddr)), m_version)) { 409 return RequestStatus_LlscFailed; 410 } 411 else { 412 m_dataCache_ptr->clearLocked(line_address(Address(request.paddr))); 413 } 414 } 415 issueRequest(request); 416 417 // TODO: issue hardware prefetches here 418 return RequestStatus_Issued; 419 } 420 else { 421 panic("Sequencer::makeRequest should never be called if the request"\ 422 "is already outstanding\n"); 423 return RequestStatus_NULL; 424 } 425 } else { 426 return status; 427 } 428} 429 430void Sequencer::issueRequest(const RubyRequest& request) { 431 432 // TODO: get rid of CacheMsg, CacheRequestType, and AccessModeTYpe, & have SLICC use RubyRequest and subtypes natively 433 CacheRequestType ctype; 434 switch(request.type) { 435 case RubyRequestType_IFETCH: 436 ctype = CacheRequestType_IFETCH; 437 break; 438 case RubyRequestType_LD: 439 ctype = CacheRequestType_LD; 440 break; 441 case RubyRequestType_ST: 442 ctype = CacheRequestType_ST; 443 break; 444 case RubyRequestType_Locked_Read: 445 case RubyRequestType_Locked_Write: 446 ctype = CacheRequestType_ATOMIC; 447 break; 448 case RubyRequestType_RMW_Read: 449 ctype = CacheRequestType_ATOMIC; 450 break; 451 case RubyRequestType_RMW_Write: 452 ctype = CacheRequestType_ATOMIC; 453 break; 454 default: 455 assert(0); 456 } 457 AccessModeType amtype; 458 switch(request.access_mode){ 459 case RubyAccessMode_User: 460 amtype = AccessModeType_UserMode; 461 break; 462 case RubyAccessMode_Supervisor: 463 amtype = AccessModeType_SupervisorMode; 464 break; 465 case RubyAccessMode_Device: 466 amtype = AccessModeType_UserMode; 467 break; 468 default: 469 assert(0); 470 } 471 Address line_addr(request.paddr); 472 line_addr.makeLineAddress(); 473 CacheMsg msg(line_addr, Address(request.paddr), ctype, Address(request.pc), amtype, request.len, PrefetchBit_No, request.proc_id); 474 475 if (Debug::getProtocolTrace()) { 476 g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(request.paddr), 477 "", "Begin", "", RubyRequestType_to_string(request.type)); 478 } 479 480 if (g_system_ptr->getTracer()->traceEnabled()) { 481 g_system_ptr->getTracer()->traceRequest(this, line_addr, Address(request.pc), 482 request.type, g_eventQueue_ptr->getTime()); 483 } 484 485 Time latency = 0; // initialzed to an null value 486 487 if (request.type == RubyRequestType_IFETCH) 488 latency = m_instCache_ptr->getLatency(); 489 else 490 latency = m_dataCache_ptr->getLatency(); 491 492 // Send the message to the cache controller 493 assert(latency > 0); 494 495 assert(m_mandatory_q_ptr != NULL); 496 m_mandatory_q_ptr->enqueue(msg, latency); 497} 498/* 499bool Sequencer::tryCacheAccess(const Address& addr, CacheRequestType type, 500 AccessModeType access_mode, 501 int size, DataBlock*& data_ptr) { 502 if (type == CacheRequestType_IFETCH) { 503 return m_instCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr); 504 } else { 505 return m_dataCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr); 506 } 507} 508*/ 509 510void Sequencer::print(ostream& out) const { 511 out << "[Sequencer: " << m_version 512 << ", outstanding requests: " << m_outstanding_count; 513 514 out << ", read request table: " << m_readRequestTable 515 << ", write request table: " << m_writeRequestTable; 516 out << "]"; 517} 518 519// this can be called from setState whenever coherence permissions are upgraded 520// when invoked, coherence violations will be checked for the given block 521void Sequencer::checkCoherence(const Address& addr) { 522#ifdef CHECK_COHERENCE 523 g_system_ptr->checkGlobalCoherenceInvariant(addr); 524#endif 525} 526 527