Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #include "instruction_set.h" |
| 18 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame^] | 19 | #include "base/stringprintf.h" |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | class InstructionSetTest : public CommonRuntimeTest {}; |
| 25 | |
| 26 | TEST_F(InstructionSetTest, GetInstructionSetFromString) { |
| 27 | EXPECT_EQ(kArm, GetInstructionSetFromString("arm")); |
| 28 | EXPECT_EQ(kArm64, GetInstructionSetFromString("arm64")); |
| 29 | EXPECT_EQ(kX86, GetInstructionSetFromString("x86")); |
| 30 | EXPECT_EQ(kX86_64, GetInstructionSetFromString("x86_64")); |
| 31 | EXPECT_EQ(kMips, GetInstructionSetFromString("mips")); |
| 32 | EXPECT_EQ(kNone, GetInstructionSetFromString("none")); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 33 | EXPECT_EQ(kNone, GetInstructionSetFromString("random-string")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST_F(InstructionSetTest, GetInstructionSetString) { |
| 37 | EXPECT_STREQ("arm", GetInstructionSetString(kArm)); |
| 38 | EXPECT_STREQ("arm", GetInstructionSetString(kThumb2)); |
| 39 | EXPECT_STREQ("arm64", GetInstructionSetString(kArm64)); |
| 40 | EXPECT_STREQ("x86", GetInstructionSetString(kX86)); |
| 41 | EXPECT_STREQ("x86_64", GetInstructionSetString(kX86_64)); |
| 42 | EXPECT_STREQ("mips", GetInstructionSetString(kMips)); |
| 43 | EXPECT_STREQ("none", GetInstructionSetString(kNone)); |
| 44 | } |
| 45 | |
| 46 | TEST_F(InstructionSetTest, TestRoundTrip) { |
| 47 | EXPECT_EQ(kRuntimeISA, GetInstructionSetFromString(GetInstructionSetString(kRuntimeISA))); |
| 48 | } |
| 49 | |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 50 | TEST_F(InstructionSetTest, PointerSize) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 51 | EXPECT_EQ(sizeof(void*), GetInstructionSetPointerSize(kRuntimeISA)); |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 52 | } |
| 53 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame^] | 54 | TEST_F(InstructionSetTest, X86Features) { |
| 55 | // Build features for a 32-bit x86 atom processor. |
| 56 | std::string error_msg; |
| 57 | std::unique_ptr<const InstructionSetFeatures> x86_features( |
| 58 | InstructionSetFeatures::FromVariant(kX86, "atom", &error_msg)); |
| 59 | ASSERT_TRUE(x86_features.get() != nullptr) << error_msg; |
| 60 | EXPECT_EQ(x86_features->GetInstructionSet(), kX86); |
| 61 | EXPECT_TRUE(x86_features->Equals(x86_features.get())); |
| 62 | EXPECT_STREQ("none", x86_features->GetFeatureString().c_str()); |
| 63 | EXPECT_EQ(x86_features->AsBitmap(), 0U); |
| 64 | |
| 65 | // Build features for a 32-bit x86 default processor. |
| 66 | std::unique_ptr<const InstructionSetFeatures> x86_default_features( |
| 67 | InstructionSetFeatures::FromFeatureString(kX86, "default", &error_msg)); |
| 68 | ASSERT_TRUE(x86_default_features.get() != nullptr) << error_msg; |
| 69 | EXPECT_EQ(x86_default_features->GetInstructionSet(), kX86); |
| 70 | EXPECT_TRUE(x86_default_features->Equals(x86_default_features.get())); |
| 71 | EXPECT_STREQ("none", x86_default_features->GetFeatureString().c_str()); |
| 72 | EXPECT_EQ(x86_default_features->AsBitmap(), 0U); |
| 73 | |
| 74 | // Build features for a 64-bit x86-64 atom processor. |
| 75 | std::unique_ptr<const InstructionSetFeatures> x86_64_features( |
| 76 | InstructionSetFeatures::FromVariant(kX86_64, "atom", &error_msg)); |
| 77 | ASSERT_TRUE(x86_64_features.get() != nullptr) << error_msg; |
| 78 | EXPECT_EQ(x86_64_features->GetInstructionSet(), kX86_64); |
| 79 | EXPECT_TRUE(x86_64_features->Equals(x86_64_features.get())); |
| 80 | EXPECT_STREQ("none", x86_64_features->GetFeatureString().c_str()); |
| 81 | EXPECT_EQ(x86_64_features->AsBitmap(), 0U); |
| 82 | |
| 83 | EXPECT_FALSE(x86_64_features->Equals(x86_features.get())); |
| 84 | EXPECT_FALSE(x86_64_features->Equals(x86_default_features.get())); |
| 85 | EXPECT_TRUE(x86_features->Equals(x86_default_features.get())); |
| 86 | } |
| 87 | |
| 88 | TEST_F(InstructionSetTest, ArmFeaturesFromVariant) { |
| 89 | // Build features for a 32-bit ARM krait processor. |
| 90 | std::string error_msg; |
| 91 | std::unique_ptr<const InstructionSetFeatures> krait_features( |
| 92 | InstructionSetFeatures::FromVariant(kArm, "krait", &error_msg)); |
| 93 | ASSERT_TRUE(krait_features.get() != nullptr) << error_msg; |
| 94 | |
| 95 | ASSERT_EQ(krait_features->GetInstructionSet(), kArm); |
| 96 | EXPECT_TRUE(krait_features->Equals(krait_features.get())); |
| 97 | EXPECT_TRUE(krait_features->AsArmInstructionSetFeatures()->HasDivideInstruction()); |
| 98 | EXPECT_TRUE(krait_features->AsArmInstructionSetFeatures()->HasLpae()); |
| 99 | EXPECT_STREQ("div,lpae", krait_features->GetFeatureString().c_str()); |
| 100 | EXPECT_EQ(krait_features->AsBitmap(), 3U); |
| 101 | |
| 102 | // Build features for a 32-bit ARM denver processor. |
| 103 | std::unique_ptr<const InstructionSetFeatures> denver_features( |
| 104 | InstructionSetFeatures::FromVariant(kArm, "denver", &error_msg)); |
| 105 | ASSERT_TRUE(denver_features.get() != nullptr) << error_msg; |
| 106 | |
| 107 | EXPECT_TRUE(denver_features->Equals(denver_features.get())); |
| 108 | EXPECT_TRUE(denver_features->Equals(krait_features.get())); |
| 109 | EXPECT_TRUE(krait_features->Equals(denver_features.get())); |
| 110 | EXPECT_TRUE(denver_features->AsArmInstructionSetFeatures()->HasDivideInstruction()); |
| 111 | EXPECT_TRUE(denver_features->AsArmInstructionSetFeatures()->HasLpae()); |
| 112 | EXPECT_STREQ("div,lpae", denver_features->GetFeatureString().c_str()); |
| 113 | EXPECT_EQ(denver_features->AsBitmap(), 3U); |
| 114 | |
| 115 | // Build features for a 32-bit ARMv7 processor. |
| 116 | std::unique_ptr<const InstructionSetFeatures> arm7_features( |
| 117 | InstructionSetFeatures::FromVariant(kArm, "arm7", &error_msg)); |
| 118 | ASSERT_TRUE(arm7_features.get() != nullptr) << error_msg; |
| 119 | |
| 120 | EXPECT_TRUE(arm7_features->Equals(arm7_features.get())); |
| 121 | EXPECT_FALSE(arm7_features->Equals(krait_features.get())); |
| 122 | EXPECT_FALSE(krait_features->Equals(arm7_features.get())); |
| 123 | EXPECT_FALSE(arm7_features->AsArmInstructionSetFeatures()->HasDivideInstruction()); |
| 124 | EXPECT_FALSE(arm7_features->AsArmInstructionSetFeatures()->HasLpae()); |
| 125 | EXPECT_STREQ("none", arm7_features->GetFeatureString().c_str()); |
| 126 | EXPECT_EQ(arm7_features->AsBitmap(), 0U); |
| 127 | |
| 128 | // ARM6 is not a supported architecture variant. |
| 129 | std::unique_ptr<const InstructionSetFeatures> arm6_features( |
| 130 | InstructionSetFeatures::FromVariant(kArm, "arm6", &error_msg)); |
| 131 | EXPECT_TRUE(arm6_features.get() == nullptr); |
| 132 | EXPECT_NE(error_msg.size(), 0U); |
| 133 | } |
| 134 | |
| 135 | TEST_F(InstructionSetTest, ArmFeaturesFromString) { |
| 136 | // Build features for a 32-bit ARM with LPAE and div processor. |
| 137 | std::string error_msg; |
| 138 | std::unique_ptr<const InstructionSetFeatures> krait_features( |
| 139 | InstructionSetFeatures::FromFeatureString(kArm, "lpae,div", &error_msg)); |
| 140 | ASSERT_TRUE(krait_features.get() != nullptr) << error_msg; |
| 141 | |
| 142 | ASSERT_EQ(krait_features->GetInstructionSet(), kArm); |
| 143 | EXPECT_TRUE(krait_features->Equals(krait_features.get())); |
| 144 | EXPECT_TRUE(krait_features->AsArmInstructionSetFeatures()->HasDivideInstruction()); |
| 145 | EXPECT_TRUE(krait_features->AsArmInstructionSetFeatures()->HasLpae()); |
| 146 | EXPECT_STREQ("div,lpae", krait_features->GetFeatureString().c_str()); |
| 147 | EXPECT_EQ(krait_features->AsBitmap(), 3U); |
| 148 | |
| 149 | // Build features for a 32-bit ARM processor with LPAE and div flipped. |
| 150 | std::unique_ptr<const InstructionSetFeatures> denver_features( |
| 151 | InstructionSetFeatures::FromFeatureString(kArm, "div,lpae", &error_msg)); |
| 152 | ASSERT_TRUE(denver_features.get() != nullptr) << error_msg; |
| 153 | |
| 154 | EXPECT_TRUE(denver_features->Equals(denver_features.get())); |
| 155 | EXPECT_TRUE(denver_features->Equals(krait_features.get())); |
| 156 | EXPECT_TRUE(krait_features->Equals(denver_features.get())); |
| 157 | EXPECT_TRUE(denver_features->AsArmInstructionSetFeatures()->HasDivideInstruction()); |
| 158 | EXPECT_TRUE(denver_features->AsArmInstructionSetFeatures()->HasLpae()); |
| 159 | EXPECT_STREQ("div,lpae", denver_features->GetFeatureString().c_str()); |
| 160 | EXPECT_EQ(denver_features->AsBitmap(), 3U); |
| 161 | |
| 162 | // Build features for a 32-bit default ARM processor. |
| 163 | std::unique_ptr<const InstructionSetFeatures> arm7_features( |
| 164 | InstructionSetFeatures::FromFeatureString(kArm, "default", &error_msg)); |
| 165 | ASSERT_TRUE(arm7_features.get() != nullptr) << error_msg; |
| 166 | |
| 167 | EXPECT_TRUE(arm7_features->Equals(arm7_features.get())); |
| 168 | EXPECT_FALSE(arm7_features->Equals(krait_features.get())); |
| 169 | EXPECT_FALSE(krait_features->Equals(arm7_features.get())); |
| 170 | EXPECT_FALSE(arm7_features->AsArmInstructionSetFeatures()->HasDivideInstruction()); |
| 171 | EXPECT_FALSE(arm7_features->AsArmInstructionSetFeatures()->HasLpae()); |
| 172 | EXPECT_STREQ("none", arm7_features->GetFeatureString().c_str()); |
| 173 | EXPECT_EQ(arm7_features->AsBitmap(), 0U); |
| 174 | } |
| 175 | |
| 176 | #ifdef HAVE_ANDROID_OS |
| 177 | #include "cutils/properties.h" |
| 178 | |
| 179 | TEST_F(InstructionSetTest, FeaturesFromSystemPropertyVariant) { |
| 180 | // Take the default set of instruction features from the build. |
| 181 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features( |
| 182 | InstructionSetFeatures::FromCppDefines()); |
| 183 | |
| 184 | // Read the features property. |
| 185 | std::string key = StringPrintf("dalvik.vm.isa.%s.variant", GetInstructionSetString(kRuntimeISA)); |
| 186 | char dex2oat_isa_variant[PROPERTY_VALUE_MAX]; |
| 187 | if (property_get(key.c_str(), dex2oat_isa_variant, nullptr) > 0) { |
| 188 | // Use features from property to build InstructionSetFeatures and check against build's |
| 189 | // features. |
| 190 | std::string error_msg; |
| 191 | std::unique_ptr<const InstructionSetFeatures> property_features( |
| 192 | InstructionSetFeatures::FromVariant(kRuntimeISA, dex2oat_isa_variant, &error_msg)); |
| 193 | ASSERT_TRUE(property_features.get() != nullptr) << error_msg; |
| 194 | |
| 195 | EXPECT_TRUE(property_features->Equals(instruction_set_features.get())) |
| 196 | << "System property features: " << *property_features.get() |
| 197 | << "\nFeatures from build: " << *instruction_set_features.get(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | TEST_F(InstructionSetTest, FeaturesFromSystemPropertyString) { |
| 202 | // Take the default set of instruction features from the build. |
| 203 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features( |
| 204 | InstructionSetFeatures::FromCppDefines()); |
| 205 | |
| 206 | // Read the features property. |
| 207 | std::string key = StringPrintf("dalvik.vm.isa.%s.features", GetInstructionSetString(kRuntimeISA)); |
| 208 | char dex2oat_isa_features[PROPERTY_VALUE_MAX]; |
| 209 | if (property_get(key.c_str(), dex2oat_isa_features, nullptr) > 0) { |
| 210 | // Use features from property to build InstructionSetFeatures and check against build's |
| 211 | // features. |
| 212 | std::string error_msg; |
| 213 | std::unique_ptr<const InstructionSetFeatures> property_features( |
| 214 | InstructionSetFeatures::FromFeatureString(kRuntimeISA, dex2oat_isa_features, &error_msg)); |
| 215 | ASSERT_TRUE(property_features.get() != nullptr) << error_msg; |
| 216 | |
| 217 | EXPECT_TRUE(property_features->Equals(instruction_set_features.get())) |
| 218 | << "System property features: " << *property_features.get() |
| 219 | << "\nFeatures from build: " << *instruction_set_features.get(); |
| 220 | } |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | TEST_F(InstructionSetTest, FeaturesFromCpuInfo) { |
| 225 | // Take the default set of instruction features from the build. |
| 226 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features( |
| 227 | InstructionSetFeatures::FromCppDefines()); |
| 228 | |
| 229 | // Check we get the same instruction set features using /proc/cpuinfo. |
| 230 | std::unique_ptr<const InstructionSetFeatures> cpuinfo_features( |
| 231 | InstructionSetFeatures::FromCpuInfo()); |
| 232 | EXPECT_TRUE(cpuinfo_features->Equals(instruction_set_features.get())) |
| 233 | << "CPU Info features: " << *cpuinfo_features.get() |
| 234 | << "\nFeatures from build: " << *instruction_set_features.get(); |
| 235 | } |
| 236 | |
| 237 | TEST_F(InstructionSetTest, FeaturesFromHwcap) { |
| 238 | // Take the default set of instruction features from the build. |
| 239 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features( |
| 240 | InstructionSetFeatures::FromCppDefines()); |
| 241 | |
| 242 | // Check we get the same instruction set features using AT_HWCAP. |
| 243 | std::unique_ptr<const InstructionSetFeatures> hwcap_features( |
| 244 | InstructionSetFeatures::FromHwcap()); |
| 245 | EXPECT_TRUE(hwcap_features->Equals(instruction_set_features.get())) |
| 246 | << "Hwcap features: " << *hwcap_features.get() |
| 247 | << "\nFeatures from build: " << *instruction_set_features.get(); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | TEST_F(InstructionSetTest, FeaturesFromAssembly) { |
| 252 | // Take the default set of instruction features from the build. |
| 253 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features( |
| 254 | InstructionSetFeatures::FromCppDefines()); |
| 255 | |
| 256 | // Check we get the same instruction set features using assembly tests. |
| 257 | std::unique_ptr<const InstructionSetFeatures> assembly_features( |
| 258 | InstructionSetFeatures::FromAssembly()); |
| 259 | EXPECT_TRUE(assembly_features->Equals(instruction_set_features.get())) |
| 260 | << "Assembly features: " << *assembly_features.get() |
| 261 | << "\nFeatures from build: " << *instruction_set_features.get(); |
| 262 | } |
| 263 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 264 | } // namespace art |