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