Commit Graph

2348 Commits

Author SHA1 Message Date
Ethan Atkins 6f7a824478 Reduce idle cpu usage
I noticed that sbt 1.3.0 was using more cpu when idling (either at the
shell or while waiting for file events) than 1.2.8. This was because I'd
reduced a number of timeouts to 2 milliseconds which was causing a
thread to keep waking up every 2 milliseconds to poll a queue. I thought
that this was cheaper than it actually is and drove the cpu utilization
to O(10%) of a cpu on my mac.

To address this, I consolidated a number of queues into a single queue
in CommandExchange and Continuous. In the CommandExchange case, I
reworked CommandChannel to have a register method that passes in a Queue
of CommandChannels. Whenever it appends an exec, it adds itself to the
queue. CommandExchange can then poll that queue directly and poll the
returned CommandChannel for the actual exec. Since the main thread is
blocking on this queue, it does not need to frequently wake up and can
just poll more or less indefinitely until a message is received. This
also reduces average latency compared to older versions of sbt since
messages will be processed almost as soon as they are received.

The continuous case is slightly more complicated because we are polling
from two sources, stdin and FileEventMonitor. In my ideal world, I'd
have a reactive api for both of those sources and they would just write
events to a shared queue that we could block on. That is nontrivial to
implement, so instead I consolidated the FileEventMonitor instances into
a single FileEventMonitor. Since there is now only one FileEventMonitor
queue, we can block on that queue for 30 milliseconds and the poll
stdin. This reduces cpu utilization to O(2%) on my machine while still
having reasonably low latency for key input events (the latency of file
events should be close to zero since we are usually polling the
FileEventMonitor queue when waiting).

I actually had a TODO about the FileEventMonitor change that this
resolves.
2019-05-31 09:34:04 -07:00
Ethan Atkins 4158716b9a
Merge branch 'develop' into meta-reload-check 2019-05-30 21:59:02 -07:00
Ethan Atkins d48f41dcf8
Merge branch 'develop' into layer-fixes 2019-05-30 21:08:59 -07:00
Ethan Atkins 5faf78af96 Add scala reflect layer
Not caching scala reflect is extremely painful if the build uses
scalatest. It adds O(1second) to my watch performance benchmarks. It
actually made sbt 1.3.0 much slower than 0.13.17
2019-05-30 17:30:14 -07:00
Ethan Atkins cf73bbbafc Fix ScalaLibrary again
I was benchmarking sbt with turbo mode on and found that tests weren't
running. This was because we were inadvertently excluding all of the
dependency jars from the dynamic classpath. I have no idea why the
scripted tests didn't catch this.

The scalatest scripted test didn't catch this because 'test' just
automaticaly succeeds if no test frameworks are found. To guard against
regression, I had to ensure that 'test' failed for every strategy if a
bad test file was present.
2019-05-30 17:30:14 -07:00
Ethan Atkins cda4713f89 Make AllLibraryJars a case object
This improves the toString and also allows it to be used in a pattern
match.
2019-05-30 17:30:14 -07:00
Ethan Atkins 525bf8fa3d Move meta build source check into Command.processCommand
We want to check the build sources before any command runs, not just
tasks. To achieve this, I moved the logic for checking for build source
changes to CommandProcess.processCommand. Also, @smarter had noticed
that if a user modified a build file and then ran reload, a warning
would be displayed about changed build sources even though they had just
ran reload. This was because running reload didn't update the previous
cache for checkBuildSources / fileInputStamps. I fixed that bug by
running 'checkBuildSources / changedInputFiles' instead of
'checkBuildSources' when the user runs reload.

I verified that after this change:

- If I changed a build file and ran 'show version' a warning was printed
  before it displayed the version. If I also set
  global / onChangedBuildSource := ReloadOnSourceChanges, it
  automatically reloaded before displaying the version.
- If I changed a build source and ran 'reload', followed by
  'show version', no warnings were ever displayed.

As an implementation detail, I had to add the Aggregation.suppressShow
attribute key. We set this key to true before checking the build
sources. Without this, log.success is called whenever we check the build
sources which is both confusing and noisy.
2019-05-30 15:55:21 -07:00
Ethan Atkins 3de3cc15cf Don't use global execution context
Because we are sharing the scala library classloader with test and run,
it is possible that sbt will be competing with for resources with the
test and run tasks when trying to get threads from the global execution
context. Also, by using our own execution context, we can shut it down
when sbt exits.

The motivation for this change is that I was looking at the active jvm
threads of an idle sbt process and noticed a bunch of global execution
context threads.
2019-05-30 14:30:31 -07:00
Ethan Atkins 9d8296fe49 Fix indefinite recompilation
@olegych reported in #4721 that play projects would get stuck in a
strange loop where modifying any source file would cause that source
file to always be recompiled every time a build was triggered regardless
of whether or not it was modified. This was because the play project
sets custom watchSources (using the legacy api) that overlap with the
fileInputs.

There were two parts to this fix:
1) When detecting an event, find if any of the dynamic inputs that cover
   the glob use a hash. If so, these are file inputs so we want to
   update the hash for the path, not the last modified time.
2) Only write hashes into the persistent file stamp cache. Computing the
   last modified time is much cheaper than the hash so it makes sense to
   avoid ever caching last modified times.

I wrote a scripted test that fails if Continuous writes a last modified
time into the file stamp cache instead of a hash. I also verified
manually that a sample play project no longer exhibits the weird
recompilation behavior.

Fixes #4721
2019-05-30 13:02:11 -07:00
Ethan Atkins b9fed2abfb Remove warning about unneeded named variable 2019-05-30 13:02:11 -07:00
Ethan Atkins 90d0c54caa Set watchTriggeredMessage by default
This allows the user to do, for example,
watchTriggeredMessage := { (count, path, commands) =>
  println(Watched.clearScreen)
  watchTriggeredMessage.value(count, path, commands)
}

Also, there was a bug where I accidentally inadvertently used the
deprecated watch message setting where I meant to use the deprecated
trigger message setting.

Fixes #4696
2019-05-30 13:02:11 -07:00
eugene yokota bbe0e62a0f
Merge pull request #4747 from eed3si9n/wip/shutdown
Create serviceTempDir lazily
2019-05-30 09:58:42 -04:00
eugene yokota bcb0294ed8
Merge pull request #4744 from eed3si9n/wip/bumpcoursier
lm-coursier-shaded 1.1.0-M14-3
2019-05-30 09:58:21 -04:00
eugene yokota 4ef0eb609f
Merge pull request #4743 from eed3si9n/wip/java
Fix Java version parsing
2019-05-30 09:58:00 -04:00
Eugene Yokota 4b10c486c4 Create serviceTempDir lazily
Ref #4741
2019-05-30 00:58:14 -04:00
Eugene Yokota a5a8c63732 Move Coursier related tasks into sbt.coursierint
Ref #4713
2019-05-30 00:24:55 -04:00
Eugene Yokota 5b96bcae06 Move to dependencyResolutionTask to Defaults 2019-05-30 00:01:00 -04:00
Eugene Yokota 81d7edb6c6 lm-coursier-shaded 1.1.0-M14-3
Fixes #4738
2019-05-29 23:48:05 -04:00
Eugene Yokota 41eca47e66 Fix Java version parsing
Fixes #4731
2019-05-29 23:11:20 -04:00
Ethan Atkins dcccd17fd2 Improve managed file management in watch
@olegych reported in https://github.com/sbt/sbt/issues/4722 that
sometimes even when a build was triggered during watch that no
recompilation would occur. The cause of this was that we never
invalidated the file stamp cache for managed sources or output files.
The optimization of persisting the source file stamps between task
evaluations in a continuous build only really makes sense for unmanaged
sources. We make the implicit assumption that unmanaged sources are
infrequently updated and generally one at a time. That assumption does
not hold for unmanaged source or output files.

To fix this, I split the fileStampCache into two caches: one for
unmanaged sources and one for everything else. We only persist the
unmanagedFileStampCache during continuous builds. The
managedFileStampCache gets invalidated every time.

I added a scripted test that simulates changing a generated source file.
Prior to this change, the test would fail because the file stamp was not
invalidated for the new source file content.

Fixes #4722
2019-05-29 17:28:04 -07:00
eugene yokota 95822e0eb0
Merge pull request #4739 from dwijnand/no-TupleSyntax
Drop the remaining TupleSyntax usage
2019-05-29 13:43:21 -04:00
eugene yokota 1426a6b48e
Merge pull request #4713 from smarter/public-coursier
Make coursier-related tasks public
2019-05-29 10:51:48 -04:00
Dale Wijnand e86a63ca1b
Drop the remaining TupleSyntax usage 2019-05-29 14:43:18 +01:00
Ethan Atkins e481ddb1fc
Merge branch 'develop' into watch-alias 2019-05-28 21:06:31 -07:00
Ethan Atkins f7dd228808 Allow aliases to be used in continuous builds
@japgolly reported in #4695 that aliased commands don't work in watch
anymore. This was because we were extracting the task from the raw
command rather than the aliased command. Since the alias wasn't a valid
key, we weren't able to parse the scoped key. The fix is to find the
aliased value and try that if we fail to parse the original command.

