RubySystem.cc revision 10917
17008Snate@binkert.org/*
27008Snate@binkert.org * Copyright (c) 1999-2011 Mark D. Hill and David A. Wood
37008Snate@binkert.org * All rights reserved.
47008Snate@binkert.org *
57008Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67008Snate@binkert.org * modification, are permitted provided that the following conditions are
77008Snate@binkert.org * met: redistributions of source code must retain the above copyright
87008Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97008Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107008Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117008Snate@binkert.org * documentation and/or other materials provided with the distribution;
127008Snate@binkert.org * neither the name of the copyright holders nor the names of its
137008Snate@binkert.org * contributors may be used to endorse or promote products derived from
147008Snate@binkert.org * this software without specific prior written permission.
157008Snate@binkert.org *
167008Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177008Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187008Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197008Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207008Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217008Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227008Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237008Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247008Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257008Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267008Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277008Snate@binkert.org */
286285Snate@binkert.org
2910472Sandreas.hansson@arm.com#include <fcntl.h>
3010472Sandreas.hansson@arm.com#include <zlib.h>
3110518Snilay@cs.wisc.edu
3210518Snilay@cs.wisc.edu#include <cstdio>
338232Snate@binkert.org
349104Shestness@cs.utexas.edu#include "base/intmath.hh"
357039Snate@binkert.org#include "base/statistics.hh"
367039Snate@binkert.org#include "debug/RubyCacheTrace.hh"
376285Snate@binkert.org#include "debug/RubySystem.hh"
3810518Snilay@cs.wisc.edu#include "mem/ruby/common/Address.hh"
396285Snate@binkert.org#include "mem/ruby/network/Network.hh"
406876Ssteve.reinhardt@amd.com#include "mem/ruby/system/System.hh"
4110518Snilay@cs.wisc.edu#include "mem/simple_mem.hh"
4210518Snilay@cs.wisc.edu#include "sim/eventq.hh"
4310706Spower.jg@gmail.com#include "sim/simulate.hh"
4410706Spower.jg@gmail.com
4510519Snilay@cs.wisc.eduusing namespace std;
466285Snate@binkert.org
4710518Snilay@cs.wisc.eduint RubySystem::m_random_seed;
486285Snate@binkert.orgbool RubySystem::m_randomization;
496285Snate@binkert.orguint32_t RubySystem::m_block_size_bytes;
507039Snate@binkert.orguint32_t RubySystem::m_block_size_bits;
517039Snate@binkert.orguint32_t RubySystem::m_memory_size_bits;
526285Snate@binkert.orgbool RubySystem::m_warmup_enabled = false;
5310518Snilay@cs.wisc.edu// To look forward to allowing multiple RubySystem instances, track the number
5410518Snilay@cs.wisc.edu// of RubySystems that need to be warmed up on checkpoint restore.
5510518Snilay@cs.wisc.eduunsigned RubySystem::m_systems_to_warmup = 0;
5610518Snilay@cs.wisc.edubool RubySystem::m_cooldown_enabled = false;
577039Snate@binkert.org
587039Snate@binkert.orgRubySystem::RubySystem(const Params *p)
5910519Snilay@cs.wisc.edu    : ClockedObject(p), m_access_backing_store(p->access_backing_store)
6010519Snilay@cs.wisc.edu{
616285Snate@binkert.org    if (g_system_ptr != NULL)
626285Snate@binkert.org        fatal("Only one RubySystem object currently allowed.\n");
6310518Snilay@cs.wisc.edu
6410518Snilay@cs.wisc.edu    m_random_seed = p->random_seed;
6510518Snilay@cs.wisc.edu    srandom(m_random_seed);
6610518Snilay@cs.wisc.edu    m_randomization = p->randomization;
6710518Snilay@cs.wisc.edu
6810518Snilay@cs.wisc.edu    m_block_size_bytes = p->block_size_bytes;
6910518Snilay@cs.wisc.edu    assert(isPowerOf2(m_block_size_bytes));
7010518Snilay@cs.wisc.edu    m_block_size_bits = floorLog2(m_block_size_bytes);
7110518Snilay@cs.wisc.edu    m_memory_size_bits = p->memory_size_bits;
7210518Snilay@cs.wisc.edu
7310518Snilay@cs.wisc.edu    // Setup the global variables used in Ruby
7410518Snilay@cs.wisc.edu    g_system_ptr = this;
7510518Snilay@cs.wisc.edu
7610518Snilay@cs.wisc.edu    // Resize to the size of different machine types
7710706Spower.jg@gmail.com    g_abs_controls.resize(MachineType_NUM);
7810706Spower.jg@gmail.com
7910706Spower.jg@gmail.com    // Collate the statistics before they are printed.
8010706Spower.jg@gmail.com    Stats::registerDumpCallback(new RubyStatsCallback(this));
8110518Snilay@cs.wisc.edu    // Create the profiler
8210518Snilay@cs.wisc.edu    m_profiler = new Profiler(p);
8310518Snilay@cs.wisc.edu    m_phys_mem = p->phys_mem;
8410518Snilay@cs.wisc.edu}
8510518Snilay@cs.wisc.edu
8610518Snilay@cs.wisc.eduvoid
8710518Snilay@cs.wisc.eduRubySystem::registerNetwork(Network* network_ptr)
8810518Snilay@cs.wisc.edu{
8910518Snilay@cs.wisc.edu    m_network = network_ptr;
9010518Snilay@cs.wisc.edu}
9110518Snilay@cs.wisc.edu
9210518Snilay@cs.wisc.eduvoid
9310518Snilay@cs.wisc.eduRubySystem::registerAbstractController(AbstractController* cntrl)
9410518Snilay@cs.wisc.edu{
9510518Snilay@cs.wisc.edu    m_abs_cntrl_vec.push_back(cntrl);
9610518Snilay@cs.wisc.edu
9710518Snilay@cs.wisc.edu    MachineID id = cntrl->getMachineID();
9810518Snilay@cs.wisc.edu    g_abs_controls[id.getType()][id.getNum()] = cntrl;
9910518Snilay@cs.wisc.edu}
10010518Snilay@cs.wisc.edu
10110518Snilay@cs.wisc.eduRubySystem::~RubySystem()
10210518Snilay@cs.wisc.edu{
10310518Snilay@cs.wisc.edu    delete m_network;
10410518Snilay@cs.wisc.edu    delete m_profiler;
10510518Snilay@cs.wisc.edu}
10610518Snilay@cs.wisc.edu
10710518Snilay@cs.wisc.eduvoid
10810518Snilay@cs.wisc.eduRubySystem::writeCompressedTrace(uint8_t *raw_data, string filename,
10910518Snilay@cs.wisc.edu                                 uint64 uncompressed_trace_size)
11010518Snilay@cs.wisc.edu{
11110518Snilay@cs.wisc.edu    // Create the checkpoint file for the memory
11210518Snilay@cs.wisc.edu    string thefile = CheckpointIn::dir() + "/" + filename.c_str();
11310518Snilay@cs.wisc.edu
11410518Snilay@cs.wisc.edu    int fd = creat(thefile.c_str(), 0664);
11510518Snilay@cs.wisc.edu    if (fd < 0) {
11610518Snilay@cs.wisc.edu        perror("creat");
11710518Snilay@cs.wisc.edu        fatal("Can't open memory trace file '%s'\n", filename);
11810518Snilay@cs.wisc.edu    }
11910518Snilay@cs.wisc.edu
12010518Snilay@cs.wisc.edu    gzFile compressedMemory = gzdopen(fd, "wb");
12110518Snilay@cs.wisc.edu    if (compressedMemory == NULL)
12210518Snilay@cs.wisc.edu        fatal("Insufficient memory to allocate compression state for %s\n",
12310518Snilay@cs.wisc.edu              filename);
12410518Snilay@cs.wisc.edu
12510518Snilay@cs.wisc.edu    if (gzwrite(compressedMemory, raw_data, uncompressed_trace_size) !=
12610518Snilay@cs.wisc.edu        uncompressed_trace_size) {
12710518Snilay@cs.wisc.edu        fatal("Write failed on memory trace file '%s'\n", filename);
12810518Snilay@cs.wisc.edu    }
12910518Snilay@cs.wisc.edu
13010518Snilay@cs.wisc.edu    if (gzclose(compressedMemory)) {
13110518Snilay@cs.wisc.edu        fatal("Close failed on memory trace file '%s'\n", filename);
13210518Snilay@cs.wisc.edu    }
13310518Snilay@cs.wisc.edu    delete[] raw_data;
13410518Snilay@cs.wisc.edu}
13510518Snilay@cs.wisc.edu
13610518Snilay@cs.wisc.eduvoid
13710518Snilay@cs.wisc.eduRubySystem::serializeOld(CheckpointOut &cp)
13810518Snilay@cs.wisc.edu{
13910518Snilay@cs.wisc.edu    m_cooldown_enabled = true;
14010518Snilay@cs.wisc.edu    vector<Sequencer*> sequencer_map;
14110713Sandreas.hansson@arm.com    Sequencer* sequencer_ptr = NULL;
14210518Snilay@cs.wisc.edu
14310518Snilay@cs.wisc.edu    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
14410518Snilay@cs.wisc.edu        sequencer_map.push_back(m_abs_cntrl_vec[cntrl]->getSequencer());
14510518Snilay@cs.wisc.edu        if (sequencer_ptr == NULL) {
14610518Snilay@cs.wisc.edu            sequencer_ptr = sequencer_map[cntrl];
14710518Snilay@cs.wisc.edu        }
14810518Snilay@cs.wisc.edu    }
14910518Snilay@cs.wisc.edu
15010518Snilay@cs.wisc.edu    assert(sequencer_ptr != NULL);
15110518Snilay@cs.wisc.edu
15210518Snilay@cs.wisc.edu    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
15310518Snilay@cs.wisc.edu        if (sequencer_map[cntrl] == NULL) {
15410518Snilay@cs.wisc.edu            sequencer_map[cntrl] = sequencer_ptr;
15510518Snilay@cs.wisc.edu        }
15610518Snilay@cs.wisc.edu    }
15710518Snilay@cs.wisc.edu
15810518Snilay@cs.wisc.edu    // Store the cache-block size, so we are able to restore on systems with a
15910518Snilay@cs.wisc.edu    // different cache-block size. CacheRecorder depends on the correct
16010518Snilay@cs.wisc.edu    // cache-block size upon unserializing.
16110518Snilay@cs.wisc.edu    uint64 block_size_bytes = getBlockSizeBytes();
16210518Snilay@cs.wisc.edu    SERIALIZE_SCALAR(block_size_bytes);
16310518Snilay@cs.wisc.edu
16410518Snilay@cs.wisc.edu    DPRINTF(RubyCacheTrace, "Recording Cache Trace\n");
16510518Snilay@cs.wisc.edu    // Create the CacheRecorder and record the cache trace
16610518Snilay@cs.wisc.edu    m_cache_recorder = new CacheRecorder(NULL, 0, sequencer_map,
16710518Snilay@cs.wisc.edu                                         block_size_bytes);
16810518Snilay@cs.wisc.edu
16910518Snilay@cs.wisc.edu    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
17010518Snilay@cs.wisc.edu        m_abs_cntrl_vec[cntrl]->recordCacheTrace(cntrl, m_cache_recorder);
17110518Snilay@cs.wisc.edu    }
17210518Snilay@cs.wisc.edu
17310518Snilay@cs.wisc.edu    DPRINTF(RubyCacheTrace, "Cache Trace Complete\n");
17410518Snilay@cs.wisc.edu    // save the current tick value
17510518Snilay@cs.wisc.edu    Tick curtick_original = curTick();
17610518Snilay@cs.wisc.edu    // save the event queue head
17710518Snilay@cs.wisc.edu    Event* eventq_head = eventq->replaceHead(NULL);
17810518Snilay@cs.wisc.edu    DPRINTF(RubyCacheTrace, "Recording current tick %ld and event queue\n",
17910518Snilay@cs.wisc.edu            curtick_original);
18010518Snilay@cs.wisc.edu
18110518Snilay@cs.wisc.edu    // Schedule an event to start cache cooldown
18210518Snilay@cs.wisc.edu    DPRINTF(RubyCacheTrace, "Starting cache flush\n");
18310518Snilay@cs.wisc.edu    enqueueRubyEvent(curTick());
18410518Snilay@cs.wisc.edu    simulate();
18510518Snilay@cs.wisc.edu    DPRINTF(RubyCacheTrace, "Cache flush complete\n");
18610518Snilay@cs.wisc.edu
18710518Snilay@cs.wisc.edu    // Restore eventq head
18810518Snilay@cs.wisc.edu    eventq_head = eventq->replaceHead(eventq_head);
18910518Snilay@cs.wisc.edu    // Restore curTick
19010518Snilay@cs.wisc.edu    setCurTick(curtick_original);
19110518Snilay@cs.wisc.edu
19210518Snilay@cs.wisc.edu    // Aggregate the trace entries together into a single array
19310518Snilay@cs.wisc.edu    uint8_t *raw_data = new uint8_t[4096];
19410518Snilay@cs.wisc.edu    uint64 cache_trace_size = m_cache_recorder->aggregateRecords(&raw_data,
19510518Snilay@cs.wisc.edu                                                                 4096);
19610518Snilay@cs.wisc.edu    string cache_trace_file = name() + ".cache.gz";
19710518Snilay@cs.wisc.edu    writeCompressedTrace(raw_data, cache_trace_file, cache_trace_size);
19810518Snilay@cs.wisc.edu
19910518Snilay@cs.wisc.edu    SERIALIZE_SCALAR(cache_trace_file);
20010518Snilay@cs.wisc.edu    SERIALIZE_SCALAR(cache_trace_size);
20110518Snilay@cs.wisc.edu
20210518Snilay@cs.wisc.edu    m_cooldown_enabled = false;
20310518Snilay@cs.wisc.edu}
20410518Snilay@cs.wisc.edu
20510518Snilay@cs.wisc.eduvoid
20610518Snilay@cs.wisc.eduRubySystem::readCompressedTrace(string filename, uint8_t *&raw_data,
20710518Snilay@cs.wisc.edu                                uint64& uncompressed_trace_size)
20810518Snilay@cs.wisc.edu{
20910518Snilay@cs.wisc.edu    // Read the trace file
21010518Snilay@cs.wisc.edu    gzFile compressedTrace;
21110518Snilay@cs.wisc.edu
21210518Snilay@cs.wisc.edu    // trace file
21310518Snilay@cs.wisc.edu    int fd = open(filename.c_str(), O_RDONLY);
21410706Spower.jg@gmail.com    if (fd < 0) {
21510706Spower.jg@gmail.com        perror("open");
21610706Spower.jg@gmail.com        fatal("Unable to open trace file %s", filename);
21710706Spower.jg@gmail.com    }
21810706Spower.jg@gmail.com
21910706Spower.jg@gmail.com    compressedTrace = gzdopen(fd, "rb");
22010706Spower.jg@gmail.com    if (compressedTrace == NULL) {
22110518Snilay@cs.wisc.edu        fatal("Insufficient memory to allocate compression state for %s\n",
22210518Snilay@cs.wisc.edu              filename);
22310518Snilay@cs.wisc.edu    }
22410518Snilay@cs.wisc.edu
22510518Snilay@cs.wisc.edu    raw_data = new uint8_t[uncompressed_trace_size];
22610518Snilay@cs.wisc.edu    if (gzread(compressedTrace, raw_data, uncompressed_trace_size) <
22710518Snilay@cs.wisc.edu            uncompressed_trace_size) {
22810519Snilay@cs.wisc.edu        fatal("Unable to read complete trace from file %s\n", filename);
22910518Snilay@cs.wisc.edu    }
23010518Snilay@cs.wisc.edu
23110518Snilay@cs.wisc.edu    if (gzclose(compressedTrace)) {
23210518Snilay@cs.wisc.edu        fatal("Failed to close cache trace file '%s'\n", filename);
23310518Snilay@cs.wisc.edu    }
23410518Snilay@cs.wisc.edu}
23510518Snilay@cs.wisc.edu
23610518Snilay@cs.wisc.eduvoid
23710518Snilay@cs.wisc.eduRubySystem::unserialize(CheckpointIn &cp)
23810518Snilay@cs.wisc.edu{
2397039Snate@binkert.org    uint8_t *uncompressed_trace = NULL;
2408615Snilay@cs.wisc.edu
2416285Snate@binkert.org    // This value should be set to the checkpoint-system's block-size.
2427544SBrad.Beckmann@amd.com    // Optional, as checkpoints without it can be run if the
2437544SBrad.Beckmann@amd.com    // checkpoint-system's block-size == current block-size.
2447544SBrad.Beckmann@amd.com    uint64 block_size_bytes = getBlockSizeBytes();
2457544SBrad.Beckmann@amd.com    UNSERIALIZE_OPT_SCALAR(block_size_bytes);
2468615Snilay@cs.wisc.edu
24710562Sandreas.hansson@arm.com    string cache_trace_file;
2488615Snilay@cs.wisc.edu    uint64 cache_trace_size = 0;
2498615Snilay@cs.wisc.edu
2506285Snate@binkert.org    UNSERIALIZE_SCALAR(cache_trace_file);
2517039Snate@binkert.org    UNSERIALIZE_SCALAR(cache_trace_size);
2527039Snate@binkert.org    cache_trace_file = cp.cptDir + "/" + cache_trace_file;
2536285Snate@binkert.org
2547039Snate@binkert.org    readCompressedTrace(cache_trace_file, uncompressed_trace,
2557039Snate@binkert.org                        cache_trace_size);
2567039Snate@binkert.org    m_warmup_enabled = true;
2577039Snate@binkert.org    m_systems_to_warmup++;
2587039Snate@binkert.org
2597039Snate@binkert.org    vector<Sequencer*> sequencer_map;
2608615Snilay@cs.wisc.edu    Sequencer* t = NULL;
2616285Snate@binkert.org    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
26210472Sandreas.hansson@arm.com        sequencer_map.push_back(m_abs_cntrl_vec[cntrl]->getSequencer());
26310472Sandreas.hansson@arm.com        if (t == NULL) t = sequencer_map[cntrl];
2647453Snate@binkert.org    }
2657453Snate@binkert.org
2667453Snate@binkert.org    assert(t != NULL);
2677039Snate@binkert.org
2686888SBrad.Beckmann@amd.com    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
2697453Snate@binkert.org        if (sequencer_map[cntrl] == NULL) {
2707039Snate@binkert.org            sequencer_map[cntrl] = t;
2716888SBrad.Beckmann@amd.com        }
2727915SBrad.Beckmann@amd.com    }
2737915SBrad.Beckmann@amd.com
2747915SBrad.Beckmann@amd.com    m_cache_recorder = new CacheRecorder(uncompressed_trace, cache_trace_size,
2757915SBrad.Beckmann@amd.com                                         sequencer_map, block_size_bytes);
2767039Snate@binkert.org}
2776888SBrad.Beckmann@amd.com
2787039Snate@binkert.orgvoid
2797039Snate@binkert.orgRubySystem::startup()
2807453Snate@binkert.org{
2816285Snate@binkert.org
2827039Snate@binkert.org    // Ruby restores state from a checkpoint by resetting the clock to 0 and
2836285Snate@binkert.org    // playing the requests that can possibly re-generate the cache state.
2846285Snate@binkert.org    // The clock value is set to the actual checkpointed value once all the
2857039Snate@binkert.org    // requests have been executed.
2867039Snate@binkert.org    //
2876285Snate@binkert.org    // This way of restoring state is pretty finicky. For example, if a
28810231Ssteve.reinhardt@amd.com    // Ruby component reads time before the state has been restored, it would
2897039Snate@binkert.org    // cache this value and hence its clock would not be reset to 0, when
2907039Snate@binkert.org    // Ruby resets the global clock. This can potentially result in a
2918162SBrad.Beckmann@amd.com    // deadlock.
2928162SBrad.Beckmann@amd.com    //
2938162SBrad.Beckmann@amd.com    // The solution is that no Ruby component should read time before the
2948162SBrad.Beckmann@amd.com    // simulation starts. And then one also needs to hope that the time
2958162SBrad.Beckmann@amd.com    // Ruby finishes restoring the state is less than the time when the
2968162SBrad.Beckmann@amd.com    // state was checkpointed.
2977039Snate@binkert.org
2987039Snate@binkert.org    if (m_warmup_enabled) {
2997039Snate@binkert.org        // save the current tick value
3006285Snate@binkert.org        Tick curtick_original = curTick();
30110472Sandreas.hansson@arm.com        // save the event queue head
30210472Sandreas.hansson@arm.com        Event* eventq_head = eventq->replaceHead(NULL);
3037453Snate@binkert.org        // set curTick to 0 and reset Ruby System's clock
3047039Snate@binkert.org        setCurTick(0);
3056888SBrad.Beckmann@amd.com        resetClock();
3067453Snate@binkert.org
3077453Snate@binkert.org        // Schedule an event to start cache warmup
3086888SBrad.Beckmann@amd.com        enqueueRubyEvent(curTick());
3097453Snate@binkert.org        simulate();
3107039Snate@binkert.org
3116888SBrad.Beckmann@amd.com        delete m_cache_recorder;
3127453Snate@binkert.org        m_cache_recorder = NULL;
3137039Snate@binkert.org        m_systems_to_warmup--;
3147039Snate@binkert.org        if (m_systems_to_warmup == 0) {
3157039Snate@binkert.org            m_warmup_enabled = false;
3167039Snate@binkert.org        }
3176888SBrad.Beckmann@amd.com
3187039Snate@binkert.org        // Restore eventq head
3197453Snate@binkert.org        eventq_head = eventq->replaceHead(eventq_head);
3207039Snate@binkert.org        // Restore curTick and Ruby System's clock
3217453Snate@binkert.org        setCurTick(curtick_original);
3227453Snate@binkert.org        resetClock();
3237039Snate@binkert.org    }
3247453Snate@binkert.org
3257039Snate@binkert.org    resetStats();
3266888SBrad.Beckmann@amd.com}
3277039Snate@binkert.org
3287039Snate@binkert.orgvoid
3297453Snate@binkert.orgRubySystem::RubyEvent::process()
3308160SBrad.Beckmann@amd.com{
3318160SBrad.Beckmann@amd.com    if (RubySystem::getWarmupEnabled()) {
3328160SBrad.Beckmann@amd.com        ruby_system->m_cache_recorder->enqueueNextFetchRequest();
3338160SBrad.Beckmann@amd.com    } else if (RubySystem::getCooldownEnabled()) {
3346285Snate@binkert.org        ruby_system->m_cache_recorder->enqueueNextFlushRequest();
3356285Snate@binkert.org    }
3367039Snate@binkert.org}
3377039Snate@binkert.org
3386285Snate@binkert.orgvoid
33910231Ssteve.reinhardt@amd.comRubySystem::resetStats()
3407039Snate@binkert.org{
3417039Snate@binkert.org    g_ruby_start = curCycle();
3427039Snate@binkert.org}
3437039Snate@binkert.org
34410231Ssteve.reinhardt@amd.combool
3457915SBrad.Beckmann@amd.comRubySystem::functionalRead(PacketPtr pkt)
3467915SBrad.Beckmann@amd.com{
3477915SBrad.Beckmann@amd.com    Address address(pkt->getAddr());
3487915SBrad.Beckmann@amd.com    Address line_address(address);
3497039Snate@binkert.org    line_address.makeLineAddress();
3506285Snate@binkert.org
3516285Snate@binkert.org    AccessPermission access_perm = AccessPermission_NotPresent;
3527039Snate@binkert.org    int num_controllers = m_abs_cntrl_vec.size();
3537039Snate@binkert.org
3546285Snate@binkert.org    DPRINTF(RubySystem, "Functional Read request for %s\n",address);
3557039Snate@binkert.org
3566285Snate@binkert.org    unsigned int num_ro = 0;
3576285Snate@binkert.org    unsigned int num_rw = 0;
3587039Snate@binkert.org    unsigned int num_busy = 0;
35910518Snilay@cs.wisc.edu    unsigned int num_backing_store = 0;
36010518Snilay@cs.wisc.edu    unsigned int num_invalid = 0;
3619104Shestness@cs.utexas.edu
3629104Shestness@cs.utexas.edu    // In this loop we count the number of controllers that have the given
3639104Shestness@cs.utexas.edu    // address in read only, read write and busy states.
3649104Shestness@cs.utexas.edu    for (unsigned int i = 0; i < num_controllers; ++i) {
3656876Ssteve.reinhardt@amd.com        access_perm = m_abs_cntrl_vec[i]-> getAccessPermission(line_address);
3666876Ssteve.reinhardt@amd.com        if (access_perm == AccessPermission_Read_Only)
3676876Ssteve.reinhardt@amd.com            num_ro++;
3686876Ssteve.reinhardt@amd.com        else if (access_perm == AccessPermission_Read_Write)
3696876Ssteve.reinhardt@amd.com            num_rw++;
370        else if (access_perm == AccessPermission_Busy)
371            num_busy++;
372        else if (access_perm == AccessPermission_Backing_Store)
373            // See RubySlicc_Exports.sm for details, but Backing_Store is meant
374            // to represent blocks in memory *for Broadcast/Snooping protocols*,
375            // where memory has no idea whether it has an exclusive copy of data
376            // or not.
377            num_backing_store++;
378        else if (access_perm == AccessPermission_Invalid ||
379                 access_perm == AccessPermission_NotPresent)
380            num_invalid++;
381    }
382    assert(num_rw <= 1);
383
384    // This if case is meant to capture what happens in a Broadcast/Snoop
385    // protocol where the block does not exist in the cache hierarchy. You
386    // only want to read from the Backing_Store memory if there is no copy in
387    // the cache hierarchy, otherwise you want to try to read the RO or RW
388    // copies existing in the cache hierarchy (covered by the else statement).
389    // The reason is because the Backing_Store memory could easily be stale, if
390    // there are copies floating around the cache hierarchy, so you want to read
391    // it only if it's not in the cache hierarchy at all.
392    if (num_invalid == (num_controllers - 1) && num_backing_store == 1) {
393        DPRINTF(RubySystem, "only copy in Backing_Store memory, read from it\n");
394        for (unsigned int i = 0; i < num_controllers; ++i) {
395            access_perm = m_abs_cntrl_vec[i]->getAccessPermission(line_address);
396            if (access_perm == AccessPermission_Backing_Store) {
397                m_abs_cntrl_vec[i]->functionalRead(line_address, pkt);
398                return true;
399            }
400        }
401    } else if (num_ro > 0 || num_rw == 1) {
402        // In Broadcast/Snoop protocols, this covers if you know the block
403        // exists somewhere in the caching hierarchy, then you want to read any
404        // valid RO or RW block.  In directory protocols, same thing, you want
405        // to read any valid readable copy of the block.
406        DPRINTF(RubySystem, "num_busy = %d, num_ro = %d, num_rw = %d\n",
407                num_busy, num_ro, num_rw);
408        // In this loop, we try to figure which controller has a read only or
409        // a read write copy of the given address. Any valid copy would suffice
410        // for a functional read.
411        for (unsigned int i = 0;i < num_controllers;++i) {
412            access_perm = m_abs_cntrl_vec[i]->getAccessPermission(line_address);
413            if (access_perm == AccessPermission_Read_Only ||
414                access_perm == AccessPermission_Read_Write) {
415                m_abs_cntrl_vec[i]->functionalRead(line_address, pkt);
416                return true;
417            }
418        }
419    }
420
421    return false;
422}
423
424// The function searches through all the buffers that exist in different
425// cache, directory and memory controllers, and in the network components
426// and writes the data portion of those that hold the address specified
427// in the packet.
428bool
429RubySystem::functionalWrite(PacketPtr pkt)
430{
431    Address addr(pkt->getAddr());
432    Address line_addr = line_address(addr);
433    AccessPermission access_perm = AccessPermission_NotPresent;
434    int num_controllers = m_abs_cntrl_vec.size();
435
436    DPRINTF(RubySystem, "Functional Write request for %s\n",addr);
437
438    uint32_t M5_VAR_USED num_functional_writes = 0;
439
440    for (unsigned int i = 0; i < num_controllers;++i) {
441        num_functional_writes +=
442            m_abs_cntrl_vec[i]->functionalWriteBuffers(pkt);
443
444        access_perm = m_abs_cntrl_vec[i]->getAccessPermission(line_addr);
445        if (access_perm != AccessPermission_Invalid &&
446            access_perm != AccessPermission_NotPresent) {
447            num_functional_writes +=
448                m_abs_cntrl_vec[i]->functionalWrite(line_addr, pkt);
449        }
450    }
451
452    num_functional_writes += m_network->functionalWrite(pkt);
453    DPRINTF(RubySystem, "Messages written = %u\n", num_functional_writes);
454
455    return true;
456}
457
458#ifdef CHECK_COHERENCE
459// This code will check for cases if the given cache block is exclusive in
460// one node and shared in another-- a coherence violation
461//
462// To use, the SLICC specification must call sequencer.checkCoherence(address)
463// when the controller changes to a state with new permissions.  Do this
464// in setState.  The SLICC spec must also define methods "isBlockShared"
465// and "isBlockExclusive" that are specific to that protocol
466//
467void
468RubySystem::checkGlobalCoherenceInvariant(const Address& addr)
469{
470#if 0
471    NodeID exclusive = -1;
472    bool sharedDetected = false;
473    NodeID lastShared = -1;
474
475    for (int i = 0; i < m_chip_vector.size(); i++) {
476        if (m_chip_vector[i]->isBlockExclusive(addr)) {
477            if (exclusive != -1) {
478                // coherence violation
479                WARN_EXPR(exclusive);
480                WARN_EXPR(m_chip_vector[i]->getID());
481                WARN_EXPR(addr);
482                WARN_EXPR(getTime());
483                ERROR_MSG("Coherence Violation Detected -- 2 exclusive chips");
484            } else if (sharedDetected) {
485                WARN_EXPR(lastShared);
486                WARN_EXPR(m_chip_vector[i]->getID());
487                WARN_EXPR(addr);
488                WARN_EXPR(getTime());
489                ERROR_MSG("Coherence Violation Detected -- exclusive chip with >=1 shared");
490            } else {
491                exclusive = m_chip_vector[i]->getID();
492            }
493        } else if (m_chip_vector[i]->isBlockShared(addr)) {
494            sharedDetected = true;
495            lastShared = m_chip_vector[i]->getID();
496
497            if (exclusive != -1) {
498                WARN_EXPR(lastShared);
499                WARN_EXPR(exclusive);
500                WARN_EXPR(addr);
501                WARN_EXPR(getTime());
502                ERROR_MSG("Coherence Violation Detected -- exclusive chip with >=1 shared");
503            }
504        }
505    }
506#endif
507}
508#endif
509
510RubySystem *
511RubySystemParams::create()
512{
513    return new RubySystem(this);
514}
515