Sequencer.cc revision 6845:9740ade45962
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
45//Sequencer::Sequencer(int core_id, MessageBuffer* mandatory_q)
46
47#define LLSC_FAIL -2
48
49Sequencer::Sequencer(const string & name)
50  :RubyPort(name)
51{
52}
53
54void Sequencer::init(const vector<string> & argv)
55{
56  m_deadlock_check_scheduled = false;
57  m_outstanding_count = 0;
58
59  m_max_outstanding_requests = 0;
60  m_deadlock_threshold = 0;
61  m_version = -1;
62  m_instCache_ptr = NULL;
63  m_dataCache_ptr = NULL;
64  m_controller = NULL;
65  m_servicing_atomic = 200;
66  m_atomics_counter = 0;
67  for (size_t i=0; i<argv.size(); i+=2) {
68    if ( argv[i] == "controller") {
69      m_controller = RubySystem::getController(argv[i+1]); // args[i] = "L1Cache"
70      m_mandatory_q_ptr = m_controller->getMandatoryQueue();
71    } else if ( argv[i] == "icache")
72      m_instCache_ptr = RubySystem::getCache(argv[i+1]);
73    else if ( argv[i] == "dcache")
74      m_dataCache_ptr = RubySystem::getCache(argv[i+1]);
75    else if ( argv[i] == "version")
76      m_version = atoi(argv[i+1].c_str());
77    else if ( argv[i] == "max_outstanding_requests")
78      m_max_outstanding_requests = atoi(argv[i+1].c_str());
79    else if ( argv[i] == "deadlock_threshold")
80      m_deadlock_threshold = atoi(argv[i+1].c_str());
81    else {
82      cerr << "WARNING: Sequencer: Unkown configuration parameter: " << argv[i] << endl;
83      assert(false);
84    }
85  }
86  assert(m_max_outstanding_requests > 0);
87  assert(m_deadlock_threshold > 0);
88  assert(m_version > -1);
89  assert(m_instCache_ptr != NULL);
90  assert(m_dataCache_ptr != NULL);
91  assert(m_controller != NULL);
92}
93
94Sequencer::~Sequencer() {
95
96}
97
98void Sequencer::wakeup() {
99  // Check for deadlock of any of the requests
100  Time current_time = g_eventQueue_ptr->getTime();
101
102  // Check across all outstanding requests
103  int total_outstanding = 0;
104
105  Vector<Address> keys = m_readRequestTable.keys();
106  for (int i=0; i<keys.size(); i++) {
107    SequencerRequest* request = m_readRequestTable.lookup(keys[i]);
108    if (current_time - request->issue_time >= m_deadlock_threshold) {
109      WARN_MSG("Possible Deadlock detected");
110      WARN_EXPR(request);
111      WARN_EXPR(m_version);
112      WARN_EXPR(request->ruby_request.paddr);
113      WARN_EXPR(keys.size());
114      WARN_EXPR(current_time);
115      WARN_EXPR(request->issue_time);
116      WARN_EXPR(current_time - request->issue_time);
117      ERROR_MSG("Aborting");
118    }
119  }
120
121  keys = m_writeRequestTable.keys();
122  for (int i=0; i<keys.size(); i++) {
123    SequencerRequest* request = m_writeRequestTable.lookup(keys[i]);
124    if (current_time - request->issue_time >= m_deadlock_threshold) {
125      WARN_MSG("Possible Deadlock detected");
126      WARN_EXPR(request);
127      WARN_EXPR(m_version);
128      WARN_EXPR(current_time);
129      WARN_EXPR(request->issue_time);
130      WARN_EXPR(current_time - request->issue_time);
131      WARN_EXPR(keys.size());
132      ERROR_MSG("Aborting");
133    }
134  }
135  total_outstanding += m_writeRequestTable.size() + m_readRequestTable.size();
136
137  assert(m_outstanding_count == total_outstanding);
138
139  if (m_outstanding_count > 0) { // If there are still outstanding requests, keep checking
140    g_eventQueue_ptr->scheduleEvent(this, m_deadlock_threshold);
141  } else {
142    m_deadlock_check_scheduled = false;
143  }
144}
145
146void Sequencer::printProgress(ostream& out) const{
147  /*
148  int total_demand = 0;
149  out << "Sequencer Stats Version " << m_version << endl;
150  out << "Current time = " << g_eventQueue_ptr->getTime() << endl;
151  out << "---------------" << endl;
152  out << "outstanding requests" << endl;
153
154  Vector<Address> rkeys = m_readRequestTable.keys();
155  int read_size = rkeys.size();
156  out << "proc " << m_version << " Read Requests = " << read_size << endl;
157  // print the request table
158  for(int i=0; i < read_size; ++i){
159    SequencerRequest * request = m_readRequestTable.lookup(rkeys[i]);
160    out << "\tRequest[ " << i << " ] = " << request->type << " Address " << rkeys[i]  << " Posted " << request->issue_time << " PF " << PrefetchBit_No << endl;
161    total_demand++;
162  }
163
164  Vector<Address> wkeys = m_writeRequestTable.keys();
165  int write_size = wkeys.size();
166  out << "proc " << m_version << " Write Requests = " << write_size << endl;
167  // print the request table
168  for(int i=0; i < write_size; ++i){
169      CacheMsg & request = m_writeRequestTable.lookup(wkeys[i]);
170      out << "\tRequest[ " << i << " ] = " << request.getType() << " Address " << wkeys[i]  << " Posted " << request.getTime() << " PF " << request.getPrefetch() << endl;
171      if( request.getPrefetch() == PrefetchBit_No ){
172        total_demand++;
173      }
174  }
175
176  out << endl;
177
178  out << "Total Number Outstanding: " << m_outstanding_count << endl;
179  out << "Total Number Demand     : " << total_demand << endl;
180  out << "Total Number Prefetches : " << m_outstanding_count - total_demand << endl;
181  out << endl;
182  out << endl;
183  */
184}
185
186void Sequencer::printConfig(ostream& out) const {
187  out << "Seqeuncer config: " << m_name << endl;
188  out << "  controller: " << m_controller->getName() << endl;
189  out << "  version: " << m_version << endl;
190  out << "  max_outstanding_requests: " << m_max_outstanding_requests << endl;
191  out << "  deadlock_threshold: " << m_deadlock_threshold << endl;
192}
193
194// Insert the request on the correct request table.  Return true if
195// the entry was already present.
196bool Sequencer::insertRequest(SequencerRequest* request) {
197  int total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
198
199  assert(m_outstanding_count == total_outstanding);
200
201  // See if we should schedule a deadlock check
202  if (m_deadlock_check_scheduled == false) {
203    g_eventQueue_ptr->scheduleEvent(this, m_deadlock_threshold);
204    m_deadlock_check_scheduled = true;
205  }
206
207  Address line_addr(request->ruby_request.paddr);
208  line_addr.makeLineAddress();
209  if ((request->ruby_request.type == RubyRequestType_ST) ||
210      (request->ruby_request.type == RubyRequestType_RMW_Read) ||
211      (request->ruby_request.type == RubyRequestType_RMW_Write) ||
212      (request->ruby_request.type == RubyRequestType_Locked_Read) ||
213      (request->ruby_request.type == RubyRequestType_Locked_Write)) {
214    if (m_writeRequestTable.exist(line_addr)) {
215      m_writeRequestTable.lookup(line_addr) = request;
216      //      return true;
217      assert(0); // drh5: isn't this an error?  do you lose the initial request?
218    }
219    m_writeRequestTable.allocate(line_addr);
220    m_writeRequestTable.lookup(line_addr) = request;
221    m_outstanding_count++;
222  } else {
223    if (m_readRequestTable.exist(line_addr)) {
224      m_readRequestTable.lookup(line_addr) = request;
225      //      return true;
226      assert(0); // drh5: isn't this an error?  do you lose the initial request?
227    }
228    m_readRequestTable.allocate(line_addr);
229    m_readRequestTable.lookup(line_addr) = request;
230    m_outstanding_count++;
231  }
232
233  g_system_ptr->getProfiler()->sequencerRequests(m_outstanding_count);
234
235  total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
236  assert(m_outstanding_count == total_outstanding);
237
238  return false;
239}
240
241void Sequencer::removeRequest(SequencerRequest* srequest) {
242
243  assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size());
244
245  const RubyRequest & ruby_request = srequest->ruby_request;
246  Address line_addr(ruby_request.paddr);
247  line_addr.makeLineAddress();
248  if ((ruby_request.type == RubyRequestType_ST) ||
249      (ruby_request.type == RubyRequestType_RMW_Read) ||
250      (ruby_request.type == RubyRequestType_RMW_Write) ||
251      (ruby_request.type == RubyRequestType_Locked_Read) ||
252      (ruby_request.type == RubyRequestType_Locked_Write)) {
253    m_writeRequestTable.deallocate(line_addr);
254  } else {
255    m_readRequestTable.deallocate(line_addr);
256  }
257  m_outstanding_count--;
258
259  assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size());
260}
261
262void Sequencer::writeCallback(const Address& address, DataBlock& data) {
263
264  assert(address == line_address(address));
265  assert(m_writeRequestTable.exist(line_address(address)));
266
267  SequencerRequest* request = m_writeRequestTable.lookup(address);
268  removeRequest(request);
269
270  assert((request->ruby_request.type == RubyRequestType_ST) ||
271         (request->ruby_request.type == RubyRequestType_RMW_Read) ||
272         (request->ruby_request.type == RubyRequestType_RMW_Write) ||
273         (request->ruby_request.type == RubyRequestType_Locked_Read) ||
274         (request->ruby_request.type == RubyRequestType_Locked_Write));
275  // POLINA: the assumption is that atomics are only on data cache and not instruction cache
276  if (request->ruby_request.type == RubyRequestType_Locked_Read) {
277    m_dataCache_ptr->setLocked(address, m_version);
278  }
279  else if (request->ruby_request.type == RubyRequestType_RMW_Read) {
280    m_controller->set_atomic(address);
281  }
282  else if (request->ruby_request.type == RubyRequestType_RMW_Write) {
283    m_controller->clear_atomic();
284  }
285
286  hitCallback(request, data);
287}
288
289void Sequencer::readCallback(const Address& address, DataBlock& data) {
290
291  assert(address == line_address(address));
292  assert(m_readRequestTable.exist(line_address(address)));
293
294  SequencerRequest* request = m_readRequestTable.lookup(address);
295  removeRequest(request);
296
297  assert((request->ruby_request.type == RubyRequestType_LD) ||
298	 (request->ruby_request.type == RubyRequestType_RMW_Read) ||
299         (request->ruby_request.type == RubyRequestType_IFETCH));
300
301  hitCallback(request, data);
302}
303
304void Sequencer::hitCallback(SequencerRequest* srequest, DataBlock& data) {
305  const RubyRequest & ruby_request = srequest->ruby_request;
306  Address request_address(ruby_request.paddr);
307  Address request_line_address(ruby_request.paddr);
308  request_line_address.makeLineAddress();
309  RubyRequestType type = ruby_request.type;
310  Time issued_time = srequest->issue_time;
311
312  // Set this cache entry to the most recently used
313  if (type == RubyRequestType_IFETCH) {
314    if (m_instCache_ptr->isTagPresent(request_line_address) )
315      m_instCache_ptr->setMRU(request_line_address);
316  } else {
317    if (m_dataCache_ptr->isTagPresent(request_line_address) )
318      m_dataCache_ptr->setMRU(request_line_address);
319  }
320
321  assert(g_eventQueue_ptr->getTime() >= issued_time);
322  Time miss_latency = g_eventQueue_ptr->getTime() - issued_time;
323
324  // Profile the miss latency for all non-zero demand misses
325  if (miss_latency != 0) {
326    g_system_ptr->getProfiler()->missLatency(miss_latency, type);
327
328    if (Debug::getProtocolTrace()) {
329      g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(ruby_request.paddr),
330                                                     "", "Done", "", int_to_string(miss_latency)+" cycles");
331    }
332  }
333  /*
334  if (request.getPrefetch() == PrefetchBit_Yes) {
335    return; // Ignore the prefetch
336  }
337  */
338
339  // update the data
340  if (ruby_request.data != NULL) {
341    if ((type == RubyRequestType_LD) ||
342        (type == RubyRequestType_IFETCH) ||
343        (type == RubyRequestType_RMW_Read)) {
344      memcpy(ruby_request.data, data.getData(request_address.getOffset(), ruby_request.len), ruby_request.len);
345    } else {
346      data.setData(ruby_request.data, request_address.getOffset(), ruby_request.len);
347    }
348  }
349  if (type == RubyRequestType_RMW_Write) {
350    if (m_servicing_atomic != ruby_request.proc_id) {
351      assert(0);
352    }
353    assert(m_atomics_counter > 0);
354    m_atomics_counter--;
355    if (m_atomics_counter == 0) {
356      m_servicing_atomic = 200;
357    }
358  }
359  m_hit_callback(srequest->id);
360  delete srequest;
361}
362
363// Returns true if the sequencer already has a load or store outstanding
364int Sequencer::isReady(const RubyRequest& request) {
365  // POLINA: check if we are currently flushing the write buffer, if so Ruby is returned as not ready
366  // to simulate stalling of the front-end
367  // Do we stall all the sequencers? If it is atomic instruction - yes!
368  if (m_outstanding_count >= m_max_outstanding_requests) {
369    return LIBRUBY_BUFFER_FULL;
370  }
371
372  if( m_writeRequestTable.exist(line_address(Address(request.paddr))) ||
373      m_readRequestTable.exist(line_address(Address(request.paddr))) ){
374    //cout << "OUTSTANDING REQUEST EXISTS " << p << " VER " << m_version << endl;
375    //printProgress(cout);
376    return LIBRUBY_ALIASED_REQUEST;
377  }
378
379  if (request.type == RubyRequestType_RMW_Read) {
380    if (m_servicing_atomic == 200) {
381      assert(m_atomics_counter == 0);
382      m_servicing_atomic = request.proc_id;
383    }
384    else {
385      assert(m_servicing_atomic == request.proc_id);
386    }
387    m_atomics_counter++;
388  }
389  else {
390    if (m_servicing_atomic == request.proc_id) {
391      if (request.type != RubyRequestType_RMW_Write) {
392	m_servicing_atomic = 200;
393	m_atomics_counter = 0;
394      }
395    }
396  }
397
398  return 1;
399}
400
401bool Sequencer::empty() const {
402  return (m_writeRequestTable.size() == 0) && (m_readRequestTable.size() == 0);
403}
404
405
406int64_t Sequencer::makeRequest(const RubyRequest & request)
407{
408  assert(Address(request.paddr).getOffset() + request.len <= RubySystem::getBlockSizeBytes());
409  int ready = isReady(request);
410  if (ready > 0) {
411    int64_t id = makeUniqueRequestID();
412    SequencerRequest *srequest = new SequencerRequest(request, id, g_eventQueue_ptr->getTime());
413    bool found = insertRequest(srequest);
414    if (!found) {
415      if (request.type == RubyRequestType_Locked_Write) {
416        // NOTE: it is OK to check the locked flag here as the mandatory queue will be checked first
417        // ensuring that nothing comes between checking the flag and servicing the store
418        if (!m_dataCache_ptr->isLocked(line_address(Address(request.paddr)), m_version)) {
419          return LLSC_FAIL;
420        }
421        else {
422          m_dataCache_ptr->clearLocked(line_address(Address(request.paddr)));
423        }
424      }
425      if (request.type == RubyRequestType_RMW_Write) {
426        m_controller->started_writes();
427      }
428      issueRequest(request);
429
430    // TODO: issue hardware prefetches here
431    return id;
432    }
433    else {
434      assert(0);
435    }
436  }
437  else {
438    return ready;
439  }
440}
441
442void Sequencer::issueRequest(const RubyRequest& request) {
443
444  // TODO: get rid of CacheMsg, CacheRequestType, and AccessModeTYpe, & have SLICC use RubyRequest and subtypes natively
445  CacheRequestType ctype;
446  switch(request.type) {
447  case RubyRequestType_IFETCH:
448    ctype = CacheRequestType_IFETCH;
449    break;
450  case RubyRequestType_LD:
451    ctype = CacheRequestType_LD;
452    break;
453  case RubyRequestType_ST:
454    ctype = CacheRequestType_ST;
455    break;
456  case RubyRequestType_Locked_Read:
457  case RubyRequestType_Locked_Write:
458  case RubyRequestType_RMW_Read:
459  case RubyRequestType_RMW_Write:
460    ctype = CacheRequestType_ATOMIC;
461    break;
462  default:
463    assert(0);
464  }
465  AccessModeType amtype;
466  switch(request.access_mode){
467  case RubyAccessMode_User:
468    amtype = AccessModeType_UserMode;
469    break;
470  case RubyAccessMode_Supervisor:
471    amtype = AccessModeType_SupervisorMode;
472    break;
473  case RubyAccessMode_Device:
474    amtype = AccessModeType_UserMode;
475    break;
476  default:
477    assert(0);
478  }
479  Address line_addr(request.paddr);
480  line_addr.makeLineAddress();
481  CacheMsg msg(line_addr, Address(request.paddr), ctype, Address(request.pc), amtype, request.len, PrefetchBit_No, request.proc_id);
482
483  if (Debug::getProtocolTrace()) {
484    g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(request.paddr),
485                                                   "", "Begin", "", RubyRequestType_to_string(request.type));
486  }
487
488  if (g_system_ptr->getTracer()->traceEnabled()) {
489    g_system_ptr->getTracer()->traceRequest(m_name, line_addr, Address(request.pc),
490                                            request.type, g_eventQueue_ptr->getTime());
491  }
492
493  Time latency = 0;  // initialzed to an null value
494
495  if (request.type == RubyRequestType_IFETCH)
496    latency = m_instCache_ptr->getLatency();
497  else
498    latency = m_dataCache_ptr->getLatency();
499
500  // Send the message to the cache controller
501  assert(latency > 0);
502
503
504  m_mandatory_q_ptr->enqueue(msg, latency);
505}
506/*
507bool Sequencer::tryCacheAccess(const Address& addr, CacheRequestType type,
508                               AccessModeType access_mode,
509                               int size, DataBlock*& data_ptr) {
510  if (type == CacheRequestType_IFETCH) {
511    return m_instCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr);
512  } else {
513    return m_dataCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr);
514  }
515}
516*/
517
518void Sequencer::print(ostream& out) const {
519  out << "[Sequencer: " << m_version
520      << ", outstanding requests: " << m_outstanding_count;
521
522  out << ", read request table: " << m_readRequestTable
523      << ", write request table: " << m_writeRequestTable;
524  out << "]";
525}
526
527// this can be called from setState whenever coherence permissions are upgraded
528// when invoked, coherence violations will be checked for the given block
529void Sequencer::checkCoherence(const Address& addr) {
530#ifdef CHECK_COHERENCE
531  g_system_ptr->checkGlobalCoherenceInvariant(addr);
532#endif
533}
534
535