Skip to content

Commit

Permalink
utils: support performance rolling tool and corresponding scripts (Op…
Browse files Browse the repository at this point in the history
…enXiangShan#2228)

* utils: support perf rolling data collection through chiseldb

* perf: add ipc rollingperf

* script: add rolling plot script

* param: disable rollingdb by default

* misc: fix typo
  • Loading branch information
wakafa1 authored Aug 10, 2023
1 parent 501ff15 commit ec9e651
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
57 changes: 57 additions & 0 deletions scripts/rollingplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import sys
import argparse
import sqlite3
import matplotlib.pyplot as plt
import numpy as np


# usage: python3 rollingplot.py DB_FILE_PATH PERF_NAME [--aggregate AGGREGATE_RATIO]


class DataSet:

def __init__(self, db_path):
self.conn = sqlite3.connect(db_path)
self.cursor = self.conn.cursor()
self.xdata = []
self.ydata = []

def derive(self, perf_name, aggregate, hart):
sql = "SELECT xAxisPt, yAxisPt FROM {}_rolling_{}".format(perf_name, hart)
self.cursor.execute(sql)
result = self.cursor.fetchall()
granularity = result[1][0] - result[0][0]
aggcnt = 0
aggydata = 0
for row in result:
aggcnt += 1
aggydata += row[1]
if aggcnt == aggregate:
self.xdata.append(row[0])
self.ydata.append(aggydata/(granularity*aggregate))
aggcnt = 0
aggydata = 0

def plot(self):
plt.plot(self.xdata, self.ydata, lw=1, ls='-', c='black')
plt.show()


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="performance rolling plot script for xs")
parser.add_argument('db_path', metavar='db_path', type=str, help='path to chiseldb file')
parser.add_argument('perf_name', metavar='perf_name', type=str, help="name of the performance counter")
parser.add_argument('--aggregate', '-A', default=1, type=int, help="aggregation ratio")
args = parser.parse_args()

if args.aggregate <= 0:
print("aggregation ratio must be no less than 1")
sys.exit(1)

db_path = args.db_path
perf_name = args.perf_name
aggregate = args.aggregate

dataset = DataSet(db_path)
dataset.derive(perf_name, aggregate, 0)
dataset.plot()
93 changes: 91 additions & 2 deletions src/main/scala/utils/PerfCounterUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utils
import chipsalliance.rocketchip.config.Parameters
import chisel3._
import chisel3.util._
import utility.ChiselDB
import xiangshan.DebugOptionsKey
import xiangshan._

Expand Down Expand Up @@ -134,6 +135,7 @@ object XSPerfHistogram extends HasRegularPerfName {
}
}
}

