|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import "dart:async"; |
| 6 | +import "dart:io"; |
| 7 | +import "dart:convert"; |
| 8 | + |
| 9 | +import 'package:expect/expect.dart'; |
| 10 | +import 'package:path/path.dart' as path; |
| 11 | + |
| 12 | +main(List<String> args) async { |
| 13 | + if (!Platform.executable.endsWith("dart_precompiled_runtime")) { |
| 14 | + return; // Running in JIT: AOT binaries not available. |
| 15 | + } |
| 16 | + |
| 17 | + if (Platform.isAndroid) { |
| 18 | + return; // SDK tree and gen_snapshot not available on the test device. |
| 19 | + } |
| 20 | + |
| 21 | + final buildDir = path.dirname(Platform.executable); |
| 22 | + final sdkDir = path.dirname(path.dirname(buildDir)); |
| 23 | + final platformDill = path.join(buildDir, 'vm_platform_strong.dill'); |
| 24 | + final genSnapshot = path.join(buildDir, 'gen_snapshot'); |
| 25 | + final aotRuntime = path.join(buildDir, 'dart_precompiled_runtime'); |
| 26 | + |
| 27 | + await withTempDir((String tempDir) async { |
| 28 | + final script = path.join(sdkDir, 'pkg/kernel/bin/dump.dart'); |
| 29 | + final scriptDill = path.join(tempDir, 'kernel_dump.dill'); |
| 30 | + final appHeapsnapshot = path.join(tempDir, 'app.heapsnapshot'); |
| 31 | + final appSizesJson = path.join(tempDir, 'app-sizes.json'); |
| 32 | + |
| 33 | + // Compile script to Kernel IR. |
| 34 | + await run('pkg/vm/tool/gen_kernel', <String>[ |
| 35 | + '--aot', |
| 36 | + '--platform=$platformDill', |
| 37 | + '-o', |
| 38 | + scriptDill, |
| 39 | + script, |
| 40 | + ]); |
| 41 | + |
| 42 | + // Run the AOT compiler with the size information flags set. |
| 43 | + final elfFile = path.join(tempDir, 'aot.snapshot'); |
| 44 | + await Future.wait(<Future>[ |
| 45 | + run(genSnapshot, <String>[ |
| 46 | + '--snapshot-kind=app-aot-elf', |
| 47 | + '--print-instructions-sizes-to=$appSizesJson', |
| 48 | + '--write-v8-snapshot-profile-to=$appHeapsnapshot', |
| 49 | + '--elf=$elfFile', |
| 50 | + scriptDill, |
| 51 | + ]), |
| 52 | + ]); |
| 53 | + |
| 54 | + // Ensure we can actually run the code. |
| 55 | + await Future.wait(<Future>[ |
| 56 | + run(aotRuntime, <String>[ |
| 57 | + elfFile, |
| 58 | + scriptDill, |
| 59 | + path.join(tempDir, 'ignored.txt'), |
| 60 | + ]), |
| 61 | + ]); |
| 62 | + |
| 63 | + // Ensure we can read the files and they look legitimate. |
| 64 | + final appHeapsnapshotBytes = await readFile(appHeapsnapshot); |
| 65 | + final snapshotMap = json.decode(appHeapsnapshotBytes); |
| 66 | + Expect.isTrue(snapshotMap is Map); |
| 67 | + Expect.isTrue(snapshotMap.keys.contains('snapshot')); |
| 68 | + |
| 69 | + final appSizesJsonBytes = await readFile(appSizesJson); |
| 70 | + final sizeList = json.decode(appSizesJsonBytes); |
| 71 | + Expect.isTrue(sizeList is List); |
| 72 | + Expect.isTrue(sizeList[0] is Map); |
| 73 | + Expect.isTrue(sizeList[0].keys.toSet().containsAll(['n', 's'])); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +Future<String> readFile(String file) { |
| 78 | + return new File(file).readAsString(); |
| 79 | +} |
| 80 | + |
| 81 | +Future run(String executable, List<String> args) async { |
| 82 | + print('Running $executable ${args.join(' ')}'); |
| 83 | + |
| 84 | + final result = await Process.run(executable, args); |
| 85 | + final String stdout = result.stdout; |
| 86 | + final String stderr = result.stderr; |
| 87 | + if (stdout.isNotEmpty) { |
| 88 | + print('stdout:'); |
| 89 | + print(stdout); |
| 90 | + } |
| 91 | + if (stderr.isNotEmpty) { |
| 92 | + print('stderr:'); |
| 93 | + print(stderr); |
| 94 | + } |
| 95 | + |
| 96 | + if (result.exitCode != 0) { |
| 97 | + throw 'Command failed with non-zero exit code (was ${result.exitCode})'; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +Future withTempDir(Future fun(String dir)) async { |
| 102 | + final tempDir = |
| 103 | + Directory.systemTemp.createTempSync('aot-size-info-flags-test'); |
| 104 | + try { |
| 105 | + await fun(tempDir.path); |
| 106 | + } finally { |
| 107 | + tempDir.deleteSync(recursive: true); |
| 108 | + } |
| 109 | +} |
0 commit comments