WeightedLRUPolicy.cc revision 12750:1dde69fad30f
1/*
2 * Copyright (c) 2013-2015 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * For use for simulation and test purposes only
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Authors: Derek Hower
34 */
35
36#include "mem/ruby/system/WeightedLRUPolicy.hh"
37
38WeightedLRUPolicy::WeightedLRUPolicy(const Params* p)
39    : AbstractReplacementPolicy(p), m_cache(p->cache)
40{
41    m_last_occ_ptr = new int*[m_num_sets];
42    for (unsigned i = 0; i < m_num_sets; i++){
43        m_last_occ_ptr[i] = new int[m_assoc];
44        for (unsigned j = 0; j < m_assoc; j++){
45            m_last_occ_ptr[i][j] = 0;
46        }
47    }
48}
49
50WeightedLRUPolicy *
51WeightedLRUReplacementPolicyParams::create()
52{
53    return new WeightedLRUPolicy(this);
54}
55
56WeightedLRUPolicy::~WeightedLRUPolicy()
57{
58    if (m_last_occ_ptr != NULL){
59        for (unsigned i = 0; i < m_num_sets; i++){
60            if (m_last_occ_ptr[i] != NULL){
61                delete[] m_last_occ_ptr[i];
62            }
63        }
64        delete[] m_last_occ_ptr;
65    }
66}
67
68void
69WeightedLRUPolicy::touch(int64_t set, int64_t index, Tick time)
70{
71    assert(index >= 0 && index < m_assoc);
72    assert(set >= 0 && set < m_num_sets);
73
74    m_last_ref_ptr[set][index] = time;
75}
76
77void
78WeightedLRUPolicy::touch(int64_t set, int64_t index, Tick time, int occupancy)
79{
80    assert(index >= 0 && index < m_assoc);
81    assert(set >= 0 && set < m_num_sets);
82
83    m_last_ref_ptr[set][index] = time;
84    m_last_occ_ptr[set][index] = occupancy;
85}
86
87int64_t
88WeightedLRUPolicy::getVictim(int64_t set) const
89{
90    Tick time, smallest_time;
91    int64_t smallest_index;
92
93    smallest_index = 0;
94    smallest_time = m_last_ref_ptr[set][0];
95    int smallest_weight = m_last_occ_ptr[set][0];
96
97    for (unsigned i = 1; i < m_assoc; i++) {
98
99        int weight = m_last_occ_ptr[set][i];
100        if (weight < smallest_weight) {
101            smallest_weight = weight;
102            smallest_index = i;
103            smallest_time = m_last_ref_ptr[set][i];
104        } else if (weight == smallest_weight) {
105            time = m_last_ref_ptr[set][i];
106            if (time < smallest_time) {
107                smallest_index = i;
108                smallest_time = time;
109            }
110        }
111    }
112    return smallest_index;
113}
114