object XSPerfMax extends HasRegularPerfName {
def apply(perfName: String, perfCnt: UInt, enable: Bool)(implicit p: Parameters) = {
judgeName(perfName)
Expand Down Expand Up @@ -169,8 +171,7 @@ object QueuePerf {
}
}

object TransactionLatencyCounter
{
object TransactionLatencyCounter {
// count the latency between start signal and stop signal
// whenever stop signals comes, we create a latency sample
def apply(start: Bool, stop: Bool): (Bool, UInt) = {
Expand All @@ -182,6 +183,94 @@ object TransactionLatencyCounter
}
}

object XSPerfRolling extends HasRegularPerfName {

class RollingEntry()(implicit p: Parameters) extends Bundle {
val xAxisPt = UInt(64.W)
val yAxisPt = UInt(64.W)

def apply(xAxisPt: UInt, yAxisPt: UInt): RollingEntry = {
val e = Wire(new RollingEntry())
e.xAxisPt := xAxisPt
e.yAxisPt := yAxisPt
e
}
}

def apply(
perfName: String,
perfCnt: UInt,
granularity: Int,
clock: Clock,
reset: Reset
)(implicit p: Parameters): Unit = {
judgeName(perfName)
val env = p(DebugOptionsKey)
if (env.EnableRollingDB && !env.FPGAPlatform) {
val tableName = perfName + "_rolling_" + p(XSCoreParamsKey).HartId.toString
val rollingTable = ChiselDB.createTable(tableName, new RollingEntry(), basicDB=true)
val logTimestamp = WireInit(0.U(64.W))
val perfClean = WireInit(false.B)
val perfDump = WireInit(false.B)
ExcitingUtils.addSink(logTimestamp, "logTimestamp")
ExcitingUtils.addSink(perfClean, "XSPERF_CLEAN")
ExcitingUtils.addSink(perfDump, "XSPERF_DUMP")

val xAxisCnt = RegInit(0.U(64.W))
val yAxisCnt = RegInit(0.U(64.W))
val xAxisPt = RegInit(0.U(64.W))
xAxisCnt := xAxisCnt + 1.U(64.W) // increment per cycle
yAxisCnt := yAxisCnt + perfCnt

val triggerDB = xAxisCnt === granularity.U
when(triggerDB) {
xAxisCnt := 1.U(64.W)
yAxisCnt := perfCnt
xAxisPt := xAxisPt + granularity.U
}
val rollingPt = new RollingEntry().apply(xAxisPt, yAxisCnt)
rollingTable.log(rollingPt, triggerDB, "", clock, reset)
}
}

def apply(
perfName: String,
perfCnt: UInt,
eventTrigger: UInt,
granularity: Int,
clock: Clock,
reset: Reset
)(implicit p: Parameters) = {
judgeName(perfName)
val env = p(DebugOptionsKey)
if (env.EnableRollingDB && !env.FPGAPlatform) {
val tableName = perfName + "_rolling_" + p(XSCoreParamsKey).HartId.toString
val rollingTable = ChiselDB.createTable(tableName, new RollingEntry(), basicDB=true)
val logTimestamp = WireInit(0.U(64.W))
val perfClean = WireInit(false.B)
val perfDump = WireInit(false.B)
ExcitingUtils.addSink(logTimestamp, "logTimestamp")
ExcitingUtils.addSink(perfClean, "XSPERF_CLEAN")
ExcitingUtils.addSink(perfDump, "XSPERF_DUMP")

val xAxisCnt = RegInit(0.U(64.W))
val yAxisCnt = RegInit(0.U(64.W))
val xAxisPt = RegInit(0.U(64.W))
xAxisCnt := xAxisCnt + eventTrigger // increment when event triggers
yAxisCnt := yAxisCnt + perfCnt

val triggerDB = xAxisCnt === granularity.U
when(triggerDB) {
xAxisCnt := eventTrigger
yAxisCnt := perfCnt
xAxisPt := xAxisPt + granularity.U
}
val rollingPt = new RollingEntry().apply(xAxisPt, yAxisCnt)
rollingTable.log(rollingPt, triggerDB, "", clock, reset)
}
}
}

object XSPerfPrint {
def apply(pable: Printable)(implicit p: Parameters): Any = {
XSLog(XSLogLevel.PERF)(true, true.B, pable)
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/xiangshan/Parameters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ case class DebugOptions
EnableConstantin: Boolean = false,
EnableChiselDB: Boolean = false,
AlwaysBasicDB: Boolean = true,
EnableRollingDB: Boolean = false
)

trait HasXSParameter {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/xiangshan/backend/rob/Rob.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ class RobImp(outer: Rob)(implicit p: Parameters) extends LazyModuleImp(outer)
QueuePerf(RobSize, PopCount((0 until RobSize).map(valid(_))), !allowEnqueue)
XSPerfAccumulate("commitUop", ifCommit(commitCnt))
XSPerfAccumulate("commitInstr", ifCommitReg(trueCommitCnt))
XSPerfRolling("ipc", ifCommitReg(trueCommitCnt), 1000, clock, reset)
val commitIsMove = commitDebugUop.map(_.ctrl.isMove)
XSPerfAccumulate("commitInstrMove", ifCommit(PopCount(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m })))
val commitMoveElim = commitDebugUop.map(_.debugInfo.eliminatedMove)
Expand Down

0 comments on commit ec9e651

Please sign in to comment.