Skip to content

Commit

Permalink
Debugger fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
propi committed Sep 14, 2020
1 parent 327fa2c commit 709303a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package com.github.propi.rdfrules.index.ops

import com.github.propi.rdfrules.index.{TripleHashIndex, TripleIndex, TripleItemIndex}
import it.unimi.dsi.fastutil.ints.{Int2ReferenceOpenHashMap, IntOpenHashSet}
import it.unimi.dsi.fastutil.ints.{Int2ReferenceOpenHashMap, IntIterator, IntOpenHashSet}
import it.unimi.dsi.fastutil.objects.ObjectIterator

import scala.collection.JavaConverters._
import scala.language.implicitConversions

/**
* Created by Vaclav Zeman on 13. 3. 2018.
*/
trait Buildable {

implicit private def intToScalaIterator(iterator: IntIterator): Iterator[Int] = new Iterator[Int] {
def hasNext: Boolean = iterator.hasNext

def next(): Int = iterator.nextInt()
}

implicit private def objectToScalaIterator[T](iterator: ObjectIterator[T]): Iterator[T] = new Iterator[T] {
def hasNext: Boolean = iterator.hasNext

def next(): T = iterator.next()
}

protected implicit val indexCollectionBuilder: TripleHashIndex.CollectionsBuilder[Int] = new TripleHashIndex.CollectionsBuilder[Int] {
def emptySet: TripleHashIndex.MutableHashSet[Int] = new TripleHashIndex.MutableHashSet[Int] {
private val hset = new IntOpenHashSet()
Expand All @@ -18,7 +31,7 @@ trait Buildable {

def -=(x: Int): Unit = hset.remove(x)

def iterator: Iterator[Int] = hset.iterator().asScala.asInstanceOf[Iterator[Int]]
def iterator: Iterator[Int] = hset.iterator()

def contains(x: Int): Boolean = hset.contains(x)

Expand Down Expand Up @@ -54,11 +67,11 @@ trait Buildable {

def get(key: Int): Option[V] = Option(hmap.get(key))

def iterator: Iterator[Int] = hmap.keySet().iterator().asScala.asInstanceOf[Iterator[Int]]
def iterator: Iterator[Int] = hmap.keySet().iterator()

def valuesIterator: Iterator[V] = hmap.values().iterator().asScala
def valuesIterator: Iterator[V] = hmap.values().iterator()

def pairIterator: Iterator[(Int, V)] = hmap.int2ReferenceEntrySet().iterator().asScala.map(x => x.getIntKey -> x.getValue)
def pairIterator: Iterator[(Int, V)] = hmap.int2ReferenceEntrySet().iterator().map(x => x.getIntKey -> x.getValue)

def size: Int = hmap.size()

Expand Down
189 changes: 62 additions & 127 deletions core/src/main/scala/com/github/propi/rdfrules/utils/Debugger.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.propi.rdfrules.utils

import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.atomic.AtomicInteger

import com.github.propi.rdfrules.utils.BasicFunctions.round
import com.github.propi.rdfrules.utils.Debugger.ActionDebugger
Expand Down Expand Up @@ -28,21 +28,8 @@ trait Debugger {

object Debugger {

import DebuggerActor.Message

def apply[T](logger: Logger = Logger[Debugger], sequentially: Boolean = false)(f: Debugger => T): T = {
val actor = if (sequentially) {
new DebuggerSeq(logger)
} else {
val actor = new DebuggerActor(logger)
new Thread(actor).start()
actor
}
try {
f(new ActorDebugger(logger, actor))
} finally {
actor ! Message.Stop
}
def apply[T](logger: Logger = Logger[Debugger])(f: Debugger => T): T = {
f(new ActorDebugger(logger))
}

implicit object EmptyDebugger extends Debugger {
Expand All @@ -55,14 +42,28 @@ object Debugger {
def debug[T](name: String, num: Int = 0)(f: ActionDebugger => T): T = f(EmptyActionDebugger)
}

private class ActorDebugger(val logger: Logger, debugger: DebuggerMessageReceiver) extends Debugger {
private class ActorDebugger(val logger: Logger) extends Debugger {
@volatile private var isActive = false

def debug[T](name: String, num: Int = 0)(f: ActionDebugger => T): T = {
val ad = new ActorActionDebugger(name, num, debugger)
debugger ! Message.NewAction(name, num)
try {
f(ad)
} finally {
debugger ! Message.CloseAction(name)
if (isActive) {
f(EmptyActionDebugger)
} else {
val ad = new ActorActionDebugger(name, num)
try {
isActive = true
val actor = new DebuggerActor(logger, ad)
new Thread(actor).start()
logger.info(ad.takeSnapshot.toString + " -- started")
try {
f(ad)
} finally {
actor.stop()
}
} finally {
logger.info(ad.takeSnapshot.toString + " -- ended")
isActive = false
}
}
}
}
Expand All @@ -77,131 +78,65 @@ object Debugger {
}
}

private class ActorActionDebugger(name: String, num: Int, debugger: DebuggerMessageReceiver) extends ActionDebugger {
def done(msg: String = ""): Unit = debugger ! Message.Debug(name, msg)
}

private object EmptyActionDebugger extends ActionDebugger {
def done(msg: String = ""): Unit = {}
}
private class ActorActionDebugger(name: String, num: Int) extends ActionDebugger {
private val currentNum = new AtomicInteger(0)
@volatile private var _currentMessage = ""

private trait DebuggerMessageReceiver {
protected val logger: Logger
private val debugClock: Long = (5 seconds).toMillis
private var currentAction: Option[Action] = None
private var lastDump = System.currentTimeMillis()
private var lastNum = 0
case class Snapshot(absoluteProgress: Int, currentMessage: String) {
def hasProgressBar: Boolean = num > 0

private class Action(val name: String, maxNum: Int) {

private var num = 0
private var _state = ""

def ++ : Action = {
num += 1
this
}

def state: String = _state

def state_=(value: String): Unit = _state = value

def absoluteProgress: Int = num

def relativeProgress: Double = if (maxNum > 0) num.toDouble / maxNum else 0.0

def hasProgressBar: Boolean = maxNum > 0
def relativeProgress: Double = if (hasProgressBar) absoluteProgress.toDouble / num else 0.0

override def toString: String = if (hasProgressBar) {
s"Action $name, steps: $num of $maxNum, progress: ${(relativeProgress * 100).floor}%"
s"Action $name, steps: $absoluteProgress of $num, progress: ${(relativeProgress * 100).floor}%"
} else {
s"Action $name, steps: $num"
}

}

private def dump(action: Action, msg: String, started: Boolean, ended: Boolean): Unit = {
action.state = msg
val isBorder = started || ended
if (lastDump + debugClock < System.currentTimeMillis() || isBorder) {
val rating = if (!isBorder) {
val windowTime = System.currentTimeMillis() - lastDump
val windowNum = action.absoluteProgress - lastNum
s" (${round((windowNum.toDouble / windowTime) * 1000, 2)} per sec)"
} else ""
logger.info(action.toString + rating + (if (action.state.nonEmpty) " -- " + action.state else ""))
lastDump = System.currentTimeMillis()
lastNum = action.absoluteProgress
s"Action $name, steps: $absoluteProgress"
}
}

protected def stop(): Unit
def takeSnapshot: Snapshot = Snapshot(currentNum.get(), _currentMessage)

protected def processMessage(message: Message): Unit = {
message match {
case Message.Debug(name, msg) =>
for (action <- currentAction if action.name == name) {
action.++
dump(action, msg, false, false)
}
case Message.NewAction(name, num) =>
if (currentAction.isEmpty) {
val action = new Action(name, num)
currentAction = Some(action)
lastNum = 0
dump(action, "started", true, false)
}
case Message.CloseAction(name) =>
for (action <- currentAction if action.name == name) {
dump(action, "ended", false, true)
currentAction = None
}
case Message.Stop => stop()
}
def done(msg: String = ""): Unit = {
currentNum.incrementAndGet()
_currentMessage = msg
}
}

def !(message: Message): Unit
private object EmptyActionDebugger extends ActionDebugger {
def done(msg: String = ""): Unit = {}
}

private class DebuggerActor(protected val logger: Logger) extends Runnable with DebuggerMessageReceiver {
private val messages = new LinkedBlockingQueue[Message]
private var stopped = false
private class DebuggerActor(logger: Logger, action: ActorActionDebugger) extends Runnable {
private val debugClock: Long = (5 seconds).toMillis
@volatile private var stopped = false
private var lastDump = System.currentTimeMillis()
private var lastNum = 0

private def dump(): Unit = {
val snapshot = action.takeSnapshot
if (lastNum != snapshot.absoluteProgress) {
val windowTime = System.currentTimeMillis() - lastDump
val windowNum = snapshot.absoluteProgress - lastNum
val rating = s"(${round((windowNum.toDouble / windowTime) * 1000, 2)} per sec)"
logger.info(s"$snapshot $rating" + (if (snapshot.currentMessage.nonEmpty) " -- " + snapshot.currentMessage else ""))
}
lastDump = System.currentTimeMillis()
lastNum = snapshot.absoluteProgress
}

protected def stop(): Unit = {
def stop(): Unit = {
stopped = true
synchronized(notify())
}

def run(): Unit = {
while (!stopped) {
processMessage(messages.take())
synchronized(wait(debugClock))
if (!stopped) {
dump()
}
}
}

def !(message: Message): Unit = messages.put(message)
}

private class DebuggerSeq(protected val logger: Logger) extends DebuggerMessageReceiver {
protected def stop(): Unit = {}

def !(message: Message): Unit = processMessage(message)
}

private object DebuggerActor {

sealed trait Message

object Message {

case class NewAction(name: String, num: Int) extends Message

case class Debug(name: String, msg: String = "") extends Message

case class CloseAction(name: String) extends Message

case object Stop extends Message

}

}

}
Binary file modified dist/lib/core_2.12-1.0.0.jar
Binary file not shown.
Binary file modified dist/lib/http_2.12-1.0.0.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions dist/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
<head>
<meta charset="UTF-8">
<title>RDFRules</title>
<link rel='stylesheet' href='css/home.css?v=1.4.1' type='text/css'/>
<link rel='stylesheet' href='css/home.css?v=1.4.2' type='text/css'/>
</head>
<body>
<header>
<h1>RDFRules</h1>
<div class="version">v1.4.1</div>
<div class="version">v1.4.2</div>
</header>
<main id="rdfrules"></main>
<script>
var endpoint = "http://localhost:8851/api";
</script>
<script src="js/tools.js"></script>
<script src="js/FileSaver.min.js"></script>
<script src="js/gui-opt.js?v=1.4.1"></script>
<script src="js/gui-opt.js?v=1.4.2"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion gui/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<header>
<h1>RDFRules</h1>
<div class="version">v1.4.1</div>
<div class="version">v1.4.2</div>
</header>
<main id="rdfrules"></main>
<script>
Expand Down

0 comments on commit 709303a

Please sign in to comment.