AddressProfiler.cc revision 6285:ce086eca1ede
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/*
31 * AddressProfiler.cc
32 *
33 * Description: See AddressProfiler.hh
34 *
35 * $Id$
36 *
37 */
38
39#include "mem/ruby/profiler/AddressProfiler.hh"
40#include "mem/protocol/CacheMsg.hh"
41#include "mem/ruby/profiler/AccessTraceForAddress.hh"
42#include "mem/gems_common/PrioHeap.hh"
43#include "mem/gems_common/Map.hh"
44#include "mem/ruby/system/System.hh"
45#include "mem/ruby/profiler/Profiler.hh"
46
47// Helper functions
48static AccessTraceForAddress& lookupTraceForAddress(const Address& addr, Map<Address, AccessTraceForAddress>* record_map);
49static void printSorted(ostream& out, const Map<Address, AccessTraceForAddress>* record_map, string description);
50
51AddressProfiler::AddressProfiler()
52{
53  m_dataAccessTrace = new Map<Address, AccessTraceForAddress>;
54  m_macroBlockAccessTrace = new Map<Address, AccessTraceForAddress>;
55  m_programCounterAccessTrace = new Map<Address, AccessTraceForAddress>;
56  m_retryProfileMap = new Map<Address, AccessTraceForAddress>;
57  m_persistentPredictionProfileMap = new Map<Address, AccessTraceForAddress>;
58  clearStats();
59}
60
61AddressProfiler::~AddressProfiler()
62{
63  delete m_dataAccessTrace;
64  delete m_macroBlockAccessTrace;
65  delete m_programCounterAccessTrace;
66  delete m_retryProfileMap;
67  delete m_persistentPredictionProfileMap;
68}
69
70void AddressProfiler::setHotLines(bool hot_lines){
71  m_hot_lines = hot_lines;
72}
73void AddressProfiler::setAllInstructions(bool all_instructions){
74  m_all_instructions = all_instructions;
75}
76
77void AddressProfiler::printStats(ostream& out) const
78{
79  if (m_hot_lines) {
80    out << endl;
81    out << "AddressProfiler Stats" << endl;
82    out << "---------------------" << endl;
83
84    out << endl;
85    out << "sharing_misses: " << m_sharing_miss_counter << endl;
86    out << "getx_sharing_histogram: " << m_getx_sharing_histogram << endl;
87    out << "gets_sharing_histogram: " << m_gets_sharing_histogram << endl;
88
89    out << endl;
90    out << "Hot Data Blocks" << endl;
91    out << "---------------" << endl;
92    out << endl;
93    printSorted(out, m_dataAccessTrace, "block_address");
94
95    out << endl;
96    out << "Hot MacroData Blocks" << endl;
97    out << "--------------------" << endl;
98    out << endl;
99    printSorted(out, m_macroBlockAccessTrace, "macroblock_address");
100
101    out << "Hot Instructions" << endl;
102    out << "----------------" << endl;
103    out << endl;
104    printSorted(out, m_programCounterAccessTrace, "pc_address");
105  }
106
107  if (m_all_instructions){
108    out << endl;
109    out << "All Instructions Profile:" << endl;
110    out << "-------------------------" << endl;
111    out << endl;
112    printSorted(out, m_programCounterAccessTrace, "pc_address");
113    out << endl;
114  }
115
116  if (m_retryProfileHisto.size() > 0) {
117    out << "Retry Profile" << endl;
118    out << "-------------" << endl;
119    out << endl;
120    out << "retry_histogram_absolute: " << m_retryProfileHisto << endl;
121    out << "retry_histogram_write: " << m_retryProfileHistoWrite << endl;
122    out << "retry_histogram_read: " << m_retryProfileHistoRead << endl;
123
124    out << "retry_histogram_percent: ";
125    m_retryProfileHisto.printPercent(out);
126    out << endl;
127
128    out << "retry_histogram_per_instruction: ";
129    m_retryProfileHisto.printWithMultiplier(out, 1.0 / double(g_system_ptr->getProfiler()->getTotalInstructionsExecuted()));
130    out << endl;
131
132    printSorted(out, m_retryProfileMap, "block_address");
133    out << endl;
134  }
135
136  if (m_persistentPredictionProfileHisto.size() > 0) {
137    out << "Persistent Prediction Profile" << endl;
138    out << "-------------" << endl;
139    out << endl;
140    out << "persistent prediction_histogram: " << m_persistentPredictionProfileHisto << endl;
141
142    out << "persistent prediction_histogram_percent: ";
143    m_persistentPredictionProfileHisto.printPercent(out);
144    out << endl;
145
146    out << "persistentPrediction_histogram_per_instruction: ";
147    m_persistentPredictionProfileHisto.printWithMultiplier(out, 1.0 / double(g_system_ptr->getProfiler()->getTotalInstructionsExecuted()));
148    out << endl;
149
150    printSorted(out, m_persistentPredictionProfileMap, "block_address");
151    out << endl;
152  }
153}
154
155void AddressProfiler::clearStats()
156{
157  // Clear the maps
158  m_sharing_miss_counter = 0;
159  m_dataAccessTrace->clear();
160  m_macroBlockAccessTrace->clear();
161  m_programCounterAccessTrace->clear();
162  m_retryProfileMap->clear();
163  m_retryProfileHisto.clear();
164  m_retryProfileHistoRead.clear();
165  m_retryProfileHistoWrite.clear();
166  m_getx_sharing_histogram.clear();
167  m_gets_sharing_histogram.clear();
168}
169
170void AddressProfiler::profileGetX(const Address& datablock, const Address& PC, const Set& owner, const Set& sharers, NodeID requestor)
171{
172  Set indirection_set;
173  indirection_set.addSet(sharers);
174  indirection_set.addSet(owner);
175  indirection_set.remove(requestor);
176  int num_indirections = indirection_set.count();
177
178  m_getx_sharing_histogram.add(num_indirections);
179  bool indirection_miss = (num_indirections > 0);
180
181  addTraceSample(datablock, PC, CacheRequestType_ST, AccessModeType(0), requestor, indirection_miss);
182}
183
184void AddressProfiler::profileGetS(const Address& datablock, const Address& PC, const Set& owner, const Set& sharers, NodeID requestor)
185{
186  Set indirection_set;
187  indirection_set.addSet(owner);
188  indirection_set.remove(requestor);
189  int num_indirections = indirection_set.count();
190
191  m_gets_sharing_histogram.add(num_indirections);
192  bool indirection_miss = (num_indirections > 0);
193
194  addTraceSample(datablock, PC, CacheRequestType_LD, AccessModeType(0), requestor, indirection_miss);
195}
196
197void AddressProfiler::addTraceSample(Address data_addr, Address pc_addr, CacheRequestType type, AccessModeType access_mode, NodeID id, bool sharing_miss)
198{
199  if (m_all_instructions) {
200    if (sharing_miss) {
201      m_sharing_miss_counter++;
202    }
203
204    // record data address trace info
205    data_addr.makeLineAddress();
206    lookupTraceForAddress(data_addr, m_dataAccessTrace).update(type, access_mode, id, sharing_miss);
207
208    // record macro data address trace info
209    Address macro_addr(data_addr.maskLowOrderBits(10)); // 6 for datablock, 4 to make it 16x more coarse
210    lookupTraceForAddress(macro_addr, m_macroBlockAccessTrace).update(type, access_mode, id, sharing_miss);
211
212    // record program counter address trace info
213    lookupTraceForAddress(pc_addr, m_programCounterAccessTrace).update(type, access_mode, id, sharing_miss);
214  }
215
216  if (m_all_instructions) {
217    // This code is used if the address profiler is an all-instructions profiler
218    // record program counter address trace info
219    lookupTraceForAddress(pc_addr, m_programCounterAccessTrace).update(type, access_mode, id, sharing_miss);
220  }
221}
222
223void AddressProfiler::profileRetry(const Address& data_addr, AccessType type, int count)
224{
225  m_retryProfileHisto.add(count);
226  if (type == AccessType_Read) {
227    m_retryProfileHistoRead.add(count);
228  } else {
229    m_retryProfileHistoWrite.add(count);
230  }
231  if (count > 1) {
232    lookupTraceForAddress(data_addr, m_retryProfileMap).addSample(count);
233  }
234}
235
236void AddressProfiler::profilePersistentPrediction(const Address& data_addr, AccessType type)
237{
238  m_persistentPredictionProfileHisto.add(1);
239  lookupTraceForAddress(data_addr, m_persistentPredictionProfileMap).addSample(1);
240}
241
242// ***** Normal Functions ******
243
244static void printSorted(ostream& out, const Map<Address, AccessTraceForAddress>* record_map, string description)
245{
246  const int records_printed = 100;
247
248  uint64 misses = 0;
249  PrioHeap<AccessTraceForAddress*> heap;
250  Vector<Address> keys = record_map->keys();
251  for(int i=0; i<keys.size(); i++){
252    AccessTraceForAddress* record = &(record_map->lookup(keys[i]));
253    misses += record->getTotal();
254    heap.insert(record);
255  }
256
257  out << "Total_entries_" << description << ": " << keys.size() << endl;
258  if (g_system_ptr->getProfiler()->getAllInstructions())
259    out << "Total_Instructions_" << description << ": " << misses << endl;
260  else
261    out << "Total_data_misses_" << description << ": " << misses << endl;
262
263  out << "total | load store atomic | user supervisor | sharing | touched-by" << endl;
264
265  Histogram remaining_records(1, 100);
266  Histogram all_records(1, 100);
267  Histogram remaining_records_log(-1);
268  Histogram all_records_log(-1);
269
270  // Allows us to track how many lines where touched by n processors
271  Vector<int64> m_touched_vec;
272  Vector<int64> m_touched_weighted_vec;
273  m_touched_vec.setSize(RubySystem::getNumberOfSequencers()+1);
274  m_touched_weighted_vec.setSize(RubySystem::getNumberOfSequencers()+1);
275  for (int i=0; i<m_touched_vec.size(); i++) {
276    m_touched_vec[i] = 0;
277    m_touched_weighted_vec[i] = 0;
278  }
279
280  int counter = 0;
281  while((heap.size() > 0) && (counter < records_printed)) {
282    AccessTraceForAddress* record = heap.extractMin();
283    double percent = 100.0*(record->getTotal()/double(misses));
284    out << description << " | " << percent << " % " << *record << endl;
285    all_records.add(record->getTotal());
286    all_records_log.add(record->getTotal());
287    counter++;
288    m_touched_vec[record->getTouchedBy()]++;
289    m_touched_weighted_vec[record->getTouchedBy()] += record->getTotal();
290  }
291
292  while(heap.size() > 0) {
293    AccessTraceForAddress* record = heap.extractMin();
294    all_records.add(record->getTotal());
295    remaining_records.add(record->getTotal());
296    all_records_log.add(record->getTotal());
297    remaining_records_log.add(record->getTotal());
298    m_touched_vec[record->getTouchedBy()]++;
299    m_touched_weighted_vec[record->getTouchedBy()] += record->getTotal();
300  }
301  out << endl;
302  out << "all_records_" << description << ": " << all_records << endl;
303  out << "all_records_log_" << description << ": " << all_records_log << endl;
304  out << "remaining_records_" << description << ": " << remaining_records << endl;
305  out << "remaining_records_log_" << description << ": " << remaining_records_log << endl;
306  out << "touched_by_" << description << ": " << m_touched_vec << endl;
307  out << "touched_by_weighted_" << description << ": " << m_touched_weighted_vec << endl;
308  out << endl;
309}
310
311static AccessTraceForAddress& lookupTraceForAddress(const Address& addr, Map<Address, AccessTraceForAddress>* record_map)
312{
313  if(record_map->exist(addr) == false){
314    record_map->add(addr, AccessTraceForAddress(addr));
315  }
316  return record_map->lookup(addr);
317}
318