• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.Build;
20 import android.os.SystemProperties;
21 import android.text.TextUtils;
22 import android.util.ArraySet;
23 
24 import com.android.server.pm.parsing.pkg.AndroidPackage;
25 import com.android.server.pm.parsing.pkg.AndroidPackageUtils;
26 
27 import dalvik.system.VMRuntime;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Provides various methods for obtaining and converting of instruction sets.
34  *
35  * @hide
36  */
37 public class InstructionSets {
38     private static final String PREFERRED_INSTRUCTION_SET =
39             VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]);
40 
getAppDexInstructionSets(String primaryCpuAbi, String secondaryCpuAbi)41     public static String[] getAppDexInstructionSets(String primaryCpuAbi, String secondaryCpuAbi) {
42         if (primaryCpuAbi != null) {
43             if (secondaryCpuAbi != null) {
44                 return new String[] {
45                         VMRuntime.getInstructionSet(primaryCpuAbi),
46                         VMRuntime.getInstructionSet(secondaryCpuAbi) };
47             } else {
48                 return new String[] {
49                         VMRuntime.getInstructionSet(primaryCpuAbi) };
50             }
51         }
52 
53         return new String[] { getPreferredInstructionSet() };
54     }
55 
getPreferredInstructionSet()56     public static String getPreferredInstructionSet() {
57         return PREFERRED_INSTRUCTION_SET;
58     }
59 
60     /**
61      * Returns the instruction set that should be used to compile dex code. In the presence of
62      * a native bridge this might be different than the one shared libraries use.
63      */
getDexCodeInstructionSet(String sharedLibraryIsa)64     public static String getDexCodeInstructionSet(String sharedLibraryIsa) {
65         // TODO b/19550105 Build mapping once instead of querying each time
66         String dexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + sharedLibraryIsa);
67         return TextUtils.isEmpty(dexCodeIsa) ? sharedLibraryIsa : dexCodeIsa;
68     }
69 
getDexCodeInstructionSets(String[] instructionSets)70     public static String[] getDexCodeInstructionSets(String[] instructionSets) {
71         ArraySet<String> dexCodeInstructionSets = new ArraySet<String>(instructionSets.length);
72         for (String instructionSet : instructionSets) {
73             dexCodeInstructionSets.add(getDexCodeInstructionSet(instructionSet));
74         }
75         return dexCodeInstructionSets.toArray(new String[dexCodeInstructionSets.size()]);
76     }
77 
78     /**
79      * Returns deduplicated list of supported instructions for dex code.
80      */
getAllDexCodeInstructionSets()81     public static String[] getAllDexCodeInstructionSets() {
82         String[] supportedInstructionSets = new String[Build.SUPPORTED_ABIS.length];
83         for (int i = 0; i < supportedInstructionSets.length; i++) {
84             String abi = Build.SUPPORTED_ABIS[i];
85             supportedInstructionSets[i] = VMRuntime.getInstructionSet(abi);
86         }
87         return getDexCodeInstructionSets(supportedInstructionSets);
88     }
89 
getAllInstructionSets()90     public static List<String> getAllInstructionSets() {
91         final String[] allAbis = Build.SUPPORTED_ABIS;
92         final List<String> allInstructionSets = new ArrayList<String>(allAbis.length);
93 
94         for (String abi : allAbis) {
95             final String instructionSet = VMRuntime.getInstructionSet(abi);
96             if (!allInstructionSets.contains(instructionSet)) {
97                 allInstructionSets.add(instructionSet);
98             }
99         }
100 
101         return allInstructionSets;
102     }
103 
104     /**
105      * Calculates the primary instruction set based on the computed Abis of a given package.
106      */
getPrimaryInstructionSet(PackageAbiHelper.Abis abis)107     public static String getPrimaryInstructionSet(PackageAbiHelper.Abis abis) {
108         if (abis.primary == null) {
109             return getPreferredInstructionSet();
110         }
111 
112         return VMRuntime.getInstructionSet(abis.primary);
113     }
114 }
115