Unverified Commit a0ffe87b authored by Ryan Macnak's avatar Ryan Macnak Committed by GitHub

Use `fx get-build-dir` and `fx netaddr --fuchsia` to make fuchsia_reload more convenient. (#14854)

parent aa04a056
...@@ -8,7 +8,6 @@ import 'dart:collection'; ...@@ -8,7 +8,6 @@ import 'dart:collection';
import '../base/common.dart'; import '../base/common.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/io.dart'; import '../base/io.dart';
import '../base/platform.dart';
import '../base/process_manager.dart'; import '../base/process_manager.dart';
import '../base/utils.dart'; import '../base/utils.dart';
import '../cache.dart'; import '../cache.dart';
...@@ -35,22 +34,17 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -35,22 +34,17 @@ class FuchsiaReloadCommand extends FlutterCommand {
argParser.addOption('address', argParser.addOption('address',
abbr: 'a', abbr: 'a',
help: 'Fuchsia device network name or address.'); help: 'Fuchsia device network name or address.');
argParser.addOption('build-type', argParser.addOption('build-dir',
abbr: 'b', abbr: 'b',
defaultsTo: 'release-x86-64', defaultsTo: null,
help: 'Fuchsia build type, e.g. release-x86-64.'); help: 'Fuchsia build directory, e.g. out/release-x86-64.');
argParser.addOption('fuchsia-root',
abbr: 'f',
defaultsTo: platform.environment['FUCHSIA_ROOT'],
help: 'Path to Fuchsia source tree.');
argParser.addOption('gn-target', argParser.addOption('gn-target',
abbr: 'g', abbr: 'g',
help: 'GN target of the application, e.g //path/to/app:app.'); help: 'GN target of the application, e.g //path/to/app:app.');
argParser.addFlag('list', argParser.addFlag('list',
abbr: 'l', abbr: 'l',
defaultsTo: false, defaultsTo: false,
help: 'Lists the running modules. ' help: 'Lists the running modules. ');
'Requires the flags --address(-a) and --fuchsia-root(-f).');
argParser.addOption('name-override', argParser.addOption('name-override',
abbr: 'n', abbr: 'n',
help: 'On-device name of the application binary.'); help: 'On-device name of the application binary.');
...@@ -71,8 +65,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -71,8 +65,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
@override @override
final String description = 'Hot reload on Fuchsia.'; final String description = 'Hot reload on Fuchsia.';
String _fuchsiaRoot; String _buildDir;
String _buildType;
String _projectRoot; String _projectRoot;
String _projectName; String _projectName;
String _binaryName; String _binaryName;
...@@ -88,7 +81,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -88,7 +81,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
Future<Null> runCommand() async { Future<Null> runCommand() async {
Cache.releaseLockEarly(); Cache.releaseLockEarly();
_validateArguments(); await _validateArguments();
// Find the network ports used on the device by VM service instances. // Find the network ports used on the device by VM service instances.
final List<int> deviceServicePorts = await _getServicePorts(); final List<int> deviceServicePorts = await _getServicePorts();
...@@ -209,6 +202,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -209,6 +202,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
String _vmServiceToString(VMService vmService, {int tabDepth: 0}) { String _vmServiceToString(VMService vmService, {int tabDepth: 0}) {
final Uri addr = vmService.httpAddress; final Uri addr = vmService.httpAddress;
final String embedder = vmService.vm.embedder;
final int numIsolates = vmService.vm.isolates.length; final int numIsolates = vmService.vm.isolates.length;
final String maxRSS = getSizeAsMB(vmService.vm.maxRSS); final String maxRSS = getSizeAsMB(vmService.vm.maxRSS);
final String heapSize = getSizeAsMB(vmService.vm.heapAllocatedMemoryUsage); final String heapSize = getSizeAsMB(vmService.vm.heapAllocatedMemoryUsage);
...@@ -233,7 +227,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -233,7 +227,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
final String tabs = '\t' * tabDepth; final String tabs = '\t' * tabDepth;
final String extraTabs = '\t' * (tabDepth + 1); final String extraTabs = '\t' * (tabDepth + 1);
final StringBuffer stringBuffer = new StringBuffer( final StringBuffer stringBuffer = new StringBuffer(
'$tabs${_bold}VM at $addr$_reset\n' '$tabs$_bold$embedder at $addr$_reset\n'
'${extraTabs}RSS: $maxRSS\n' '${extraTabs}RSS: $maxRSS\n'
'${extraTabs}Native allocations: $heapSize\n' '${extraTabs}Native allocations: $heapSize\n'
'${extraTabs}New Spaces: $newUsed of $newCap\n' '${extraTabs}New Spaces: $newUsed of $newCap\n'
...@@ -290,21 +284,29 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -290,21 +284,29 @@ class FuchsiaReloadCommand extends FlutterCommand {
} }
} }
void _validateArguments() { Future<Null> _validateArguments() async {
_fuchsiaRoot = argResults['fuchsia-root']; _buildDir = argResults['build-dir'];
if (_fuchsiaRoot == null) if (_buildDir == null) {
throwToolExit('Please give the location of the Fuchsia tree with --fuchsia-root.'); final ProcessResult result = await processManager.run(<String>['fx', 'get-build-dir']);
if (!_directoryExists(_fuchsiaRoot)) if (result.exitCode == 0)
throwToolExit('Specified --fuchsia-root "$_fuchsiaRoot" does not exist.'); _buildDir = result.stdout.trim();
else
printStatus('get-build-dir failed:\nstdout: ${result.stdout}\nstderr: ${result.stderr}');
}
if (!_directoryExists(_buildDir))
throwToolExit('Specified --build-dir "$_buildDir" does not exist.');
_address = argResults['address']; _address = argResults['address'];
if (_address == null) {
final ProcessResult result = await processManager.run(<String>['fx', 'netaddr', '--fuchsia']);
if (result.exitCode == 0)
_address = result.stdout.trim();
else
printStatus('netaddr failed:\nstdout: ${result.stdout}\nstderr: ${result.stderr}');
}
if (_address == null) if (_address == null)
throwToolExit('Give the address of the device running Fuchsia with --address.'); throwToolExit('Give the address of the device running Fuchsia with --address.');
_buildType = argResults['build-type'];
if (_buildType == null)
throwToolExit('Give the build type with --build-type.');
_list = argResults['list']; _list = argResults['list'];
if (_list) { if (_list) {
// For --list, we only need the device address and the Fuchsia tree root. // For --list, we only need the device address and the Fuchsia tree root.
...@@ -317,7 +319,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -317,7 +319,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
final List<String> targetInfo = _extractPathAndName(gnTarget); final List<String> targetInfo = _extractPathAndName(gnTarget);
_projectRoot = targetInfo[0]; _projectRoot = targetInfo[0];
_projectName = targetInfo[1]; _projectName = targetInfo[1];
_fuchsiaProjectPath = '$_fuchsiaRoot/$_projectRoot'; _fuchsiaProjectPath = '$_buildDir/../../$_projectRoot';
if (!_directoryExists(_fuchsiaProjectPath)) if (!_directoryExists(_fuchsiaProjectPath))
throwToolExit('Target does not exist in the Fuchsia tree: $_fuchsiaProjectPath.'); throwToolExit('Target does not exist in the Fuchsia tree: $_fuchsiaProjectPath.');
...@@ -329,7 +331,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -329,7 +331,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
throwToolExit('Couldn\'t find application entry point at $_target.'); throwToolExit('Couldn\'t find application entry point at $_target.');
final String packagesFileName = '${_projectName}_dart_library.packages'; final String packagesFileName = '${_projectName}_dart_library.packages';
_dotPackagesPath = '$_fuchsiaRoot/out/$_buildType/dartlang/gen/$_projectRoot/$packagesFileName'; _dotPackagesPath = '$_buildDir/dartlang/gen/$_projectRoot/$packagesFileName';
if (!_fileExists(_dotPackagesPath)) if (!_fileExists(_dotPackagesPath))
throwToolExit('Couldn\'t find .packages file at $_dotPackagesPath.'); throwToolExit('Couldn\'t find .packages file at $_dotPackagesPath.');
...@@ -365,7 +367,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -365,7 +367,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
} }
Future<List<_PortForwarder>> _forwardPorts(List<int> remotePorts) { Future<List<_PortForwarder>> _forwardPorts(List<int> remotePorts) {
final String config = '$_fuchsiaRoot/out/$_buildType/ssh-keys/ssh_config'; final String config = '$_buildDir/ssh-keys/ssh_config';
return Future.wait(remotePorts.map((int remotePort) { return Future.wait(remotePorts.map((int remotePort) {
return _PortForwarder.start(config, _address, remotePort); return _PortForwarder.start(config, _address, remotePort);
})); }));
...@@ -373,17 +375,19 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -373,17 +375,19 @@ class FuchsiaReloadCommand extends FlutterCommand {
Future<List<int>> _getServicePorts() async { Future<List<int>> _getServicePorts() async {
final FuchsiaDeviceCommandRunner runner = final FuchsiaDeviceCommandRunner runner =
new FuchsiaDeviceCommandRunner(_address, _fuchsiaRoot, _buildType); new FuchsiaDeviceCommandRunner(_address, _buildDir);
final List<String> lsOutput = await runner.run('ls /tmp/dart.services'); final List<String> lsOutput = await runner.run('ls /tmp/dart.services');
final List<int> ports = <int>[]; final List<int> ports = <int>[];
for (String s in lsOutput) { if (lsOutput != null) {
final String trimmed = s.trim(); for (String s in lsOutput) {
final int lastSpace = trimmed.lastIndexOf(' '); final String trimmed = s.trim();
final String lastWord = trimmed.substring(lastSpace + 1); final int lastSpace = trimmed.lastIndexOf(' ');
if ((lastWord != '.') && (lastWord != '..')) { final String lastWord = trimmed.substring(lastSpace + 1);
final int value = int.parse(lastWord, onError: (_) => null); if ((lastWord != '.') && (lastWord != '..')) {
if (value != null) final int value = int.parse(lastWord, onError: (_) => null);
ports.add(value); if (value != null)
ports.add(value);
}
} }
} }
return ports; return ports;
...@@ -473,16 +477,13 @@ class _PortForwarder { ...@@ -473,16 +477,13 @@ class _PortForwarder {
} }
class FuchsiaDeviceCommandRunner { class FuchsiaDeviceCommandRunner {
// TODO(zra): Get rid of _address and instead use
// $_fuchsiaRoot/out/build-magenta/tools/netaddr --fuchsia
final String _address; final String _address;
final String _buildType; final String _buildDir;
final String _fuchsiaRoot;
FuchsiaDeviceCommandRunner(this._address, this._fuchsiaRoot, this._buildType); FuchsiaDeviceCommandRunner(this._address, this._buildDir);
Future<List<String>> run(String command) async { Future<List<String>> run(String command) async {
final String config = '$_fuchsiaRoot/out/$_buildType/ssh-keys/ssh_config'; final String config = '$_buildDir/ssh-keys/ssh_config';
final List<String> args = <String>['ssh', '-F', config, _address, command]; final List<String> args = <String>['ssh', '-F', config, _address, command];
printTrace(args.join(' ')); printTrace(args.join(' '));
final ProcessResult result = await processManager.run(args); final ProcessResult result = await processManager.run(args);
......
...@@ -248,6 +248,8 @@ class VMService { ...@@ -248,6 +248,8 @@ class VMService {
} }
Future<Null> waitForViews({int attempts = 5, int attemptSeconds = 1}) async { Future<Null> waitForViews({int attempts = 5, int attemptSeconds = 1}) async {
if (!vm.isFlutterEngine)
return;
await vm.refreshViews(); await vm.refreshViews();
for (int i = 0; (vm.firstView == null) && (i < attempts); i++) { for (int i = 0; (vm.firstView == null) && (i < attempts); i++) {
// If the VM doesn't yet have a view, wait for one to show up. // If the VM doesn't yet have a view, wait for one to show up.
...@@ -587,6 +589,7 @@ class VM extends ServiceObjectOwner { ...@@ -587,6 +589,7 @@ class VM extends ServiceObjectOwner {
_heapAllocatedMemoryUsage = map['_heapAllocatedMemoryUsage']; _heapAllocatedMemoryUsage = map['_heapAllocatedMemoryUsage'];
} }
_maxRSS = map['_maxRSS']; _maxRSS = map['_maxRSS'];
_embedder = map['_embedder'];
// Remove any isolates which are now dead from the isolate cache. // Remove any isolates which are now dead from the isolate cache.
_removeDeadIsolates(map['isolates']); _removeDeadIsolates(map['isolates']);
...@@ -615,6 +618,12 @@ class VM extends ServiceObjectOwner { ...@@ -615,6 +618,12 @@ class VM extends ServiceObjectOwner {
int _maxRSS; int _maxRSS;
int get maxRSS => _maxRSS == null ? 0 : _maxRSS; int get maxRSS => _maxRSS == null ? 0 : _maxRSS;
// The embedder's name, Flutter or dart_runner.
String _embedder;
String get embedder => _embedder;
bool get isFlutterEngine => embedder == 'Flutter';
bool get isDartRunner => embedder == 'dart_runner';
int _compareIsolates(Isolate a, Isolate b) { int _compareIsolates(Isolate a, Isolate b) {
final DateTime aStart = a.startTime; final DateTime aStart = a.startTime;
final DateTime bStart = b.startTime; final DateTime bStart = b.startTime;
...@@ -850,6 +859,8 @@ class VM extends ServiceObjectOwner { ...@@ -850,6 +859,8 @@ class VM extends ServiceObjectOwner {
} }
Future<Null> refreshViews() async { Future<Null> refreshViews() async {
if (!isFlutterEngine)
return;
_viewCache.clear(); _viewCache.clear();
await vmService.vm.invokeRpc('_flutter.listViews', timeout: kLongRequestTimeout); await vmService.vm.invokeRpc('_flutter.listViews', timeout: kLongRequestTimeout);
} }
......
...@@ -18,8 +18,7 @@ void main() { ...@@ -18,8 +18,7 @@ void main() {
testUsingContext('a test', () async { testUsingContext('a test', () async {
final FuchsiaDeviceCommandRunner commandRunner = final FuchsiaDeviceCommandRunner commandRunner =
new FuchsiaDeviceCommandRunner('8.8.9.9', new FuchsiaDeviceCommandRunner('8.8.9.9',
'~/fuchsia', '~/fuchsia/out/release-x86-64');
'release-x86-64');
final List<String> ports = await commandRunner.run('ls /tmp'); final List<String> ports = await commandRunner.run('ls /tmp');
expect(ports, hasLength(3)); expect(ports, hasLength(3));
expect(ports[0], equals('1234')); expect(ports[0], equals('1234'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment