diff --git a/src/hre/hre/perf/Profile.scala b/src/hre/hre/perf/Profile.scala index 450ebd339c..24343932f7 100644 --- a/src/hre/hre/perf/Profile.scala +++ b/src/hre/hre/perf/Profile.scala @@ -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")), @@ -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 diff --git a/src/hre/hre/perf/ResourceUsage.scala b/src/hre/hre/perf/ResourceUsage.scala index 9e6b327ea7..fa3fb6342c 100644 --- a/src/hre/hre/perf/ResourceUsage.scala +++ b/src/hre/hre/perf/ResourceUsage.scala @@ -1,6 +1,7 @@ package hre.perf import hre.perf.ResourceUsage.Microseconds +import hre.platform.Platform import hre.unix.{LibC, RUsage} object ResourceUsage { @@ -8,16 +9,26 @@ object ResourceUsage { 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) } diff --git a/src/hre/hre/platform/Platform.scala b/src/hre/hre/platform/Platform.scala index 4a59955428..1ed04b56f2 100644 --- a/src/hre/hre/platform/Platform.scala +++ b/src/hre/hre/platform/Platform.scala @@ -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 diff --git a/src/hre/hre/progress/TaskRegistry.scala b/src/hre/hre/progress/TaskRegistry.scala index e048d312d3..016f5e403d 100644 --- a/src/hre/hre/progress/TaskRegistry.scala +++ b/src/hre/hre/progress/TaskRegistry.scala @@ -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