diff options
author | Paul Phillips <paulp@improving.org> | 2009-08-24 01:07:45 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-08-24 01:07:45 +0000 |
commit | db8c41b53586961f10c2ba3a48ad14fa622d507f (patch) | |
tree | d3bb02c75e0e4eb3b63e6a1c97450c8e23d818c1 /src/compiler/scala/tools/nsc | |
parent | 0f7296a008e0e4090e69b489c23a492df26fd4f6 (diff) | |
download | scala-db8c41b53586961f10c2ba3a48ad14fa622d507f.tar.gz scala-db8c41b53586961f10c2ba3a48ad14fa622d507f.tar.bz2 scala-db8c41b53586961f10c2ba3a48ad14fa622d507f.zip |
Taking a little more advantage of some recent a...
Taking a little more advantage of some recent abstractions.
Diffstat (limited to 'src/compiler/scala/tools/nsc')
-rw-r--r-- | src/compiler/scala/tools/nsc/CompileServer.scala | 50 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/CompileSocket.scala | 48 |
2 files changed, 49 insertions, 49 deletions
diff --git a/src/compiler/scala/tools/nsc/CompileServer.scala b/src/compiler/scala/tools/nsc/CompileServer.scala index 010d183bc8..7cd4be214a 100644 --- a/src/compiler/scala/tools/nsc/CompileServer.scala +++ b/src/compiler/scala/tools/nsc/CompileServer.scala @@ -6,10 +6,9 @@ package scala.tools.nsc -import java.io.{BufferedOutputStream, File, FileOutputStream, PrintStream} -import java.lang.{Runtime, System, Thread} +import java.io.{ BufferedOutputStream, FileOutputStream, PrintStream, File => JFile } +import scala.io.File -import scala.concurrent.ops.spawn import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} import scala.tools.nsc.util.FakePos //Position import scala.tools.util.SocketServer @@ -22,7 +21,8 @@ import scala.tools.util.SocketServer * @author Martin Odersky * @version 1.0 */ -class StandardCompileServer extends SocketServer { +class StandardCompileServer extends SocketServer +{ def compileSocket: CompileSocket = CompileSocket // todo: make this a lazy val val versionMsg = "Fast Scala compiler " + @@ -44,6 +44,7 @@ class StandardCompileServer extends SocketServer { } private val runtime = Runtime.getRuntime() + import runtime.{ totalMemory, freeMemory, maxMemory } var reporter: ConsoleReporter = _ @@ -54,24 +55,31 @@ class StandardCompileServer extends SocketServer { } override def timeout() { - if (!compileSocket.portFile(port).exists()) + if (!compileSocket.portFile(port).file.exists()) fatal("port file no longer exists; skipping cleanup") } + def printMemoryStats() { + System.out.println("New session, total memory = %s, max memory = %s, free memory = %s".format( + totalMemory, maxMemory, freeMemory)) + System.out.flush() + } + + def isMemoryFullEnough() = { + runtime.gc() + (totalMemory - freeMemory).toDouble / maxMemory.toDouble > MaxCharge + } + protected def newOfflineCompilerCommand( arguments: List[String], settings: Settings, error: String => Unit, - interactive: Boolean) - = new OfflineCompilerCommand(arguments, settings, error, interactive) + interactive: Boolean + ) = new OfflineCompilerCommand(arguments, settings, error, interactive) def session() { - System.out.println("New session" + - ", total memory = "+ runtime.totalMemory() + - ", max memory = " + runtime.maxMemory() + - ", free memory = " + runtime.freeMemory) - System.out.flush() - val password = compileSocket.getPassword(port) + printMemoryStats() + val password = compileSocket getPassword port val guessedPassword = in.readLine() val input = in.readLine() if ((input ne null) && password == guessedPassword) { @@ -127,24 +135,18 @@ class StandardCompileServer extends SocketServer { shutDown = true } reporter.printSummary() - runtime.gc() - if ((runtime.totalMemory() - runtime.freeMemory()).toDouble / - runtime.maxMemory().toDouble > MaxCharge) compiler = null + if (isMemoryFullEnough) + compiler = null } } } /** A directory holding redirected output */ - private val redirectDir = new File(compileSocket.tmpDir, "output-redirects") + private val redirectDir = File(compileSocket.tmpDir) / "output-redirects" redirectDir.mkdirs - private def redirect(setter: PrintStream => Unit, filename: String) { - setter( - new PrintStream( - new BufferedOutputStream( - new FileOutputStream( - new File(redirectDir, filename))))) - } + private def redirect(setter: PrintStream => Unit, filename: String): Unit = + setter(new PrintStream(redirectDir / filename bufferedOutput())) def main(args: Array[String]) { redirect(System.setOut, "scala-compile-server-out.log") diff --git a/src/compiler/scala/tools/nsc/CompileSocket.scala b/src/compiler/scala/tools/nsc/CompileSocket.scala index 42b91a59ad..fb4d4c50dc 100644 --- a/src/compiler/scala/tools/nsc/CompileSocket.scala +++ b/src/compiler/scala/tools/nsc/CompileSocket.scala @@ -6,10 +6,16 @@ package scala.tools.nsc -import java.io.{ File, IOException, FileNotFoundException, PrintWriter, FileOutputStream } +import java.io.{ IOException, FileNotFoundException, PrintWriter, FileOutputStream } import java.io.{ BufferedReader, FileReader } import java.util.regex.Pattern import java.net._ +import java.security.SecureRandom + +import scala.io.File +import scala.util.control.Exception.catching + +// class CompileChannel { } /** This class manages sockets for the fsc offline compiler. */ class CompileSocket { @@ -26,7 +32,7 @@ class CompileSocket { protected val vmCommand = Properties.scalaHome match { case null => cmdName case dirname => - val trial = scala.io.File(dirname) / "bin" / cmdName + val trial = File(dirname) / "bin" / cmdName if (trial.canRead) trial.path else cmdName } @@ -53,7 +59,7 @@ class CompileSocket { /** A temporary directory to use */ val tmpDir = { val udir = Option(Properties.userName) getOrElse "shared" - val f = (scala.io.File(Properties.tmpDir) / "scala-devel" / udir).file + val f = (File(Properties.tmpDir) / "scala-devel" / udir).file f.mkdirs() if (f.isDirectory && f.canWrite) { @@ -64,7 +70,7 @@ class CompileSocket { } /* A directory holding port identification files */ - val portsDir = new File(tmpDir, dirName) + val portsDir = File(tmpDir) / dirName portsDir.mkdirs /** Maximum number of polls for an available port */ @@ -97,24 +103,16 @@ class CompileSocket { } /** The port identification file */ - def portFile(port: Int) = new File(portsDir, port.toString()) + def portFile(port: Int) = portsDir / port.toString /** Poll for a server port number; return -1 if none exists yet */ - private def pollPort(): Int = { - val hits = portsDir.listFiles() - if (hits.length == 0) -1 - else - try { - for (i <- 1 until hits.length) hits(i).delete() - hits(0).getName.toInt - } catch { - case ex: NumberFormatException => - fatal(ex.toString() + - "\nbad file in temp directory: " + - hits(0).getAbsolutePath() + - "\nplease remove the file and try again") - } - } + private def pollPort(): Int = + portsDir.iterator.toList match { + case Nil => -1 + case p :: xs => + xs forall (_.delete()) + p.name.toInt + } /** Get the port number to which a scala compile server is connected; * If no server is running yet, then create one. @@ -138,8 +136,8 @@ class CompileSocket { /** Set the port number to which a scala compile server is connected */ def setPort(port: Int) { - val file = scala.io.File(portFile(port)) - val secret = new java.security.SecureRandom().nextInt.toString + val file = portFile(port) + val secret = new SecureRandom().nextInt.toString try file writeAll List(secret) catch { case e @ (_: FileNotFoundException | _: SecurityException) => @@ -148,7 +146,7 @@ class CompileSocket { } /** Delete the port number to which a scala compile server was connected */ - def deletePort(port: Int) { portFile(port).delete() } + def deletePort(port: Int) = portFile(port).delete() /** Get a socket connected to a daemon. If create is true, then * create a new daemon if necessary. Returns null if the connection @@ -210,8 +208,8 @@ class CompileSocket { } def getPassword(port: Int): String = { - val ff = scala.io.File(portFile(port)) - val f = ff.bufferedReader() + val ff = portFile(port) + val f = ff.bufferedReader() // allow some time for the server to start up def check = { |