Commit 9a14926f authored by Kunshan Wang's avatar Kunshan Wang

Added two comminsts for printing statistics.

parent 0382ec47
......@@ -881,6 +881,10 @@ struct MuIRBuilder {
#define MU_CI_UVM_IRBUILDER_NEW_COMMINST ((MuCommInst)0x34f) /// MUAPIPARSER muname:@uvm.irbuilder.new_comminst
/// GEN:END:COMMINSTS
// Proprietary CommInsts specific to the refimpl
#define MU_CI_UVM_IRBUILDER_NEW_COMMINST ((MuCommInst)0xc001) /// MUAPIPARSER muname:@uvm.ext.print_stats
#define MU_CI_UVM_IRBUILDER_NEW_COMMINST ((MuCommInst)0xc002) /// MUAPIPARSER muname:@uvm.ext.clear_stats
#ifdef __cplusplus
}
#endif
......
......@@ -64,6 +64,10 @@ object CommInsts extends SimpleNamespace[CommInst] {
commInst(0x25f, "@uvm.meta.disable_watchPoint")
commInst(0x260, "@uvm.meta.set_trap_handler")
// Proprietary extensions specific to this refimpl
commInst(0xc001, "@uvm.ext.print_stats")
commInst(0xc002, "@uvm.ext.clear_stats")
// Common instructions for IR builder API here:
/// GEN:BEGIN:IRBUILDER_COMMINSTS
......
......@@ -236,7 +236,7 @@ class MicroVM private (val vmConf: VMConf, val allArgs: Option[Seq[String]]) ext
threadStackManager.execute()
MicroVM.logger.info("Execution statistics:")
MicroVM.logger.info(stats.getStatesString())
MicroVM.logger.info(stats.getStatsString(0))
}
// Automatically load the boot image if provided
......
......@@ -363,6 +363,21 @@ trait CommInstExecutor extends InterpreterActions with ObjectPinner with IRBuild
case "@uvm.meta.disable_watchPoint" => ???
case "@uvm.meta.set_trap_handler" => ???
case "@uvm.ext.print_stats" => {
val Seq(vUserNum) = argList
val userNum = vUserNum.asSInt64.toLong
microVM.stats.printStats(userNum)
continueNormally()
}
case "@uvm.ext.clear_stats" => {
microVM.stats.clearStats()
continueNormally()
}
// Insert more CommInsts here.
......
......@@ -3,7 +3,46 @@ package uvm.refimpl
import scala.collection.mutable.HashMap
import uvm.ssavariables.Instruction
object IndentableStringBuilder {
def apply(indentSize: Int) = new IndentableStringBuilder(indentSize)
def apply() = new IndentableStringBuilder(2)
}
class IndentableStringBuilder private (indentSize: Int) {
val sb = new StringBuilder()
var curIndent = 0
def indent(): IndentableStringBuilder = {
curIndent += indentSize
this
}
def dedent(): IndentableStringBuilder = {
curIndent -= indentSize
this
}
def addLine(line: String): IndentableStringBuilder = {
for (_ <- 0 until curIndent) {
sb += ' '
}
sb ++= line += '\n'
this
}
def blockIndent(f: => Unit): Unit = {
indent()
f
dedent()
}
override def toString() = sb.toString()
}
class VMStats {
var printCount: Long = 0L
var exceptionsThrown: Long = 0L
var peiCalled: Long = 0L
var allocaExecuted: Long = 0L
......@@ -11,44 +50,65 @@ class VMStats {
val heapAllocSizeMap: HashMap[Long, Long] = new HashMap()
val instExecCount: HashMap[String, Long] = new HashMap()
def onInstExec(i: String): Unit = {
val oldCount = instExecCount.getOrElse(i, 0L)
val newCount = oldCount + 1
instExecCount.update(i, newCount)
}
def onHeapAlloc(sz: Long): Unit = {
bytesAllocatedHeap += sz
val oldCount = heapAllocSizeMap.getOrElse(sz, 0L)
val newCount = oldCount + 1
heapAllocSizeMap.update(sz, newCount)
}
def getStatesString(): String = {
val sb = new StringBuilder()
sb ++= "Exceptions thrown: %d\n".format(exceptionsThrown)
sb ++= "PEI called: %d\n".format(peiCalled)
sb ++= "ALLOCA and ALLOCAHYBRID executed: %d\n".format(peiCalled)
def getStatsString(userNum: Long): String = {
printCount += 1
val sb = IndentableStringBuilder()
sb ++= "Instructions executed:\n"
for ((i, c) <- instExecCount.toSeq.sortBy(_._2).reverse) {
sb ++= " %s: %d\n".format(i, c)
}
sb addLine s"======== VM Statistics Begin | printCount=${printCount} userNum=${userNum}"
sb ++= "Bytes allocated on the heap (only small obj space): %d\n".format(bytesAllocatedHeap)
sb ++= "Heap object size : allocation count:\n"
for ((i, c) <- heapAllocSizeMap.toSeq.sortBy(_._1)) {
sb ++= " %d: %d\n".format(i, c)
sb blockIndent {
sb addLine "Exceptions thrown: %d".format(exceptionsThrown)
sb addLine "PEI called: %d".format(peiCalled)
sb addLine "ALLOCA and ALLOCAHYBRID executed: %d".format(allocaExecuted)
sb addLine "Instructions executed:"
sb blockIndent {
for ((i, c) <- instExecCount.toSeq.sortBy(_._2).reverse) {
sb addLine "%s: %d".format(i, c)
}
}
sb addLine "Bytes allocated on the heap (only small obj space): %d".format(bytesAllocatedHeap)
sb addLine "Heap object size : allocation count:"
sb blockIndent {
for ((i, c) <- heapAllocSizeMap.toSeq.sortBy(_._1)) {
sb addLine "%d: %d".format(i, c)
}
}
}
sb addLine s"======== VM Statistics End | printCount=${printCount} userNum=${userNum}"
sb.toString()
}
def printState(): Unit = {
print(getStatesString())
def printStats(userNum: Long): Unit = {
print(getStatsString(userNum))
}
def clearStats(): Unit = {
exceptionsThrown = 0L
peiCalled = 0L
allocaExecuted = 0L
bytesAllocatedHeap = 0L
heapAllocSizeMap.clear()
instExecCount.clear()
}
}
\ No newline at end of file
package uvm.refimpl.itpr
import org.scalatest._
import ch.qos.logback.classic.Level._
import uvm._
import uvm.refimpl._
import uvm.refimpl.UvmBundleTesterBase
import uvm.refimpl.itpr._
import uvm.ssavariables._
import uvm.types._
class UvmInterpreterProprietaryTests extends UvmBundleTesterBase {
setLogLevels(
ROOT_LOGGER_NAME -> INFO,
"uvm.refimpl.itpr" -> DEBUG)
preloadBundles("tests/uvm-refimpl-test/primitives.uir",
"tests/uvm-refimpl-test/proprietary-tests.uir")
"The micro VM" should "print statistics when requested" in {
val ctx = microVM.newContext()
val func = ctx.handleFromFunc("@stats_test")
val arg1 = ctx.handleFromInt(1, 64)
val arg2 = ctx.handleFromInt(2, 64)
testFunc(ctx, func, Seq(arg1, arg2)) { (ctx, th, st, wp) =>
returnFromTrap(st)
}
ctx.closeContext()
}
}
\ No newline at end of file
// require "primitives.uir"
.funcsig @stats_test.sig = (@i64 @i64) -> ()
.funcdef @stats_test VERSION %v1 <@stats_test.sig> {
%entry (<@i64> %a <@i64> %b):
%c = ADD <@i64> %a %b
COMMINST @uvm.ext.print_stats (@I64_5)
COMMINST @uvm.ext.clear_stats
COMMINST @uvm.ext.print_stats (@I64_6)
COMMINST @uvm.thread_exit
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment