PackageManagerServiceCompilerMapping.java revision de0272b85c4ff1eba1c58a3cdfd53bd5eb9925ef
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
17package com.android.server.pm;
18
19import android.os.SystemProperties;
20
21import dalvik.system.DexFile;
22
23/**
24 * Manage (retrieve) mappings from compilation reason to compilation filter.
25 */
26public class PackageManagerServiceCompilerMapping {
27    // Names for compilation reasons.
28    static final String REASON_STRINGS[] = {
29            "first-boot", "boot", "install", "bg-dexopt", "ab-ota", "shared-apk", "forced-dexopt"
30    };
31
32    // Static block to ensure the strings array is of the right length.
33    static {
34        if (PackageManagerService.REASON_LAST + 1 != REASON_STRINGS.length) {
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) {
59            case PackageManagerService.REASON_SHARED_APK:
60            case PackageManagerService.REASON_FORCED_DEXOPT:
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
78        for (int reason = 0; reason <= PackageManagerService.REASON_LAST; reason++) {
79            try {
80                // Check that the system property name is legal.
81                String sysPropName = getSystemPropertyName(reason);
82                if (sysPropName == null || sysPropName.isEmpty()) {
83                    throw new IllegalStateException("Reason system property name \"" +
84                            sysPropName +"\" for reason " + REASON_STRINGS[reason]);
85                }
86
87                // Check validity, ignore result.
88                getAndCheckValidity(reason);
89            } catch (Exception exc) {
90                if (toThrow == null) {
91                    toThrow = new IllegalStateException("PMS compiler filter settings are bad.");
92                }
93                toThrow.addSuppressed(exc);
94            }
95        }
96
97        if (toThrow != null) {
98            throw toThrow;
99        }
100    }
101
102    public static String getCompilerFilterForReason(int reason) {
103        return getAndCheckValidity(reason);
104    }
105
106    /**
107     * Return the compiler filter for "full" compilation.
108     *
109     * We derive that from the traditional "dalvik.vm.dex2oat-filter" property and just make
110     * sure this isn't profile-guided. Returns "speed" in case of invalid (or missing) values.
111     */
112    public static String getFullCompilerFilter() {
113        String value = SystemProperties.get("dalvik.vm.dex2oat-filter");
114        if (value == null || value.isEmpty()) {
115            return "speed";
116        }
117
118        if (!DexFile.isValidCompilerFilter(value) ||
119                DexFile.isProfileGuidedCompilerFilter(value)) {
120            return "speed";
121        }
122
123        return value;
124    }
125
126    /**
127     * Return the non-profile-guided filter corresponding to the given filter.
128     */
129    public static String getNonProfileGuidedCompilerFilter(String filter) {
130        return DexFile.getNonProfileGuidedCompilerFilter(filter);
131    }
132}
133