Profiler.cc revision 7010
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
296145Snate@binkert.org/*
306145Snate@binkert.org   This file has been modified by Kevin Moore and Dan Nussbaum of the
316145Snate@binkert.org   Scalable Systems Research Group at Sun Microsystems Laboratories
326145Snate@binkert.org   (http://research.sun.com/scalable/) to support the Adaptive
336145Snate@binkert.org   Transactional Memory Test Platform (ATMTP).
346145Snate@binkert.org
356145Snate@binkert.org   Please send email to atmtp-interest@sun.com with feedback, questions, or
366145Snate@binkert.org   to request future announcements about ATMTP.
376145Snate@binkert.org
386145Snate@binkert.org   ----------------------------------------------------------------------
396145Snate@binkert.org
406145Snate@binkert.org   File modification date: 2008-02-23
416145Snate@binkert.org
426145Snate@binkert.org   ----------------------------------------------------------------------
436145Snate@binkert.org*/
446145Snate@binkert.org
456145Snate@binkert.org/*
466284Snate@binkert.org * Profiler.cc
476145Snate@binkert.org *
486284Snate@binkert.org * Description: See Profiler.hh
496145Snate@binkert.org *
506145Snate@binkert.org * $Id$
516145Snate@binkert.org *
526145Snate@binkert.org */
536145Snate@binkert.org
547002Snate@binkert.org// Allows use of times() library call, which determines virtual runtime
557002Snate@binkert.org#include <sys/resource.h>
567002Snate@binkert.org#include <sys/times.h>
577002Snate@binkert.org
586154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
596154Snate@binkert.org#include "mem/ruby/profiler/AddressProfiler.hh"
606154Snate@binkert.org#include "mem/ruby/system/System.hh"
616154Snate@binkert.org#include "mem/ruby/network/Network.hh"
626154Snate@binkert.org#include "mem/gems_common/PrioHeap.hh"
636154Snate@binkert.org#include "mem/protocol/CacheMsg.hh"
646154Snate@binkert.org#include "mem/protocol/Protocol.hh"
656154Snate@binkert.org#include "mem/gems_common/util.hh"
666154Snate@binkert.org#include "mem/gems_common/Map.hh"
676154Snate@binkert.org#include "mem/ruby/common/Debug.hh"
686154Snate@binkert.org#include "mem/protocol/MachineType.hh"
696145Snate@binkert.org
706876Ssteve.reinhardt@amd.com#include "mem/ruby/system/System.hh"
716876Ssteve.reinhardt@amd.com
726145Snate@binkert.orgextern std::ostream * debug_cout_ptr;
736145Snate@binkert.org
746145Snate@binkert.orgstatic double process_memory_total();
756145Snate@binkert.orgstatic double process_memory_resident();
766145Snate@binkert.org
776876Ssteve.reinhardt@amd.comProfiler::Profiler(const Params *p)
786876Ssteve.reinhardt@amd.com    : SimObject(p)
796145Snate@binkert.org{
806145Snate@binkert.org  m_requestProfileMap_ptr = new Map<string, int>;
816145Snate@binkert.org
826285Snate@binkert.org  m_inst_profiler_ptr = NULL;
836285Snate@binkert.org  m_address_profiler_ptr = NULL;
846285Snate@binkert.org
856145Snate@binkert.org  m_real_time_start_time = time(NULL); // Not reset in clearStats()
866145Snate@binkert.org  m_stats_period = 1000000; // Default
876145Snate@binkert.org  m_periodic_output_file_ptr = &cerr;
886145Snate@binkert.org
896876Ssteve.reinhardt@amd.com  m_hot_lines = p->hot_lines;
906876Ssteve.reinhardt@amd.com  m_all_instructions = p->all_instructions;
916876Ssteve.reinhardt@amd.com
926896SBrad.Beckmann@amd.com  m_num_of_sequencers = p->num_of_sequencers;
936896SBrad.Beckmann@amd.com
946285Snate@binkert.org  m_hot_lines = false;
956285Snate@binkert.org  m_all_instructions = false;
966285Snate@binkert.org
976896SBrad.Beckmann@amd.com  m_address_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
986285Snate@binkert.org  m_address_profiler_ptr -> setHotLines(m_hot_lines);
996285Snate@binkert.org  m_address_profiler_ptr -> setAllInstructions(m_all_instructions);
1006285Snate@binkert.org
1016285Snate@binkert.org  if (m_all_instructions) {
1026896SBrad.Beckmann@amd.com    m_inst_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
1036285Snate@binkert.org    m_inst_profiler_ptr -> setHotLines(m_hot_lines);
1046285Snate@binkert.org    m_inst_profiler_ptr -> setAllInstructions(m_all_instructions);
1056285Snate@binkert.org  }
1066285Snate@binkert.org}
1076285Snate@binkert.org
1086889SBrad.Beckmann@amd.comProfiler::~Profiler()
1096889SBrad.Beckmann@amd.com{
1106889SBrad.Beckmann@amd.com  if (m_periodic_output_file_ptr != &cerr) {
1116889SBrad.Beckmann@amd.com    delete m_periodic_output_file_ptr;
1126889SBrad.Beckmann@amd.com  }
1136889SBrad.Beckmann@amd.com
1146889SBrad.Beckmann@amd.com  delete m_requestProfileMap_ptr;
1156889SBrad.Beckmann@amd.com}
1166889SBrad.Beckmann@amd.com
1176145Snate@binkert.orgvoid Profiler::wakeup()
1186145Snate@binkert.org{
1196145Snate@binkert.org  // FIXME - avoid the repeated code
1206145Snate@binkert.org
1216145Snate@binkert.org  Vector<integer_t> perProcCycleCount;
1226896SBrad.Beckmann@amd.com  perProcCycleCount.setSize(m_num_of_sequencers);
1236145Snate@binkert.org
1246896SBrad.Beckmann@amd.com  for(int i=0; i < m_num_of_sequencers; i++) {
1256285Snate@binkert.org    perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
1266145Snate@binkert.org    // The +1 allows us to avoid division by zero
1276145Snate@binkert.org  }
1286145Snate@binkert.org
1296889SBrad.Beckmann@amd.com  (*m_periodic_output_file_ptr) << "ruby_cycles: "
1306889SBrad.Beckmann@amd.com                                << g_eventQueue_ptr->getTime()-m_ruby_start
1316889SBrad.Beckmann@amd.com                                << endl;
1326889SBrad.Beckmann@amd.com
1336889SBrad.Beckmann@amd.com  (*m_periodic_output_file_ptr) << "mbytes_resident: "
1346889SBrad.Beckmann@amd.com                                << process_memory_resident()
1356889SBrad.Beckmann@amd.com                                << endl;
1366889SBrad.Beckmann@amd.com
1376889SBrad.Beckmann@amd.com  (*m_periodic_output_file_ptr) << "mbytes_total: "
1386889SBrad.Beckmann@amd.com                                << process_memory_total()
1396889SBrad.Beckmann@amd.com                                << endl;
1406889SBrad.Beckmann@amd.com
1416145Snate@binkert.org  if (process_memory_total() > 0) {
1426889SBrad.Beckmann@amd.com    (*m_periodic_output_file_ptr) << "resident_ratio: "
1436889SBrad.Beckmann@amd.com                          << process_memory_resident()/process_memory_total()
1446889SBrad.Beckmann@amd.com                          << endl;
1456145Snate@binkert.org  }
1466889SBrad.Beckmann@amd.com
1476889SBrad.Beckmann@amd.com  (*m_periodic_output_file_ptr) << "miss_latency: "
1486889SBrad.Beckmann@amd.com                                << m_allMissLatencyHistogram
1496889SBrad.Beckmann@amd.com                                << endl;
1506145Snate@binkert.org
1516145Snate@binkert.org  *m_periodic_output_file_ptr << endl;
1526145Snate@binkert.org
1536285Snate@binkert.org  if (m_all_instructions) {
1546145Snate@binkert.org    m_inst_profiler_ptr->printStats(*m_periodic_output_file_ptr);
1556145Snate@binkert.org  }
1566145Snate@binkert.org
1576145Snate@binkert.org  //g_system_ptr->getNetwork()->printStats(*m_periodic_output_file_ptr);
1586145Snate@binkert.org  g_eventQueue_ptr->scheduleEvent(this, m_stats_period);
1596145Snate@binkert.org}
1606145Snate@binkert.org
1616145Snate@binkert.orgvoid Profiler::setPeriodicStatsFile(const string& filename)
1626145Snate@binkert.org{
1636145Snate@binkert.org  cout << "Recording periodic statistics to file '" << filename << "' every "
1646145Snate@binkert.org       << m_stats_period << " Ruby cycles" << endl;
1656145Snate@binkert.org
1666145Snate@binkert.org  if (m_periodic_output_file_ptr != &cerr) {
1676145Snate@binkert.org    delete m_periodic_output_file_ptr;
1686145Snate@binkert.org  }
1696145Snate@binkert.org
1706145Snate@binkert.org  m_periodic_output_file_ptr = new ofstream(filename.c_str());
1716145Snate@binkert.org  g_eventQueue_ptr->scheduleEvent(this, 1);
1726145Snate@binkert.org}
1736145Snate@binkert.org
1746145Snate@binkert.orgvoid Profiler::setPeriodicStatsInterval(integer_t period)
1756145Snate@binkert.org{
1766889SBrad.Beckmann@amd.com  cout << "Recording periodic statistics every " << m_stats_period
1776889SBrad.Beckmann@amd.com       << " Ruby cycles" << endl;
1786889SBrad.Beckmann@amd.com
1796145Snate@binkert.org  m_stats_period = period;
1806145Snate@binkert.org  g_eventQueue_ptr->scheduleEvent(this, 1);
1816145Snate@binkert.org}
1826145Snate@binkert.org
1836145Snate@binkert.orgvoid Profiler::printConfig(ostream& out) const
1846145Snate@binkert.org{
1856145Snate@binkert.org  out << endl;
1866145Snate@binkert.org  out << "Profiler Configuration" << endl;
1876145Snate@binkert.org  out << "----------------------" << endl;
1886145Snate@binkert.org  out << "periodic_stats_period: " << m_stats_period << endl;
1896145Snate@binkert.org}
1906145Snate@binkert.org
1916145Snate@binkert.orgvoid Profiler::print(ostream& out) const
1926145Snate@binkert.org{
1936145Snate@binkert.org  out << "[Profiler]";
1946145Snate@binkert.org}
1956145Snate@binkert.org
1966145Snate@binkert.orgvoid Profiler::printStats(ostream& out, bool short_stats)
1976145Snate@binkert.org{
1986145Snate@binkert.org  out << endl;
1996145Snate@binkert.org  if (short_stats) {
2006145Snate@binkert.org    out << "SHORT ";
2016145Snate@binkert.org  }
2026145Snate@binkert.org  out << "Profiler Stats" << endl;
2036145Snate@binkert.org  out << "--------------" << endl;
2046145Snate@binkert.org
2056145Snate@binkert.org  time_t real_time_current = time(NULL);
2066145Snate@binkert.org  double seconds = difftime(real_time_current, m_real_time_start_time);
2076145Snate@binkert.org  double minutes = seconds/60.0;
2086145Snate@binkert.org  double hours = minutes/60.0;
2096145Snate@binkert.org  double days = hours/24.0;
2106145Snate@binkert.org  Time ruby_cycles = g_eventQueue_ptr->getTime()-m_ruby_start;
2116145Snate@binkert.org
2126145Snate@binkert.org  if (!short_stats) {
2136145Snate@binkert.org    out << "Elapsed_time_in_seconds: " << seconds << endl;
2146145Snate@binkert.org    out << "Elapsed_time_in_minutes: " << minutes << endl;
2156145Snate@binkert.org    out << "Elapsed_time_in_hours: " << hours << endl;
2166145Snate@binkert.org    out << "Elapsed_time_in_days: " << days << endl;
2176145Snate@binkert.org    out << endl;
2186145Snate@binkert.org  }
2196145Snate@binkert.org
2206145Snate@binkert.org  // print the virtual runtimes as well
2216145Snate@binkert.org  struct tms vtime;
2226145Snate@binkert.org  times(&vtime);
2236145Snate@binkert.org  seconds = (vtime.tms_utime + vtime.tms_stime) / 100.0;
2246145Snate@binkert.org  minutes = seconds / 60.0;
2256145Snate@binkert.org  hours = minutes / 60.0;
2266145Snate@binkert.org  days = hours / 24.0;
2276145Snate@binkert.org  out << "Virtual_time_in_seconds: " << seconds << endl;
2286145Snate@binkert.org  out << "Virtual_time_in_minutes: " << minutes << endl;
2296145Snate@binkert.org  out << "Virtual_time_in_hours:   " << hours << endl;
2306433Sdrh5@cs.wisc.edu  out << "Virtual_time_in_days:    " << days << endl;
2316145Snate@binkert.org  out << endl;
2326145Snate@binkert.org
2336145Snate@binkert.org  out << "Ruby_current_time: " << g_eventQueue_ptr->getTime() << endl;
2346145Snate@binkert.org  out << "Ruby_start_time: " << m_ruby_start << endl;
2356145Snate@binkert.org  out << "Ruby_cycles: " << ruby_cycles << endl;
2366145Snate@binkert.org  out << endl;
2376145Snate@binkert.org
2386145Snate@binkert.org  if (!short_stats) {
2396145Snate@binkert.org    out << "mbytes_resident: " << process_memory_resident() << endl;
2406145Snate@binkert.org    out << "mbytes_total: " << process_memory_total() << endl;
2416145Snate@binkert.org    if (process_memory_total() > 0) {
2426889SBrad.Beckmann@amd.com      out << "resident_ratio: "
2436889SBrad.Beckmann@amd.com          << process_memory_resident()/process_memory_total() << endl;
2446145Snate@binkert.org    }
2456145Snate@binkert.org    out << endl;
2466145Snate@binkert.org
2476145Snate@binkert.org  }
2486145Snate@binkert.org
2496145Snate@binkert.org  Vector<integer_t> perProcCycleCount;
2506896SBrad.Beckmann@amd.com  perProcCycleCount.setSize(m_num_of_sequencers);
2516145Snate@binkert.org
2526896SBrad.Beckmann@amd.com  for(int i=0; i < m_num_of_sequencers; i++) {
2536285Snate@binkert.org    perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
2546145Snate@binkert.org    // The +1 allows us to avoid division by zero
2556145Snate@binkert.org  }
2566145Snate@binkert.org
2577010SBrad.Beckmann@amd.com  out << "ruby_cycles_executed: " << perProcCycleCount << endl;
2586145Snate@binkert.org
2596145Snate@binkert.org  out << endl;
2606145Snate@binkert.org
2616145Snate@binkert.org  if (!short_stats) {
2626145Snate@binkert.org    out << "Busy Controller Counts:" << endl;
2636145Snate@binkert.org    for(int i=0; i < MachineType_NUM; i++) {
2646145Snate@binkert.org      for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
2656145Snate@binkert.org        MachineID machID;
2666145Snate@binkert.org        machID.type = (MachineType)i;
2676145Snate@binkert.org        machID.num = j;
2686145Snate@binkert.org        out << machID << ":" << m_busyControllerCount[i][j] << "  ";
2696145Snate@binkert.org        if ((j+1)%8 == 0) {
2706145Snate@binkert.org          out << endl;
2716145Snate@binkert.org        }
2726145Snate@binkert.org      }
2736145Snate@binkert.org      out << endl;
2746145Snate@binkert.org    }
2756145Snate@binkert.org    out << endl;
2766145Snate@binkert.org
2776145Snate@binkert.org    out << "Busy Bank Count:" << m_busyBankCount << endl;
2786145Snate@binkert.org    out << endl;
2796145Snate@binkert.org
2806145Snate@binkert.org    out << "sequencer_requests_outstanding: " << m_sequencer_requests << endl;
2816145Snate@binkert.org    out << endl;
2826145Snate@binkert.org  }
2836145Snate@binkert.org
2846145Snate@binkert.org  if (!short_stats) {
2856145Snate@binkert.org    out << "All Non-Zero Cycle Demand Cache Accesses" << endl;
2866145Snate@binkert.org    out << "----------------------------------------" << endl;
2876145Snate@binkert.org    out << "miss_latency: " << m_allMissLatencyHistogram << endl;
2886145Snate@binkert.org    for(int i=0; i<m_missLatencyHistograms.size(); i++) {
2896145Snate@binkert.org      if (m_missLatencyHistograms[i].size() > 0) {
2906285Snate@binkert.org        out << "miss_latency_" << RubyRequestType(i) << ": " << m_missLatencyHistograms[i] << endl;
2916145Snate@binkert.org      }
2926145Snate@binkert.org    }
2936145Snate@binkert.org    for(int i=0; i<m_machLatencyHistograms.size(); i++) {
2946145Snate@binkert.org      if (m_machLatencyHistograms[i].size() > 0) {
2956145Snate@binkert.org        out << "miss_latency_" << GenericMachineType(i) << ": " << m_machLatencyHistograms[i] << endl;
2966145Snate@binkert.org      }
2976145Snate@binkert.org    }
2986145Snate@binkert.org
2996145Snate@binkert.org    out << endl;
3006145Snate@binkert.org
3016145Snate@binkert.org    out << "All Non-Zero Cycle SW Prefetch Requests" << endl;
3026145Snate@binkert.org    out << "------------------------------------" << endl;
3036145Snate@binkert.org    out << "prefetch_latency: " << m_allSWPrefetchLatencyHistogram << endl;
3046145Snate@binkert.org    for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
3056145Snate@binkert.org      if (m_SWPrefetchLatencyHistograms[i].size() > 0) {
3066145Snate@binkert.org        out << "prefetch_latency_" << CacheRequestType(i) << ": " << m_SWPrefetchLatencyHistograms[i] << endl;
3076145Snate@binkert.org      }
3086145Snate@binkert.org    }
3096145Snate@binkert.org    for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
3106145Snate@binkert.org      if (m_SWPrefetchMachLatencyHistograms[i].size() > 0) {
3116145Snate@binkert.org        out << "prefetch_latency_" << GenericMachineType(i) << ": " << m_SWPrefetchMachLatencyHistograms[i] << endl;
3126145Snate@binkert.org      }
3136145Snate@binkert.org    }
3146145Snate@binkert.org    out << "prefetch_latency_L2Miss:" << m_SWPrefetchL2MissLatencyHistogram << endl;
3156145Snate@binkert.org
3166145Snate@binkert.org    if (m_all_sharing_histogram.size() > 0) {
3176145Snate@binkert.org      out << "all_sharing: " << m_all_sharing_histogram << endl;
3186145Snate@binkert.org      out << "read_sharing: " << m_read_sharing_histogram << endl;
3196145Snate@binkert.org      out << "write_sharing: " << m_write_sharing_histogram << endl;
3206145Snate@binkert.org
3216145Snate@binkert.org      out << "all_sharing_percent: "; m_all_sharing_histogram.printPercent(out); out << endl;
3226145Snate@binkert.org      out << "read_sharing_percent: "; m_read_sharing_histogram.printPercent(out); out << endl;
3236145Snate@binkert.org      out << "write_sharing_percent: "; m_write_sharing_histogram.printPercent(out); out << endl;
3246145Snate@binkert.org
3256145Snate@binkert.org      int64 total_miss = m_cache_to_cache +  m_memory_to_cache;
3266145Snate@binkert.org      out << "all_misses: " << total_miss << endl;
3276145Snate@binkert.org      out << "cache_to_cache_misses: " << m_cache_to_cache << endl;
3286145Snate@binkert.org      out << "memory_to_cache_misses: " << m_memory_to_cache << endl;
3296145Snate@binkert.org      out << "cache_to_cache_percent: " << 100.0 * (double(m_cache_to_cache) / double(total_miss)) << endl;
3306145Snate@binkert.org      out << "memory_to_cache_percent: " << 100.0 * (double(m_memory_to_cache) / double(total_miss)) << endl;
3316145Snate@binkert.org      out << endl;
3326145Snate@binkert.org    }
3336145Snate@binkert.org
3346145Snate@binkert.org    if (m_outstanding_requests.size() > 0) {
3356145Snate@binkert.org      out << "outstanding_requests: "; m_outstanding_requests.printPercent(out); out << endl;
3366145Snate@binkert.org      out << endl;
3376145Snate@binkert.org    }
3386145Snate@binkert.org  }
3396145Snate@binkert.org
3406145Snate@binkert.org  if (!short_stats) {
3416148Ssanchezd@stanford.edu    out << "Request vs. RubySystem State Profile" << endl;
3426145Snate@binkert.org    out << "--------------------------------" << endl;
3436145Snate@binkert.org    out << endl;
3446145Snate@binkert.org
3456145Snate@binkert.org    Vector<string> requestProfileKeys = m_requestProfileMap_ptr->keys();
3466145Snate@binkert.org    requestProfileKeys.sortVector();
3476145Snate@binkert.org
3486145Snate@binkert.org    for(int i=0; i<requestProfileKeys.size(); i++) {
3496145Snate@binkert.org      int temp_int = m_requestProfileMap_ptr->lookup(requestProfileKeys[i]);
3506145Snate@binkert.org      double percent = (100.0*double(temp_int))/double(m_requests);
3516145Snate@binkert.org      while (requestProfileKeys[i] != "") {
3526145Snate@binkert.org        out << setw(10) << string_split(requestProfileKeys[i], ':');
3536145Snate@binkert.org      }
3546145Snate@binkert.org      out << setw(11) << temp_int;
3556145Snate@binkert.org      out << setw(14) << percent << endl;
3566145Snate@binkert.org    }
3576145Snate@binkert.org    out << endl;
3586145Snate@binkert.org
3596145Snate@binkert.org    out << "filter_action: " << m_filter_action_histogram << endl;
3606145Snate@binkert.org
3616285Snate@binkert.org    if (!m_all_instructions) {
3626145Snate@binkert.org      m_address_profiler_ptr->printStats(out);
3636145Snate@binkert.org    }
3646145Snate@binkert.org
3656285Snate@binkert.org    if (m_all_instructions) {
3666145Snate@binkert.org      m_inst_profiler_ptr->printStats(out);
3676145Snate@binkert.org    }
3686145Snate@binkert.org
3696145Snate@binkert.org    out << endl;
3706145Snate@binkert.org    out << "Message Delayed Cycles" << endl;
3716145Snate@binkert.org    out << "----------------------" << endl;
3726145Snate@binkert.org    out << "Total_delay_cycles: " <<   m_delayedCyclesHistogram << endl;
3736145Snate@binkert.org    out << "Total_nonPF_delay_cycles: " << m_delayedCyclesNonPFHistogram << endl;
3746145Snate@binkert.org    for (int i = 0; i < m_delayedCyclesVCHistograms.size(); i++) {
3756145Snate@binkert.org      out << "  virtual_network_" << i << "_delay_cycles: " << m_delayedCyclesVCHistograms[i] << endl;
3766145Snate@binkert.org    }
3776145Snate@binkert.org
3786145Snate@binkert.org    printResourceUsage(out);
3796145Snate@binkert.org  }
3806145Snate@binkert.org
3816145Snate@binkert.org}
3826145Snate@binkert.org
3836145Snate@binkert.orgvoid Profiler::printResourceUsage(ostream& out) const
3846145Snate@binkert.org{
3856145Snate@binkert.org  out << endl;
3866145Snate@binkert.org  out << "Resource Usage" << endl;
3876145Snate@binkert.org  out << "--------------" << endl;
3886145Snate@binkert.org
3896145Snate@binkert.org  integer_t pagesize = getpagesize(); // page size in bytes
3906145Snate@binkert.org  out << "page_size: " << pagesize << endl;
3916145Snate@binkert.org
3926145Snate@binkert.org  rusage usage;
3936145Snate@binkert.org  getrusage (RUSAGE_SELF, &usage);
3946145Snate@binkert.org
3956145Snate@binkert.org  out << "user_time: " << usage.ru_utime.tv_sec << endl;
3966145Snate@binkert.org  out << "system_time: " << usage.ru_stime.tv_sec << endl;
3976145Snate@binkert.org  out << "page_reclaims: " << usage.ru_minflt << endl;
3986145Snate@binkert.org  out << "page_faults: " << usage.ru_majflt << endl;
3996145Snate@binkert.org  out << "swaps: " << usage.ru_nswap << endl;
4006145Snate@binkert.org  out << "block_inputs: " << usage.ru_inblock << endl;
4016145Snate@binkert.org  out << "block_outputs: " << usage.ru_oublock << endl;
4026145Snate@binkert.org}
4036145Snate@binkert.org
4046145Snate@binkert.orgvoid Profiler::clearStats()
4056145Snate@binkert.org{
4066145Snate@binkert.org  m_ruby_start = g_eventQueue_ptr->getTime();
4076145Snate@binkert.org
4086896SBrad.Beckmann@amd.com  m_cycles_executed_at_start.setSize(m_num_of_sequencers);
4096896SBrad.Beckmann@amd.com  for (int i=0; i < m_num_of_sequencers; i++) {
4106145Snate@binkert.org    if (g_system_ptr == NULL) {
4116145Snate@binkert.org      m_cycles_executed_at_start[i] = 0;
4126145Snate@binkert.org    } else {
4136285Snate@binkert.org      m_cycles_executed_at_start[i] = g_system_ptr->getCycleCount(i);
4146145Snate@binkert.org    }
4156145Snate@binkert.org  }
4166145Snate@binkert.org
4176145Snate@binkert.org  m_busyControllerCount.setSize(MachineType_NUM); // all machines
4186145Snate@binkert.org  for(int i=0; i < MachineType_NUM; i++) {
4196145Snate@binkert.org    m_busyControllerCount[i].setSize(MachineType_base_count((MachineType)i));
4206145Snate@binkert.org    for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
4216145Snate@binkert.org      m_busyControllerCount[i][j] = 0;
4226145Snate@binkert.org    }
4236145Snate@binkert.org  }
4246145Snate@binkert.org  m_busyBankCount = 0;
4256145Snate@binkert.org
4266145Snate@binkert.org  m_delayedCyclesHistogram.clear();
4276145Snate@binkert.org  m_delayedCyclesNonPFHistogram.clear();
4286285Snate@binkert.org  m_delayedCyclesVCHistograms.setSize(RubySystem::getNetwork()->getNumberOfVirtualNetworks());
4296285Snate@binkert.org  for (int i = 0; i < RubySystem::getNetwork()->getNumberOfVirtualNetworks(); i++) {
4306145Snate@binkert.org    m_delayedCyclesVCHistograms[i].clear();
4316145Snate@binkert.org  }
4326145Snate@binkert.org
4336433Sdrh5@cs.wisc.edu  m_missLatencyHistograms.setSize(RubyRequestType_NUM);
4346145Snate@binkert.org  for(int i=0; i<m_missLatencyHistograms.size(); i++) {
4356145Snate@binkert.org    m_missLatencyHistograms[i].clear(200);
4366145Snate@binkert.org  }
4376145Snate@binkert.org  m_machLatencyHistograms.setSize(GenericMachineType_NUM+1);
4386145Snate@binkert.org  for(int i=0; i<m_machLatencyHistograms.size(); i++) {
4396145Snate@binkert.org    m_machLatencyHistograms[i].clear(200);
4406145Snate@binkert.org  }
4416145Snate@binkert.org  m_allMissLatencyHistogram.clear(200);
4426145Snate@binkert.org
4436145Snate@binkert.org  m_SWPrefetchLatencyHistograms.setSize(CacheRequestType_NUM);
4446145Snate@binkert.org  for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
4456145Snate@binkert.org    m_SWPrefetchLatencyHistograms[i].clear(200);
4466145Snate@binkert.org  }
4476145Snate@binkert.org  m_SWPrefetchMachLatencyHistograms.setSize(GenericMachineType_NUM+1);
4486145Snate@binkert.org  for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
4496145Snate@binkert.org    m_SWPrefetchMachLatencyHistograms[i].clear(200);
4506145Snate@binkert.org  }
4516145Snate@binkert.org  m_allSWPrefetchLatencyHistogram.clear(200);
4526145Snate@binkert.org
4536145Snate@binkert.org  m_sequencer_requests.clear();
4546145Snate@binkert.org  m_read_sharing_histogram.clear();
4556145Snate@binkert.org  m_write_sharing_histogram.clear();
4566145Snate@binkert.org  m_all_sharing_histogram.clear();
4576145Snate@binkert.org  m_cache_to_cache = 0;
4586145Snate@binkert.org  m_memory_to_cache = 0;
4596145Snate@binkert.org
4606145Snate@binkert.org  // clear HashMaps
4616145Snate@binkert.org  m_requestProfileMap_ptr->clear();
4626145Snate@binkert.org
4636145Snate@binkert.org  // count requests profiled
4646145Snate@binkert.org  m_requests = 0;
4656145Snate@binkert.org
4666145Snate@binkert.org  m_outstanding_requests.clear();
4676145Snate@binkert.org  m_outstanding_persistent_requests.clear();
4686145Snate@binkert.org
4696145Snate@binkert.org  // Flush the prefetches through the system - used so that there are no outstanding requests after stats are cleared
4706145Snate@binkert.org  //g_eventQueue_ptr->triggerAllEvents();
4716145Snate@binkert.org
4726145Snate@binkert.org  // update the start time
4736145Snate@binkert.org  m_ruby_start = g_eventQueue_ptr->getTime();
4746145Snate@binkert.org}
4756145Snate@binkert.org
4766145Snate@binkert.orgvoid Profiler::addAddressTraceSample(const CacheMsg& msg, NodeID id)
4776145Snate@binkert.org{
4786145Snate@binkert.org  if (msg.getType() != CacheRequestType_IFETCH) {
4796145Snate@binkert.org
4806145Snate@binkert.org    // Note: The following line should be commented out if you want to
4816145Snate@binkert.org    // use the special profiling that is part of the GS320 protocol
4826145Snate@binkert.org
4836372Sdrh5@cs.wisc.edu    // NOTE: Unless PROFILE_HOT_LINES is enabled, nothing will be profiled by the AddressProfiler
4846285Snate@binkert.org    m_address_profiler_ptr->addTraceSample(msg.getLineAddress(), msg.getProgramCounter(), msg.getType(), msg.getAccessMode(), id, false);
4856145Snate@binkert.org  }
4866145Snate@binkert.org}
4876145Snate@binkert.org
4886145Snate@binkert.orgvoid Profiler::profileSharing(const Address& addr, AccessType type, NodeID requestor, const Set& sharers, const Set& owner)
4896145Snate@binkert.org{
4906145Snate@binkert.org  Set set_contacted(owner);
4916145Snate@binkert.org  if (type == AccessType_Write) {
4926145Snate@binkert.org    set_contacted.addSet(sharers);
4936145Snate@binkert.org  }
4946145Snate@binkert.org  set_contacted.remove(requestor);
4956145Snate@binkert.org  int number_contacted = set_contacted.count();
4966145Snate@binkert.org
4976145Snate@binkert.org  if (type == AccessType_Write) {
4986145Snate@binkert.org    m_write_sharing_histogram.add(number_contacted);
4996145Snate@binkert.org  } else {
5006145Snate@binkert.org    m_read_sharing_histogram.add(number_contacted);
5016145Snate@binkert.org  }
5026145Snate@binkert.org  m_all_sharing_histogram.add(number_contacted);
5036145Snate@binkert.org
5046145Snate@binkert.org  if (number_contacted == 0) {
5056145Snate@binkert.org    m_memory_to_cache++;
5066145Snate@binkert.org  } else {
5076145Snate@binkert.org    m_cache_to_cache++;
5086145Snate@binkert.org  }
5096145Snate@binkert.org
5106145Snate@binkert.org}
5116145Snate@binkert.org
5126145Snate@binkert.orgvoid Profiler::profileMsgDelay(int virtualNetwork, int delayCycles) {
5136145Snate@binkert.org  assert(virtualNetwork < m_delayedCyclesVCHistograms.size());
5146145Snate@binkert.org  m_delayedCyclesHistogram.add(delayCycles);
5156145Snate@binkert.org  m_delayedCyclesVCHistograms[virtualNetwork].add(delayCycles);
5166145Snate@binkert.org  if (virtualNetwork != 0) {
5176145Snate@binkert.org    m_delayedCyclesNonPFHistogram.add(delayCycles);
5186145Snate@binkert.org  }
5196145Snate@binkert.org}
5206145Snate@binkert.org
5216145Snate@binkert.org// profiles original cache requests including PUTs
5226145Snate@binkert.orgvoid Profiler::profileRequest(const string& requestStr)
5236145Snate@binkert.org{
5246145Snate@binkert.org  m_requests++;
5256145Snate@binkert.org
5266145Snate@binkert.org  if (m_requestProfileMap_ptr->exist(requestStr)) {
5276145Snate@binkert.org    (m_requestProfileMap_ptr->lookup(requestStr))++;
5286145Snate@binkert.org  } else {
5296145Snate@binkert.org    m_requestProfileMap_ptr->add(requestStr, 1);
5306145Snate@binkert.org  }
5316145Snate@binkert.org}
5326145Snate@binkert.org
5336145Snate@binkert.orgvoid Profiler::controllerBusy(MachineID machID)
5346145Snate@binkert.org{
5356145Snate@binkert.org  m_busyControllerCount[(int)machID.type][(int)machID.num]++;
5366145Snate@binkert.org}
5376145Snate@binkert.org
5386145Snate@binkert.orgvoid Profiler::profilePFWait(Time waitTime)
5396145Snate@binkert.org{
5406145Snate@binkert.org  m_prefetchWaitHistogram.add(waitTime);
5416145Snate@binkert.org}
5426145Snate@binkert.org
5436145Snate@binkert.orgvoid Profiler::bankBusy()
5446145Snate@binkert.org{
5456145Snate@binkert.org  m_busyBankCount++;
5466145Snate@binkert.org}
5476145Snate@binkert.org
5486145Snate@binkert.org// non-zero cycle demand request
5496285Snate@binkert.orgvoid Profiler::missLatency(Time t, RubyRequestType type)
5506145Snate@binkert.org{
5516145Snate@binkert.org  m_allMissLatencyHistogram.add(t);
5526145Snate@binkert.org  m_missLatencyHistograms[type].add(t);
5536145Snate@binkert.org}
5546145Snate@binkert.org
5556145Snate@binkert.org// non-zero cycle prefetch request
5566145Snate@binkert.orgvoid Profiler::swPrefetchLatency(Time t, CacheRequestType type, GenericMachineType respondingMach)
5576145Snate@binkert.org{
5586145Snate@binkert.org  m_allSWPrefetchLatencyHistogram.add(t);
5596145Snate@binkert.org  m_SWPrefetchLatencyHistograms[type].add(t);
5606145Snate@binkert.org  m_SWPrefetchMachLatencyHistograms[respondingMach].add(t);
5616145Snate@binkert.org  if(respondingMach == GenericMachineType_Directory || respondingMach == GenericMachineType_NUM) {
5626145Snate@binkert.org    m_SWPrefetchL2MissLatencyHistogram.add(t);
5636145Snate@binkert.org  }
5646145Snate@binkert.org}
5656145Snate@binkert.org
5666285Snate@binkert.orgvoid Profiler::profileTransition(const string& component, NodeID version, Address addr,
5676145Snate@binkert.org                                 const string& state, const string& event,
5686145Snate@binkert.org                                 const string& next_state, const string& note)
5696145Snate@binkert.org{
5706145Snate@binkert.org  const int EVENT_SPACES = 20;
5716145Snate@binkert.org  const int ID_SPACES = 3;
5726145Snate@binkert.org  const int TIME_SPACES = 7;
5736145Snate@binkert.org  const int COMP_SPACES = 10;
5746145Snate@binkert.org  const int STATE_SPACES = 6;
5756145Snate@binkert.org
5766145Snate@binkert.org  if ((g_debug_ptr->getDebugTime() > 0) &&
5776145Snate@binkert.org      (g_eventQueue_ptr->getTime() >= g_debug_ptr->getDebugTime())) {
5786145Snate@binkert.org    (* debug_cout_ptr).flags(ios::right);
5796145Snate@binkert.org    (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
5806145Snate@binkert.org    (* debug_cout_ptr) << setw(ID_SPACES) << version << " ";
5816145Snate@binkert.org    (* debug_cout_ptr) << setw(COMP_SPACES) << component;
5826145Snate@binkert.org    (* debug_cout_ptr) << setw(EVENT_SPACES) << event << " ";
5836145Snate@binkert.org
5846285Snate@binkert.org    (* debug_cout_ptr).flags(ios::right);
5856285Snate@binkert.org    (* debug_cout_ptr) << setw(STATE_SPACES) << state;
5866285Snate@binkert.org    (* debug_cout_ptr) << ">";
5876285Snate@binkert.org    (* debug_cout_ptr).flags(ios::left);
5886285Snate@binkert.org    (* debug_cout_ptr) << setw(STATE_SPACES) << next_state;
5896285Snate@binkert.org
5906145Snate@binkert.org    (* debug_cout_ptr) << " " << addr << " " << note;
5916145Snate@binkert.org
5926145Snate@binkert.org    (* debug_cout_ptr) << endl;
5936145Snate@binkert.org  }
5946145Snate@binkert.org}
5956145Snate@binkert.org
5966145Snate@binkert.org// Helper function
5976145Snate@binkert.orgstatic double process_memory_total()
5986145Snate@binkert.org{
5996145Snate@binkert.org  const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
6006145Snate@binkert.org  ifstream proc_file;
6016145Snate@binkert.org  proc_file.open("/proc/self/statm");
6026145Snate@binkert.org  int total_size_in_pages = 0;
6036145Snate@binkert.org  int res_size_in_pages = 0;
6046145Snate@binkert.org  proc_file >> total_size_in_pages;
6056145Snate@binkert.org  proc_file >> res_size_in_pages;
6066145Snate@binkert.org  return double(total_size_in_pages)*MULTIPLIER; // size in megabytes
6076145Snate@binkert.org}
6086145Snate@binkert.org
6096145Snate@binkert.orgstatic double process_memory_resident()
6106145Snate@binkert.org{
6116145Snate@binkert.org  const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
6126145Snate@binkert.org  ifstream proc_file;
6136145Snate@binkert.org  proc_file.open("/proc/self/statm");
6146145Snate@binkert.org  int total_size_in_pages = 0;
6156145Snate@binkert.org  int res_size_in_pages = 0;
6166145Snate@binkert.org  proc_file >> total_size_in_pages;
6176145Snate@binkert.org  proc_file >> res_size_in_pages;
6186145Snate@binkert.org  return double(res_size_in_pages)*MULTIPLIER; // size in megabytes
6196145Snate@binkert.org}
6206145Snate@binkert.org
6216285Snate@binkert.orgvoid Profiler::rubyWatch(int id){
6227010SBrad.Beckmann@amd.com    uint64 tr = 0;
6236285Snate@binkert.org    Address watch_address = Address(tr);
6246285Snate@binkert.org    const int ID_SPACES = 3;
6256285Snate@binkert.org    const int TIME_SPACES = 7;
6266285Snate@binkert.org
6276285Snate@binkert.org    (* debug_cout_ptr).flags(ios::right);
6286285Snate@binkert.org    (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
6296285Snate@binkert.org    (* debug_cout_ptr) << setw(ID_SPACES) << id << " "
6306285Snate@binkert.org                       << "RUBY WATCH "
6316285Snate@binkert.org                       << watch_address
6326285Snate@binkert.org                       << endl;
6336285Snate@binkert.org
6346285Snate@binkert.org    if(!m_watch_address_list_ptr->exist(watch_address)){
6356285Snate@binkert.org      m_watch_address_list_ptr->add(watch_address, 1);
6366285Snate@binkert.org    }
6376285Snate@binkert.org}
6386285Snate@binkert.org
6396285Snate@binkert.orgbool Profiler::watchAddress(Address addr){
6406285Snate@binkert.org    if (m_watch_address_list_ptr->exist(addr))
6416285Snate@binkert.org      return true;
6426285Snate@binkert.org    else
6436285Snate@binkert.org      return false;
6446285Snate@binkert.org}
6456285Snate@binkert.org
6466876Ssteve.reinhardt@amd.comProfiler *
6476876Ssteve.reinhardt@amd.comRubyProfilerParams::create()
6486876Ssteve.reinhardt@amd.com{
6496876Ssteve.reinhardt@amd.com    return new Profiler(this);
6506876Ssteve.reinhardt@amd.com}
651