From d561199300427758d17b5ff3c002ca20e4cce384 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Wed, 29 Jan 2014 19:53:26 -0600 Subject: [PATCH] Added some logic to limit the number of heavy threads produced and used by the concurrent engine. --- .../com/sothr/imagetools/ConcurrentEngine.scala | 16 ++++++++++++++-- src/main/scala/com/sothr/imagetools/Engine.scala | 3 --- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/scala/com/sothr/imagetools/ConcurrentEngine.scala b/src/main/scala/com/sothr/imagetools/ConcurrentEngine.scala index f8c5527..3936296 100644 --- a/src/main/scala/com/sothr/imagetools/ConcurrentEngine.scala +++ b/src/main/scala/com/sothr/imagetools/ConcurrentEngine.scala @@ -109,7 +109,13 @@ case object EngineActorReactivate class ConcurrentEngineProcessingController extends Actor with ActorLogging { val imageCache = AppConfig.cacheManager.getCache("images") - val numOfRouters = PropertiesService.get(PropertiesEnum.ConcurrentProcessingLimit.toString).toInt + val numOfRouters = { + val max = PropertiesService.get(PropertiesEnum.ConcurrentProcessingLimit.toString).toInt + val processors = Runtime.getRuntime.availableProcessors() + var threads = 0 + if (processors > max) threads = max else if (processors > 1) threads = processors - 1 else threads = 1 + threads + } val router = context.actorOf(Props[ConcurrentEngineProcessingActor].withRouter(SmallestMailboxRouter(nrOfInstances = numOfRouters))) var images:mutable.MutableList[Image] = new mutable.MutableList[Image]() @@ -228,7 +234,13 @@ case object EngineActorCompareImagesFinished class ConcurrentEngineSimilarityController extends Actor with ActorLogging { val imageCache = AppConfig.cacheManager.getCache("images") - val numOfRouters = PropertiesService.get(PropertiesEnum.ConcurrentSimiliartyLimit.toString).toInt + val numOfRouters = { + val max = PropertiesService.get(PropertiesEnum.ConcurrentSimiliartyLimit.toString).toInt + val processors = Runtime.getRuntime.availableProcessors() + var threads = 0 + if (processors > max) threads = max else if (processors > 1) threads = processors - 1 else threads = 1 + threads + } val router = context.actorOf(Props[ConcurrentEngineSimilarityActor].withRouter(SmallestMailboxRouter(nrOfInstances = numOfRouters))) val allSimilarImages = new mutable.MutableList[SimilarImages] diff --git a/src/main/scala/com/sothr/imagetools/Engine.scala b/src/main/scala/com/sothr/imagetools/Engine.scala index aaeaa8a..6edff72 100644 --- a/src/main/scala/com/sothr/imagetools/Engine.scala +++ b/src/main/scala/com/sothr/imagetools/Engine.scala @@ -1,10 +1,7 @@ package com.sothr.imagetools import com.sothr.imagetools.image.{SimilarImages, ImageFilter, Image} -import scala.collection.mutable -import java.io.File import grizzled.slf4j.Logging -import net.sf.ehcache.Element /** * Created by drew on 1/26/14.