Skip to content

Commit

Permalink
Merge pull request #997 from utwente-fmt/issue-995
Browse files Browse the repository at this point in the history
add resourceusage fallback, used immediately in non-unix
  • Loading branch information
pieter-bos authored Mar 29, 2023
2 parents 0e59bfc + 6b9f261 commit 575b26f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/hre/hre/perf/Profile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ case class Profile() {

private val epochStartNanos = System.currentTimeMillis() * 1_000_000L

private var lastChildUsage = ResourceUsage.getAggregateChildren.get
private var lastChildUsage = ResourceUsage.getAggregateChildren

private val valueTypes = Seq(
ValueType(str("agg"), str("microseconds")),
Expand All @@ -44,7 +44,7 @@ case class Profile() {

def update(stack: Seq[String], ownUsage: ResourceUsage, doUpdateChildUsage: Boolean): Unit = synchronized {
val deltaChild = if (doUpdateChildUsage) {
val childUsage = ResourceUsage.getAggregateChildren.get
val childUsage = ResourceUsage.getAggregateChildren
val deltaChild = childUsage - lastChildUsage
lastChildUsage = childUsage
deltaChild
Expand Down
25 changes: 18 additions & 7 deletions src/hre/hre/perf/ResourceUsage.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
package hre.perf

import hre.perf.ResourceUsage.Microseconds
import hre.platform.Platform
import hre.unix.{LibC, RUsage}

object ResourceUsage {
type Microseconds = Long

private val boot = System.nanoTime() / 1000L

private def get(who: Int): Option[ResourceUsage] = {
val usage = new RUsage()
if(LibC.INSTANCE.getrusage(who, usage) != 0) return None
def fallback(): ResourceUsage =
zero.copy(wallTime = System.nanoTime() / 1000L - boot)

Some(ResourceUsage(usage.ru_utime.toUsec, usage.ru_stime.toUsec, usage.ru_inblock, usage.ru_oublock, usage.ru_nvcsw, usage.ru_nivcsw, System.nanoTime() / 1000L - boot))
private def get(who: Int): ResourceUsage = {
Platform.getCurrent match {
case Platform.Unix | Platform.Mac =>
try {
val usage = new RUsage()
if (LibC.INSTANCE.getrusage(who, usage) != 0) return fallback()
ResourceUsage(usage.ru_utime.toUsec, usage.ru_stime.toUsec, usage.ru_inblock, usage.ru_oublock, usage.ru_nvcsw, usage.ru_nivcsw, System.nanoTime() / 1000L - boot)
} catch {
case _: UnsatisfiedLinkError => fallback()
}
case _ => fallback()
}
}

def getProcess: Option[ResourceUsage] = get(0)
def getCallingThread: Option[ResourceUsage] = get(1)
def getAggregateChildren: Option[ResourceUsage] = get(-1)
def getProcess: ResourceUsage = get(0)
def getCallingThread: ResourceUsage = get(1)
def getAggregateChildren: ResourceUsage = get(-1)

def zero: ResourceUsage = ResourceUsage(0, 0, 0, 0, 0, 0, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/hre/hre/platform/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ case object Platform {
override def code: String = "unknownPlatform"
}

def getCurrent: Platform = {
val getCurrent: Platform = {
val name = System.getProperty("os.name").toLowerCase
if(name.contains("win")) Platform.Windows
else if(name.contains("linux")) Platform.Unix
Expand Down
4 changes: 2 additions & 2 deletions src/hre/hre/progress/TaskRegistry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ case object TaskRegistry {

def ownUsage(): ResourceUsage =
if(enabled) {
if(isMainThread) ResourceUsage.getProcess.get
else ResourceUsage.getCallingThread.getOrElse(ResourceUsage.zero)
if(isMainThread) ResourceUsage.getProcess
else ResourceUsage.getCallingThread
}
else ResourceUsage.zero

Expand Down

0 comments on commit 575b26f

Please sign in to comment.