110381Sdam.sunwoo@arm.com/*
210381Sdam.sunwoo@arm.com * Copyright (c) 2012-2014 ARM Limited
310381Sdam.sunwoo@arm.com * All rights reserved.
410381Sdam.sunwoo@arm.com *
510381Sdam.sunwoo@arm.com * The license below extends only to copyright in the software and shall
610381Sdam.sunwoo@arm.com * not be construed as granting a license to any other intellectual
710381Sdam.sunwoo@arm.com * property including but not limited to intellectual property relating
810381Sdam.sunwoo@arm.com * to a hardware implementation of the functionality of the software
910381Sdam.sunwoo@arm.com * licensed hereunder.  You may use the software subject to the license
1010381Sdam.sunwoo@arm.com * terms below provided that you ensure that this notice is replicated
1110381Sdam.sunwoo@arm.com * unmodified and in its entirety in all distributions of the software,
1210381Sdam.sunwoo@arm.com * modified or unmodified, in source code or in binary form.
1310381Sdam.sunwoo@arm.com *
1410381Sdam.sunwoo@arm.com * Redistribution and use in source and binary forms, with or without
1510381Sdam.sunwoo@arm.com * modification, are permitted provided that the following conditions are
1610381Sdam.sunwoo@arm.com * met: redistributions of source code must retain the above copyright
1710381Sdam.sunwoo@arm.com * notice, this list of conditions and the following disclaimer;
1810381Sdam.sunwoo@arm.com * redistributions in binary form must reproduce the above copyright
1910381Sdam.sunwoo@arm.com * notice, this list of conditions and the following disclaimer in the
2010381Sdam.sunwoo@arm.com * documentation and/or other materials provided with the distribution;
2110381Sdam.sunwoo@arm.com * neither the name of the copyright holders nor the names of its
2210381Sdam.sunwoo@arm.com * contributors may be used to endorse or promote products derived from
2310381Sdam.sunwoo@arm.com * this software without specific prior written permission.
2410381Sdam.sunwoo@arm.com *
2510381Sdam.sunwoo@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610381Sdam.sunwoo@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710381Sdam.sunwoo@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810381Sdam.sunwoo@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910381Sdam.sunwoo@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010381Sdam.sunwoo@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110381Sdam.sunwoo@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210381Sdam.sunwoo@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310381Sdam.sunwoo@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410381Sdam.sunwoo@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510381Sdam.sunwoo@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610381Sdam.sunwoo@arm.com *
3710381Sdam.sunwoo@arm.com * Authors: Dam Sunwoo
3810381Sdam.sunwoo@arm.com *          Curtis Dunham
3910381Sdam.sunwoo@arm.com */
4010381Sdam.sunwoo@arm.com
4111793Sbrandon.potter@amd.com#include "cpu/simple/probes/simpoint.hh"
4211793Sbrandon.potter@amd.com
4310381Sdam.sunwoo@arm.com#include "base/output.hh"
4410381Sdam.sunwoo@arm.com
4510381Sdam.sunwoo@arm.comSimPoint::SimPoint(const SimPointParams *p)
4610381Sdam.sunwoo@arm.com    : ProbeListenerObject(p),
4710381Sdam.sunwoo@arm.com      intervalSize(p->interval),
4810381Sdam.sunwoo@arm.com      intervalCount(0),
4910381Sdam.sunwoo@arm.com      intervalDrift(0),
5010381Sdam.sunwoo@arm.com      simpointStream(NULL),
5110381Sdam.sunwoo@arm.com      currentBBV(0, 0),
5210381Sdam.sunwoo@arm.com      currentBBVInstCount(0)
5310381Sdam.sunwoo@arm.com{
5410381Sdam.sunwoo@arm.com    simpointStream = simout.create(p->profile_file, false);
5510381Sdam.sunwoo@arm.com    if (!simpointStream)
5610381Sdam.sunwoo@arm.com        fatal("unable to open SimPoint profile_file");
5710381Sdam.sunwoo@arm.com}
5810381Sdam.sunwoo@arm.com
5910381Sdam.sunwoo@arm.comSimPoint::~SimPoint()
6010381Sdam.sunwoo@arm.com{
6110381Sdam.sunwoo@arm.com    simout.close(simpointStream);
6210381Sdam.sunwoo@arm.com}
6310381Sdam.sunwoo@arm.com
6410381Sdam.sunwoo@arm.comvoid
6510381Sdam.sunwoo@arm.comSimPoint::init()
6610381Sdam.sunwoo@arm.com{}
6710381Sdam.sunwoo@arm.com
6810381Sdam.sunwoo@arm.comvoid
6910381Sdam.sunwoo@arm.comSimPoint::regProbeListeners()
7010381Sdam.sunwoo@arm.com{
7110381Sdam.sunwoo@arm.com    typedef ProbeListenerArg<SimPoint, std::pair<SimpleThread*,StaticInstPtr>>
7210381Sdam.sunwoo@arm.com        SimPointListener;
7310381Sdam.sunwoo@arm.com    listeners.push_back(new SimPointListener(this, "Commit",
7410381Sdam.sunwoo@arm.com                                             &SimPoint::profile));
7510381Sdam.sunwoo@arm.com}
7610381Sdam.sunwoo@arm.com
7710381Sdam.sunwoo@arm.comvoid
7810381Sdam.sunwoo@arm.comSimPoint::profile(const std::pair<SimpleThread*, StaticInstPtr>& p)
7910381Sdam.sunwoo@arm.com{
8010381Sdam.sunwoo@arm.com    SimpleThread* thread = p.first;
8110417Sandreas.hansson@arm.com    const StaticInstPtr &inst = p.second;
8210381Sdam.sunwoo@arm.com
8310651Snikos.nikoleris@gmail.com    if (inst->isMicroop() && !inst->isLastMicroop())
8410651Snikos.nikoleris@gmail.com        return;
8510651Snikos.nikoleris@gmail.com
8610381Sdam.sunwoo@arm.com    if (!currentBBVInstCount)
8710381Sdam.sunwoo@arm.com        currentBBV.first = thread->pcState().instAddr();
8810381Sdam.sunwoo@arm.com
8910381Sdam.sunwoo@arm.com    ++intervalCount;
9010381Sdam.sunwoo@arm.com    ++currentBBVInstCount;
9110381Sdam.sunwoo@arm.com
9210381Sdam.sunwoo@arm.com    // If inst is control inst, assume end of basic block.
9310381Sdam.sunwoo@arm.com    if (inst->isControl()) {
9410381Sdam.sunwoo@arm.com        currentBBV.second = thread->pcState().instAddr();
9510381Sdam.sunwoo@arm.com
9610381Sdam.sunwoo@arm.com        auto map_itr = bbMap.find(currentBBV);
9710381Sdam.sunwoo@arm.com        if (map_itr == bbMap.end()){
9810381Sdam.sunwoo@arm.com            // If a new (previously unseen) basic block is found,
9910381Sdam.sunwoo@arm.com            // add a new unique id, record num of insts and insert into bbMap.
10010381Sdam.sunwoo@arm.com            BBInfo info;
10110381Sdam.sunwoo@arm.com            info.id = bbMap.size() + 1;
10210381Sdam.sunwoo@arm.com            info.insts = currentBBVInstCount;
10310381Sdam.sunwoo@arm.com            info.count = currentBBVInstCount;
10410381Sdam.sunwoo@arm.com            bbMap.insert(std::make_pair(currentBBV, info));
10510381Sdam.sunwoo@arm.com        } else {
10610381Sdam.sunwoo@arm.com            // If basic block is seen before, just increment the count by the
10710381Sdam.sunwoo@arm.com            // number of insts in basic block.
10810381Sdam.sunwoo@arm.com            BBInfo& info = map_itr->second;
10910381Sdam.sunwoo@arm.com            info.count += currentBBVInstCount;
11010381Sdam.sunwoo@arm.com        }
11110381Sdam.sunwoo@arm.com        currentBBVInstCount = 0;
11210381Sdam.sunwoo@arm.com
11310381Sdam.sunwoo@arm.com        // Reached end of interval if the sum of the current inst count
11410381Sdam.sunwoo@arm.com        // (intervalCount) and the excessive inst count from the previous
11510381Sdam.sunwoo@arm.com        // interval (intervalDrift) is greater than/equal to the interval size.
11610381Sdam.sunwoo@arm.com        if (intervalCount + intervalDrift >= intervalSize) {
11710381Sdam.sunwoo@arm.com            // summarize interval and display BBV info
11810381Sdam.sunwoo@arm.com            std::vector<std::pair<uint64_t, uint64_t> > counts;
11910381Sdam.sunwoo@arm.com            for (auto map_itr = bbMap.begin(); map_itr != bbMap.end();
12010381Sdam.sunwoo@arm.com                    ++map_itr) {
12110381Sdam.sunwoo@arm.com                BBInfo& info = map_itr->second;
12210381Sdam.sunwoo@arm.com                if (info.count != 0) {
12310381Sdam.sunwoo@arm.com                    counts.push_back(std::make_pair(info.id, info.count));
12410381Sdam.sunwoo@arm.com                    info.count = 0;
12510381Sdam.sunwoo@arm.com                }
12610381Sdam.sunwoo@arm.com            }
12710381Sdam.sunwoo@arm.com            std::sort(counts.begin(), counts.end());
12810381Sdam.sunwoo@arm.com
12910381Sdam.sunwoo@arm.com            // Print output BBV info
13011359Sandreas@sandberg.pp.se            *simpointStream->stream() << "T";
13110381Sdam.sunwoo@arm.com            for (auto cnt_itr = counts.begin(); cnt_itr != counts.end();
13210381Sdam.sunwoo@arm.com                    ++cnt_itr) {
13311359Sandreas@sandberg.pp.se                *simpointStream->stream() << ":" << cnt_itr->first
13410381Sdam.sunwoo@arm.com                                << ":" << cnt_itr->second << " ";
13510381Sdam.sunwoo@arm.com            }
13611359Sandreas@sandberg.pp.se            *simpointStream->stream() << "\n";
13710381Sdam.sunwoo@arm.com
13810381Sdam.sunwoo@arm.com            intervalDrift = (intervalCount + intervalDrift) - intervalSize;
13910381Sdam.sunwoo@arm.com            intervalCount = 0;
14010381Sdam.sunwoo@arm.com        }
14110381Sdam.sunwoo@arm.com    }
14210381Sdam.sunwoo@arm.com}
14310381Sdam.sunwoo@arm.com
14410381Sdam.sunwoo@arm.com/** SimPoint SimObject */
14510381Sdam.sunwoo@arm.comSimPoint*
14610381Sdam.sunwoo@arm.comSimPointParams::create()
14710381Sdam.sunwoo@arm.com{
14810381Sdam.sunwoo@arm.com    return new SimPoint(this);
14910381Sdam.sunwoo@arm.com}
150