1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/* NSC -- new Scala compiler
* Copyright 2005-2013 LAMP/EPFL
* @author Martin Odersky
*/
package scala.tools.nsc
import java.io.{ BufferedOutputStream, FileOutputStream, PrintStream }
import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}
import scala.reflect.internal.util.FakePos //Position
import scala.tools.util.SocketServer
import settings.FscSettings
/**
* The server part of the fsc offline compiler. It awaits compilation
* commands and executes them. It caches a compiler instance so
* that it can respond more quickly.
*
* @author Martin Odersky
* @version 1.0
*/
class StandardCompileServer extends SocketServer {
lazy val compileSocket: CompileSocket = CompileSocket
private var compiler: Global = null
private def clearCompiler() = compiler = null
var reporter: ConsoleReporter = _
var shutdown = false
var verbose = false
val versionMsg = "Fast " + Properties.versionMsg
val MaxCharge = 0.8
private val runtime = Runtime.getRuntime()
import runtime.{ totalMemory, freeMemory, maxMemory }
/** Create a new compiler instance */
def newGlobal(settings: Settings, reporter: Reporter) =
new Global(settings, reporter) {
override def inform(msg: String) = out.println(msg)
}
override def timeout() {
if (!compileSocket.portFile(port).exists)
fatal("port file no longer exists; skipping cleanup")
}
def printMemoryStats() {
def mb(bytes: Long) = "%dMB".format(bytes / 1000000)
info("New session: total memory = %s, max memory = %s, free memory = %s".format(
mb(totalMemory), mb(maxMemory), mb(freeMemory)))
}
def isMemoryFullEnough() = {
runtime.gc()
(totalMemory - freeMemory).toDouble / maxMemory.toDouble > MaxCharge
}
protected def newOfflineCompilerCommand(arguments: List[String], settings: FscSettings): OfflineCompilerCommand =
new OfflineCompilerCommand(arguments, settings)
/** Problematically, Settings are only considered equal if every setting
* is exactly equal. In fsc this immediately breaks down because the randomly
* chosen temporary outdirs differ between client and server. Among other
* things. Long term we could use a meaningful equality; short term I'm just
* ignoring options which I can see causing a new compiler instance every time
* and which do not interestingly influence compilation products.
*/
def unequalSettings(s1: Settings, s2: Settings): Set[Settings#Setting] = {
val ignoreSettings = Set("-d", "-encoding", "-currentDir")
def trim (s: Settings): Set[Settings#Setting] = (
s.userSetSettings.toSet[Settings#Setting] filterNot (ss => ignoreSettings exists (ss respondsTo _))
)
val ss1 = trim(s1)
val ss2 = trim(s2)
(ss1 union ss2) -- (ss1 intersect ss2)
}
def session() {
val password = compileSocket getPassword port
val guessedPassword = in.readLine()
val input = in.readLine()
def fscError(msg: String): Unit = out println (
FakePos("fsc") + msg + "\n fsc -help gives more information"
)
if (input == null || password != guessedPassword)
return
val args = input.split("\0", -1).toList
val newSettings = new FscSettings(fscError)
val command = newOfflineCompilerCommand(args, newSettings)
this.verbose = newSettings.verbose.value
info("Settings after normalizing paths: " + newSettings)
if (!command.files.isEmpty) info("Input files after normalizing paths: " + (command.files mkString ","))
printMemoryStats()
// Update the idle timeout if given
if (!newSettings.idleMins.isDefault) {
val mins = newSettings.idleMins.value
if (mins == 0) echo("Disabling idle timeout on compile server.")
else echo("Setting idle timeout to " + mins + " minutes.")
this.idleMinutes = mins
}
if (newSettings.shutdown.value) {
shutdown = true
return out.println("[Compile server exited]")
}
if (newSettings.reset.value) {
clearCompiler()
out.println("[Compile server was reset]")
if (command.files.isEmpty)
return
}
reporter = new ConsoleReporter(newSettings, in, out) {
// disable prompts, so that compile server cannot block
override def displayPrompt = ()
}
def isCompilerReusable: Boolean = {
if (compiler == null) {
info("[Creating new instance for compile server.]")
info("[Compiler version: " + Properties.versionString + ".]")
return false
}
val unequal = unequalSettings(newSettings, compiler.settings)
if (unequal.nonEmpty) {
info("[Replacing compiler with new instance because settings are unequal.]")
info("[Asymmetric settings: " + unequal.mkString(", ") + "]")
}
unequal.isEmpty
}
if (command.shouldStopWithInfo)
reporter.echo(command.getInfoMessage(newGlobal(newSettings, reporter)))
else if (command.files.isEmpty)
reporter.echo(command.usageMsg)
else {
if (isCompilerReusable) {
info("[Reusing existing Global instance.]")
compiler.currentSettings = newSettings
compiler.reporter = reporter
}
else {
compiler = newGlobal(newSettings, reporter)
}
val c = compiler
try new c.Run() compile command.files
catch {
case ex @ FatalError(msg) =>
reporter.error(null, "fatal error: " + msg)
clearCompiler()
case ex: Throwable =>
warn("Compile server encountered fatal condition: " + ex)
shutdown = true
throw ex
}
}
reporter.printSummary()
if (isMemoryFullEnough) {
info("Nulling out compiler due to memory utilization.")
clearCompiler()
}
}
}
object CompileServer extends StandardCompileServer {
/** A directory holding redirected output */
private lazy val redirectDir = (compileSocket.tmpDir / "output-redirects").createDirectory()
private def createRedirect(filename: String) =
new PrintStream((redirectDir / filename).createFile().bufferedOutput())
def main(args: Array[String]) =
execute(() => (), args)
/**
* Used for internal testing. The callback is called upon
* server start, notifying the caller that the server is
* ready to run. WARNING: the callback runs in the
* server's thread, blocking the server from doing any work
* until the callback is finished. Callbacks should be kept
* simple and clients should not try to interact with the
* server while the callback is processing.
*/
def execute(startupCallback : () => Unit, args: Array[String]) {
val debug = args contains "-v"
if (debug) {
echo("Starting CompileServer on port " + port)
echo("Redirect dir is " + redirectDir)
}
Console.withErr(createRedirect("scala-compile-server-err.log")) {
Console.withOut(createRedirect("scala-compile-server-out.log")) {
Console.err.println("...starting server on socket "+port+"...")
Console.err.flush()
compileSocket setPort port
startupCallback()
run()
compileSocket deletePort port
}
}
}
}
|