PackageManagerServiceCompilerMapping.java revision 84bf6b809d5b1d80137f1653ab2346ebe0b08ca6
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", "inactive"
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 sysPropName = getSystemPropertyName(reason);
51        String sysPropValue;
52        // TODO: This is a temporary hack to keep marlin booting on aosp/master while we
53        // figure out how to deal with these system properties that currently appear on
54        // vendor.
55        if ("pm.dexopt.inactive".equals(sysPropName)) {
56            sysPropValue = "verify";
57        } else {
58            sysPropValue = SystemProperties.get(sysPropName);
59        }
60        if (sysPropValue == null || sysPropValue.isEmpty() ||
61                !DexFile.isValidCompilerFilter(sysPropValue)) {
62            throw new IllegalStateException("Value \"" + sysPropValue +"\" not valid "
63                    + "(reason " + REASON_STRINGS[reason] + ")");
64        }
65
66        return sysPropValue;
67    }
68
69    // Check that the properties are set and valid.
70    // Note: this is done in a separate method so this class can be statically initialized.
71    static void checkProperties() {
72        // We're gonna check all properties and collect the exceptions, so we can give a general
73        // overview. Store the exceptions here.
74        RuntimeException toThrow = null;
75
76        for (int reason = 0; reason <= PackageManagerService.REASON_LAST; reason++) {
77            try {
78                // Check that the system property name is legal.
79                String sysPropName = getSystemPropertyName(reason);
80                if (sysPropName == null || sysPropName.isEmpty()) {
81                    throw new IllegalStateException("Reason system property name \"" +
82                            sysPropName +"\" for reason " + REASON_STRINGS[reason]);
83                }
84
85                // Check validity, ignore result.
86                getAndCheckValidity(reason);
87            } catch (Exception exc) {
88                if (toThrow == null) {
89                    toThrow = new IllegalStateException("PMS compiler filter settings are bad.");
90                }
91                toThrow.addSuppressed(exc);
92            }
93        }
94
95        if (toThrow != null) {
96            throw toThrow;
97        }
98    }
99
100    public static String getCompilerFilterForReason(int reason) {
101        return getAndCheckValidity(reason);
102    }
103
104    /**
105     * Return the default compiler filter for compilation.
106     *
107     * We derive that from the traditional "dalvik.vm.dex2oat-filter" property and just make
108     * sure this isn't profile-guided. Returns "speed" in case of invalid (or missing) values.
109     */
110    public static String getDefaultCompilerFilter() {
111        String value = SystemProperties.get("dalvik.vm.dex2oat-filter");
112        if (value == null || value.isEmpty()) {
113            return "speed";
114        }
115
116        if (!DexFile.isValidCompilerFilter(value) ||
117                DexFile.isProfileGuidedCompilerFilter(value)) {
118            return "speed";
119        }
120
121        return value;
122    }
123}
124