OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "include/dart_tools_api.h" | 10 #include "include/dart_tools_api.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we | 37 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we |
38 // link in a snapshot otherwise it is initialized to NULL. | 38 // link in a snapshot otherwise it is initialized to NULL. |
39 extern const uint8_t* vm_isolate_snapshot_buffer; | 39 extern const uint8_t* vm_isolate_snapshot_buffer; |
40 | 40 |
41 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a | 41 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a |
42 // snapshot otherwise it is initialized to NULL. | 42 // snapshot otherwise it is initialized to NULL. |
43 extern const uint8_t* isolate_snapshot_buffer; | 43 extern const uint8_t* isolate_snapshot_buffer; |
44 | 44 |
45 /** | 45 /** |
46 * Global state used to control and store generation of application snapshots | 46 * Global state used to control and store generation of application snapshots |
47 * (script/full). | 47 * An application snapshot can be generated and run using the following |
48 * A full application snapshot can be generated and run using the following | 48 * command |
49 * commands | 49 * dart --snapshot-kind=app-jit --snapshot=<app_snapshot_filename> |
50 * - Generating a full application snapshot : | 50 * <script_uri> [<script_options>] |
51 * dart_bootstrap --full-snapshot-after-run=<filename> --package-root=<dirs> | 51 * To Run the application snapshot generated above, use : |
52 * <script_uri> [<script_options>] | 52 * dart <app_snapshot_filename> [<script_options>] |
rmacnak
2016/10/18 23:55:46
We still need the no code option as a command line
siva
2016/10/19 22:16:52
Addressed by the change of generating an app snaps
| |
53 * - Running the full application snapshot generated above : | |
54 * dart --run-full-snapshot=<filename> <script_uri> [<script_options>] | |
55 */ | 53 */ |
56 static bool run_app_snapshot = false; | 54 static bool run_app_snapshot = false; |
57 static const char* snapshot_filename = NULL; | 55 static const char* snapshot_filename = NULL; |
58 enum SnapshotKind { | 56 enum SnapshotKind { |
59 kNone, | 57 kNone, |
60 kScript, | 58 kScript, |
61 kAppAOT, | 59 kAppAOT, |
62 kAppJITAfterRun, | 60 kAppJIT, |
63 kAppAfterRun, | |
64 }; | 61 }; |
65 static SnapshotKind gen_snapshot_kind = kNone; | 62 static SnapshotKind gen_snapshot_kind = kNone; |
66 | 63 |
67 // Value of the --package-root flag. | 64 // Value of the --package-root flag. |
68 // (This pointer points into an argv buffer and does not need to be | 65 // (This pointer points into an argv buffer and does not need to be |
69 // free'd.) | 66 // free'd.) |
70 static const char* commandline_package_root = NULL; | 67 static const char* commandline_package_root = NULL; |
71 | 68 |
72 // Value of the --packages flag. | 69 // Value of the --packages flag. |
73 // (This pointer points into an argv buffer and does not need to be | 70 // (This pointer points into an argv buffer and does not need to be |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 | 350 |
354 | 351 |
355 static bool ProcessSnapshotKindOption(const char* kind, | 352 static bool ProcessSnapshotKindOption(const char* kind, |
356 CommandLineOptions* vm_options) { | 353 CommandLineOptions* vm_options) { |
357 if (strcmp(kind, "script") == 0) { | 354 if (strcmp(kind, "script") == 0) { |
358 gen_snapshot_kind = kScript; | 355 gen_snapshot_kind = kScript; |
359 return true; | 356 return true; |
360 } else if (strcmp(kind, "app-aot") == 0) { | 357 } else if (strcmp(kind, "app-aot") == 0) { |
361 gen_snapshot_kind = kAppAOT; | 358 gen_snapshot_kind = kAppAOT; |
362 return true; | 359 return true; |
363 } else if (strcmp(kind, "app-jit-after-run") == 0) { | 360 } else if (strcmp(kind, "app-jit") == 0) { |
364 gen_snapshot_kind = kAppJITAfterRun; | 361 gen_snapshot_kind = kAppJIT; |
365 return true; | |
366 } else if (strcmp(kind, "app-after-run") == 0) { | |
367 gen_snapshot_kind = kAppAfterRun; | |
368 return true; | 362 return true; |
369 } | 363 } |
370 Log::PrintErr("Unrecognized snapshot kind: '%s'\nValid kinds are: " | 364 Log::PrintErr("Unrecognized snapshot kind: '%s'\nValid kinds are: " |
371 "script, app-aot, app-jit-after-run, app-after-run\n", kind); | 365 "script, app-aot, app-jit\n", kind); |
372 return false; | 366 return false; |
373 } | 367 } |
374 | 368 |
375 | 369 |
376 static bool ProcessEnableVmServiceOption(const char* option_value, | 370 static bool ProcessEnableVmServiceOption(const char* option_value, |
377 CommandLineOptions* vm_options) { | 371 CommandLineOptions* vm_options) { |
378 ASSERT(option_value != NULL); | 372 ASSERT(option_value != NULL); |
379 | 373 |
380 if (!ExtractPortAndIP(option_value, | 374 if (!ExtractPortAndIP(option_value, |
381 &vm_service_server_port, | 375 &vm_service_server_port, |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1145 int64_t data[File::kStatSize]; | 1139 int64_t data[File::kStatSize]; |
1146 File::Stat(url + 7, data); | 1140 File::Stat(url + 7, data); |
1147 if (data[File::kType] == File::kDoesNotExist) { | 1141 if (data[File::kType] == File::kDoesNotExist) { |
1148 return true; | 1142 return true; |
1149 } | 1143 } |
1150 bool modified = data[File::kModifiedTime] > since; | 1144 bool modified = data[File::kModifiedTime] > since; |
1151 return modified; | 1145 return modified; |
1152 } | 1146 } |
1153 | 1147 |
1154 | 1148 |
1155 static void WriteSnapshotFile(const char* snapshot_directory, | 1149 static void WriteSnapshotFile(const char* filename, |
1156 const char* filename, | |
1157 bool write_magic_number, | 1150 bool write_magic_number, |
1158 const uint8_t* buffer, | 1151 const uint8_t* buffer, |
1159 const intptr_t size) { | 1152 const intptr_t size) { |
1160 char* concat = NULL; | 1153 char* concat = NULL; |
1161 const char* qualified_filename; | 1154 File* file = File::Open(filename, File::kWriteTruncate); |
1162 if ((snapshot_directory != NULL) && (strlen(snapshot_directory) > 0)) { | |
1163 intptr_t len = snprintf(NULL, 0, "%s/%s", snapshot_directory, filename); | |
1164 concat = new char[len + 1]; | |
1165 snprintf(concat, len + 1, "%s/%s", snapshot_directory, filename); | |
1166 qualified_filename = concat; | |
1167 } else { | |
1168 qualified_filename = filename; | |
1169 } | |
1170 | |
1171 File* file = File::Open(qualified_filename, File::kWriteTruncate); | |
1172 if (file == NULL) { | 1155 if (file == NULL) { |
1173 ErrorExit(kErrorExitCode, | 1156 ErrorExit(kErrorExitCode, |
1174 "Unable to open file %s for writing snapshot\n", | 1157 "Unable to open file %s for writing snapshot\n", |
1175 qualified_filename); | 1158 filename); |
1176 } | 1159 } |
1177 | 1160 |
1178 if (write_magic_number) { | 1161 if (write_magic_number) { |
1179 // Write the magic number to indicate file is a script snapshot. | 1162 // Write the magic number to indicate file is a script snapshot. |
1180 DartUtils::WriteMagicNumber(file); | 1163 DartUtils::WriteMagicNumber(file); |
1181 } | 1164 } |
1182 | 1165 |
1183 if (!file->WriteFully(buffer, size)) { | 1166 if (!file->WriteFully(buffer, size)) { |
1184 ErrorExit(kErrorExitCode, | 1167 ErrorExit(kErrorExitCode, |
1185 "Unable to write file %s for writing snapshot\n", | 1168 "Unable to write file %s for writing snapshot\n", |
1186 qualified_filename); | 1169 filename); |
1187 } | 1170 } |
1188 file->Release(); | 1171 file->Release(); |
1189 if (concat != NULL) { | 1172 if (concat != NULL) { |
1190 delete concat; | 1173 delete concat; |
1191 } | 1174 } |
1192 } | 1175 } |
1193 | 1176 |
1194 | 1177 |
1195 static const int64_t kAppSnapshotHeaderSize = 5 * sizeof(int64_t); // NOLINT | 1178 static const int64_t kAppSnapshotHeaderSize = 5 * sizeof(int64_t); // NOLINT |
1196 static const int64_t kAppSnapshotMagicNumber = 0xf6f6dcdc; | 1179 static const int64_t kAppSnapshotMagicNumber = 0xf6f6dcdc; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1390 | 1373 |
1391 static void GenerateScriptSnapshot() { | 1374 static void GenerateScriptSnapshot() { |
1392 // First create a snapshot. | 1375 // First create a snapshot. |
1393 uint8_t* buffer = NULL; | 1376 uint8_t* buffer = NULL; |
1394 intptr_t size = 0; | 1377 intptr_t size = 0; |
1395 Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size); | 1378 Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size); |
1396 if (Dart_IsError(result)) { | 1379 if (Dart_IsError(result)) { |
1397 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | 1380 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
1398 } | 1381 } |
1399 | 1382 |
1400 WriteSnapshotFile(NULL, snapshot_filename, true, buffer, size); | 1383 WriteSnapshotFile(snapshot_filename, true, buffer, size); |
1401 } | 1384 } |
1402 | 1385 |
1403 | 1386 |
1404 static void GeneratePrecompiledSnapshot() { | 1387 static void GeneratePrecompiledSnapshot() { |
1405 uint8_t* vm_isolate_buffer = NULL; | 1388 uint8_t* vm_isolate_buffer = NULL; |
1406 intptr_t vm_isolate_size = 0; | 1389 intptr_t vm_isolate_size = 0; |
1407 uint8_t* isolate_buffer = NULL; | 1390 uint8_t* isolate_buffer = NULL; |
1408 intptr_t isolate_size = 0; | 1391 intptr_t isolate_size = 0; |
1409 uint8_t* assembly_buffer = NULL; | 1392 uint8_t* assembly_buffer = NULL; |
1410 intptr_t assembly_size = 0; | 1393 intptr_t assembly_size = 0; |
(...skipping 24 matching lines...) Expand all Loading... | |
1435 WriteAppSnapshot(snapshot_filename, | 1418 WriteAppSnapshot(snapshot_filename, |
1436 vm_isolate_buffer, | 1419 vm_isolate_buffer, |
1437 vm_isolate_size, | 1420 vm_isolate_size, |
1438 isolate_buffer, | 1421 isolate_buffer, |
1439 isolate_size, | 1422 isolate_size, |
1440 instructions_blob_buffer, | 1423 instructions_blob_buffer, |
1441 instructions_blob_size, | 1424 instructions_blob_size, |
1442 rodata_blob_buffer, | 1425 rodata_blob_buffer, |
1443 rodata_blob_size); | 1426 rodata_blob_size); |
1444 } else { | 1427 } else { |
1445 WriteSnapshotFile(NULL, snapshot_filename, | 1428 WriteSnapshotFile(snapshot_filename, |
1446 false, | 1429 false, |
1447 assembly_buffer, | 1430 assembly_buffer, |
1448 assembly_size); | 1431 assembly_size); |
1449 } | 1432 } |
1450 } | 1433 } |
1451 | 1434 |
1452 | 1435 |
1453 static void GeneratePrecompiledJITSnapshot() { | 1436 static void GeneratePrecompiledJITSnapshot() { |
1454 uint8_t* vm_isolate_buffer = NULL; | 1437 uint8_t* vm_isolate_buffer = NULL; |
1455 intptr_t vm_isolate_size = 0; | 1438 intptr_t vm_isolate_size = 0; |
(...skipping 20 matching lines...) Expand all Loading... | |
1476 vm_isolate_size, | 1459 vm_isolate_size, |
1477 isolate_buffer, | 1460 isolate_buffer, |
1478 isolate_size, | 1461 isolate_size, |
1479 instructions_blob_buffer, | 1462 instructions_blob_buffer, |
1480 instructions_blob_size, | 1463 instructions_blob_size, |
1481 rodata_blob_buffer, | 1464 rodata_blob_buffer, |
1482 rodata_blob_size); | 1465 rodata_blob_size); |
1483 } | 1466 } |
1484 | 1467 |
1485 | 1468 |
1486 static void GenerateFullSnapshot() { | |
1487 // Create a full snapshot of the script. | |
1488 Dart_Handle result; | |
1489 uint8_t* vm_isolate_buffer = NULL; | |
1490 intptr_t vm_isolate_size = 0; | |
1491 uint8_t* isolate_buffer = NULL; | |
1492 intptr_t isolate_size = 0; | |
1493 | |
1494 result = Dart_CreateSnapshot(&vm_isolate_buffer, | |
1495 &vm_isolate_size, | |
1496 &isolate_buffer, | |
1497 &isolate_size); | |
1498 if (Dart_IsError(result)) { | |
1499 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | |
1500 } | |
1501 | |
1502 WriteAppSnapshot(snapshot_filename, | |
1503 vm_isolate_buffer, | |
1504 vm_isolate_size, | |
1505 isolate_buffer, | |
1506 isolate_size, | |
1507 NULL, 0, NULL, 0); | |
1508 } | |
1509 | |
1510 | |
1511 #define CHECK_RESULT(result) \ | 1469 #define CHECK_RESULT(result) \ |
1512 if (Dart_IsError(result)) { \ | 1470 if (Dart_IsError(result)) { \ |
1513 if (Dart_IsVMRestartRequest(result)) { \ | 1471 if (Dart_IsVMRestartRequest(result)) { \ |
1514 Dart_ExitScope(); \ | 1472 Dart_ExitScope(); \ |
1515 Dart_ShutdownIsolate(); \ | 1473 Dart_ShutdownIsolate(); \ |
1516 return true; \ | 1474 return true; \ |
1517 } \ | 1475 } \ |
1518 const int exit_code = Dart_IsCompilationError(result) ? \ | 1476 const int exit_code = Dart_IsCompilationError(result) ? \ |
1519 kCompilationErrorExitCode : kErrorExitCode; \ | 1477 kCompilationErrorExitCode : kErrorExitCode; \ |
1520 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ | 1478 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1567 } else { | 1525 } else { |
1568 // Lookup the library of the root script. | 1526 // Lookup the library of the root script. |
1569 Dart_Handle root_lib = Dart_RootLibrary(); | 1527 Dart_Handle root_lib = Dart_RootLibrary(); |
1570 // Import the root library into the builtin library so that we can easily | 1528 // Import the root library into the builtin library so that we can easily |
1571 // lookup the main entry point exported from the root library. | 1529 // lookup the main entry point exported from the root library. |
1572 IsolateData* isolate_data = | 1530 IsolateData* isolate_data = |
1573 reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate)); | 1531 reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate)); |
1574 result = Dart_LibraryImportLibrary( | 1532 result = Dart_LibraryImportLibrary( |
1575 isolate_data->builtin_lib(), root_lib, Dart_Null()); | 1533 isolate_data->builtin_lib(), root_lib, Dart_Null()); |
1576 if (is_noopt || | 1534 if (is_noopt || |
1577 (gen_snapshot_kind == kAppAfterRun) || | |
1578 (gen_snapshot_kind == kAppAOT) || | 1535 (gen_snapshot_kind == kAppAOT) || |
1579 (gen_snapshot_kind == kAppJITAfterRun)) { | 1536 (gen_snapshot_kind == kAppJIT)) { |
1580 // Load the embedder's portion of the VM service's Dart code so it will | 1537 // Load the embedder's portion of the VM service's Dart code so it will |
1581 // be included in the app snapshot. | 1538 // be included in the app snapshot. |
1582 if (!VmService::LoadForGenPrecompiled()) { | 1539 if (!VmService::LoadForGenPrecompiled()) { |
1583 fprintf(stderr, | 1540 fprintf(stderr, |
1584 "VM service loading failed: %s\n", | 1541 "VM service loading failed: %s\n", |
1585 VmService::GetErrorMessage()); | 1542 VmService::GetErrorMessage()); |
1586 fflush(stderr); | 1543 fflush(stderr); |
1587 exit(kErrorExitCode); | 1544 exit(kErrorExitCode); |
1588 } | 1545 } |
1589 } | 1546 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1671 Dart_Handle isolate_lib = | 1628 Dart_Handle isolate_lib = |
1672 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); | 1629 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); |
1673 result = Dart_Invoke(isolate_lib, | 1630 result = Dart_Invoke(isolate_lib, |
1674 Dart_NewStringFromCString("_startMainIsolate"), | 1631 Dart_NewStringFromCString("_startMainIsolate"), |
1675 kNumIsolateArgs, isolate_args); | 1632 kNumIsolateArgs, isolate_args); |
1676 CHECK_RESULT(result); | 1633 CHECK_RESULT(result); |
1677 | 1634 |
1678 // Keep handling messages until the last active receive port is closed. | 1635 // Keep handling messages until the last active receive port is closed. |
1679 result = Dart_RunLoop(); | 1636 result = Dart_RunLoop(); |
1680 // Generate an app snapshot after execution if specified. | 1637 // Generate an app snapshot after execution if specified. |
1681 if ((gen_snapshot_kind == kAppAfterRun) || | 1638 if ((gen_snapshot_kind == kAppJIT)) { |
1682 (gen_snapshot_kind == kAppJITAfterRun)) { | |
1683 if (!Dart_IsCompilationError(result) && | 1639 if (!Dart_IsCompilationError(result) && |
rmacnak
2016/10/19 17:41:21
To get us going, perhaps
#if defined(TARGET_ARCH_
siva
2016/10/19 22:16:52
Done.
| |
1684 !Dart_IsVMRestartRequest(result)) { | 1640 !Dart_IsVMRestartRequest(result)) { |
1685 if (gen_snapshot_kind == kAppAfterRun) { | 1641 Dart_Handle prepare_result = Dart_PrecompileJIT(); |
1686 GenerateFullSnapshot(); | 1642 CHECK_RESULT(prepare_result); |
1687 } else { | 1643 GeneratePrecompiledJITSnapshot(); |
1688 Dart_Handle prepare_result = Dart_PrecompileJIT(); | |
1689 CHECK_RESULT(prepare_result); | |
1690 GeneratePrecompiledJITSnapshot(); | |
1691 } | |
1692 } | 1644 } |
1693 } | 1645 } |
1694 CHECK_RESULT(result); | 1646 CHECK_RESULT(result); |
1695 } | 1647 } |
1696 } | 1648 } |
1697 | 1649 |
1698 Dart_ExitScope(); | 1650 Dart_ExitScope(); |
1699 // Shutdown the isolate. | 1651 // Shutdown the isolate. |
1700 Dart_ShutdownIsolate(); | 1652 Dart_ShutdownIsolate(); |
1701 | 1653 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1859 run_app_snapshot = true; | 1811 run_app_snapshot = true; |
1860 } | 1812 } |
1861 | 1813 |
1862 #if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME) | 1814 #if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME) |
1863 // Constant true if PRODUCT or DART_PRECOMPILED_RUNTIME. | 1815 // Constant true if PRODUCT or DART_PRECOMPILED_RUNTIME. |
1864 if ((gen_snapshot_kind != kNone) || run_app_snapshot) { | 1816 if ((gen_snapshot_kind != kNone) || run_app_snapshot) { |
1865 vm_options.AddArgument("--load_deferred_eagerly"); | 1817 vm_options.AddArgument("--load_deferred_eagerly"); |
1866 } | 1818 } |
1867 #endif | 1819 #endif |
1868 | 1820 |
1869 if (gen_snapshot_kind == kAppJITAfterRun) { | 1821 if (gen_snapshot_kind == kAppJIT) { |
1870 vm_options.AddArgument("--fields_may_be_reset"); | 1822 vm_options.AddArgument("--fields_may_be_reset"); |
1871 } | 1823 } |
1872 if ((gen_snapshot_kind == kAppAOT) || is_noopt) { | 1824 if ((gen_snapshot_kind == kAppAOT) || is_noopt) { |
1873 vm_options.AddArgument("--precompilation"); | 1825 vm_options.AddArgument("--precompilation"); |
1874 } | 1826 } |
1875 #if defined(DART_PRECOMPILED_RUNTIME) | 1827 #if defined(DART_PRECOMPILED_RUNTIME) |
1876 vm_options.AddArgument("--precompilation"); | 1828 vm_options.AddArgument("--precompilation"); |
1877 #endif | 1829 #endif |
1878 | 1830 |
1879 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 1831 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1949 Platform::Exit(Process::GlobalExitCode()); | 1901 Platform::Exit(Process::GlobalExitCode()); |
1950 } | 1902 } |
1951 | 1903 |
1952 } // namespace bin | 1904 } // namespace bin |
1953 } // namespace dart | 1905 } // namespace dart |
1954 | 1906 |
1955 int main(int argc, char** argv) { | 1907 int main(int argc, char** argv) { |
1956 dart::bin::main(argc, argv); | 1908 dart::bin::main(argc, argv); |
1957 UNREACHABLE(); | 1909 UNREACHABLE(); |
1958 } | 1910 } |
OLD | NEW |