Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
| 19 | import android.os.SystemProperties; |
| 20 | |
| 21 | import dalvik.system.DexFile; |
| 22 | |
| 23 | /** |
| 24 | * Manage (retrieve) mappings from compilation reason to compilation filter. |
| 25 | */ |
| 26 | class PackageManagerServiceCompilerMapping { |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 27 | // Names for compilation reasons. |
| 28 | static final String REASON_STRINGS[] = { |
| 29 | "boot", "install", "bg-dexopt", "ab-ota", "nsys-library", "shared-apk", "forced-dexopt" |
| 30 | }; |
| 31 | |
| 32 | // Static block to ensure the strings array is of the right length. |
| 33 | static { |
Andreas Gampe | 43fc244 | 2016-03-24 20:13:34 -0700 | [diff] [blame] | 34 | if (PackageManagerService.REASON_LAST + 1 != REASON_STRINGS.length) { |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 35 | throw new IllegalStateException("REASON_STRINGS not correct"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private static String getSystemPropertyName(int reason) { |
| 40 | if (reason < 0 || reason >= REASON_STRINGS.length) { |
| 41 | throw new IllegalArgumentException("reason " + reason + " invalid"); |
| 42 | } |
| 43 | |
| 44 | return "pm.dexopt." + REASON_STRINGS[reason]; |
| 45 | } |
| 46 | |
| 47 | // Load the property for the given reason and check for validity. This will throw an |
| 48 | // exception in case the reason or value are invalid. |
| 49 | private static String getAndCheckValidity(int reason) { |
| 50 | String sysPropValue = SystemProperties.get(getSystemPropertyName(reason)); |
| 51 | if (sysPropValue == null || sysPropValue.isEmpty() || |
| 52 | !DexFile.isValidCompilerFilter(sysPropValue)) { |
| 53 | throw new IllegalStateException("Value \"" + sysPropValue +"\" not valid " |
| 54 | + "(reason " + REASON_STRINGS[reason] + ")"); |
| 55 | } |
| 56 | |
| 57 | // Ensure that some reasons are not mapped to profile-guided filters. |
| 58 | switch (reason) { |
Andreas Gampe | 43fc244 | 2016-03-24 20:13:34 -0700 | [diff] [blame] | 59 | case PackageManagerService.REASON_SHARED_APK: |
| 60 | case PackageManagerService.REASON_FORCED_DEXOPT: |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 61 | if (DexFile.isProfileGuidedCompilerFilter(sysPropValue)) { |
| 62 | throw new IllegalStateException("\"" + sysPropValue + "\" is profile-guided, " |
| 63 | + "but not allowed for " + REASON_STRINGS[reason]); |
| 64 | } |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | return sysPropValue; |
| 69 | } |
| 70 | |
| 71 | // Check that the properties are set and valid. |
| 72 | // Note: this is done in a separate method so this class can be statically initialized. |
| 73 | static void checkProperties() { |
| 74 | // We're gonna check all properties and collect the exceptions, so we can give a general |
| 75 | // overview. Store the exceptions here. |
| 76 | RuntimeException toThrow = null; |
| 77 | |
Andreas Gampe | 43fc244 | 2016-03-24 20:13:34 -0700 | [diff] [blame] | 78 | for (int reason = 0; reason <= PackageManagerService.REASON_LAST; reason++) { |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 79 | try { |
| 80 | // Check that the system property name is legal. |
| 81 | String sysPropName = getSystemPropertyName(reason); |
| 82 | if (sysPropName == null || |
| 83 | sysPropName.isEmpty() || |
| 84 | sysPropName.length() > SystemProperties.PROP_NAME_MAX) { |
| 85 | throw new IllegalStateException("Reason system property name \"" + |
| 86 | sysPropName +"\" for reason " + REASON_STRINGS[reason]); |
| 87 | } |
| 88 | |
| 89 | // Check validity, ignore result. |
| 90 | getAndCheckValidity(reason); |
| 91 | } catch (Exception exc) { |
| 92 | if (toThrow == null) { |
| 93 | toThrow = new IllegalStateException("PMS compiler filter settings are bad."); |
| 94 | } |
| 95 | toThrow.addSuppressed(exc); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (toThrow != null) { |
| 100 | throw toThrow; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public static String getCompilerFilterForReason(int reason) { |
| 105 | return getAndCheckValidity(reason); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return the compiler filter for "full" compilation. |
| 110 | * |
| 111 | * We derive that from the traditional "dalvik.vm.dex2oat-filter" property and just make |
| 112 | * sure this isn't profile-guided. Returns "speed" in case of invalid (or missing) values. |
| 113 | */ |
| 114 | public static String getFullCompilerFilter() { |
| 115 | String value = SystemProperties.get("dalvik.vm.dex2oat-filter"); |
| 116 | if (value == null || value.isEmpty()) { |
| 117 | return "speed"; |
| 118 | } |
| 119 | |
| 120 | if (!DexFile.isValidCompilerFilter(value) || |
| 121 | DexFile.isProfileGuidedCompilerFilter(value)) { |
| 122 | return "speed"; |
| 123 | } |
| 124 | |
| 125 | return value; |
| 126 | } |
| 127 | |
| 128 | } |