AddressProfiler.cc revision 10919
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 <vector>
30
31#include "base/stl_helpers.hh"
32#include "mem/protocol/RubyRequest.hh"
33#include "mem/ruby/profiler/AddressProfiler.hh"
34#include "mem/ruby/profiler/Profiler.hh"
35
36using namespace std;
37typedef AddressProfiler::AddressMap AddressMap;
38
39using m5::stl_helpers::operator<<;
40
41// Helper functions
42AccessTraceForAddress&
43lookupTraceForAddress(const Address& addr, AddressMap& record_map)
44{
45    // we create a static default object here that is used to insert
46    // since the insertion will create a copy of the object in the
47    // process.  Perhaps this is optimizing early, but it doesn't seem
48    // like it could hurt.
49    static const AccessTraceForAddress dflt;
50
51    pair<AddressMap::iterator, bool> r =
52        record_map.insert(make_pair(addr, dflt));
53    AddressMap::iterator i = r.first;
54    AccessTraceForAddress &access_trace = i->second;
55    if (r.second) {
56        // there was nothing there and the insert succeed, so we need
57        // to actually set the address.
58        access_trace.setAddress(addr);
59    }
60
61    return access_trace;
62}
63
64void
65printSorted(ostream& out, int num_of_sequencers, const AddressMap &record_map,
66            string description, Profiler *profiler)
67{
68    const int records_printed = 100;
69
70    uint64 misses = 0;
71    std::vector<const AccessTraceForAddress *> sorted;
72
73    AddressMap::const_iterator i = record_map.begin();
74    AddressMap::const_iterator end = record_map.end();
75    for (; i != end; ++i) {
76        const AccessTraceForAddress* record = &i->second;
77        misses += record->getTotal();
78        sorted.push_back(record);
79    }
80    sort(sorted.begin(), sorted.end(), AccessTraceForAddress::less_equal);
81
82    out << "Total_entries_" << description << ": " << record_map.size()
83        << endl;
84    if (profiler->getAllInstructions())
85        out << "Total_Instructions_" << description << ": " << misses << endl;
86    else
87        out << "Total_data_misses_" << description << ": " << misses << endl;
88
89    out << "total | load store atomic | user supervisor | sharing | touched-by"
90        << endl;
91
92    Histogram remaining_records(1, 100);
93    Histogram all_records(1, 100);
94    Histogram remaining_records_log(-1);
95    Histogram all_records_log(-1);
96
97    // Allows us to track how many lines where touched by n processors
98    std::vector<int64> m_touched_vec;
99    std::vector<int64> m_touched_weighted_vec;
100    m_touched_vec.resize(num_of_sequencers+1);
101    m_touched_weighted_vec.resize(num_of_sequencers+1);
102    for (int j = 0; j < m_touched_vec.size(); j++) {
103        m_touched_vec[j] = 0;
104        m_touched_weighted_vec[j] = 0;
105    }
106
107    int counter = 0;
108    int max = sorted.size();
109    while (counter < max && counter < records_printed) {
110        const AccessTraceForAddress* record = sorted[counter];
111        double percent = 100.0 * (record->getTotal() / double(misses));
112        out << description << " | " << percent << " % " << *record << endl;
113        all_records.add(record->getTotal());
114        all_records_log.add(record->getTotal());
115        counter++;
116        m_touched_vec[record->getTouchedBy()]++;
117        m_touched_weighted_vec[record->getTouchedBy()] += record->getTotal();
118    }
119
120    while (counter < max) {
121        const AccessTraceForAddress* record = sorted[counter];
122        all_records.add(record->getTotal());
123        remaining_records.add(record->getTotal());
124        all_records_log.add(record->getTotal());
125        remaining_records_log.add(record->getTotal());
126        m_touched_vec[record->getTouchedBy()]++;
127        m_touched_weighted_vec[record->getTouchedBy()] += record->getTotal();
128    }
129    out << endl;
130    out << "all_records_" << description << ": "
131        << all_records << endl
132        << "all_records_log_" << description << ": "
133        << all_records_log << endl
134        << "remaining_records_" << description << ": "
135        << remaining_records << endl
136        << "remaining_records_log_" << description << ": "
137        << remaining_records_log << endl
138        << "touched_by_" << description << ": "
139        << m_touched_vec << endl
140        << "touched_by_weighted_" << description << ": "
141        << m_touched_weighted_vec << endl
142        << endl;
143}
144
145AddressProfiler::AddressProfiler(int num_of_sequencers, Profiler *profiler)
146    : m_profiler(profiler)
147{
148    m_num_of_sequencers = num_of_sequencers;
149    clearStats();
150}
151
152AddressProfiler::~AddressProfiler()
153{
154}
155
156void
157AddressProfiler::setHotLines(bool hot_lines)
158{
159    m_hot_lines = hot_lines;
160}
161
162void
163AddressProfiler::setAllInstructions(bool all_instructions)
164{
165    m_all_instructions = all_instructions;
166}
167
168void
169AddressProfiler::printStats(ostream& out) const
170{
171    if (m_hot_lines) {
172        out << endl;
173        out << "AddressProfiler Stats" << endl;
174        out << "---------------------" << endl;
175
176        out << endl;
177        out << "sharing_misses: " << m_sharing_miss_counter << endl;
178        out << "getx_sharing_histogram: " << m_getx_sharing_histogram << endl;
179        out << "gets_sharing_histogram: " << m_gets_sharing_histogram << endl;
180
181        out << endl;
182        out << "Hot Data Blocks" << endl;
183        out << "---------------" << endl;
184        out << endl;
185        printSorted(out, m_num_of_sequencers, m_dataAccessTrace,
186                    "block_address", m_profiler);
187
188        out << endl;
189        out << "Hot MacroData Blocks" << endl;
190        out << "--------------------" << endl;
191        out << endl;
192        printSorted(out, m_num_of_sequencers, m_macroBlockAccessTrace,
193                    "macroblock_address", m_profiler);
194
195        out << "Hot Instructions" << endl;
196        out << "----------------" << endl;
197        out << endl;
198        printSorted(out, m_num_of_sequencers, m_programCounterAccessTrace,
199                    "pc_address", m_profiler);
200    }
201
202    if (m_all_instructions) {
203        out << endl;
204        out << "All Instructions Profile:" << endl;
205        out << "-------------------------" << endl;
206        out << endl;
207        printSorted(out, m_num_of_sequencers, m_programCounterAccessTrace,
208                    "pc_address", m_profiler);
209        out << endl;
210    }
211
212    if (m_retryProfileHisto.size() > 0) {
213        out << "Retry Profile" << endl;
214        out << "-------------" << endl;
215        out << endl;
216        out << "retry_histogram_absolute: " << m_retryProfileHisto << endl;
217        out << "retry_histogram_write: " << m_retryProfileHistoWrite << endl;
218        out << "retry_histogram_read: " << m_retryProfileHistoRead << endl;
219
220        out << "retry_histogram_percent: ";
221        m_retryProfileHisto.printPercent(out);
222        out << endl;
223
224        printSorted(out, m_num_of_sequencers, m_retryProfileMap,
225                    "block_address", m_profiler);
226        out << endl;
227    }
228}
229
230void
231AddressProfiler::clearStats()
232{
233    // Clear the maps
234    m_sharing_miss_counter = 0;
235    m_dataAccessTrace.clear();
236    m_macroBlockAccessTrace.clear();
237    m_programCounterAccessTrace.clear();
238    m_retryProfileMap.clear();
239    m_retryProfileHisto.clear();
240    m_retryProfileHistoRead.clear();
241    m_retryProfileHistoWrite.clear();
242    m_getx_sharing_histogram.clear();
243    m_gets_sharing_histogram.clear();
244}
245
246void
247AddressProfiler::profileGetX(const Address& datablock, const Address& PC,
248                             const Set& owner, const Set& sharers,
249                             NodeID requestor)
250{
251    Set indirection_set;
252    indirection_set.addSet(sharers);
253    indirection_set.addSet(owner);
254    indirection_set.remove(requestor);
255    int num_indirections = indirection_set.count();
256
257    m_getx_sharing_histogram.add(num_indirections);
258    bool indirection_miss = (num_indirections > 0);
259
260    addTraceSample(datablock, PC, RubyRequestType_ST, RubyAccessMode(0),
261                   requestor, indirection_miss);
262}
263
264void
265AddressProfiler::profileGetS(const Address& datablock, const Address& PC,
266                             const Set& owner, const Set& sharers,
267                             NodeID requestor)
268{
269    Set indirection_set;
270    indirection_set.addSet(owner);
271    indirection_set.remove(requestor);
272    int num_indirections = indirection_set.count();
273
274    m_gets_sharing_histogram.add(num_indirections);
275    bool indirection_miss = (num_indirections > 0);
276
277    addTraceSample(datablock, PC, RubyRequestType_LD, RubyAccessMode(0),
278                   requestor, indirection_miss);
279}
280
281void
282AddressProfiler::addTraceSample(Address data_addr, Address pc_addr,
283                                RubyRequestType type,
284                                RubyAccessMode access_mode, NodeID id,
285                                bool sharing_miss)
286{
287    if (m_all_instructions) {
288        if (sharing_miss) {
289            m_sharing_miss_counter++;
290        }
291
292        // record data address trace info
293        data_addr.makeLineAddress();
294        lookupTraceForAddress(data_addr, m_dataAccessTrace).
295            update(type, access_mode, id, sharing_miss);
296
297        // record macro data address trace info
298
299        // 6 for datablock, 4 to make it 16x more coarse
300        Address macro_addr(data_addr.maskLowOrderBits(10));
301        lookupTraceForAddress(macro_addr, m_macroBlockAccessTrace).
302            update(type, access_mode, id, sharing_miss);
303
304        // record program counter address trace info
305        lookupTraceForAddress(pc_addr, m_programCounterAccessTrace).
306            update(type, access_mode, id, sharing_miss);
307    }
308
309    if (m_all_instructions) {
310        // This code is used if the address profiler is an
311        // all-instructions profiler record program counter address
312        // trace info
313        lookupTraceForAddress(pc_addr, m_programCounterAccessTrace).
314            update(type, access_mode, id, sharing_miss);
315    }
316}
317
318void
319AddressProfiler::profileRetry(const Address& data_addr, AccessType type,
320                              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