1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "mem/ruby/profiler/AddressProfiler.hh"
30
31#include <vector>
32
33#include "base/stl_helpers.hh"
34#include "mem/ruby/profiler/Profiler.hh"
35#include "mem/ruby/protocol/RubyRequest.hh"
36
37using namespace std;
38typedef AddressProfiler::AddressMap AddressMap;
39
40using m5::stl_helpers::operator<<;
41
42// Helper functions
43AccessTraceForAddress&
44lookupTraceForAddress(Addr addr, AddressMap& record_map)
45{
46    // we create a static default object here that is used to insert
47    // since the insertion will create a copy of the object in the
48    // process.  Perhaps this is optimizing early, but it doesn't seem
49    // like it could hurt.
50    static const AccessTraceForAddress dflt;
51
52    pair<AddressMap::iterator, bool> r =
53        record_map.insert(make_pair(addr, dflt));
54    AddressMap::iterator i = r.first;
55    AccessTraceForAddress &access_trace = i->second;
56    if (r.second) {
57        // there was nothing there and the insert succeed, so we need
58        // to actually set the address.
59        access_trace.setAddress(addr);
60    }
61
62    return access_trace;
63}
64
65void
66printSorted(ostream& out, int num_of_sequencers, const AddressMap &record_map,
67            string description, Profiler *profiler)
68{
69    const int records_printed = 100;
70
71    uint64_t misses = 0;
72    std::vector<const AccessTraceForAddress *> sorted;
73
74    AddressMap::const_iterator i = record_map.begin();
75    AddressMap::const_iterator end = record_map.end();
76    for (; i != end; ++i) {
77        const AccessTraceForAddress* record = &i->second;
78        misses += record->getTotal();
79        sorted.push_back(record);
80    }
81    sort(sorted.begin(), sorted.end(), AccessTraceForAddress::less_equal);
82
83    out << "Total_entries_" << description << ": " << record_map.size()
84        << endl;
85    if (profiler->getAllInstructions())
86        out << "Total_Instructions_" << description << ": " << misses << endl;
87    else
88        out << "Total_data_misses_" << description << ": " << misses << endl;
89
90    out << "total | load store atomic | user supervisor | sharing | touched-by"
91        << endl;
92
93    Histogram remaining_records(1, 100);
94    Histogram all_records(1, 100);
95    Histogram remaining_records_log(-1);
96    Histogram all_records_log(-1);
97
98    // Allows us to track how many lines where touched by n processors
99    std::vector<int64_t> m_touched_vec;
100    std::vector<int64_t> m_touched_weighted_vec;
101    m_touched_vec.resize(num_of_sequencers+1);
102    m_touched_weighted_vec.resize(num_of_sequencers+1);
103    for (int j = 0; j < m_touched_vec.size(); j++) {
104        m_touched_vec[j] = 0;
105        m_touched_weighted_vec[j] = 0;
106    }
107
108    int counter = 0;
109    int max = sorted.size();
110    while (counter < max && counter < records_printed) {
111        const AccessTraceForAddress* record = sorted[counter];
112        double percent = 100.0 * (record->getTotal() / double(misses));
113        out << description << " | " << percent << " % " << *record << endl;
114        all_records.add(record->getTotal());
115        all_records_log.add(record->getTotal());
116        counter++;
117        m_touched_vec[record->getTouchedBy()]++;
118        m_touched_weighted_vec[record->getTouchedBy()] += record->getTotal();
119    }
120
121    while (counter < max) {
122        const AccessTraceForAddress* record = sorted[counter];
123        all_records.add(record->getTotal());
124        remaining_records.add(record->getTotal());
125        all_records_log.add(record->getTotal());
126        remaining_records_log.add(record->getTotal());
127        m_touched_vec[record->getTouchedBy()]++;
128        m_touched_weighted_vec[record->getTouchedBy()] += record->getTotal();
129    }
130    out << endl;
131    out << "all_records_" << description << ": "
132        << all_records << endl
133        << "all_records_log_" << description << ": "
134        << all_records_log << endl
135        << "remaining_records_" << description << ": "
136        << remaining_records << endl
137        << "remaining_records_log_" << description << ": "
138        << remaining_records_log << endl
139        << "touched_by_" << description << ": "
140        << m_touched_vec << endl
141        << "touched_by_weighted_" << description << ": "
142        << m_touched_weighted_vec << endl
143        << endl;
144}
145
146AddressProfiler::AddressProfiler(int num_of_sequencers, Profiler *profiler)
147    : m_profiler(profiler)
148{
149    m_num_of_sequencers = num_of_sequencers;
150    clearStats();
151}
152
153AddressProfiler::~AddressProfiler()
154{
155}
156
157void
158AddressProfiler::setHotLines(bool hot_lines)
159{
160    m_hot_lines = hot_lines;
161}
162
163void
164AddressProfiler::setAllInstructions(bool all_instructions)
165{
166    m_all_instructions = all_instructions;
167}
168
169void
170AddressProfiler::printStats(ostream& out) const
171{
172    if (m_hot_lines) {
173        out << endl;
174        out << "AddressProfiler Stats" << endl;
175        out << "---------------------" << endl;
176
177        out << endl;
178        out << "sharing_misses: " << m_sharing_miss_counter << endl;
179        out << "getx_sharing_histogram: " << m_getx_sharing_histogram << endl;
180        out << "gets_sharing_histogram: " << m_gets_sharing_histogram << endl;
181
182        out << endl;
183        out << "Hot Data Blocks" << endl;
184        out << "---------------" << endl;
185        out << endl;
186        printSorted(out, m_num_of_sequencers, m_dataAccessTrace,
187                    "block_address", m_profiler);
188
189        out << endl;
190        out << "Hot MacroData Blocks" << endl;
191        out << "--------------------" << endl;
192        out << endl;
193        printSorted(out, m_num_of_sequencers, m_macroBlockAccessTrace,
194                    "macroblock_address", m_profiler);
195
196        out << "Hot Instructions" << endl;
197        out << "----------------" << endl;
198        out << endl;
199        printSorted(out, m_num_of_sequencers, m_programCounterAccessTrace,
200                    "pc_address", m_profiler);
201    }
202
203    if (m_all_instructions) {
204        out << endl;
205        out << "All Instructions Profile:" << endl;
206        out << "-------------------------" << endl;
207        out << endl;
208        printSorted(out, m_num_of_sequencers, m_programCounterAccessTrace,
209                    "pc_address", m_profiler);
210        out << endl;
211    }
212
213    if (m_retryProfileHisto.size() > 0) {
214        out << "Retry Profile" << endl;
215        out << "-------------" << endl;
216        out << endl;
217        out << "retry_histogram_absolute: " << m_retryProfileHisto << endl;
218        out << "retry_histogram_write: " << m_retryProfileHistoWrite << endl;
219        out << "retry_histogram_read: " << m_retryProfileHistoRead << endl;
220
221        out << "retry_histogram_percent: ";
222        m_retryProfileHisto.printPercent(out);
223        out << endl;
224
225        printSorted(out, m_num_of_sequencers, m_retryProfileMap,
226                    "block_address", m_profiler);
227        out << endl;
228    }
229}
230
231void
232AddressProfiler::clearStats()
233{
234    // Clear the maps
235    m_sharing_miss_counter = 0;
236    m_dataAccessTrace.clear();
237    m_macroBlockAccessTrace.clear();
238    m_programCounterAccessTrace.clear();
239    m_retryProfileMap.clear();
240    m_retryProfileHisto.clear();
241    m_retryProfileHistoRead.clear();
242    m_retryProfileHistoWrite.clear();
243    m_getx_sharing_histogram.clear();
244    m_gets_sharing_histogram.clear();
245}
246
247void
248AddressProfiler::profileGetX(Addr datablock, Addr PC,
249                             const Set& owner, const Set& sharers,
250                             NodeID requestor)
251{
252    Set indirection_set;
253    indirection_set.addSet(sharers);
254    indirection_set.addSet(owner);
255    indirection_set.remove(requestor);
256    int num_indirections = indirection_set.count();
257
258    m_getx_sharing_histogram.add(num_indirections);
259    bool indirection_miss = (num_indirections > 0);
260
261    addTraceSample(datablock, PC, RubyRequestType_ST, RubyAccessMode(0),
262                   requestor, indirection_miss);
263}
264
265void
266AddressProfiler::profileGetS(Addr datablock, Addr PC,
267                             const Set& owner, const Set& sharers,
268                             NodeID requestor)
269{
270    Set indirection_set;
271    indirection_set.addSet(owner);
272    indirection_set.remove(requestor);
273    int num_indirections = indirection_set.count();
274
275    m_gets_sharing_histogram.add(num_indirections);
276    bool indirection_miss = (num_indirections > 0);
277
278    addTraceSample(datablock, PC, RubyRequestType_LD, RubyAccessMode(0),
279                   requestor, indirection_miss);
280}
281
282void
283AddressProfiler::addTraceSample(Addr data_addr, Addr pc_addr,
284                                RubyRequestType type,
285                                RubyAccessMode access_mode, NodeID id,
286                                bool sharing_miss)
287{
288    if (m_all_instructions) {
289        if (sharing_miss) {
290            m_sharing_miss_counter++;
291        }
292
293        // record data address trace info
294        data_addr = makeLineAddress(data_addr);
295        lookupTraceForAddress(data_addr, m_dataAccessTrace).
296            update(type, access_mode, id, sharing_miss);
297
298        // record macro data address trace info
299
300        // 6 for datablock, 4 to make it 16x more coarse
301        Addr macro_addr = maskLowOrderBits(data_addr, 10);
302        lookupTraceForAddress(macro_addr, m_macroBlockAccessTrace).
303            update(type, access_mode, id, sharing_miss);
304
305        // record program counter address trace info
306        lookupTraceForAddress(pc_addr, m_programCounterAccessTrace).
307            update(type, access_mode, id, sharing_miss);
308    }
309
310    if (m_all_instructions) {
311        // This code is used if the address profiler is an
312        // all-instructions profiler record program counter address
313        // trace info
314        lookupTraceForAddress(pc_addr, m_programCounterAccessTrace).
315            update(type, access_mode, id, sharing_miss);
316    }
317}
318
319void
320AddressProfiler::profileRetry(Addr data_addr, AccessType type, int count)
321{
322    m_retryProfileHisto.add(count);
323    if (type == AccessType_Read) {
324        m_retryProfileHistoRead.add(count);
325    } else {
326        m_retryProfileHistoWrite.add(count);
327    }
328    if (count > 1) {
329        lookupTraceForAddress(data_addr, m_retryProfileMap).addSample(count);
330    }
331}
332