// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'dart:async'; import 'dart:collection'; import '../base/common.dart'; import '../base/file_system.dart'; import '../base/io.dart'; import '../base/platform.dart'; import '../base/process_manager.dart'; import '../base/utils.dart'; import '../cache.dart'; import '../device.dart'; import '../flx.dart' as flx; import '../fuchsia/fuchsia_device.dart'; import '../globals.dart'; import '../resident_runner.dart'; import '../run_hot.dart'; import '../runner/flutter_command.dart'; import '../vmservice.dart'; // Usage: // With e.g. flutter_gallery already running, a HotRunner can be attached to it // with: // $ flutter fuchsia_reload -f ~/fuchsia -a 192.168.1.39 \ // -g //lib/flutter/examples/flutter_gallery:flutter_gallery class FuchsiaReloadCommand extends FlutterCommand { FuchsiaReloadCommand() { addBuildModeFlags(defaultToRelease: false); argParser.addOption('address', abbr: 'a', help: 'Fuchsia device network name or address.'); argParser.addOption('build-type', abbr: 'b', defaultsTo: 'release-x86-64', help: 'Fuchsia build type, e.g. release-x86-64.'); argParser.addOption('fuchsia-root', abbr: 'f', defaultsTo: platform.environment['FUCHSIA_ROOT'], help: 'Path to Fuchsia source tree.'); argParser.addOption('gn-target', abbr: 'g', help: 'GN target of the application, e.g //path/to/app:app.'); argParser.addFlag('list', abbr: 'l', defaultsTo: false, help: 'Lists the running modules. ' 'Requires the flags --address(-a) and --fuchsia-root(-f).'); argParser.addOption('name-override', abbr: 'n', help: 'On-device name of the application binary.'); argParser.addOption('isolate-number', abbr: 'i', help: 'To reload only one instance, speficy the isolate number, e.g. ' 'the number in foo\$main-###### given by --list.'); argParser.addOption('target', abbr: 't', defaultsTo: flx.defaultMainPath, help: 'Target app path / main entry-point file. ' 'Relative to --gn-target path, e.g. lib/main.dart.'); } @override final String name = 'fuchsia_reload'; @override final String description = 'Hot reload on Fuchsia.'; String _fuchsiaRoot; String _buildType; String _projectRoot; String _projectName; String _binaryName; String _isolateNumber; String _fuchsiaProjectPath; String _target; String _address; String _dotPackagesPath; bool _list; @override Future runCommand() async { Cache.releaseLockEarly(); _validateArguments(); // Find the network ports used on the device by VM service instances. final List servicePorts = await _getServicePorts(); if (servicePorts.isEmpty) throwToolExit('Couldn\'t find any running Observatory instances.'); for (int port in servicePorts) printTrace('Fuchsia service port: $port'); if (_list) { await _listVMs(servicePorts); return; } // Check that there are running VM services on the returned // ports, and find the Isolates that are running the target app. final String isolateName = '$_binaryName\$main$_isolateNumber'; final List targetPorts = await _filterPorts(servicePorts, isolateName); if (targetPorts.isEmpty) throwToolExit('No VMs found running $_binaryName.'); for (int port in targetPorts) printTrace('Found $_binaryName at $port'); // Set up a device and hot runner and attach the hot runner to the first // vm service we found. final List fullAddresses = targetPorts.map( (int p) => '$_address:$p' ).toList(); final List observatoryUris = fullAddresses.map( (String a) => Uri.parse('http://$a') ).toList(); final FuchsiaDevice device = new FuchsiaDevice(fullAddresses[0]); final FlutterDevice flutterDevice = new FlutterDevice(device); flutterDevice.observatoryUris = observatoryUris; final HotRunner hotRunner = new HotRunner( [flutterDevice], debuggingOptions: new DebuggingOptions.enabled(getBuildMode()), target: _target, projectRootPath: _fuchsiaProjectPath, packagesFilePath: _dotPackagesPath ); printStatus('Connecting to $_binaryName'); await hotRunner.attach(viewFilter: isolateName); } // A cache of VMService connections. HashMap _vmServiceCache = new HashMap(); VMService _getVMService(int port) { if (!_vmServiceCache.containsKey(port)) { final String addr = 'http://$_address:$port'; final Uri uri = Uri.parse(addr); final VMService vmService = VMService.connect(uri); _vmServiceCache[port] = vmService; } return _vmServiceCache[port]; } Future _checkPort(int port) async { bool connected = true; Socket s; try { s = await Socket.connect("$_address", port); } catch (_) { connected = false; } if (s != null) await s.close(); return connected; } Future> _getViews(List ports) async { final List views = []; for (int port in ports) { if (!await _checkPort(port)) continue; final VMService vmService = _getVMService(port); await vmService.getVM(); await vmService.waitForViews(); views.addAll(vmService.vm.views); } return views; } // Find ports where there is a view isolate with the given name Future> _filterPorts(List ports, String viewFilter) async { final List result = []; for (FlutterView v in await _getViews(ports)) { final Uri addr = v.owner.vmService.httpAddress; printTrace('At $addr, found view: ${v.uiIsolate.name}'); if (v.uiIsolate.name.indexOf(viewFilter) == 0) result.add(addr.port); } return result; } static const String _bold = '\u001B[0;1m'; static const String _reset = '\u001B[0m'; String _vmServiceToString(VMService vmService, {int tabDepth: 0}) { final Uri addr = vmService.httpAddress; final int numIsolates = vmService.vm.isolates.length; final String maxRSS = getSizeAsMB(vmService.vm.maxRSS); final String heapSize = getSizeAsMB(vmService.vm.heapAllocatedMemoryUsage); int totalNewUsed = 0; int totalNewCap = 0; int totalOldUsed = 0; int totalOldCap = 0; int totalExternal = 0; for (Isolate i in vmService.vm.isolates) { totalNewUsed += i.newSpace.used; totalNewCap += i.newSpace.capacity; totalOldUsed += i.oldSpace.used; totalOldCap += i.oldSpace.capacity; totalExternal += i.newSpace.external; totalExternal += i.oldSpace.external; } final String newUsed = getSizeAsMB(totalNewUsed); final String newCap = getSizeAsMB(totalNewCap); final String oldUsed = getSizeAsMB(totalOldUsed); final String oldCap = getSizeAsMB(totalOldCap); final String external = getSizeAsMB(totalExternal); final String tabs = '\t' * tabDepth; final String extraTabs = '\t' * (tabDepth + 1); final StringBuffer stringBuffer = new StringBuffer( '$tabs${_bold}VM at $addr$_reset\n' '${extraTabs}RSS: $maxRSS\n' '${extraTabs}Native allocations: $heapSize\n' '${extraTabs}New Spaces: $newUsed of $newCap\n' '${extraTabs}Old Spaces: $oldUsed of $oldCap\n' '${extraTabs}External: $external\n' '${extraTabs}Isolates: $numIsolates\n' ); for (Isolate isolate in vmService.vm.isolates) { stringBuffer.write(_isolateToString(isolate, tabDepth: tabDepth + 1)); } return stringBuffer.toString(); } String _isolateToString(Isolate isolate, {int tabDepth: 0}) { final Uri vmServiceAddr = isolate.owner.vmService.httpAddress; final String name = isolate.name; final String shortName = name.substring(0, name.indexOf('\$')); final String main = '\$main-'; final String number = name.substring(name.indexOf(main) + main.length); // The Observatory requires somewhat non-standard URIs that the Uri class // can't build for us, so instead we build them by hand. final String isolateIdQuery = "?isolateId=isolates%2F$number"; final String isolateAddr = "$vmServiceAddr/#/inspect$isolateIdQuery"; final String debuggerAddr = "$vmServiceAddr/#/debugger$isolateIdQuery"; final String newUsed = getSizeAsMB(isolate.newSpace.used); final String newCap = getSizeAsMB(isolate.newSpace.capacity); final String newFreq = '${isolate.newSpace.avgCollectionTime.inMilliseconds}ms'; final String newPer = '${isolate.newSpace.avgCollectionPeriod.inSeconds}s'; final String oldUsed = getSizeAsMB(isolate.oldSpace.used); final String oldCap = getSizeAsMB(isolate.oldSpace.capacity); final String oldFreq = '${isolate.oldSpace.avgCollectionTime.inMilliseconds}ms'; final String oldPer = '${isolate.oldSpace.avgCollectionPeriod.inSeconds}s'; final String external = getSizeAsMB(isolate.newSpace.external + isolate.oldSpace.external); final String tabs = '\t' * tabDepth; final String extraTabs = '\t' * (tabDepth + 1); return '$tabs$_bold$shortName$_reset\n' '${extraTabs}Isolate number: $number\n' '${extraTabs}Observatory: $isolateAddr\n' '${extraTabs}Debugger: $debuggerAddr\n' '${extraTabs}New gen: $newUsed used of $newCap, GC: $newFreq every $newPer\n' '${extraTabs}Old gen: $oldUsed used of $oldCap, GC: $oldFreq every $oldPer\n' '${extraTabs}External: $external\n'; } Future _listVMs(List ports) async { for (int port in ports) { final VMService vmService = _getVMService(port); await vmService.getVM(); await vmService.waitForViews(); printStatus(_vmServiceToString(vmService)); } } void _validateArguments() { _fuchsiaRoot = argResults['fuchsia-root']; if (_fuchsiaRoot == null) throwToolExit('Please give the location of the Fuchsia tree with --fuchsia-root.'); if (!_directoryExists(_fuchsiaRoot)) throwToolExit('Specified --fuchsia-root "$_fuchsiaRoot" does not exist.'); _address = argResults['address']; if (_address == null) 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']; if (_list) { // For --list, we only need the device address and the Fuchsia tree root. return; } final String gnTarget = argResults['gn-target']; if (gnTarget == null) throwToolExit('Give the GN target with --gn-target(-g).'); final List targetInfo = _extractPathAndName(gnTarget); _projectRoot = targetInfo[0]; _projectName = targetInfo[1]; _fuchsiaProjectPath = '$_fuchsiaRoot/$_projectRoot'; if (!_directoryExists(_fuchsiaProjectPath)) throwToolExit('Target does not exist in the Fuchsia tree: $_fuchsiaProjectPath.'); final String relativeTarget = argResults['target']; if (relativeTarget == null) throwToolExit('Give the application entry point with --target.'); _target = '$_fuchsiaProjectPath/$relativeTarget'; if (!_fileExists(_target)) throwToolExit('Couldn\'t find application entry point at $_target.'); final String packagesFileName = '${_projectName}_dart_package.packages'; _dotPackagesPath = '$_fuchsiaRoot/out/$_buildType/gen/$_projectRoot/$packagesFileName'; if (!_fileExists(_dotPackagesPath)) throwToolExit('Couldn\'t find .packages file at $_dotPackagesPath.'); final String nameOverride = argResults['name-override']; if (nameOverride == null) { _binaryName = _projectName; } else { _binaryName = nameOverride; } final String isolateNumber = argResults['isolate-number']; if (isolateNumber == null) { _isolateNumber = ''; } else { _isolateNumber = '-$isolateNumber'; } } List _extractPathAndName(String gnTarget) { final String errorMessage = 'fuchsia_reload --target "$gnTarget" should have the form: ' '"//path/to/app:name"'; // Separate strings like //path/to/target:app into [path/to/target, app] final int lastColon = gnTarget.lastIndexOf(':'); if (lastColon < 0) throwToolExit(errorMessage); final String name = gnTarget.substring(lastColon + 1); // Skip '//' and chop off after : if ((gnTarget.length < 3) || (gnTarget[0] != '/') || (gnTarget[1] != '/')) throwToolExit(errorMessage); final String path = gnTarget.substring(2, lastColon); return [path, name]; } Future> _getServicePorts() async { final FuchsiaDeviceCommandRunner runner = new FuchsiaDeviceCommandRunner(_address, _fuchsiaRoot, _buildType); final List lsOutput = await runner.run('ls /tmp/dart.services'); final List ports = []; for (String s in lsOutput) { final String trimmed = s.trim(); final int lastSpace = trimmed.lastIndexOf(' '); final String lastWord = trimmed.substring(lastSpace + 1); if ((lastWord != '.') && (lastWord != '..')) { final int value = int.parse(lastWord, onError: (_) => null); if (value != null) ports.add(value); } } return ports; } bool _directoryExists(String path) { final Directory d = fs.directory(path); return d.existsSync(); } bool _fileExists(String path) { final File f = fs.file(path); return f.existsSync(); } } class FuchsiaDeviceCommandRunner { // TODO(zra): Get rid of _address and instead use // $_fuchsiaRoot/out/build-magenta/tools/netaddr --fuchsia final String _address; final String _buildType; final String _fuchsiaRoot; FuchsiaDeviceCommandRunner(this._address, this._fuchsiaRoot, this._buildType); Future> run(String command) async { final String config = '$_fuchsiaRoot/out/$_buildType/ssh-keys/ssh_config'; final List args = ['-F', config, _address, command]; printTrace('ssh ${args.join(' ')}'); final ProcessResult result = await processManager.run(['ssh', '-F', config, _address, command]); if (result.exitCode != 0) { printStatus("Command failed: $command\nstdout: ${result.stdout}\nstderr: ${result.stderr}"); return null; } printTrace(result.stdout); return result.stdout.split('\n'); } }