sim: Added PcCountTracker and PcCountTrackerManager

PcCountTracker is a probelistener that connects to one core and listens
for a list of Program Counter addresses(PCs). It notifys the
PcCountTrackerManager every time it encounters a Program Counter
address in the list.
PcCountTrackerManager is a SimObject that is responsible for keeping
track of a list of PC-count pairs and the number of time a particular
PC has been executed globally.

This patch adds a way to track the number of times a set of specific
PCs have been executed.

Change-Id: I8f47bfa7e29aa2bb6ab817417266033439b85d51
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/67194
Maintainer: Bobby Bruce <bbruce@ucdavis.edu>
Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Zhantong Qiu
2023-01-06 16:58:06 -08:00
parent 717d3b239c
commit 0d129a6bf2
6 changed files with 455 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
# Copyright (c) 2023 The Regents of the University of California
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.params import *
from m5.util.pybind import *
from m5.objects.Probe import ProbeListenerObject
from m5.objects import SimObject
class PcCountTrackerManager(SimObject):
"""This class manages global PC-count pair tracking.
It keeps the global counters for all target PC-count pairs and raises exit
events when a PC executed a target number of times.
It gets called every time a PcCountTracker encounters a target PC.
"""
type = "PcCountTrackerManager"
cxx_header = "cpu/probes/pc_count_tracker_manager.hh"
cxx_class = "gem5::PcCountTrackerManager"
cxx_exports = [
PyBindMethod("getPcCount"),
PyBindMethod("getCurrentPcCountPair"),
]
targets = VectorParam.PcCountPair("the target PC Count pairs")
class PcCountTracker(ProbeListenerObject):
"""This probe listener tracks the number of times a particular pc has been
executed. It needs to be connected to a manager to track the global
information.
"""
type = "PcCountTracker"
cxx_header = "cpu/probes/pc_count_tracker.hh"
cxx_class = "gem5::PcCountTracker"
targets = VectorParam.PcCountPair("the target PC Count pairs")
core = Param.BaseCPU("the connected cpu")
ptmanager = Param.PcCountTrackerManager("the PcCountTracker manager")

37
src/cpu/probes/SConscript Normal file
View File

@@ -0,0 +1,37 @@
# Copyright (c) 2022 The Regents of the University of California
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Import("*")
if not env["CONF"]["USE_NULL_ISA"]:
SimObject(
"PcCountTracker.py",
sim_objects=["PcCountTracker", "PcCountTrackerManager"],
)
Source("pc_count_tracker.cc")
Source("pc_count_tracker_manager.cc")
DebugFlag("PcCountTracker")

View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) 2022 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cpu/probes/pc_count_tracker.hh"
namespace gem5
{
PcCountTracker::PcCountTracker(const PcCountTrackerParams &p)
: ProbeListenerObject(p),
cpuptr(p.core),
manager(p.ptmanager)
{
if (!cpuptr || !manager) {
fatal("%s is NULL", !cpuptr ? "CPU": "PcCountTrackerManager");
}
for (int i = 0; i < p.targets.size(); i++) {
// initialize the set of targeting Program Counter addresses
targetPC.insert(p.targets[i].getPC());
}
}
void
PcCountTracker::regProbeListeners()
{
// connect the probe listener with the probe "RetriedInstsPC" in the
// corresponding core.
// when "RetiredInstsPC" notifies the probe listener, then the function
// 'check_pc' is automatically called
typedef ProbeListenerArg<PcCountTracker, Addr> PcCountTrackerListener;
listeners.push_back(new PcCountTrackerListener(this, "RetiredInstsPC",
&PcCountTracker::checkPc));
}
void
PcCountTracker::checkPc(const Addr& pc) {
if (targetPC.find(pc) != targetPC.end()) {
// if the PC is one of the target PCs, then notify the
// PcCounterTrackerManager by calling its `check_count` function
manager->checkCount(pc);
}
}
} // namespace gem5

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2022 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __CPU_PROBES_PC_COUNT_TRACKER_HH__
#define __CPU_PROBES_PC_COUNT_TRACKER_HH__
#include <unordered_set>
#include "cpu/probes/pc_count_tracker_manager.hh"
#include "params/PcCountTracker.hh"
#include "sim/probe/probe.hh"
namespace gem5
{
class PcCountTracker : public ProbeListenerObject
{
public:
PcCountTracker(const PcCountTrackerParams &params);
/** setup the probelistener */
virtual void regProbeListeners();
/**
* this function is called when the probelistener receives signal from the
* probe
*
* @param pc the targeting Program Counter address
*/
void checkPc(const Addr& pc);
private:
/**
* a set of Program Counter addresses that should notify the
* PcCounterTrackerManager for
*/
std::unordered_set<Addr> targetPC;
/** the core this PcCountTracker is tracking at */
BaseCPU *cpuptr;
/** the PcCounterTrackerManager */
PcCountTrackerManager *manager;
};
}
#endif // __CPU_PROBES_PC_COUNT_TRACKER_HH__

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2022 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cpu/probes/pc_count_tracker_manager.hh"
namespace gem5
{
PcCountTrackerManager::PcCountTrackerManager(
const PcCountTrackerManagerParams &p)
: SimObject(p)
{
currentPair = PcCountPair(0,0);
ifListNotEmpty = true;
for (int i = 0 ; i < p.targets.size() ; i++) {
// initialize the counter for the inputted PC Count pair
// unordered_map does not allow duplicate, so counter won't
// have duplicates
counter.insert(std::make_pair(p.targets[i].getPC(),0));
// store all the PC Count pair into the targetPair set
targetPair.insert(p.targets[i]);
}
DPRINTF(PcCountTracker,
"total %i PCs in counter\n", counter.size());
DPRINTF(PcCountTracker,
"all targets: \n%s", printAllTargets());
}
void
PcCountTrackerManager::checkCount(Addr pc)
{
if(ifListNotEmpty) {
int count = ++counter.find(pc)->second;
// increment the counter of the encountered PC address by 1
currentPair = PcCountPair(pc,count);
// update the current PC Count pair
if(targetPair.find(currentPair) != targetPair.end()) {
// if the current PC Count pair is one of the target pairs
DPRINTF(PcCountTracker,
"pc:%s encountered\n", currentPair.to_string());
exitSimLoopNow("simpoint starting point found");
// raise the SIMPOINT_BEGIN exit event
targetPair.erase(currentPair);
// erase the encountered PC Count pair from the target pairs
DPRINTF(PcCountTracker,
"There are %i targets remained\n", targetPair.size());
}
if(targetPair.empty()) {
// if all target PC Count pairs are encountered
DPRINTF(PcCountTracker,
"all targets are encountered.\n");
ifListNotEmpty = false;
}
}
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (c) 2022 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __CPU_PROBES_PC_COUNT_TRACKER_MANAGER_HH__
#define __CPU_PROBES_PC_COUNT_TRACKER_MANAGER_HH__
#include <unordered_map>
#include <unordered_set>
#include "cpu/base.hh"
#include "params/PcCountTrackerManager.hh"
#include "sim/sim_exit.hh"
#include "debug/PcCountTracker.hh"
namespace gem5
{
class PcCountTrackerManager : public SimObject {
public:
PcCountTrackerManager(const PcCountTrackerManagerParams &params);
/** this function is called when PcCountTrackerProbeListener finds a target
* PC
*/
void checkCount(Addr pc);
private:
/** a counter that stores all the target PC addresses and the number
* of times the target PC has been executed
*/
std::unordered_map<Addr, int> counter;
/** a set that stores all the PC Count pairs that should raise an
* exit event at
*/
std::unordered_set<PcCountPair,
PcCountPair::HashFunction> targetPair;
/** the current PC Count pair */
PcCountPair currentPair;
/** when all the PC Count pairs in the `targetPair` are encountered,
* and the PCCOUNTTRACK_END exit event is raised, this boolean
* variable becomes false and is used to stop the `check_count`
* from functioning. This is default as true.
*/
bool ifListNotEmpty;
public:
/** this function returns the corresponding value of count for the
* inputted Program Counter address. If the PC address does not
* exist in the counter, then it returns a -1.
*
* @param pc the targeting Program Counter address
* @return the corresponding value of count for the inputted Program
* Counter address
*/
int
getPcCount(Addr pc) const
{
if (counter.find(pc) != counter.end()) {
return counter.find(pc)->second;
}
return -1;
}
/** this function returns the current PC Count pair
*
* @return current PC Count pair
*/
PcCountPair
getCurrentPcCountPair() const
{
return currentPair;
}
/** this function print all targets
*
* @return formatted string that contains all targets
*/
std::string
printAllTargets() const
{
std::string s;
for(auto itr = targetPair.begin();
itr != targetPair.end();
++itr) {
s += itr->to_string();
s += "\n";
}
return s;
}
};
}
#endif // __CPU_PROBES_PC_COUNT_TRACKER_MANAGER_HH__