Profiler.cc revision 7002:48a19d52d939
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/*
30   This file has been modified by Kevin Moore and Dan Nussbaum of the
31   Scalable Systems Research Group at Sun Microsystems Laboratories
32   (http://research.sun.com/scalable/) to support the Adaptive
33   Transactional Memory Test Platform (ATMTP).
34
35   Please send email to atmtp-interest@sun.com with feedback, questions, or
36   to request future announcements about ATMTP.
37
38   ----------------------------------------------------------------------
39
40   File modification date: 2008-02-23
41
42   ----------------------------------------------------------------------
43*/
44
45/*
46 * Profiler.cc
47 *
48 * Description: See Profiler.hh
49 *
50 * $Id$
51 *
52 */
53
54// Allows use of times() library call, which determines virtual runtime
55#include <sys/resource.h>
56#include <sys/times.h>
57
58#include "mem/ruby/profiler/Profiler.hh"
59#include "mem/ruby/profiler/AddressProfiler.hh"
60#include "mem/ruby/system/System.hh"
61#include "mem/ruby/network/Network.hh"
62#include "mem/gems_common/PrioHeap.hh"
63#include "mem/protocol/CacheMsg.hh"
64#include "mem/protocol/Protocol.hh"
65#include "mem/gems_common/util.hh"
66#include "mem/gems_common/Map.hh"
67#include "mem/ruby/common/Debug.hh"
68#include "mem/protocol/MachineType.hh"
69
70#include "mem/ruby/system/System.hh"
71
72extern std::ostream * debug_cout_ptr;
73
74static double process_memory_total();
75static double process_memory_resident();
76
77Profiler::Profiler(const Params *p)
78    : SimObject(p)
79{
80  m_requestProfileMap_ptr = new Map<string, int>;
81
82  m_inst_profiler_ptr = NULL;
83  m_address_profiler_ptr = NULL;
84
85  m_real_time_start_time = time(NULL); // Not reset in clearStats()
86  m_stats_period = 1000000; // Default
87  m_periodic_output_file_ptr = &cerr;
88
89  m_hot_lines = p->hot_lines;
90  m_all_instructions = p->all_instructions;
91
92  m_num_of_sequencers = p->num_of_sequencers;
93
94  m_hot_lines = false;
95  m_all_instructions = false;
96
97  m_address_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
98  m_address_profiler_ptr -> setHotLines(m_hot_lines);
99  m_address_profiler_ptr -> setAllInstructions(m_all_instructions);
100
101  if (m_all_instructions) {
102    m_inst_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
103    m_inst_profiler_ptr -> setHotLines(m_hot_lines);
104    m_inst_profiler_ptr -> setAllInstructions(m_all_instructions);
105  }
106}
107
108Profiler::~Profiler()
109{
110  if (m_periodic_output_file_ptr != &cerr) {
111    delete m_periodic_output_file_ptr;
112  }
113
114  delete m_requestProfileMap_ptr;
115}
116
117void Profiler::wakeup()
118{
119  // FIXME - avoid the repeated code
120
121  Vector<integer_t> perProcCycleCount;
122  perProcCycleCount.setSize(m_num_of_sequencers);
123
124  for(int i=0; i < m_num_of_sequencers; i++) {
125    perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
126    // The +1 allows us to avoid division by zero
127  }
128
129  integer_t total_misses = m_perProcTotalMisses.sum();
130  integer_t simics_cycles_executed = perProcCycleCount.sum();
131  integer_t transactions_started = m_perProcStartTransaction.sum();
132  integer_t transactions_ended = m_perProcEndTransaction.sum();
133
134  (*m_periodic_output_file_ptr) << "ruby_cycles: "
135                                << g_eventQueue_ptr->getTime()-m_ruby_start
136                                << endl;
137
138  (*m_periodic_output_file_ptr) << "total_misses: "
139                                << total_misses
140                                << " "
141                                << m_perProcTotalMisses
142                                << endl;
143
144  (*m_periodic_output_file_ptr) << "simics_cycles_executed: "
145                                << simics_cycles_executed
146                                << " "
147                                << perProcCycleCount
148                                << endl;
149
150  (*m_periodic_output_file_ptr) << "transactions_started: "
151                                << transactions_started
152                                << " "
153                                << m_perProcStartTransaction
154                                << endl;
155
156  (*m_periodic_output_file_ptr) << "transactions_ended: "
157                                << transactions_ended
158                                << " "
159                                << m_perProcEndTransaction
160                                << endl;
161
162  (*m_periodic_output_file_ptr) << "mbytes_resident: "
163                                << process_memory_resident()
164                                << endl;
165
166  (*m_periodic_output_file_ptr) << "mbytes_total: "
167                                << process_memory_total()
168                                << endl;
169
170  if (process_memory_total() > 0) {
171    (*m_periodic_output_file_ptr) << "resident_ratio: "
172                          << process_memory_resident()/process_memory_total()
173                          << endl;
174  }
175
176  (*m_periodic_output_file_ptr) << "miss_latency: "
177                                << m_allMissLatencyHistogram
178                                << endl;
179
180  *m_periodic_output_file_ptr << endl;
181
182  if (m_all_instructions) {
183    m_inst_profiler_ptr->printStats(*m_periodic_output_file_ptr);
184  }
185
186  //g_system_ptr->getNetwork()->printStats(*m_periodic_output_file_ptr);
187  g_eventQueue_ptr->scheduleEvent(this, m_stats_period);
188}
189
190void Profiler::setPeriodicStatsFile(const string& filename)
191{
192  cout << "Recording periodic statistics to file '" << filename << "' every "
193       << m_stats_period << " Ruby cycles" << endl;
194
195  if (m_periodic_output_file_ptr != &cerr) {
196    delete m_periodic_output_file_ptr;
197  }
198
199  m_periodic_output_file_ptr = new ofstream(filename.c_str());
200  g_eventQueue_ptr->scheduleEvent(this, 1);
201}
202
203void Profiler::setPeriodicStatsInterval(integer_t period)
204{
205  cout << "Recording periodic statistics every " << m_stats_period
206       << " Ruby cycles" << endl;
207
208  m_stats_period = period;
209  g_eventQueue_ptr->scheduleEvent(this, 1);
210}
211
212void Profiler::printConfig(ostream& out) const
213{
214  out << endl;
215  out << "Profiler Configuration" << endl;
216  out << "----------------------" << endl;
217  out << "periodic_stats_period: " << m_stats_period << endl;
218}
219
220void Profiler::print(ostream& out) const
221{
222  out << "[Profiler]";
223}
224
225void Profiler::printStats(ostream& out, bool short_stats)
226{
227  out << endl;
228  if (short_stats) {
229    out << "SHORT ";
230  }
231  out << "Profiler Stats" << endl;
232  out << "--------------" << endl;
233
234  time_t real_time_current = time(NULL);
235  double seconds = difftime(real_time_current, m_real_time_start_time);
236  double minutes = seconds/60.0;
237  double hours = minutes/60.0;
238  double days = hours/24.0;
239  Time ruby_cycles = g_eventQueue_ptr->getTime()-m_ruby_start;
240
241  if (!short_stats) {
242    out << "Elapsed_time_in_seconds: " << seconds << endl;
243    out << "Elapsed_time_in_minutes: " << minutes << endl;
244    out << "Elapsed_time_in_hours: " << hours << endl;
245    out << "Elapsed_time_in_days: " << days << endl;
246    out << endl;
247  }
248
249  // print the virtual runtimes as well
250  struct tms vtime;
251  times(&vtime);
252  seconds = (vtime.tms_utime + vtime.tms_stime) / 100.0;
253  minutes = seconds / 60.0;
254  hours = minutes / 60.0;
255  days = hours / 24.0;
256  out << "Virtual_time_in_seconds: " << seconds << endl;
257  out << "Virtual_time_in_minutes: " << minutes << endl;
258  out << "Virtual_time_in_hours:   " << hours << endl;
259  out << "Virtual_time_in_days:    " << days << endl;
260  out << endl;
261
262  out << "Ruby_current_time: " << g_eventQueue_ptr->getTime() << endl;
263  out << "Ruby_start_time: " << m_ruby_start << endl;
264  out << "Ruby_cycles: " << ruby_cycles << endl;
265  out << endl;
266
267  if (!short_stats) {
268    out << "mbytes_resident: " << process_memory_resident() << endl;
269    out << "mbytes_total: " << process_memory_total() << endl;
270    if (process_memory_total() > 0) {
271      out << "resident_ratio: "
272          << process_memory_resident()/process_memory_total() << endl;
273    }
274    out << endl;
275
276  }
277
278  Vector<integer_t> perProcCycleCount;
279  Vector<double> perProcCyclesPerTrans;
280  Vector<double> perProcMissesPerTrans;
281
282
283  perProcCycleCount.setSize(m_num_of_sequencers);
284  perProcCyclesPerTrans.setSize(m_num_of_sequencers);
285  perProcMissesPerTrans.setSize(m_num_of_sequencers);
286
287  for(int i=0; i < m_num_of_sequencers; i++) {
288    perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
289    // The +1 allows us to avoid division by zero
290
291    int trans = m_perProcEndTransaction[i];
292    if (trans == 0) {
293      perProcCyclesPerTrans[i] = 0;
294      perProcMissesPerTrans[i] = 0;
295    } else {
296      perProcCyclesPerTrans[i] = ruby_cycles / double(trans);
297      perProcMissesPerTrans[i] = m_perProcTotalMisses[i] / double(trans);
298    }
299  }
300
301  integer_t total_misses = m_perProcTotalMisses.sum();
302  integer_t user_misses = m_perProcUserMisses.sum();
303  integer_t supervisor_misses = m_perProcSupervisorMisses.sum();
304  integer_t simics_cycles_executed = perProcCycleCount.sum();
305  integer_t transactions_started = m_perProcStartTransaction.sum();
306  integer_t transactions_ended = m_perProcEndTransaction.sum();
307
308  double cycles_per_transaction = (transactions_ended != 0) ? (m_num_of_sequencers * double(ruby_cycles)) / double(transactions_ended) : 0;
309  double misses_per_transaction = (transactions_ended != 0) ? double(total_misses) / double(transactions_ended) : 0;
310
311  out << "Total_misses: " << total_misses << endl;
312  out << "total_misses: " << total_misses << " " << m_perProcTotalMisses << endl;
313  out << "user_misses: " << user_misses << " " << m_perProcUserMisses << endl;
314  out << "supervisor_misses: " << supervisor_misses << " " << m_perProcSupervisorMisses << endl;
315  out << endl;
316  out << "ruby_cycles_executed: " << simics_cycles_executed << " " << perProcCycleCount << endl;
317  out << endl;
318  out << "transactions_started: " << transactions_started << " " << m_perProcStartTransaction << endl;
319  out << "transactions_ended: " << transactions_ended << " " << m_perProcEndTransaction << endl;
320  out << "cycles_per_transaction: " << cycles_per_transaction  << " " << perProcCyclesPerTrans << endl;
321  out << "misses_per_transaction: " << misses_per_transaction << " " << perProcMissesPerTrans << endl;
322
323  out << endl;
324
325  out << endl;
326
327  if (!short_stats) {
328    out << "Busy Controller Counts:" << endl;
329    for(int i=0; i < MachineType_NUM; i++) {
330      for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
331        MachineID machID;
332        machID.type = (MachineType)i;
333        machID.num = j;
334        out << machID << ":" << m_busyControllerCount[i][j] << "  ";
335        if ((j+1)%8 == 0) {
336          out << endl;
337        }
338      }
339      out << endl;
340    }
341    out << endl;
342
343    out << "Busy Bank Count:" << m_busyBankCount << endl;
344    out << endl;
345
346    out << "sequencer_requests_outstanding: " << m_sequencer_requests << endl;
347    out << endl;
348  }
349
350  if (!short_stats) {
351    out << "All Non-Zero Cycle Demand Cache Accesses" << endl;
352    out << "----------------------------------------" << endl;
353    out << "miss_latency: " << m_allMissLatencyHistogram << endl;
354    for(int i=0; i<m_missLatencyHistograms.size(); i++) {
355      if (m_missLatencyHistograms[i].size() > 0) {
356        out << "miss_latency_" << RubyRequestType(i) << ": " << m_missLatencyHistograms[i] << endl;
357      }
358    }
359    for(int i=0; i<m_machLatencyHistograms.size(); i++) {
360      if (m_machLatencyHistograms[i].size() > 0) {
361        out << "miss_latency_" << GenericMachineType(i) << ": " << m_machLatencyHistograms[i] << endl;
362      }
363    }
364
365    out << endl;
366
367    out << "All Non-Zero Cycle SW Prefetch Requests" << endl;
368    out << "------------------------------------" << endl;
369    out << "prefetch_latency: " << m_allSWPrefetchLatencyHistogram << endl;
370    for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
371      if (m_SWPrefetchLatencyHistograms[i].size() > 0) {
372        out << "prefetch_latency_" << CacheRequestType(i) << ": " << m_SWPrefetchLatencyHistograms[i] << endl;
373      }
374    }
375    for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
376      if (m_SWPrefetchMachLatencyHistograms[i].size() > 0) {
377        out << "prefetch_latency_" << GenericMachineType(i) << ": " << m_SWPrefetchMachLatencyHistograms[i] << endl;
378      }
379    }
380    out << "prefetch_latency_L2Miss:" << m_SWPrefetchL2MissLatencyHistogram << endl;
381
382    if (m_all_sharing_histogram.size() > 0) {
383      out << "all_sharing: " << m_all_sharing_histogram << endl;
384      out << "read_sharing: " << m_read_sharing_histogram << endl;
385      out << "write_sharing: " << m_write_sharing_histogram << endl;
386
387      out << "all_sharing_percent: "; m_all_sharing_histogram.printPercent(out); out << endl;
388      out << "read_sharing_percent: "; m_read_sharing_histogram.printPercent(out); out << endl;
389      out << "write_sharing_percent: "; m_write_sharing_histogram.printPercent(out); out << endl;
390
391      int64 total_miss = m_cache_to_cache +  m_memory_to_cache;
392      out << "all_misses: " << total_miss << endl;
393      out << "cache_to_cache_misses: " << m_cache_to_cache << endl;
394      out << "memory_to_cache_misses: " << m_memory_to_cache << endl;
395      out << "cache_to_cache_percent: " << 100.0 * (double(m_cache_to_cache) / double(total_miss)) << endl;
396      out << "memory_to_cache_percent: " << 100.0 * (double(m_memory_to_cache) / double(total_miss)) << endl;
397      out << endl;
398    }
399
400    if (m_outstanding_requests.size() > 0) {
401      out << "outstanding_requests: "; m_outstanding_requests.printPercent(out); out << endl;
402      out << endl;
403    }
404  }
405
406  if (!short_stats) {
407    out << "Request vs. RubySystem State Profile" << endl;
408    out << "--------------------------------" << endl;
409    out << endl;
410
411    Vector<string> requestProfileKeys = m_requestProfileMap_ptr->keys();
412    requestProfileKeys.sortVector();
413
414    for(int i=0; i<requestProfileKeys.size(); i++) {
415      int temp_int = m_requestProfileMap_ptr->lookup(requestProfileKeys[i]);
416      double percent = (100.0*double(temp_int))/double(m_requests);
417      while (requestProfileKeys[i] != "") {
418        out << setw(10) << string_split(requestProfileKeys[i], ':');
419      }
420      out << setw(11) << temp_int;
421      out << setw(14) << percent << endl;
422    }
423    out << endl;
424
425    out << "filter_action: " << m_filter_action_histogram << endl;
426
427    if (!m_all_instructions) {
428      m_address_profiler_ptr->printStats(out);
429    }
430
431    if (m_all_instructions) {
432      m_inst_profiler_ptr->printStats(out);
433    }
434
435    out << endl;
436    out << "Message Delayed Cycles" << endl;
437    out << "----------------------" << endl;
438    out << "Total_delay_cycles: " <<   m_delayedCyclesHistogram << endl;
439    out << "Total_nonPF_delay_cycles: " << m_delayedCyclesNonPFHistogram << endl;
440    for (int i = 0; i < m_delayedCyclesVCHistograms.size(); i++) {
441      out << "  virtual_network_" << i << "_delay_cycles: " << m_delayedCyclesVCHistograms[i] << endl;
442    }
443
444    printResourceUsage(out);
445  }
446
447}
448
449void Profiler::printResourceUsage(ostream& out) const
450{
451  out << endl;
452  out << "Resource Usage" << endl;
453  out << "--------------" << endl;
454
455  integer_t pagesize = getpagesize(); // page size in bytes
456  out << "page_size: " << pagesize << endl;
457
458  rusage usage;
459  getrusage (RUSAGE_SELF, &usage);
460
461  out << "user_time: " << usage.ru_utime.tv_sec << endl;
462  out << "system_time: " << usage.ru_stime.tv_sec << endl;
463  out << "page_reclaims: " << usage.ru_minflt << endl;
464  out << "page_faults: " << usage.ru_majflt << endl;
465  out << "swaps: " << usage.ru_nswap << endl;
466  out << "block_inputs: " << usage.ru_inblock << endl;
467  out << "block_outputs: " << usage.ru_oublock << endl;
468}
469
470void Profiler::clearStats()
471{
472  m_ruby_start = g_eventQueue_ptr->getTime();
473
474  m_cycles_executed_at_start.setSize(m_num_of_sequencers);
475  for (int i=0; i < m_num_of_sequencers; i++) {
476    if (g_system_ptr == NULL) {
477      m_cycles_executed_at_start[i] = 0;
478    } else {
479      m_cycles_executed_at_start[i] = g_system_ptr->getCycleCount(i);
480    }
481  }
482
483  m_perProcTotalMisses.setSize(m_num_of_sequencers);
484  m_perProcUserMisses.setSize(m_num_of_sequencers);
485  m_perProcSupervisorMisses.setSize(m_num_of_sequencers);
486  m_perProcStartTransaction.setSize(m_num_of_sequencers);
487  m_perProcEndTransaction.setSize(m_num_of_sequencers);
488
489  for(int i=0; i < m_num_of_sequencers; i++) {
490    m_perProcTotalMisses[i] = 0;
491    m_perProcUserMisses[i] = 0;
492    m_perProcSupervisorMisses[i] = 0;
493    m_perProcStartTransaction[i] = 0;
494    m_perProcEndTransaction[i] = 0;
495  }
496
497  m_busyControllerCount.setSize(MachineType_NUM); // all machines
498  for(int i=0; i < MachineType_NUM; i++) {
499    m_busyControllerCount[i].setSize(MachineType_base_count((MachineType)i));
500    for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
501      m_busyControllerCount[i][j] = 0;
502    }
503  }
504  m_busyBankCount = 0;
505
506  m_delayedCyclesHistogram.clear();
507  m_delayedCyclesNonPFHistogram.clear();
508  m_delayedCyclesVCHistograms.setSize(RubySystem::getNetwork()->getNumberOfVirtualNetworks());
509  for (int i = 0; i < RubySystem::getNetwork()->getNumberOfVirtualNetworks(); i++) {
510    m_delayedCyclesVCHistograms[i].clear();
511  }
512
513  m_missLatencyHistograms.setSize(RubyRequestType_NUM);
514  for(int i=0; i<m_missLatencyHistograms.size(); i++) {
515    m_missLatencyHistograms[i].clear(200);
516  }
517  m_machLatencyHistograms.setSize(GenericMachineType_NUM+1);
518  for(int i=0; i<m_machLatencyHistograms.size(); i++) {
519    m_machLatencyHistograms[i].clear(200);
520  }
521  m_allMissLatencyHistogram.clear(200);
522
523  m_SWPrefetchLatencyHistograms.setSize(CacheRequestType_NUM);
524  for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
525    m_SWPrefetchLatencyHistograms[i].clear(200);
526  }
527  m_SWPrefetchMachLatencyHistograms.setSize(GenericMachineType_NUM+1);
528  for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
529    m_SWPrefetchMachLatencyHistograms[i].clear(200);
530  }
531  m_allSWPrefetchLatencyHistogram.clear(200);
532
533  m_sequencer_requests.clear();
534  m_read_sharing_histogram.clear();
535  m_write_sharing_histogram.clear();
536  m_all_sharing_histogram.clear();
537  m_cache_to_cache = 0;
538  m_memory_to_cache = 0;
539
540  // clear HashMaps
541  m_requestProfileMap_ptr->clear();
542
543  // count requests profiled
544  m_requests = 0;
545
546  m_outstanding_requests.clear();
547  m_outstanding_persistent_requests.clear();
548
549  // Flush the prefetches through the system - used so that there are no outstanding requests after stats are cleared
550  //g_eventQueue_ptr->triggerAllEvents();
551
552  // update the start time
553  m_ruby_start = g_eventQueue_ptr->getTime();
554}
555
556void Profiler::addAddressTraceSample(const CacheMsg& msg, NodeID id)
557{
558  if (msg.getType() != CacheRequestType_IFETCH) {
559
560    // Note: The following line should be commented out if you want to
561    // use the special profiling that is part of the GS320 protocol
562
563    // NOTE: Unless PROFILE_HOT_LINES is enabled, nothing will be profiled by the AddressProfiler
564    m_address_profiler_ptr->addTraceSample(msg.getLineAddress(), msg.getProgramCounter(), msg.getType(), msg.getAccessMode(), id, false);
565  }
566}
567
568void Profiler::profileSharing(const Address& addr, AccessType type, NodeID requestor, const Set& sharers, const Set& owner)
569{
570  Set set_contacted(owner);
571  if (type == AccessType_Write) {
572    set_contacted.addSet(sharers);
573  }
574  set_contacted.remove(requestor);
575  int number_contacted = set_contacted.count();
576
577  if (type == AccessType_Write) {
578    m_write_sharing_histogram.add(number_contacted);
579  } else {
580    m_read_sharing_histogram.add(number_contacted);
581  }
582  m_all_sharing_histogram.add(number_contacted);
583
584  if (number_contacted == 0) {
585    m_memory_to_cache++;
586  } else {
587    m_cache_to_cache++;
588  }
589
590}
591
592void Profiler::profileMsgDelay(int virtualNetwork, int delayCycles) {
593  assert(virtualNetwork < m_delayedCyclesVCHistograms.size());
594  m_delayedCyclesHistogram.add(delayCycles);
595  m_delayedCyclesVCHistograms[virtualNetwork].add(delayCycles);
596  if (virtualNetwork != 0) {
597    m_delayedCyclesNonPFHistogram.add(delayCycles);
598  }
599}
600
601// profiles original cache requests including PUTs
602void Profiler::profileRequest(const string& requestStr)
603{
604  m_requests++;
605
606  if (m_requestProfileMap_ptr->exist(requestStr)) {
607    (m_requestProfileMap_ptr->lookup(requestStr))++;
608  } else {
609    m_requestProfileMap_ptr->add(requestStr, 1);
610  }
611}
612
613void Profiler::startTransaction(int cpu)
614{
615  m_perProcStartTransaction[cpu]++;
616}
617
618void Profiler::endTransaction(int cpu)
619{
620  m_perProcEndTransaction[cpu]++;
621}
622
623void Profiler::controllerBusy(MachineID machID)
624{
625  m_busyControllerCount[(int)machID.type][(int)machID.num]++;
626}
627
628void Profiler::profilePFWait(Time waitTime)
629{
630  m_prefetchWaitHistogram.add(waitTime);
631}
632
633void Profiler::bankBusy()
634{
635  m_busyBankCount++;
636}
637
638// non-zero cycle demand request
639void Profiler::missLatency(Time t, RubyRequestType type)
640{
641  m_allMissLatencyHistogram.add(t);
642  m_missLatencyHistograms[type].add(t);
643}
644
645// non-zero cycle prefetch request
646void Profiler::swPrefetchLatency(Time t, CacheRequestType type, GenericMachineType respondingMach)
647{
648  m_allSWPrefetchLatencyHistogram.add(t);
649  m_SWPrefetchLatencyHistograms[type].add(t);
650  m_SWPrefetchMachLatencyHistograms[respondingMach].add(t);
651  if(respondingMach == GenericMachineType_Directory || respondingMach == GenericMachineType_NUM) {
652    m_SWPrefetchL2MissLatencyHistogram.add(t);
653  }
654}
655
656void Profiler::profileTransition(const string& component, NodeID version, Address addr,
657                                 const string& state, const string& event,
658                                 const string& next_state, const string& note)
659{
660  const int EVENT_SPACES = 20;
661  const int ID_SPACES = 3;
662  const int TIME_SPACES = 7;
663  const int COMP_SPACES = 10;
664  const int STATE_SPACES = 6;
665
666  if ((g_debug_ptr->getDebugTime() > 0) &&
667      (g_eventQueue_ptr->getTime() >= g_debug_ptr->getDebugTime())) {
668    (* debug_cout_ptr).flags(ios::right);
669    (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
670    (* debug_cout_ptr) << setw(ID_SPACES) << version << " ";
671    (* debug_cout_ptr) << setw(COMP_SPACES) << component;
672    (* debug_cout_ptr) << setw(EVENT_SPACES) << event << " ";
673
674    (* debug_cout_ptr).flags(ios::right);
675    (* debug_cout_ptr) << setw(STATE_SPACES) << state;
676    (* debug_cout_ptr) << ">";
677    (* debug_cout_ptr).flags(ios::left);
678    (* debug_cout_ptr) << setw(STATE_SPACES) << next_state;
679
680    (* debug_cout_ptr) << " " << addr << " " << note;
681
682    (* debug_cout_ptr) << endl;
683  }
684}
685
686// Helper function
687static double process_memory_total()
688{
689  const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
690  ifstream proc_file;
691  proc_file.open("/proc/self/statm");
692  int total_size_in_pages = 0;
693  int res_size_in_pages = 0;
694  proc_file >> total_size_in_pages;
695  proc_file >> res_size_in_pages;
696  return double(total_size_in_pages)*MULTIPLIER; // size in megabytes
697}
698
699static double process_memory_resident()
700{
701  const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
702  ifstream proc_file;
703  proc_file.open("/proc/self/statm");
704  int total_size_in_pages = 0;
705  int res_size_in_pages = 0;
706  proc_file >> total_size_in_pages;
707  proc_file >> res_size_in_pages;
708  return double(res_size_in_pages)*MULTIPLIER; // size in megabytes
709}
710
711void Profiler::rubyWatch(int id){
712    //int rn_g1 = 0;//SIMICS_get_register_number(id, "g1");
713  uint64 tr = 0;//SIMICS_read_register(id, rn_g1);
714    Address watch_address = Address(tr);
715    const int ID_SPACES = 3;
716    const int TIME_SPACES = 7;
717
718    (* debug_cout_ptr).flags(ios::right);
719    (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
720    (* debug_cout_ptr) << setw(ID_SPACES) << id << " "
721                       << "RUBY WATCH "
722                       << watch_address
723                       << endl;
724
725    if(!m_watch_address_list_ptr->exist(watch_address)){
726      m_watch_address_list_ptr->add(watch_address, 1);
727    }
728}
729
730bool Profiler::watchAddress(Address addr){
731    if (m_watch_address_list_ptr->exist(addr))
732      return true;
733    else
734      return false;
735}
736
737int64 Profiler::getTotalTransactionsExecuted() const {
738  return m_perProcEndTransaction.sum();
739}
740
741
742Profiler *
743RubyProfilerParams::create()
744{
745    return new Profiler(this);
746}
747