blob: 9523e92d7c674f3faf8dbed0af1722e724566b39 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughes11e45072011-08-16 17:40:46 -070017#include "reference_table.h"
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070021#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_runtime_test.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070023#include "handle_scope-inl.h"
Andreas Gampe8db4c882014-07-17 14:52:06 -070024#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "mirror/class-inl.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070026#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "mirror/string.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "primitive.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070029#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070031#include "thread-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032
Elliott Hughes11e45072011-08-16 17:40:46 -070033namespace art {
34
Andreas Gampe46ee31b2016-12-14 10:11:49 -080035using android::base::StringPrintf;
36
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080037class ReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070038
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070039static mirror::Object* CreateWeakReference(mirror::Object* referent)
40 REQUIRES_SHARED(Locks::mutator_lock_) {
41 Thread* self = Thread::Current();
42 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
43
44 StackHandleScope<3> scope(self);
45 Handle<mirror::Object> h_referent(scope.NewHandle<mirror::Object>(referent));
46
47 Handle<mirror::Class> h_ref_class(scope.NewHandle<mirror::Class>(
48 class_linker->FindClass(self,
49 "Ljava/lang/ref/WeakReference;",
50 ScopedNullHandle<mirror::ClassLoader>())));
51 CHECK(h_ref_class.Get() != nullptr);
52 CHECK(class_linker->EnsureInitialized(self, h_ref_class, true, true));
53
54 Handle<mirror::Object> h_ref_instance(scope.NewHandle<mirror::Object>(
55 h_ref_class->AllocObject(self)));
56 CHECK(h_ref_instance.Get() != nullptr);
57
58 ArtMethod* constructor = h_ref_class->FindDeclaredDirectMethod(
59 "<init>", "(Ljava/lang/Object;)V", class_linker->GetImagePointerSize());
60 CHECK(constructor != nullptr);
61
62 uint32_t args[2];
63 args[0] = PointerToLowMemUInt32(h_ref_instance.Get());
64 args[1] = PointerToLowMemUInt32(h_referent.Get());
65 JValue result;
66 constructor->Invoke(self, args, sizeof(uint32_t), &result, constructor->GetShorty());
67 CHECK(!self->IsExceptionPending());
68
69 return h_ref_instance.Get();
70}
71
Elliott Hughes11e45072011-08-16 17:40:46 -070072TEST_F(ReferenceTableTest, Basics) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070075
Ian Rogers63818dc2012-09-26 12:23:04 -070076 ReferenceTable rt("test", 0, 11);
77
78 // Check dumping the empty table.
79 {
80 std::ostringstream oss;
81 rt.Dump(oss);
82 EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str();
83 EXPECT_EQ(0U, rt.Size());
84 }
85
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 // Check removal of all nullss in a empty table is a no-op.
87 rt.Remove(nullptr);
Elliott Hughes11e45072011-08-16 17:40:46 -070088 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070089
90 // Check removal of all o1 in a empty table is a no-op.
Elliott Hughes11e45072011-08-16 17:40:46 -070091 rt.Remove(o1);
92 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070093
94 // Add o1 and check we have 1 element and can dump.
95 {
96 rt.Add(o1);
97 EXPECT_EQ(1U, rt.Size());
98 std::ostringstream oss;
99 rt.Dump(oss);
100 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
101 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
102 }
103
104 // Add a second object 10 times and check dumping is sane.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
Ian Rogers63818dc2012-09-26 12:23:04 -0700106 for (size_t i = 0; i < 10; ++i) {
107 rt.Add(o2);
108 EXPECT_EQ(i + 2, rt.Size());
109 std::ostringstream oss;
110 rt.Dump(oss);
111 EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):",
112 i + 2 > 10 ? 10 : i + 2,
113 i + 2)),
114 std::string::npos) << oss.str();
115 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
116 if (i == 0) {
117 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
118 } else {
119 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)),
120 std::string::npos) << oss.str();
121 }
122 }
123
124 // Remove o1 (first element).
125 {
126 rt.Remove(o1);
127 EXPECT_EQ(10U, rt.Size());
128 std::ostringstream oss;
129 rt.Dump(oss);
130 EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str();
131 }
132
133 // Remove o2 ten times.
134 for (size_t i = 0; i < 10; ++i) {
135 rt.Remove(o2);
136 EXPECT_EQ(9 - i, rt.Size());
137 std::ostringstream oss;
138 rt.Dump(oss);
139 if (i == 9) {
140 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
141 } else if (i == 8) {
142 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
143 } else {
144 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)),
145 std::string::npos) << oss.str();
146 }
147 }
Andreas Gampea3bbf8b2016-09-27 18:45:02 -0700148
149 // Add a reference and check that the type of the referent is dumped.
150 {
151 mirror::Object* empty_reference = CreateWeakReference(nullptr);
152 ASSERT_TRUE(empty_reference->IsReferenceInstance());
153 rt.Add(empty_reference);
154 std::ostringstream oss;
155 rt.Dump(oss);
156 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is null)"), std::string::npos)
157 << oss.str();
158 }
159
160 {
161 mirror::Object* string_referent = mirror::String::AllocFromModifiedUtf8(Thread::Current(), "A");
162 mirror::Object* non_empty_reference = CreateWeakReference(string_referent);
163 ASSERT_TRUE(non_empty_reference->IsReferenceInstance());
164 rt.Add(non_empty_reference);
165 std::ostringstream oss;
166 rt.Dump(oss);
167 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is a java.lang.String)"),
168 std::string::npos)
169 << oss.str();
170 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700171}
172
Andreas Gampe6e970e72016-11-14 17:00:28 -0800173static std::vector<size_t> FindAll(const std::string& haystack, const char* needle) {
174 std::vector<size_t> res;
175 size_t start = 0;
176 do {
177 size_t pos = haystack.find(needle, start);
178 if (pos == std::string::npos) {
179 break;
180 }
181 res.push_back(pos);
182 start = pos + 1;
183 } while (start < haystack.size());
184 return res;
185}
186
187TEST_F(ReferenceTableTest, SummaryOrder) {
188 // Check that the summary statistics are sorted.
189 ScopedObjectAccess soa(Thread::Current());
190
191 ReferenceTable rt("test", 0, 20);
192
193 {
194 mirror::Object* s1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
195 mirror::Object* s2 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "world");
196
197 // 3 copies of s1, 2 copies of s2, interleaved.
198 for (size_t i = 0; i != 2; ++i) {
199 rt.Add(s1);
200 rt.Add(s2);
201 }
202 rt.Add(s1);
203 }
204
205 {
206 // Differently sized byte arrays. Should be sorted by identical (non-unique cound).
207 mirror::Object* b1_1 = mirror::ByteArray::Alloc(soa.Self(), 1);
208 rt.Add(b1_1);
209 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
210 rt.Add(b1_1);
211 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
212 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 1));
213 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
214 }
215
216 rt.Add(mirror::CharArray::Alloc(soa.Self(), 0));
217
218 // Now dump, and ensure order.
219 std::ostringstream oss;
220 rt.Dump(oss);
221
222 // Only do this on the part after Summary.
223 std::string base = oss.str();
224 size_t summary_pos = base.find("Summary:");
225 ASSERT_NE(summary_pos, std::string::npos);
226
227 std::string haystack = base.substr(summary_pos);
228
229 std::vector<size_t> strCounts = FindAll(haystack, "java.lang.String");
230 std::vector<size_t> b1Counts = FindAll(haystack, "byte[] (1 elements)");
231 std::vector<size_t> b2Counts = FindAll(haystack, "byte[] (2 elements)");
232 std::vector<size_t> cCounts = FindAll(haystack, "char[]");
233
234 // Only one each.
235 EXPECT_EQ(1u, strCounts.size());
236 EXPECT_EQ(1u, b1Counts.size());
237 EXPECT_EQ(1u, b2Counts.size());
238 EXPECT_EQ(1u, cCounts.size());
239
240 // Expect them to be in order.
241 EXPECT_LT(strCounts[0], b1Counts[0]);
242 EXPECT_LT(b1Counts[0], b2Counts[0]);
243 EXPECT_LT(b2Counts[0], cCounts[0]);
244}
245
Elliott Hughes11e45072011-08-16 17:40:46 -0700246} // namespace art