Fixes #4695
2019-05-28 16:49:23 -07:00
Guillaume Martres 2aab767962 Replace usages of deprecated ScalaInstance#libraryJar 2019-05-29 00:10:47 +02:00
eugene yokota aefd0969b1
Merge pull request #4732 from smarter/fix-updatesbtclass
updateSbtClassifiers: use the correct scalaOrganization
2019-05-28 17:52:12 -04:00
Ethan Atkins 5d08d82f3a Use libraryJars rather than libraryJar in ClassLoaders
Dotty uses multiple library jars. It also simplifies the code to use the
libraryJars method.
2019-05-28 11:22:34 -07:00
Guillaume Martres 7a84808f74 updateSbtClassifiers: use the correct scalaOrganization 2019-05-28 19:58:05 +02:00
Ethan Atkins df51281d90 Remove dead test 2019-05-28 10:39:08 -07:00
Ethan Atkins 7b870d647a Add missing header 2019-05-28 10:36:44 -07:00
Ethan Atkins d78d8d650c Don't automatically die on OOM: metaspace
In an interactive session, it's possible for task evaluation to trigger
an OOM: Metaspace but for sbt to continue working after that failure.
Moreover, the metaspace oom can be caused by using a dependency
classloader layer. If the user changes the layering strategy, they may
be able to re-run their command successfully.
2019-05-28 09:53:36 -07:00
Ethan Atkins 92992a8243 Use file stamps for resource loader
Instead of caching based on the classpath of the resources, we should
instead cache based on the actual resource files. This commit achieves
that by adding the classpathFiles key which just transforms the
attributed classpath to a Seq[Path]. This implicitly generates the
outputFileStamps key for classpathFiles which we can use to read the
stamps (the file stamp entries for the classpath should get filled by
the compile task so this shouldn't actually cause any additional io).
2019-05-28 09:53:36 -07:00
Ethan Atkins b6cdd60cf8 Simplify layering strategies
The ShareRuntimeDependenciesLayerWithTestDependencies strategy doesn't
really work with resources, so it makes sense to get rid of it. Without
the share layer, there is no point in having separate
RuntimeDependencies and TestDependencies layers so I consolidated them
to Dependencies.

If we really care about binary/source compatibility for the 1.3.0-RCx
series, I can restore the traits and objects and set them private[sbt].
I think it was kind of a bug that they existed at all given the issue
with resources so it makes sense to just remove them.
2019-05-28 09:53:36 -07:00
Ethan Atkins 468334f142 Don't use anonymous URLClassLoaders
This makes debugging a bit easier in the eclipse memory analyzer tool
since we get a more specific classloader type than URLClassLoader and by
giving the class a meaningful name, we can tell from where it
originated.
2019-05-28 09:53:35 -07:00
Ethan Atkins af9f665649 Use new ClassLoaderCache for layered classloaders
This commit removes the ClassLoaderCache that I'd added for the purpose
of caching layered classloaders. Instead, we will use the state's global
ClassLoaderCache. This is better both because it centralizes the
classloader caching and because the new ClassLoaderCache will evict
unused classloaders when the jvm is under memory pressure.

I also add a new layer for the resources that goes between the scala
library layer and the dependency layer. This should help in cases where
users depend on libraries that require access to resources, e.g.
logback.xml.
2019-05-28 09:53:35 -07:00
Ethan Atkins a128ddf4a6 Route all ScalaInstance creations through the cache
It was possible to make new classloaders for the scala library and other
jars with each new scala instance. To avoid this, I audited all of the
places within sbt where we make a ScalaInstance and ensure that we
instantiate them in such a way that the classloaders are retrieved
through the state's ClassLoaderCache.

After this change, I found from a heap dump that it was possible to run
test in a project that uses scala 2.12.8 and have only ONE classloader
for the scala library present in the heap dump. With older versions,
there were would be up to 3 or 4 in most heap dumps.
2019-05-28 09:53:35 -07:00
Ethan Atkins 03bf539ce9 Add new ClassLoaderCache implementation
This commit adds a new ClassLoaderCache that builds on the
ClassLoaderCache that is present in zinc (and can be used to build an
instance of the zinc ClassLoaderCache to preserve compatibility). It
differs from the zinc classloader cache that it does not use direct
SoftReferences to classloaders. Instead, we create a wrapper loader
that can't load any classes and just delegates to its parent. This
allows us to add a thread that reaps the soft reference to the wrapper
loader. Crucially, we add a custom SoftReference class that has a strong
reference to the underlying classloader. This allows us to call close on
the strong reference.

The one issue with this approach is that we can't
rescue the jvm from crashing with an OOM: metaspace because the jvm
doesn't give us a chance to close and dereference the underlying
classloaders before it crashes. It WILL collect classloaders under
normal memory pressure, just not metaspace pressure. To fix this, I
check if the MaxMetaspaceSize is set via an MxBean and, if it is, we
fill the cache with regular soft references. We are going to change the
bash script to not set -XX:MaxMetaspaceSize by default so most builds
should probably end up correctly closing the classloaders after this
change. But we should break existing builds that set MaxMetaspaceSize
but don't crash.

As part of this commit, I audited all of the places where we were
instantiating ClassLoaderCache instances and instead pass in the
state's ClassLoaderCache instance. This reduces the total number of
classloaders created.
2019-05-28 09:53:35 -07:00
Ethan Atkins 20f6d22439 Fix memory leak
Using a lazy val causes the log manager to hold onto a reference to the
state. These would accumulate with each task evaluation. I found that
that in the beanpuree project, that if I ran compile 10 times in a row,
the heap usage was 40mb lower after this change.
2019-05-28 09:53:35 -07:00
Ethan Atkins f8d729cd3b Don't set fileOutputs at the compile config level
This was problematic because it had no dependency on the compile task
which meant that any other task in the config would pick up those
fileOutputs which did not make sense. I noticed this because
(resources / outputFileStamps).value would include class files.
2019-05-28 09:53:35 -07:00
Ethan Atkins df628d4f87 Improve legacy launcher
To minimize classloading and consistency between sbt instances launched
with the latest launcher compared to old launchers, I overhauled code
that replaces the app configuration and meta build classloader at
startup. The goals of this change for legacy launchers were:

1) Do not ever load the scala-library.jar from the app provider class loader.
2) Close the class loaders that are below the topLoader in the class
   loading hierarcy

