From 460ecda3f1a1ca47ff99a8b196b836ddc3112ad4 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Mon, 1 Dec 2025 16:55:56 +0100 Subject: debug: checkpoint for attempt for out of bounds exception --- src/main/scala/main/Main.scala | 12 ++++++ src/main/scala/main/domain/MemoryOperations.scala | 41 ++++++++++++++---- src/main/scala/main/domain/ResourceModels.scala | 1 + src/main/scala/main/infrastructure/Http.scala | 1 - src/main/scala/main/infrastructure/Resources.scala | 50 ++++++++++++++++------ src/main/scala/main/services/Stress.scala | 30 ++++++++----- src/main/scala/main/view/View.scala | 46 +++++++++++++------- 7 files changed, 132 insertions(+), 49 deletions(-) (limited to 'src/main') diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala index 05770a9..b22e0f7 100644 --- a/src/main/scala/main/Main.scala +++ b/src/main/scala/main/Main.scala @@ -27,4 +27,16 @@ object Torque extends ZIOAppDefault with Runner { v.serveView } + + /** + * This method runs after the UI finishes which means that the stress tests also finished + * Which allows us to post the gathered data to the API + * */ + + def runHttp: Unit = { + + + + } + } diff --git a/src/main/scala/main/domain/MemoryOperations.scala b/src/main/scala/main/domain/MemoryOperations.scala index 5c58996..c0b1cbf 100644 --- a/src/main/scala/main/domain/MemoryOperations.scala +++ b/src/main/scala/main/domain/MemoryOperations.scala @@ -2,7 +2,7 @@ package main.domain import main.services._ import scala.util.hashing -import scala.util.hashing.MurmurHash3 +import scala.util.hashing.MurmurHash3 import scala.collection.immutable.ListSet import scala.math._ import java.lang.foreign._ @@ -19,22 +19,45 @@ import scala.util.Using * https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/package-summary.html * */ +case class Memory (arena: Arena, memorySegment: MemorySegment) + + class MemoryAllocater { - private val address: String = null - private val arena: Arena = Arena.global() - // TODO: get user to define the size to allocate - private val memorySegment = arena.allocate(1024 * 1024 * 1024) + + private var arena: Arena = _ + private var memorySegment: MemorySegment = _ /** * adding an offset so that the next piece of memory wont be overlapping with the one we just assigned * */ - def setValues(values: List[Int], offset: Int): Unit = for i <- values.indices do memorySegment.set(ValueLayout.JAVA_INT, offset * i, values(i)) + def createMemorySegment: Memory = Memory ( arena = Arena.ofConfined(), memorySegment = arena.allocate(1024 * 1024 * 1024)) + + def setValues(values: List[Int], baseOffset: Int): Unit = { + + val stride = Integer.BYTES.toLong + val start = baseOffset.toLong + + for (i <- values.indices) { + val addr = start + (i * stride) + memorySegment.set(ValueLayout.JAVA_INT, addr, values(i)) + } + + } + + def getValues(offset: Int, len: Int): List[Int] = { + + val stride = Integer.BYTES.toLong + val start = offset.toLong + (0 until len).map { i => + val addr = start + (i * stride) + memorySegment.get(ValueLayout.JAVA_INT, addr) + }.toList + } - def getValues(offset: Int): Unit = (0 until 10).map(i => memorySegment.get(ValueLayout.JAVA_INT, offset * i)) - def deallocateMemory: Unit = arena.close() + def deallocateMemory: Unit = if arena != null then arena.close() - def run(): Unit = println("address: " + memorySegment.address()) + def run(): Unit = println("address: " + memorySegment.address()) } diff --git a/src/main/scala/main/domain/ResourceModels.scala b/src/main/scala/main/domain/ResourceModels.scala index d473fd6..d0e4f10 100644 --- a/src/main/scala/main/domain/ResourceModels.scala +++ b/src/main/scala/main/domain/ResourceModels.scala @@ -9,6 +9,7 @@ package main.domain enum Status: case PASS + case RUNNING case FAIL case class CpuInfo(load: Double, temperature: Double, cores: Int, physicalCores: Int, status: Status) diff --git a/src/main/scala/main/infrastructure/Http.scala b/src/main/scala/main/infrastructure/Http.scala index 2f63452..01103d7 100644 --- a/src/main/scala/main/infrastructure/Http.scala +++ b/src/main/scala/main/infrastructure/Http.scala @@ -14,7 +14,6 @@ import main.domain._ def httpHandler(cpu: CpuInfo, ram: RamInfo, platform: PlatformInfo): Unit = { - Server.serve(routes) .provide(Server.default) .exitCode diff --git a/src/main/scala/main/infrastructure/Resources.scala b/src/main/scala/main/infrastructure/Resources.scala index 10b0466..c32d9e3 100644 --- a/src/main/scala/main/infrastructure/Resources.scala +++ b/src/main/scala/main/infrastructure/Resources.scala @@ -20,43 +20,67 @@ class Resources { /** * Platform Mehtods * */ - def getPlatform: ZIO[Any, Throwable, Unit] = { + def getPlatform: Task[PlatformInfo] = { ZIO.attempt { - println(sysInfo.getHardware) - }.catchAll { error => Console.printError(s"failed :$error")} + /** + * Pass the OS version to the DTO to later transfer it over the API + **/ + PlatformInfo ( currentPlatform = sysInfo.getHardware.toString ) + } } /** * CPU methods * */ - def getCpuInfo: ZIO[Any, Throwable, Unit] = { + def getCpuInfo: Task[CpuInfo] = { ZIO.attempt { /* * 227 oshi/hardware/CentralProcessor.java * method takes long value as delay * */ - println("load: " + cpu.getSystemCpuLoad(1000) * 1000) - println("logical cores: " + cpu.getLogicalProcessorCount()) - println("cores: " + cpu.getPhysicalProcessorCount()) - println("temperature: " + sensors.getCpuTemperature()) + CpuInfo ( + load = cpu.getSystemCpuLoad(1000) * 1000, + temperature = sensors.getCpuTemperature(), + cores = cpu.getLogicalProcessorCount(), + physicalCores = cpu.getPhysicalProcessorCount() , - // TODO: assign the information in here to a method + /** + * I will propably call this method when i'm about to run + * a stress test so it's safe to set it to RUNNING + * */ + status = Status.RUNNING + ) } } + + /** + * + * When CPU's can't handle theyre current clock they often drop + * clock speed + * This runs a check to see if it's stable in that aspect + * */ + // TODO: complete this together with the safety check + // def compareCpuFrequencyToMax: ZIO[Any, Throwable, Boolean] = if cpu.getMaxFreq() > cpu.getCurrentFreq() then false else true + // def compareCpuVolege: ZIO[Any, Throwable, Boolean] = if sensors.getCpuVoltage() + /** * Memory specific methods * */ - def getRamInfo: ZIO[Any, Throwable, Unit] = { + def getRamInfo: Task[RamInfo] = { ZIO.attempt { - memory.getTotal() val totalMemory = memory.getTotal() - println("total memory" +totalMemory) - }.catchAll { error => Console.printError(s"failed: $error")} + + RamInfo ( + usage = totalMemory - memory.getAvailable().toDouble, + total = totalMemory, + status = Status.RUNNING + ) + } } } diff --git a/src/main/scala/main/services/Stress.scala b/src/main/scala/main/services/Stress.scala index a4349f5..dd74c9a 100644 --- a/src/main/scala/main/services/Stress.scala +++ b/src/main/scala/main/services/Stress.scala @@ -33,7 +33,8 @@ class StressCpu() extends Stress { val systemInfo = new SystemInfo val sensors = systemInfo.getHardware.getSensors ZIO.attempt { - while (true) do if sensors.getCpuTemperature > 80 then println("overheat") + while (true) do + if sensors.getCpuTemperature > 80 then println("overheat") }.catchAll { error => Console.printError(s"failed: $error") } } @@ -83,8 +84,11 @@ class StressRam() extends Stress { * The memory methods have a Unit return type so we wrap them in a * ZIO attempt which returns a [R, E, A] * */ - val r = ZIO.attempt { memoryAllocater.setValues(list, offset) } *> ZIO.attempt { memoryAllocater.deallocateMemory } - r.repeat(Schedule.forever.unit) + val r = ZIO.attempt { + memoryAllocater.createMemorySegment + memoryAllocater.setValues(list, offset) } *> + ZIO.attempt { memoryAllocater.deallocateMemory } + r.repeat(Schedule.forever).unit } @@ -106,8 +110,12 @@ class StressRam() extends Stress { * The memory methods have a Unit return type so we wrap them in a * ZIO attempt which returns a [R, E, A] * */ - var r = ZIO.attempt { memoryAllocater.setValues(list, offset) } *> ZIO.attempt { memoryAllocater.deallocateMemory } - r.repeat(Schedule.forever.unit) + var r = ZIO.attempt { + + memoryAllocater.createMemorySegment + memoryAllocater.setValues(list, offset) } *> + ZIO.attempt { memoryAllocater.deallocateMemory } + r.repeat(Schedule.forever).unit } @@ -120,14 +128,14 @@ trait Runner { def heavyCpuRun: Task[Unit] = { - for { + for { - par <- c.runParallel.fork - seq <- c.runSequential.fork - _ <- par.join - _ <- seq.join + par <- c.runParallel.fork + seq <- c.runSequential.fork + _ <- par.join + _ <- seq.join - } yield () + } yield () } def lightCpuRun: Task[Unit] = { diff --git a/src/main/scala/main/view/View.scala b/src/main/scala/main/view/View.scala index 5213865..3e60bf8 100644 --- a/src/main/scala/main/view/View.scala +++ b/src/main/scala/main/view/View.scala @@ -4,6 +4,7 @@ import zio._ import main.infrastructure._ import main.services._ import java.lang.System +import oshi._ /** * @@ -45,12 +46,12 @@ class View extends Runner { println( """ - _/ |_ ___________ ________ __ ____ - \ __\/ _ \_ __ \/ ____/ | \_/ __ \ + _/ |_ ___________ ________ __ ____ + \ __\/ _ \_ __ \/ ____/ | \_/ __ \ | | ( <_> ) | \< <_| | | /\ ___/ |__| \____/|__| \__ |____/ \___ > - |__| \/ - + |__| \/ + |========== Welcome to Torque ==========| | Cpu and Ram Stress testing tool | | in Scala | @@ -58,7 +59,7 @@ class View extends Runner { |=======================================| """ ) - pressToContinue + pressToContinue } /** @@ -74,6 +75,22 @@ class View extends Runner { } } + def resourcesView: ZIO[Any, Throwable, Unit] = { + val sysInfo: SystemInfo = new SystemInfo + val hardware = sysInfo.getHardware + val memory = hardware.getMemory + val sensors = hardware.getSensors + val cpu = hardware.getProcessor + + println("load: " + cpu.getSystemCpuLoad(1000) * 1000) + println("logical cores: " + cpu.getLogicalProcessorCount()) + println("cores: " + cpu.getPhysicalProcessorCount()) + println("temperature: " + sensors.getCpuTemperature()) + + + } + + def menu: ZIO[Any, Throwable, Unit] = { var continue = true @@ -86,12 +103,12 @@ class View extends Runner { println( """ - _/ |_ ___________ ________ __ ____ - \ __\/ _ \_ __ \/ ____/ | \_/ __ \ + _/ |_ ___________ ________ __ ____ + \ __\/ _ \_ __ \/ ____/ | \_/ __ \ | | ( <_> ) | \< <_| | | /\ ___/ |__| \____/|__| \__ |____/ \___ > - |__| \/ - + |__| \/ + |======= Select the stress test ========| | 1: light Cpu | | 2: Heavy Cpu | @@ -99,7 +116,7 @@ class View extends Runner { | 4: Heavy Ram | | 5: Exit | |=======================================| - + """ ) target = scala.io.StdIn.readLine() @@ -109,11 +126,10 @@ class View extends Runner { clearScreen toInt(target) match { - case 1 => lightCpuRun <&> resources.getCpuInfo - case 2 => heavyCpuRun <&> resources.getCpuInfo - case 3 => lightCpuRun <&> resources.getRamInfo - case 4 => heavyCpuRun <&> resources.getRamInfo - + case 1 => lightCpuRun <&> ZIO.attempt { resourcesView } + case 2 => heavyCpuRun + case 3 => lightCpuRun + case 4 => heavyCpuRun } } -- cgit v1.2.3-70-g09d2