reference_table_test.cc revision 6e970e7fa88efd5ee38b0d6f9010a3985c62778f
1/* 2 * Copyright (C) 2011 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 "reference_table.h" 18 19#include "class_linker.h" 20#include "common_runtime_test.h" 21#include "handle_scope-inl.h" 22#include "mirror/array-inl.h" 23#include "mirror/class-inl.h" 24#include "mirror/class_loader.h" 25#include "mirror/string.h" 26#include "primitive.h" 27#include "runtime.h" 28#include "scoped_thread_state_change-inl.h" 29#include "thread-inl.h" 30 31namespace art { 32 33class ReferenceTableTest : public CommonRuntimeTest {}; 34 35static mirror::Object* CreateWeakReference(mirror::Object* referent) 36 REQUIRES_SHARED(Locks::mutator_lock_) { 37 Thread* self = Thread::Current(); 38 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 39 40 StackHandleScope<3> scope(self); 41 Handle<mirror::Object> h_referent(scope.NewHandle<mirror::Object>(referent)); 42 43 Handle<mirror::Class> h_ref_class(scope.NewHandle<mirror::Class>( 44 class_linker->FindClass(self, 45 "Ljava/lang/ref/WeakReference;", 46 ScopedNullHandle<mirror::ClassLoader>()))); 47 CHECK(h_ref_class.Get() != nullptr); 48 CHECK(class_linker->EnsureInitialized(self, h_ref_class, true, true)); 49 50 Handle<mirror::Object> h_ref_instance(scope.NewHandle<mirror::Object>( 51 h_ref_class->AllocObject(self))); 52 CHECK(h_ref_instance.Get() != nullptr); 53 54 ArtMethod* constructor = h_ref_class->FindDeclaredDirectMethod( 55 "<init>", "(Ljava/lang/Object;)V", class_linker->GetImagePointerSize()); 56 CHECK(constructor != nullptr); 57 58 uint32_t args[2]; 59 args[0] = PointerToLowMemUInt32(h_ref_instance.Get()); 60 args[1] = PointerToLowMemUInt32(h_referent.Get()); 61 JValue result; 62 constructor->Invoke(self, args, sizeof(uint32_t), &result, constructor->GetShorty()); 63 CHECK(!self->IsExceptionPending()); 64 65 return h_ref_instance.Get(); 66} 67 68TEST_F(ReferenceTableTest, Basics) { 69 ScopedObjectAccess soa(Thread::Current()); 70 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"); 71 72 ReferenceTable rt("test", 0, 11); 73 74 // Check dumping the empty table. 75 { 76 std::ostringstream oss; 77 rt.Dump(oss); 78 EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str(); 79 EXPECT_EQ(0U, rt.Size()); 80 } 81 82 // Check removal of all nullss in a empty table is a no-op. 83 rt.Remove(nullptr); 84 EXPECT_EQ(0U, rt.Size()); 85 86 // Check removal of all o1 in a empty table is a no-op. 87 rt.Remove(o1); 88 EXPECT_EQ(0U, rt.Size()); 89 90 // Add o1 and check we have 1 element and can dump. 91 { 92 rt.Add(o1); 93 EXPECT_EQ(1U, rt.Size()); 94 std::ostringstream oss; 95 rt.Dump(oss); 96 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str(); 97 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str(); 98 } 99 100 // Add a second object 10 times and check dumping is sane. 101 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0); 102 for (size_t i = 0; i < 10; ++i) { 103 rt.Add(o2); 104 EXPECT_EQ(i + 2, rt.Size()); 105 std::ostringstream oss; 106 rt.Dump(oss); 107 EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):", 108 i + 2 > 10 ? 10 : i + 2, 109 i + 2)), 110 std::string::npos) << oss.str(); 111 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str(); 112 if (i == 0) { 113 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str(); 114 } else { 115 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)), 116 std::string::npos) << oss.str(); 117 } 118 } 119 120 // Remove o1 (first element). 121 { 122 rt.Remove(o1); 123 EXPECT_EQ(10U, rt.Size()); 124 std::ostringstream oss; 125 rt.Dump(oss); 126 EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str(); 127 } 128 129 // Remove o2 ten times. 130 for (size_t i = 0; i < 10; ++i) { 131 rt.Remove(o2); 132 EXPECT_EQ(9 - i, rt.Size()); 133 std::ostringstream oss; 134 rt.Dump(oss); 135 if (i == 9) { 136 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str(); 137 } else if (i == 8) { 138 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str(); 139 } else { 140 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)), 141 std::string::npos) << oss.str(); 142 } 143 } 144 145 // Add a reference and check that the type of the referent is dumped. 146 { 147 mirror::Object* empty_reference = CreateWeakReference(nullptr); 148 ASSERT_TRUE(empty_reference->IsReferenceInstance()); 149 rt.Add(empty_reference); 150 std::ostringstream oss; 151 rt.Dump(oss); 152 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is null)"), std::string::npos) 153 << oss.str(); 154 } 155 156 { 157 mirror::Object* string_referent = mirror::String::AllocFromModifiedUtf8(Thread::Current(), "A"); 158 mirror::Object* non_empty_reference = CreateWeakReference(string_referent); 159 ASSERT_TRUE(non_empty_reference->IsReferenceInstance()); 160 rt.Add(non_empty_reference); 161 std::ostringstream oss; 162 rt.Dump(oss); 163 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is a java.lang.String)"), 164 std::string::npos) 165 << oss.str(); 166 } 167} 168 169static std::vector<size_t> FindAll(const std::string& haystack, const char* needle) { 170 std::vector<size_t> res; 171 size_t start = 0; 172 do { 173 size_t pos = haystack.find(needle, start); 174 if (pos == std::string::npos) { 175 break; 176 } 177 res.push_back(pos); 178 start = pos + 1; 179 } while (start < haystack.size()); 180 return res; 181} 182 183TEST_F(ReferenceTableTest, SummaryOrder) { 184 // Check that the summary statistics are sorted. 185 ScopedObjectAccess soa(Thread::Current()); 186 187 ReferenceTable rt("test", 0, 20); 188 189 { 190 mirror::Object* s1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"); 191 mirror::Object* s2 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "world"); 192 193 // 3 copies of s1, 2 copies of s2, interleaved. 194 for (size_t i = 0; i != 2; ++i) { 195 rt.Add(s1); 196 rt.Add(s2); 197 } 198 rt.Add(s1); 199 } 200 201 { 202 // Differently sized byte arrays. Should be sorted by identical (non-unique cound). 203 mirror::Object* b1_1 = mirror::ByteArray::Alloc(soa.Self(), 1); 204 rt.Add(b1_1); 205 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2)); 206 rt.Add(b1_1); 207 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2)); 208 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 1)); 209 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2)); 210 } 211 212 rt.Add(mirror::CharArray::Alloc(soa.Self(), 0)); 213 214 // Now dump, and ensure order. 215 std::ostringstream oss; 216 rt.Dump(oss); 217 218 // Only do this on the part after Summary. 219 std::string base = oss.str(); 220 size_t summary_pos = base.find("Summary:"); 221 ASSERT_NE(summary_pos, std::string::npos); 222 223 std::string haystack = base.substr(summary_pos); 224 225 std::vector<size_t> strCounts = FindAll(haystack, "java.lang.String"); 226 std::vector<size_t> b1Counts = FindAll(haystack, "byte[] (1 elements)"); 227 std::vector<size_t> b2Counts = FindAll(haystack, "byte[] (2 elements)"); 228 std::vector<size_t> cCounts = FindAll(haystack, "char[]"); 229 230 // Only one each. 231 EXPECT_EQ(1u, strCounts.size()); 232 EXPECT_EQ(1u, b1Counts.size()); 233 EXPECT_EQ(1u, b2Counts.size()); 234 EXPECT_EQ(1u, cCounts.size()); 235 236 // Expect them to be in order. 237 EXPECT_LT(strCounts[0], b1Counts[0]); 238 EXPECT_LT(b1Counts[0], b2Counts[0]); 239 EXPECT_LT(b2Counts[0], cCounts[0]); 240} 241 242} // namespace art 243