For the new launcher, we simply want to avoid modifying the loader at
all.

I added the SbtParserInit class so that it was more straightforward to
preload the global instance using reflection. We now use reflection to
instantiate an SbtParserInit instance for both the legacy and new
launcher cases to simplify the logic.

After this change, the legacy loader still uses somewhat more metaspace
than the new loader, but the difference seems to be O(10MB), which
should only impact projects that were close their MaxMetaspaceSize to
begin with.

I verified using javap that none of the code in this class uses the
scala standard library which should help metaspace since we don't load
much of the scala standard library until we enter xMainImpl.run.
2019-05-28 09:53:35 -07:00
Ethan Atkins 22d5fbad13 Move external hooks definition
I verified manually that ExternalHooks were still applied by default but
that I could set the incOptions in the Test and Compile configs so that
they weren't used.

Fixes #4624
2019-05-26 19:15:55 -07:00
Eugene Yokota 5936bd1ff2 Revert Defaults.collectFiles
Fixes #4681
Ref #4649
2019-05-25 14:10:14 -04:00
Eugene Yokota 0ff97e4561 scalaCompilerBridgeDependencyResolution
Fixes #4712

This adds a specialized DependencyResolution instance called `scalaCompilerBridgeDependencyResolution` to download the compiler bridge. It has its own list of resolvers set by `scalaCompilerBridgeResolvers`. For backward compatibility, it will append `externalResolvers.value` as well.
2019-05-24 01:02:44 -04:00
Guillaume Martres 186693368d Make coursier-related tasks public
This follows the discussion in
https://github.com/coursier/coursier/issues/1181.
2019-05-21 19:10:38 +02:00
eugene yokota ecce47e5b7
Merge pull request #4703 from eed3si9n/wip/scalatest
Fixes layer 4 missing scala-reflect
2019-05-20 14:26:42 -04:00
Eugene Yokota 9d3b626567 Fix ScalaTest issue with ScalaLibrary classloader
Ref #4689
Ref #4671
2019-05-17 14:24:55 -04:00
Ethan Atkins 5a9f5a69d5 Fix warnings
These lines added new warnings that slipped through the cracks.
2019-05-15 17:27:07 -07:00
eugene yokota 15b4befa9c
Merge pull request #4679 from eatkins/rt-jar
Don't ever invalidate rt.jar
2019-05-14 22:13:45 -04:00
Ethan Atkins a820bb5623 Sort the supershell tasks by task name
This should make the output less jumpy.
2019-05-14 17:27:17 -07:00