Profiler.cc revision 7055
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
457002Snate@binkert.org// Allows use of times() library call, which determines virtual runtime
467002Snate@binkert.org#include <sys/resource.h>
477002Snate@binkert.org#include <sys/times.h>
487002Snate@binkert.org
497048Snate@binkert.org#include "mem/gems_common/Map.hh"
507048Snate@binkert.org#include "mem/gems_common/PrioHeap.hh"
517048Snate@binkert.org#include "mem/gems_common/util.hh"
527048Snate@binkert.org#include "mem/protocol/CacheMsg.hh"
537048Snate@binkert.org#include "mem/protocol/MachineType.hh"
547048Snate@binkert.org#include "mem/protocol/Protocol.hh"
557048Snate@binkert.org#include "mem/ruby/common/Debug.hh"
567048Snate@binkert.org#include "mem/ruby/network/Network.hh"
577048Snate@binkert.org#include "mem/ruby/profiler/AddressProfiler.hh"
586154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
596154Snate@binkert.org#include "mem/ruby/system/System.hh"
606876Ssteve.reinhardt@amd.com#include "mem/ruby/system/System.hh"
616876Ssteve.reinhardt@amd.com
627055Snate@binkert.orgusing namespace std;
637055Snate@binkert.org
647055Snate@binkert.orgextern ostream* debug_cout_ptr;
656145Snate@binkert.org
666145Snate@binkert.orgstatic double process_memory_total();
676145Snate@binkert.orgstatic double process_memory_resident();
686145Snate@binkert.org
696876Ssteve.reinhardt@amd.comProfiler::Profiler(const Params *p)
706876Ssteve.reinhardt@amd.com    : SimObject(p)
716145Snate@binkert.org{
727048Snate@binkert.org    m_requestProfileMap_ptr = new Map<string, int>;
736145Snate@binkert.org
747048Snate@binkert.org    m_inst_profiler_ptr = NULL;
757048Snate@binkert.org    m_address_profiler_ptr = NULL;
766285Snate@binkert.org
777048Snate@binkert.org    m_real_time_start_time = time(NULL); // Not reset in clearStats()
787048Snate@binkert.org    m_stats_period = 1000000; // Default
797048Snate@binkert.org    m_periodic_output_file_ptr = &cerr;
806145Snate@binkert.org
817048Snate@binkert.org    m_hot_lines = p->hot_lines;
827048Snate@binkert.org    m_all_instructions = p->all_instructions;
836876Ssteve.reinhardt@amd.com
847048Snate@binkert.org    m_num_of_sequencers = p->num_of_sequencers;
856896SBrad.Beckmann@amd.com
867048Snate@binkert.org    m_hot_lines = false;
877048Snate@binkert.org    m_all_instructions = false;
886285Snate@binkert.org
897048Snate@binkert.org    m_address_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
907048Snate@binkert.org    m_address_profiler_ptr->setHotLines(m_hot_lines);
917048Snate@binkert.org    m_address_profiler_ptr->setAllInstructions(m_all_instructions);
926285Snate@binkert.org
937048Snate@binkert.org    if (m_all_instructions) {
947048Snate@binkert.org        m_inst_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
957048Snate@binkert.org        m_inst_profiler_ptr->setHotLines(m_hot_lines);
967048Snate@binkert.org        m_inst_profiler_ptr->setAllInstructions(m_all_instructions);
977048Snate@binkert.org    }
986285Snate@binkert.org}
996285Snate@binkert.org
1006889SBrad.Beckmann@amd.comProfiler::~Profiler()
1016889SBrad.Beckmann@amd.com{
1027048Snate@binkert.org    if (m_periodic_output_file_ptr != &cerr) {
1037048Snate@binkert.org        delete m_periodic_output_file_ptr;
1047048Snate@binkert.org    }
1056889SBrad.Beckmann@amd.com
1067048Snate@binkert.org    delete m_requestProfileMap_ptr;
1076889SBrad.Beckmann@amd.com}
1086889SBrad.Beckmann@amd.com
1097048Snate@binkert.orgvoid
1107048Snate@binkert.orgProfiler::wakeup()
1116145Snate@binkert.org{
1127048Snate@binkert.org    // FIXME - avoid the repeated code
1136145Snate@binkert.org
1147048Snate@binkert.org    Vector<integer_t> perProcCycleCount;
1157048Snate@binkert.org    perProcCycleCount.setSize(m_num_of_sequencers);
1166145Snate@binkert.org
1177048Snate@binkert.org    for (int i = 0; i < m_num_of_sequencers; i++) {
1187048Snate@binkert.org        perProcCycleCount[i] =
1197048Snate@binkert.org            g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
1207048Snate@binkert.org        // The +1 allows us to avoid division by zero
1217048Snate@binkert.org    }
1226145Snate@binkert.org
1237048Snate@binkert.org    ostream &out = *m_periodic_output_file_ptr;
1246889SBrad.Beckmann@amd.com
1257048Snate@binkert.org    out << "ruby_cycles: " << g_eventQueue_ptr->getTime()-m_ruby_start << endl
1267048Snate@binkert.org        << "mbytes_resident: " << process_memory_resident() << endl
1277048Snate@binkert.org        << "mbytes_total: " << process_memory_total() << endl;
1286889SBrad.Beckmann@amd.com
1297048Snate@binkert.org    if (process_memory_total() > 0) {
1307054Snate@binkert.org        out << "resident_ratio: "
1317048Snate@binkert.org            << process_memory_resident() / process_memory_total() << endl;
1327048Snate@binkert.org    }
1336889SBrad.Beckmann@amd.com
1346145Snate@binkert.org    out << "miss_latency: " << m_allMissLatencyHistogram << endl;
1356145Snate@binkert.org
1366145Snate@binkert.org    out << endl;
1376145Snate@binkert.org
1387048Snate@binkert.org    if (m_all_instructions) {
1397048Snate@binkert.org        m_inst_profiler_ptr->printStats(out);
1406145Snate@binkert.org    }
1416145Snate@binkert.org
1427048Snate@binkert.org    //g_system_ptr->getNetwork()->printStats(out);
1437048Snate@binkert.org    g_eventQueue_ptr->scheduleEvent(this, m_stats_period);
1447048Snate@binkert.org}
1457048Snate@binkert.org
1467048Snate@binkert.orgvoid
1477048Snate@binkert.orgProfiler::setPeriodicStatsFile(const string& filename)
1487048Snate@binkert.org{
1497048Snate@binkert.org    cout << "Recording periodic statistics to file '" << filename << "' every "
1507048Snate@binkert.org         << m_stats_period << " Ruby cycles" << endl;
1517048Snate@binkert.org
1527048Snate@binkert.org    if (m_periodic_output_file_ptr != &cerr) {
1537048Snate@binkert.org        delete m_periodic_output_file_ptr;
1546145Snate@binkert.org    }
1556145Snate@binkert.org
1567048Snate@binkert.org    m_periodic_output_file_ptr = new ofstream(filename.c_str());
1577048Snate@binkert.org    g_eventQueue_ptr->scheduleEvent(this, 1);
1587048Snate@binkert.org}
1597048Snate@binkert.org
1607048Snate@binkert.orgvoid
1617048Snate@binkert.orgProfiler::setPeriodicStatsInterval(integer_t period)
1627048Snate@binkert.org{
1637054Snate@binkert.org    cout << "Recording periodic statistics every " << m_stats_period
1647048Snate@binkert.org         << " Ruby cycles" << endl;
1657048Snate@binkert.org
1667048Snate@binkert.org    m_stats_period = period;
1677048Snate@binkert.org    g_eventQueue_ptr->scheduleEvent(this, 1);
1687048Snate@binkert.org}
1697048Snate@binkert.org
1707048Snate@binkert.orgvoid
1717048Snate@binkert.orgProfiler::printConfig(ostream& out) const
1727048Snate@binkert.org{
1737048Snate@binkert.org    out << endl;
1747048Snate@binkert.org    out << "Profiler Configuration" << endl;
1757048Snate@binkert.org    out << "----------------------" << endl;
1767048Snate@binkert.org    out << "periodic_stats_period: " << m_stats_period << endl;
1777048Snate@binkert.org}
1787048Snate@binkert.org
1797048Snate@binkert.orgvoid
1807048Snate@binkert.orgProfiler::print(ostream& out) const
1817048Snate@binkert.org{
1827048Snate@binkert.org    out << "[Profiler]";
1837048Snate@binkert.org}
1847048Snate@binkert.org
1857048Snate@binkert.orgvoid
1867048Snate@binkert.orgProfiler::printStats(ostream& out, bool short_stats)
1877048Snate@binkert.org{
1887048Snate@binkert.org    out << endl;
1897048Snate@binkert.org    if (short_stats) {
1907048Snate@binkert.org        out << "SHORT ";
1917048Snate@binkert.org    }
1927048Snate@binkert.org    out << "Profiler Stats" << endl;
1937048Snate@binkert.org    out << "--------------" << endl;
1947048Snate@binkert.org
1957048Snate@binkert.org    time_t real_time_current = time(NULL);
1967048Snate@binkert.org    double seconds = difftime(real_time_current, m_real_time_start_time);
1977048Snate@binkert.org    double minutes = seconds / 60.0;
1987048Snate@binkert.org    double hours = minutes / 60.0;
1997048Snate@binkert.org    double days = hours / 24.0;
2007048Snate@binkert.org    Time ruby_cycles = g_eventQueue_ptr->getTime()-m_ruby_start;
2017048Snate@binkert.org
2027048Snate@binkert.org    if (!short_stats) {
2037048Snate@binkert.org        out << "Elapsed_time_in_seconds: " << seconds << endl;
2047048Snate@binkert.org        out << "Elapsed_time_in_minutes: " << minutes << endl;
2057048Snate@binkert.org        out << "Elapsed_time_in_hours: " << hours << endl;
2067048Snate@binkert.org        out << "Elapsed_time_in_days: " << days << endl;
2077048Snate@binkert.org        out << endl;
2087048Snate@binkert.org    }
2097048Snate@binkert.org
2107048Snate@binkert.org    // print the virtual runtimes as well
2117048Snate@binkert.org    struct tms vtime;
2127048Snate@binkert.org    times(&vtime);
2137048Snate@binkert.org    seconds = (vtime.tms_utime + vtime.tms_stime) / 100.0;
2147048Snate@binkert.org    minutes = seconds / 60.0;
2157048Snate@binkert.org    hours = minutes / 60.0;
2167048Snate@binkert.org    days = hours / 24.0;
2177048Snate@binkert.org    out << "Virtual_time_in_seconds: " << seconds << endl;
2187048Snate@binkert.org    out << "Virtual_time_in_minutes: " << minutes << endl;
2197048Snate@binkert.org    out << "Virtual_time_in_hours:   " << hours << endl;
2207048Snate@binkert.org    out << "Virtual_time_in_days:    " << days << endl;
2216145Snate@binkert.org    out << endl;
2226145Snate@binkert.org
2237048Snate@binkert.org    out << "Ruby_current_time: " << g_eventQueue_ptr->getTime() << endl;
2247048Snate@binkert.org    out << "Ruby_start_time: " << m_ruby_start << endl;
2257048Snate@binkert.org    out << "Ruby_cycles: " << ruby_cycles << endl;
2266145Snate@binkert.org    out << endl;
2276145Snate@binkert.org
2287048Snate@binkert.org    if (!short_stats) {
2297048Snate@binkert.org        out << "mbytes_resident: " << process_memory_resident() << endl;
2307048Snate@binkert.org        out << "mbytes_total: " << process_memory_total() << endl;
2317048Snate@binkert.org        if (process_memory_total() > 0) {
2327054Snate@binkert.org            out << "resident_ratio: "
2337048Snate@binkert.org                << process_memory_resident()/process_memory_total() << endl;
2347048Snate@binkert.org        }
2357048Snate@binkert.org        out << endl;
2366145Snate@binkert.org    }
2376145Snate@binkert.org
2387048Snate@binkert.org    Vector<integer_t> perProcCycleCount;
2397048Snate@binkert.org    perProcCycleCount.setSize(m_num_of_sequencers);
2407048Snate@binkert.org
2417048Snate@binkert.org    for (int i = 0; i < m_num_of_sequencers; i++) {
2427048Snate@binkert.org        perProcCycleCount[i] =
2437048Snate@binkert.org            g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
2447048Snate@binkert.org        // The +1 allows us to avoid division by zero
2456145Snate@binkert.org    }
2466145Snate@binkert.org
2477048Snate@binkert.org    out << "ruby_cycles_executed: " << perProcCycleCount << endl;
2487048Snate@binkert.org
2496145Snate@binkert.org    out << endl;
2507048Snate@binkert.org
2517048Snate@binkert.org    if (!short_stats) {
2527048Snate@binkert.org        out << "Busy Controller Counts:" << endl;
2537048Snate@binkert.org        for (int i = 0; i < MachineType_NUM; i++) {
2547048Snate@binkert.org            int size = MachineType_base_count((MachineType)i);
2557048Snate@binkert.org            for (int j = 0; j < size; j++) {
2567048Snate@binkert.org                MachineID machID;
2577048Snate@binkert.org                machID.type = (MachineType)i;
2587048Snate@binkert.org                machID.num = j;
2597048Snate@binkert.org                out << machID << ":" << m_busyControllerCount[i][j] << "  ";
2607048Snate@binkert.org                if ((j + 1) % 8 == 0) {
2617048Snate@binkert.org                    out << endl;
2627048Snate@binkert.org                }
2637048Snate@binkert.org            }
2647048Snate@binkert.org            out << endl;
2657048Snate@binkert.org        }
2667048Snate@binkert.org        out << endl;
2677048Snate@binkert.org
2687048Snate@binkert.org        out << "Busy Bank Count:" << m_busyBankCount << endl;
2697048Snate@binkert.org        out << endl;
2707048Snate@binkert.org
2717048Snate@binkert.org        out << "sequencer_requests_outstanding: "
2727048Snate@binkert.org            << m_sequencer_requests << endl;
2737048Snate@binkert.org        out << endl;
2746145Snate@binkert.org    }
2756145Snate@binkert.org
2767048Snate@binkert.org    if (!short_stats) {
2777048Snate@binkert.org        out << "All Non-Zero Cycle Demand Cache Accesses" << endl;
2787048Snate@binkert.org        out << "----------------------------------------" << endl;
2797048Snate@binkert.org        out << "miss_latency: " << m_allMissLatencyHistogram << endl;
2807048Snate@binkert.org        for (int i = 0; i < m_missLatencyHistograms.size(); i++) {
2817048Snate@binkert.org            if (m_missLatencyHistograms[i].size() > 0) {
2827048Snate@binkert.org                out << "miss_latency_" << RubyRequestType(i) << ": "
2837048Snate@binkert.org                    << m_missLatencyHistograms[i] << endl;
2847048Snate@binkert.org            }
2857048Snate@binkert.org        }
2867048Snate@binkert.org        for (int i = 0; i < m_machLatencyHistograms.size(); i++) {
2877048Snate@binkert.org            if (m_machLatencyHistograms[i].size() > 0) {
2887048Snate@binkert.org                out << "miss_latency_" << GenericMachineType(i) << ": "
2897048Snate@binkert.org                    << m_machLatencyHistograms[i] << endl;
2907048Snate@binkert.org            }
2917048Snate@binkert.org        }
2926145Snate@binkert.org
2937048Snate@binkert.org        out << endl;
2947048Snate@binkert.org
2957048Snate@binkert.org        out << "All Non-Zero Cycle SW Prefetch Requests" << endl;
2967048Snate@binkert.org        out << "------------------------------------" << endl;
2977048Snate@binkert.org        out << "prefetch_latency: " << m_allSWPrefetchLatencyHistogram << endl;
2987048Snate@binkert.org        for (int i = 0; i < m_SWPrefetchLatencyHistograms.size(); i++) {
2997048Snate@binkert.org            if (m_SWPrefetchLatencyHistograms[i].size() > 0) {
3007048Snate@binkert.org                out << "prefetch_latency_" << CacheRequestType(i) << ": "
3017048Snate@binkert.org                    << m_SWPrefetchLatencyHistograms[i] << endl;
3027048Snate@binkert.org            }
3037048Snate@binkert.org        }
3047048Snate@binkert.org        for (int i = 0; i < m_SWPrefetchMachLatencyHistograms.size(); i++) {
3057048Snate@binkert.org            if (m_SWPrefetchMachLatencyHistograms[i].size() > 0) {
3067048Snate@binkert.org                out << "prefetch_latency_" << GenericMachineType(i) << ": "
3077048Snate@binkert.org                    << m_SWPrefetchMachLatencyHistograms[i] << endl;
3087048Snate@binkert.org            }
3097048Snate@binkert.org        }
3107048Snate@binkert.org        out << "prefetch_latency_L2Miss:"
3117048Snate@binkert.org            << m_SWPrefetchL2MissLatencyHistogram << endl;
3127048Snate@binkert.org
3137048Snate@binkert.org        if (m_all_sharing_histogram.size() > 0) {
3147048Snate@binkert.org            out << "all_sharing: " << m_all_sharing_histogram << endl;
3157048Snate@binkert.org            out << "read_sharing: " << m_read_sharing_histogram << endl;
3167048Snate@binkert.org            out << "write_sharing: " << m_write_sharing_histogram << endl;
3177048Snate@binkert.org
3187048Snate@binkert.org            out << "all_sharing_percent: ";
3197048Snate@binkert.org            m_all_sharing_histogram.printPercent(out);
3207048Snate@binkert.org            out << endl;
3217048Snate@binkert.org
3227048Snate@binkert.org            out << "read_sharing_percent: ";
3237048Snate@binkert.org            m_read_sharing_histogram.printPercent(out);
3247048Snate@binkert.org            out << endl;
3257048Snate@binkert.org
3267048Snate@binkert.org            out << "write_sharing_percent: ";
3277048Snate@binkert.org            m_write_sharing_histogram.printPercent(out);
3287048Snate@binkert.org            out << endl;
3297048Snate@binkert.org
3307048Snate@binkert.org            int64 total_miss = m_cache_to_cache +  m_memory_to_cache;
3317048Snate@binkert.org            out << "all_misses: " << total_miss << endl;
3327048Snate@binkert.org            out << "cache_to_cache_misses: " << m_cache_to_cache << endl;
3337048Snate@binkert.org            out << "memory_to_cache_misses: " << m_memory_to_cache << endl;
3347048Snate@binkert.org            out << "cache_to_cache_percent: "
3357048Snate@binkert.org                << 100.0 * (double(m_cache_to_cache) / double(total_miss))
3367048Snate@binkert.org                << endl;
3377048Snate@binkert.org            out << "memory_to_cache_percent: "
3387048Snate@binkert.org                << 100.0 * (double(m_memory_to_cache) / double(total_miss))
3397048Snate@binkert.org                << endl;
3407048Snate@binkert.org            out << endl;
3417048Snate@binkert.org        }
3427048Snate@binkert.org
3437048Snate@binkert.org        if (m_outstanding_requests.size() > 0) {
3447048Snate@binkert.org            out << "outstanding_requests: ";
3457048Snate@binkert.org            m_outstanding_requests.printPercent(out);
3467048Snate@binkert.org            out << endl;
3477048Snate@binkert.org            out << endl;
3487048Snate@binkert.org        }
3497048Snate@binkert.org    }
3507048Snate@binkert.org
3517048Snate@binkert.org    if (!short_stats) {
3527048Snate@binkert.org        out << "Request vs. RubySystem State Profile" << endl;
3537048Snate@binkert.org        out << "--------------------------------" << endl;
3547048Snate@binkert.org        out << endl;
3557048Snate@binkert.org
3567048Snate@binkert.org        Vector<string> requestProfileKeys = m_requestProfileMap_ptr->keys();
3577048Snate@binkert.org        requestProfileKeys.sortVector();
3587048Snate@binkert.org
3597048Snate@binkert.org        for (int i = 0; i < requestProfileKeys.size(); i++) {
3607048Snate@binkert.org            int temp_int =
3617048Snate@binkert.org                m_requestProfileMap_ptr->lookup(requestProfileKeys[i]);
3627048Snate@binkert.org            double percent = (100.0 * double(temp_int)) / double(m_requests);
3637048Snate@binkert.org            while (requestProfileKeys[i] != "") {
3647048Snate@binkert.org                out << setw(10) << string_split(requestProfileKeys[i], ':');
3657048Snate@binkert.org            }
3667048Snate@binkert.org            out << setw(11) << temp_int;
3677048Snate@binkert.org            out << setw(14) << percent << endl;
3687048Snate@binkert.org        }
3697048Snate@binkert.org        out << endl;
3707048Snate@binkert.org
3717048Snate@binkert.org        out << "filter_action: " << m_filter_action_histogram << endl;
3727048Snate@binkert.org
3737048Snate@binkert.org        if (!m_all_instructions) {
3747048Snate@binkert.org            m_address_profiler_ptr->printStats(out);
3757048Snate@binkert.org        }
3767048Snate@binkert.org
3777048Snate@binkert.org        if (m_all_instructions) {
3787048Snate@binkert.org            m_inst_profiler_ptr->printStats(out);
3797048Snate@binkert.org        }
3807048Snate@binkert.org
3817048Snate@binkert.org        out << endl;
3827048Snate@binkert.org        out << "Message Delayed Cycles" << endl;
3837048Snate@binkert.org        out << "----------------------" << endl;
3847048Snate@binkert.org        out << "Total_delay_cycles: " <<   m_delayedCyclesHistogram << endl;
3857048Snate@binkert.org        out << "Total_nonPF_delay_cycles: "
3867048Snate@binkert.org            << m_delayedCyclesNonPFHistogram << endl;
3877048Snate@binkert.org        for (int i = 0; i < m_delayedCyclesVCHistograms.size(); i++) {
3887048Snate@binkert.org            out << "  virtual_network_" << i << "_delay_cycles: "
3897048Snate@binkert.org                << m_delayedCyclesVCHistograms[i] << endl;
3907048Snate@binkert.org        }
3917048Snate@binkert.org
3927048Snate@binkert.org        printResourceUsage(out);
3937048Snate@binkert.org    }
3946145Snate@binkert.org}
3956145Snate@binkert.org
3967048Snate@binkert.orgvoid
3977048Snate@binkert.orgProfiler::printResourceUsage(ostream& out) const
3986145Snate@binkert.org{
3997048Snate@binkert.org    out << endl;
4007048Snate@binkert.org    out << "Resource Usage" << endl;
4017048Snate@binkert.org    out << "--------------" << endl;
4026145Snate@binkert.org
4037048Snate@binkert.org    integer_t pagesize = getpagesize(); // page size in bytes
4047048Snate@binkert.org    out << "page_size: " << pagesize << endl;
4056145Snate@binkert.org
4067048Snate@binkert.org    rusage usage;
4077048Snate@binkert.org    getrusage (RUSAGE_SELF, &usage);
4086145Snate@binkert.org
4097048Snate@binkert.org    out << "user_time: " << usage.ru_utime.tv_sec << endl;
4107048Snate@binkert.org    out << "system_time: " << usage.ru_stime.tv_sec << endl;
4117048Snate@binkert.org    out << "page_reclaims: " << usage.ru_minflt << endl;
4127048Snate@binkert.org    out << "page_faults: " << usage.ru_majflt << endl;
4137048Snate@binkert.org    out << "swaps: " << usage.ru_nswap << endl;
4147048Snate@binkert.org    out << "block_inputs: " << usage.ru_inblock << endl;
4157048Snate@binkert.org    out << "block_outputs: " << usage.ru_oublock << endl;
4166145Snate@binkert.org}
4176145Snate@binkert.org
4187048Snate@binkert.orgvoid
4197048Snate@binkert.orgProfiler::clearStats()
4206145Snate@binkert.org{
4217048Snate@binkert.org    m_ruby_start = g_eventQueue_ptr->getTime();
4226145Snate@binkert.org
4237048Snate@binkert.org    m_cycles_executed_at_start.setSize(m_num_of_sequencers);
4247048Snate@binkert.org    for (int i = 0; i < m_num_of_sequencers; i++) {
4257048Snate@binkert.org        if (g_system_ptr == NULL) {
4267048Snate@binkert.org            m_cycles_executed_at_start[i] = 0;
4277048Snate@binkert.org        } else {
4287048Snate@binkert.org            m_cycles_executed_at_start[i] = g_system_ptr->getCycleCount(i);
4297048Snate@binkert.org        }
4306145Snate@binkert.org    }
4316145Snate@binkert.org
4327048Snate@binkert.org    m_busyControllerCount.setSize(MachineType_NUM); // all machines
4337048Snate@binkert.org    for (int i = 0; i < MachineType_NUM; i++) {
4347048Snate@binkert.org        int size = MachineType_base_count((MachineType)i);
4357048Snate@binkert.org        m_busyControllerCount[i].setSize(size);
4367048Snate@binkert.org        for (int j = 0; j < size; j++) {
4377048Snate@binkert.org            m_busyControllerCount[i][j] = 0;
4387048Snate@binkert.org        }
4396145Snate@binkert.org    }
4407048Snate@binkert.org    m_busyBankCount = 0;
4416145Snate@binkert.org
4427048Snate@binkert.org    m_delayedCyclesHistogram.clear();
4437048Snate@binkert.org    m_delayedCyclesNonPFHistogram.clear();
4447048Snate@binkert.org    int size = RubySystem::getNetwork()->getNumberOfVirtualNetworks();
4457048Snate@binkert.org    m_delayedCyclesVCHistograms.setSize(size);
4467048Snate@binkert.org    for (int i = 0; i < size; i++) {
4477048Snate@binkert.org        m_delayedCyclesVCHistograms[i].clear();
4487048Snate@binkert.org    }
4496145Snate@binkert.org
4507048Snate@binkert.org    m_missLatencyHistograms.setSize(RubyRequestType_NUM);
4517048Snate@binkert.org    for (int i = 0; i < m_missLatencyHistograms.size(); i++) {
4527048Snate@binkert.org        m_missLatencyHistograms[i].clear(200);
4537048Snate@binkert.org    }
4547048Snate@binkert.org    m_machLatencyHistograms.setSize(GenericMachineType_NUM+1);
4557048Snate@binkert.org    for (int i = 0; i < m_machLatencyHistograms.size(); i++) {
4567048Snate@binkert.org        m_machLatencyHistograms[i].clear(200);
4577048Snate@binkert.org    }
4587048Snate@binkert.org    m_allMissLatencyHistogram.clear(200);
4596145Snate@binkert.org
4607048Snate@binkert.org    m_SWPrefetchLatencyHistograms.setSize(CacheRequestType_NUM);
4617048Snate@binkert.org    for (int i = 0; i < m_SWPrefetchLatencyHistograms.size(); i++) {
4627048Snate@binkert.org        m_SWPrefetchLatencyHistograms[i].clear(200);
4637048Snate@binkert.org    }
4647048Snate@binkert.org    m_SWPrefetchMachLatencyHistograms.setSize(GenericMachineType_NUM+1);
4657048Snate@binkert.org    for (int i = 0; i < m_SWPrefetchMachLatencyHistograms.size(); i++) {
4667048Snate@binkert.org        m_SWPrefetchMachLatencyHistograms[i].clear(200);
4677048Snate@binkert.org    }
4687048Snate@binkert.org    m_allSWPrefetchLatencyHistogram.clear(200);
4696145Snate@binkert.org
4707048Snate@binkert.org    m_sequencer_requests.clear();
4717048Snate@binkert.org    m_read_sharing_histogram.clear();
4727048Snate@binkert.org    m_write_sharing_histogram.clear();
4737048Snate@binkert.org    m_all_sharing_histogram.clear();
4747048Snate@binkert.org    m_cache_to_cache = 0;
4757048Snate@binkert.org    m_memory_to_cache = 0;
4766145Snate@binkert.org
4777048Snate@binkert.org    // clear HashMaps
4787048Snate@binkert.org    m_requestProfileMap_ptr->clear();
4796145Snate@binkert.org
4807048Snate@binkert.org    // count requests profiled
4817048Snate@binkert.org    m_requests = 0;
4826145Snate@binkert.org
4837048Snate@binkert.org    m_outstanding_requests.clear();
4847048Snate@binkert.org    m_outstanding_persistent_requests.clear();
4856145Snate@binkert.org
4867048Snate@binkert.org    // Flush the prefetches through the system - used so that there
4877048Snate@binkert.org    // are no outstanding requests after stats are cleared
4887048Snate@binkert.org    //g_eventQueue_ptr->triggerAllEvents();
4896145Snate@binkert.org
4907048Snate@binkert.org    // update the start time
4917048Snate@binkert.org    m_ruby_start = g_eventQueue_ptr->getTime();
4926145Snate@binkert.org}
4936145Snate@binkert.org
4947048Snate@binkert.orgvoid
4957048Snate@binkert.orgProfiler::addAddressTraceSample(const CacheMsg& msg, NodeID id)
4966145Snate@binkert.org{
4977048Snate@binkert.org    if (msg.getType() != CacheRequestType_IFETCH) {
4987048Snate@binkert.org        // Note: The following line should be commented out if you
4997048Snate@binkert.org        // want to use the special profiling that is part of the GS320
5007048Snate@binkert.org        // protocol
5016145Snate@binkert.org
5027048Snate@binkert.org        // NOTE: Unless PROFILE_HOT_LINES is enabled, nothing will be
5037048Snate@binkert.org        // profiled by the AddressProfiler
5047048Snate@binkert.org        m_address_profiler_ptr->
5057048Snate@binkert.org            addTraceSample(msg.getLineAddress(), msg.getProgramCounter(),
5067048Snate@binkert.org                           msg.getType(), msg.getAccessMode(), id, false);
5077048Snate@binkert.org    }
5086145Snate@binkert.org}
5096145Snate@binkert.org
5107048Snate@binkert.orgvoid
5117048Snate@binkert.orgProfiler::profileSharing(const Address& addr, AccessType type,
5127048Snate@binkert.org                         NodeID requestor, const Set& sharers,
5137048Snate@binkert.org                         const Set& owner)
5146145Snate@binkert.org{
5157048Snate@binkert.org    Set set_contacted(owner);
5167048Snate@binkert.org    if (type == AccessType_Write) {
5177048Snate@binkert.org        set_contacted.addSet(sharers);
5187048Snate@binkert.org    }
5197048Snate@binkert.org    set_contacted.remove(requestor);
5207048Snate@binkert.org    int number_contacted = set_contacted.count();
5216145Snate@binkert.org
5227048Snate@binkert.org    if (type == AccessType_Write) {
5237048Snate@binkert.org        m_write_sharing_histogram.add(number_contacted);
5247048Snate@binkert.org    } else {
5257048Snate@binkert.org        m_read_sharing_histogram.add(number_contacted);
5267048Snate@binkert.org    }
5277048Snate@binkert.org    m_all_sharing_histogram.add(number_contacted);
5286145Snate@binkert.org
5297048Snate@binkert.org    if (number_contacted == 0) {
5307048Snate@binkert.org        m_memory_to_cache++;
5317048Snate@binkert.org    } else {
5327048Snate@binkert.org        m_cache_to_cache++;
5337048Snate@binkert.org    }
5346145Snate@binkert.org}
5356145Snate@binkert.org
5367048Snate@binkert.orgvoid
5377048Snate@binkert.orgProfiler::profileMsgDelay(int virtualNetwork, int delayCycles)
5387048Snate@binkert.org{
5397048Snate@binkert.org    assert(virtualNetwork < m_delayedCyclesVCHistograms.size());
5407048Snate@binkert.org    m_delayedCyclesHistogram.add(delayCycles);
5417048Snate@binkert.org    m_delayedCyclesVCHistograms[virtualNetwork].add(delayCycles);
5427048Snate@binkert.org    if (virtualNetwork != 0) {
5437048Snate@binkert.org        m_delayedCyclesNonPFHistogram.add(delayCycles);
5447048Snate@binkert.org    }
5456145Snate@binkert.org}
5466145Snate@binkert.org
5476145Snate@binkert.org// profiles original cache requests including PUTs
5487048Snate@binkert.orgvoid
5497048Snate@binkert.orgProfiler::profileRequest(const string& requestStr)
5506145Snate@binkert.org{
5517048Snate@binkert.org    m_requests++;
5526145Snate@binkert.org
5537048Snate@binkert.org    if (m_requestProfileMap_ptr->exist(requestStr)) {
5547048Snate@binkert.org        (m_requestProfileMap_ptr->lookup(requestStr))++;
5557048Snate@binkert.org    } else {
5567048Snate@binkert.org        m_requestProfileMap_ptr->add(requestStr, 1);
5577048Snate@binkert.org    }
5586145Snate@binkert.org}
5596145Snate@binkert.org
5607048Snate@binkert.orgvoid
5617048Snate@binkert.orgProfiler::controllerBusy(MachineID machID)
5626145Snate@binkert.org{
5637048Snate@binkert.org    m_busyControllerCount[(int)machID.type][(int)machID.num]++;
5646145Snate@binkert.org}
5656145Snate@binkert.org
5667048Snate@binkert.orgvoid
5677048Snate@binkert.orgProfiler::profilePFWait(Time waitTime)
5686145Snate@binkert.org{
5697048Snate@binkert.org    m_prefetchWaitHistogram.add(waitTime);
5706145Snate@binkert.org}
5716145Snate@binkert.org
5727048Snate@binkert.orgvoid
5737048Snate@binkert.orgProfiler::bankBusy()
5746145Snate@binkert.org{
5757048Snate@binkert.org    m_busyBankCount++;
5766145Snate@binkert.org}
5776145Snate@binkert.org
5786145Snate@binkert.org// non-zero cycle demand request
5797048Snate@binkert.orgvoid
5807048Snate@binkert.orgProfiler::missLatency(Time t, RubyRequestType type)
5816145Snate@binkert.org{
5827048Snate@binkert.org    m_allMissLatencyHistogram.add(t);
5837048Snate@binkert.org    m_missLatencyHistograms[type].add(t);
5846145Snate@binkert.org}
5856145Snate@binkert.org
5866145Snate@binkert.org// non-zero cycle prefetch request
5877048Snate@binkert.orgvoid
5887048Snate@binkert.orgProfiler::swPrefetchLatency(Time t, CacheRequestType type,
5897048Snate@binkert.org                            GenericMachineType respondingMach)
5906145Snate@binkert.org{
5917048Snate@binkert.org    m_allSWPrefetchLatencyHistogram.add(t);
5927048Snate@binkert.org    m_SWPrefetchLatencyHistograms[type].add(t);
5937048Snate@binkert.org    m_SWPrefetchMachLatencyHistograms[respondingMach].add(t);
5947048Snate@binkert.org    if (respondingMach == GenericMachineType_Directory ||
5957048Snate@binkert.org        respondingMach == GenericMachineType_NUM) {
5967048Snate@binkert.org        m_SWPrefetchL2MissLatencyHistogram.add(t);
5977048Snate@binkert.org    }
5986145Snate@binkert.org}
5996145Snate@binkert.org
6007048Snate@binkert.orgvoid
6017048Snate@binkert.orgProfiler::profileTransition(const string& component, NodeID version,
6027048Snate@binkert.org    Address addr, const string& state, const string& event,
6037048Snate@binkert.org    const string& next_state, const string& note)
6046145Snate@binkert.org{
6057048Snate@binkert.org    const int EVENT_SPACES = 20;
6067048Snate@binkert.org    const int ID_SPACES = 3;
6077048Snate@binkert.org    const int TIME_SPACES = 7;
6087048Snate@binkert.org    const int COMP_SPACES = 10;
6097048Snate@binkert.org    const int STATE_SPACES = 6;
6106145Snate@binkert.org
6117048Snate@binkert.org    if (g_debug_ptr->getDebugTime() <= 0 ||
6127048Snate@binkert.org        g_eventQueue_ptr->getTime() < g_debug_ptr->getDebugTime())
6137048Snate@binkert.org        return;
6146145Snate@binkert.org
6157048Snate@binkert.org    ostream &out = *debug_cout_ptr;
6167048Snate@binkert.org    out.flags(ios::right);
6177048Snate@binkert.org    out << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
6187048Snate@binkert.org    out << setw(ID_SPACES) << version << " ";
6197048Snate@binkert.org    out << setw(COMP_SPACES) << component;
6207048Snate@binkert.org    out << setw(EVENT_SPACES) << event << " ";
6216285Snate@binkert.org
6227048Snate@binkert.org    out.flags(ios::right);
6237048Snate@binkert.org    out << setw(STATE_SPACES) << state;
6247048Snate@binkert.org    out << ">";
6257048Snate@binkert.org    out.flags(ios::left);
6267048Snate@binkert.org    out << setw(STATE_SPACES) << next_state;
6276145Snate@binkert.org
6287048Snate@binkert.org    out << " " << addr << " " << note;
6297048Snate@binkert.org
6307048Snate@binkert.org    out << endl;
6316145Snate@binkert.org}
6326145Snate@binkert.org
6336145Snate@binkert.org// Helper function
6347048Snate@binkert.orgstatic double
6357048Snate@binkert.orgprocess_memory_total()
6366145Snate@binkert.org{
6377048Snate@binkert.org    // 4kB page size, 1024*1024 bytes per MB,
6387054Snate@binkert.org    const double MULTIPLIER = 4096.0 / (1024.0 * 1024.0);
6397048Snate@binkert.org    ifstream proc_file;
6407048Snate@binkert.org    proc_file.open("/proc/self/statm");
6417048Snate@binkert.org    int total_size_in_pages = 0;
6427048Snate@binkert.org    int res_size_in_pages = 0;
6437048Snate@binkert.org    proc_file >> total_size_in_pages;
6447048Snate@binkert.org    proc_file >> res_size_in_pages;
6457048Snate@binkert.org    return double(total_size_in_pages) * MULTIPLIER; // size in megabytes
6466145Snate@binkert.org}
6476145Snate@binkert.org
6487048Snate@binkert.orgstatic double
6497048Snate@binkert.orgprocess_memory_resident()
6506145Snate@binkert.org{
6517048Snate@binkert.org    // 4kB page size, 1024*1024 bytes per MB,
6527048Snate@binkert.org    const double MULTIPLIER = 4096.0 / (1024.0 * 1024.0);
6537048Snate@binkert.org    ifstream proc_file;
6547048Snate@binkert.org    proc_file.open("/proc/self/statm");
6557048Snate@binkert.org    int total_size_in_pages = 0;
6567048Snate@binkert.org    int res_size_in_pages = 0;
6577048Snate@binkert.org    proc_file >> total_size_in_pages;
6587048Snate@binkert.org    proc_file >> res_size_in_pages;
6597048Snate@binkert.org    return double(res_size_in_pages) * MULTIPLIER; // size in megabytes
6606145Snate@binkert.org}
6616145Snate@binkert.org
6627048Snate@binkert.orgvoid
6637048Snate@binkert.orgProfiler::rubyWatch(int id)
6647048Snate@binkert.org{
6657010SBrad.Beckmann@amd.com    uint64 tr = 0;
6666285Snate@binkert.org    Address watch_address = Address(tr);
6676285Snate@binkert.org    const int ID_SPACES = 3;
6686285Snate@binkert.org    const int TIME_SPACES = 7;
6696285Snate@binkert.org
6707048Snate@binkert.org    ostream &out = *debug_cout_ptr;
6716285Snate@binkert.org
6727048Snate@binkert.org    out.flags(ios::right);
6737048Snate@binkert.org    out << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
6747048Snate@binkert.org    out << setw(ID_SPACES) << id << " "
6757048Snate@binkert.org        << "RUBY WATCH " << watch_address << endl;
6767048Snate@binkert.org
6777048Snate@binkert.org    if (!m_watch_address_list_ptr->exist(watch_address)) {
6787048Snate@binkert.org        m_watch_address_list_ptr->add(watch_address, 1);
6796285Snate@binkert.org    }
6806285Snate@binkert.org}
6816285Snate@binkert.org
6827048Snate@binkert.orgbool
6837048Snate@binkert.orgProfiler::watchAddress(Address addr)
6847048Snate@binkert.org{
6856285Snate@binkert.org    if (m_watch_address_list_ptr->exist(addr))
6867048Snate@binkert.org        return true;
6876285Snate@binkert.org    else
6887048Snate@binkert.org        return false;
6896285Snate@binkert.org}
6906285Snate@binkert.org
6916876Ssteve.reinhardt@amd.comProfiler *
6926876Ssteve.reinhardt@amd.comRubyProfilerParams::create()
6936876Ssteve.reinhardt@amd.com{
6946876Ssteve.reinhardt@amd.com    return new Profiler(this);
6956876Ssteve.reinhardt@amd.com}
696