This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Speed up emulator port allocation #46
Open
jakob-grabner
wants to merge
4
commits into
gojuno:master
Choose a base branch
from
jakob-grabner:feature/faster_port_selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,18 +33,14 @@ fun startEmulators( | |
connectedAdbDevices: () -> Observable<Set<AdbDevice>> = ::connectedAdbDevices, | ||
createAvd: (args: Commands.Start) -> Observable<Unit> = ::createAvd, | ||
applyConfig: (args: Commands.Start) -> Observable<Unit> = ::applyConfig, | ||
emulator: (args: Commands.Start) -> String = ::emulatorBinary, | ||
emulatorCmd: (args: Commands.Start) -> String = ::emulatorBinary, | ||
findAvailablePortsForNewEmulator: () -> Observable<Pair<Int, Int>> = ::findAvailablePortsForNewEmulator, | ||
startEmulatorProcess: (List<String>, Commands.Start) -> Observable<Notification> = ::startEmulatorProcess, | ||
waitForEmulatorToStart: (Commands.Start, () -> Observable<Set<AdbDevice>>, Observable<Notification>, Pair<Int, Int>) -> Observable<Emulator> = ::waitForEmulatorToStart, | ||
waitForEmulatorToFinishBoot: (Emulator, Commands.Start) -> Observable<Emulator> = ::waitForEmulatorToFinishBoot | ||
) { | ||
val startTime = System.nanoTime() | ||
|
||
// Sometimes on Linux "emulator -verbose -avd" does not print serial id of started emulator, | ||
// so by allocating ports manually we know which serial id emulator will have. | ||
val availablePortsSemaphore = Semaphore(1) | ||
|
||
val startedEmulators = connectedAdbDevices() | ||
.doOnNext { log("Already running emulators: $it") } | ||
.flatMap { | ||
|
@@ -54,12 +50,11 @@ fun startEmulators( | |
args = command, | ||
createAvd = createAvd, | ||
applyConfig = applyConfig, | ||
availablePortsSemaphore = availablePortsSemaphore, | ||
findAvailablePortsForNewEmulator = findAvailablePortsForNewEmulator, | ||
startEmulatorProcess = startEmulatorProcess, | ||
waitForEmulatorToStart = waitForEmulatorToStart, | ||
connectedAdbDevices = connectedAdbDevices, | ||
emulator = emulator, | ||
emulatorCmd = emulatorCmd, | ||
waitForEmulatorToFinishBoot = waitForEmulatorToFinishBoot | ||
) | ||
} | ||
|
@@ -87,29 +82,26 @@ private fun startEmulator( | |
args: Commands.Start, | ||
createAvd: (args: Commands.Start) -> Observable<Unit>, | ||
applyConfig: (args: Commands.Start) -> Observable<Unit>, | ||
availablePortsSemaphore: Semaphore, | ||
findAvailablePortsForNewEmulator: () -> Observable<Pair<Int, Int>>, | ||
startEmulatorProcess: (List<String>, Commands.Start) -> Observable<Notification>, | ||
waitForEmulatorToStart: (Commands.Start, () -> Observable<Set<AdbDevice>>, Observable<Notification>, Pair<Int, Int>) -> Observable<Emulator>, | ||
connectedAdbDevices: () -> Observable<Set<AdbDevice>> = ::connectedAdbDevices, | ||
emulator: (Commands.Start) -> String, | ||
emulatorCmd: (Commands.Start) -> String, | ||
waitForEmulatorToFinishBoot: (Emulator, Commands.Start) -> Observable<Emulator> | ||
): Observable<Emulator> = | ||
createAvd(args) | ||
.flatMap { applyConfig(args) } | ||
.map { availablePortsSemaphore.acquire() } | ||
.flatMap { findAvailablePortsForNewEmulator() } | ||
.doOnNext { log("Ports for emulator ${args.emulatorName}: ${it.first}, ${it.second}.") } | ||
.flatMap { ports -> | ||
startEmulatorProcess( | ||
// Unix only, PR welcome. | ||
listOf(sh, "-c", "${emulator(args)} ${if (args.verbose) "-verbose" else ""} -avd ${args.emulatorName} -ports ${ports.first},${ports.second} ${args.emulatorStartOptions.joinToString(" ")} &"), | ||
listOf(sh, "-c", "${emulatorCmd(args)} ${if (args.verbose) "-verbose" else ""} -avd ${args.emulatorName} -ports ${ports.first},${ports.second} ${args.emulatorStartOptions.joinToString(" ")} &"), | ||
args | ||
).let { process -> | ||
waitForEmulatorToStart(args, connectedAdbDevices, process, ports) | ||
} | ||
} | ||
.map { emulator -> availablePortsSemaphore.release().let { emulator } } | ||
.flatMap { emulator -> | ||
when (args.redirectLogcatTo) { | ||
null -> Observable.just(emulator) | ||
|
@@ -219,17 +211,22 @@ private fun emulatorBinary(args: Commands.Start): String = | |
emulator | ||
} | ||
|
||
private val usedPorts: MutableList<Int> = mutableListOf() | ||
/** | ||
* Sometimes on Linux "emulator -verbose -avd" does not print serial id of started emulator, | ||
* so by allocating ports manually we know which serial id emulator will have. | ||
*/ | ||
private fun findAvailablePortsForNewEmulator(): Observable<Pair<Int, Int>> = connectedAdbDevices() | ||
.map { it.filter { it.isEmulator } } | ||
.map { | ||
if (it.isEmpty()) { | ||
5554 | ||
} else { | ||
synchronized(usedPorts) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would the usage look like? We basically need to read the entire list and then write to it before the next thread is allowed to read it. Not sure if |
||
it | ||
.map { it.id } | ||
.map { it.substringAfter("emulator-") } | ||
.map { it.toInt() } | ||
.max()!! + 2 | ||
.let { it + usedPorts } | ||
.let { (it.max() ?: 5552) + 2 } | ||
.also { usedPorts.add(it) } | ||
} | ||
} | ||
.map { it to it + 1 } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is scary change, I actually used something like this in the beggining, but then figured that it breaks when something else starts emulators outside of Swarmer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I might be wrong here, if this is just the used ports it should be fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is always a small risk if something starts emulators outside of Swarmer that can't be avoided. If a emulator would start after
findAvailablePortsForNewEmulator
but before Swarmer starts the emulator. Not sure if there is a way to avoid possible port conflicts in that case. But that is a small window so I think we should be fine.