Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | /* |
| 18 | * Test the indirect reference table implementation. |
| 19 | */ |
| 20 | #include "Dalvik.h" |
| 21 | |
| 22 | #include <stdlib.h> |
Jeff Brown | 476157d | 2011-10-26 18:41:12 -0700 | [diff] [blame] | 23 | #include <sys/time.h> |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 24 | |
| 25 | #ifndef NDEBUG |
| 26 | |
Steve Block | 4308417 | 2012-01-04 20:04:51 +0000 | [diff] [blame] | 27 | #define DBUG_MSG ALOGI |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 28 | |
Jeff Brown | 476157d | 2011-10-26 18:41:12 -0700 | [diff] [blame] | 29 | class Stopwatch { |
| 30 | public: |
| 31 | Stopwatch() { |
| 32 | reset(); |
| 33 | } |
| 34 | |
| 35 | void reset() { |
| 36 | start_ = now(); |
| 37 | } |
| 38 | |
| 39 | float elapsedSeconds() { |
| 40 | return (now() - start_) * 0.000001f; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | u8 start_; |
| 45 | |
| 46 | static u8 now() { |
| 47 | #ifdef HAVE_POSIX_CLOCKS |
| 48 | struct timespec tm; |
| 49 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 50 | return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000; |
| 51 | #else |
| 52 | struct timeval tv; |
| 53 | gettimeofday(&tv, NULL); |
| 54 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
| 55 | #endif |
| 56 | } |
| 57 | }; |
| 58 | |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Basic add/get/delete tests in an unsegmented table. |
| 61 | */ |
Carl Shapiro | 1e1433e | 2011-04-20 16:51:38 -0700 | [diff] [blame] | 62 | static bool basicTest() |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 63 | { |
| 64 | static const int kTableMax = 20; |
| 65 | IndirectRefTable irt; |
| 66 | IndirectRef iref0, iref1, iref2, iref3; |
| 67 | IndirectRef manyRefs[kTableMax]; |
| 68 | ClassObject* clazz = dvmFindClass("Ljava/lang/Object;", NULL); |
| 69 | Object* obj0 = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 70 | Object* obj1 = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 71 | Object* obj2 = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 72 | Object* obj3 = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 73 | const u4 cookie = IRT_FIRST_SEGMENT; |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 74 | bool result = false; |
| 75 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 76 | if (!irt.init(kTableMax/2, kTableMax, kIndirectKindGlobal)) { |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
| 80 | iref0 = (IndirectRef) 0x11110; |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 81 | if (irt.remove(cookie, iref0)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 82 | ALOGE("unexpectedly successful removal"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 83 | goto bail; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Add three, check, remove in the order in which they were added. |
| 88 | */ |
| 89 | DBUG_MSG("+++ START fifo\n"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 90 | iref0 = irt.add(cookie, obj0); |
| 91 | iref1 = irt.add(cookie, obj1); |
| 92 | iref2 = irt.add(cookie, obj2); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 93 | if (iref0 == NULL || iref1 == NULL || iref2 == NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 94 | ALOGE("trivial add1 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 95 | goto bail; |
| 96 | } |
| 97 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 98 | if (irt.get(iref0) != obj0 || |
| 99 | irt.get(iref1) != obj1 || |
| 100 | irt.get(iref2) != obj2) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 101 | ALOGE("objects don't match expected values %p %p %p vs. %p %p %p", |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 102 | irt.get(iref0), irt.get(iref1), irt.get(iref2), |
| 103 | obj0, obj1, obj2); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 104 | goto bail; |
| 105 | } else { |
| 106 | DBUG_MSG("+++ obj1=%p --> iref1=%p\n", obj1, iref1); |
| 107 | } |
| 108 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 109 | if (!irt.remove(cookie, iref0) || |
| 110 | !irt.remove(cookie, iref1) || |
| 111 | !irt.remove(cookie, iref2)) |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 112 | { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 113 | ALOGE("fifo deletion failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 114 | goto bail; |
| 115 | } |
| 116 | |
| 117 | /* table should be empty now */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 118 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 119 | ALOGE("fifo del not empty"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 120 | goto bail; |
| 121 | } |
| 122 | |
| 123 | /* get invalid entry (off the end of the list) */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 124 | if (irt.get(iref0) != kInvalidIndirectRefObject) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 125 | ALOGE("stale entry get succeeded unexpectedly"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 126 | goto bail; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Add three, remove in the opposite order. |
| 131 | */ |
| 132 | DBUG_MSG("+++ START lifo\n"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 133 | iref0 = irt.add(cookie, obj0); |
| 134 | iref1 = irt.add(cookie, obj1); |
| 135 | iref2 = irt.add(cookie, obj2); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 136 | if (iref0 == NULL || iref1 == NULL || iref2 == NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 137 | ALOGE("trivial add2 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 138 | goto bail; |
| 139 | } |
| 140 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 141 | if (!irt.remove(cookie, iref2) || |
| 142 | !irt.remove(cookie, iref1) || |
| 143 | !irt.remove(cookie, iref0)) |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 144 | { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 145 | ALOGE("lifo deletion failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 146 | goto bail; |
| 147 | } |
| 148 | |
| 149 | /* table should be empty now */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 150 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 151 | ALOGE("lifo del not empty"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 152 | goto bail; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Add three, remove middle / middle / bottom / top. (Second attempt |
| 157 | * to remove middle should fail.) |
| 158 | */ |
| 159 | DBUG_MSG("+++ START unorder\n"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 160 | iref0 = irt.add(cookie, obj0); |
| 161 | iref1 = irt.add(cookie, obj1); |
| 162 | iref2 = irt.add(cookie, obj2); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 163 | if (iref0 == NULL || iref1 == NULL || iref2 == NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 164 | ALOGE("trivial add3 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 165 | goto bail; |
| 166 | } |
| 167 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 168 | if (irt.capacity() != 3) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 169 | ALOGE("expected 3 entries, found %d", irt.capacity()); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 170 | goto bail; |
| 171 | } |
| 172 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 173 | if (!irt.remove(cookie, iref1) || irt.remove(cookie, iref1)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 174 | ALOGE("unorder deletion1 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 175 | goto bail; |
| 176 | } |
| 177 | |
| 178 | /* get invalid entry (from hole) */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 179 | if (irt.get(iref1) != kInvalidIndirectRefObject) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 180 | ALOGE("hole get succeeded unexpectedly"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 181 | goto bail; |
| 182 | } |
| 183 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 184 | if (!irt.remove(cookie, iref2) || !irt.remove(cookie, iref0)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 185 | ALOGE("unorder deletion2 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 186 | goto bail; |
| 187 | } |
| 188 | |
| 189 | /* table should be empty now */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 190 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 191 | ALOGE("unorder del not empty"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 192 | goto bail; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Add four entries. Remove #1, add new entry, verify that table size |
| 197 | * is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify |
| 198 | * that we delete one and don't hole-compact the other. |
| 199 | */ |
| 200 | DBUG_MSG("+++ START hole fill\n"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 201 | iref0 = irt.add(cookie, obj0); |
| 202 | iref1 = irt.add(cookie, obj1); |
| 203 | iref2 = irt.add(cookie, obj2); |
| 204 | iref3 = irt.add(cookie, obj3); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 205 | if (iref0 == NULL || iref1 == NULL || iref2 == NULL || iref3 == NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 206 | ALOGE("trivial add4 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 207 | goto bail; |
| 208 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 209 | if (!irt.remove(cookie, iref1)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 210 | ALOGE("remove 1 of 4 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 211 | goto bail; |
| 212 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 213 | iref1 = irt.add(cookie, obj1); |
| 214 | if (irt.capacity() != 4) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 215 | ALOGE("hole not filled"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 216 | goto bail; |
| 217 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 218 | if (!irt.remove(cookie, iref1) || !irt.remove(cookie, iref3)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 219 | ALOGE("remove 1/3 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 220 | goto bail; |
| 221 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 222 | if (irt.capacity() != 3) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 223 | ALOGE("should be 3 after two deletions"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 224 | goto bail; |
| 225 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 226 | if (!irt.remove(cookie, iref2) || !irt.remove(cookie, iref0)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 227 | ALOGE("remove 2/0 failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 228 | goto bail; |
| 229 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 230 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 231 | ALOGE("not empty after split remove"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 232 | goto bail; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Add an entry, remove it, add a new entry, and try to use the original |
| 237 | * iref. They have the same slot number but are for different objects. |
| 238 | * With the extended checks in place, this should fail. |
| 239 | */ |
| 240 | DBUG_MSG("+++ START switched\n"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 241 | iref0 = irt.add(cookie, obj0); |
| 242 | irt.remove(cookie, iref0); |
| 243 | iref1 = irt.add(cookie, obj1); |
| 244 | if (irt.remove(cookie, iref0)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 245 | ALOGE("mismatched del succeeded (%p vs %p)", iref0, iref1); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 246 | goto bail; |
| 247 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 248 | if (!irt.remove(cookie, iref1)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 249 | ALOGE("switched del failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 250 | goto bail; |
| 251 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 252 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 253 | ALOGE("switching del not empty"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 254 | goto bail; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Same as above, but with the same object. A more rigorous checker |
| 259 | * (e.g. with slot serialization) will catch this. |
| 260 | */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 261 | DBUG_MSG("+++ START switched same object\n"); |
| 262 | iref0 = irt.add(cookie, obj0); |
| 263 | irt.remove(cookie, iref0); |
| 264 | iref1 = irt.add(cookie, obj0); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 265 | if (iref0 != iref1) { |
Andy McFadden | 5d59960 | 2009-08-31 16:39:23 -0700 | [diff] [blame] | 266 | /* try 0, should not work */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 267 | if (irt.remove(cookie, iref0)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 268 | ALOGE("temporal del succeeded (%p vs %p)", iref0, iref1); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 269 | goto bail; |
| 270 | } |
Andy McFadden | 5d59960 | 2009-08-31 16:39:23 -0700 | [diff] [blame] | 271 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 272 | if (!irt.remove(cookie, iref1)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 273 | ALOGE("temporal cleanup failed"); |
Andy McFadden | 5d59960 | 2009-08-31 16:39:23 -0700 | [diff] [blame] | 274 | goto bail; |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 275 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 276 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 277 | ALOGE("temporal del not empty"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 278 | goto bail; |
| 279 | } |
| 280 | |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 281 | DBUG_MSG("+++ START null lookup\n"); |
| 282 | if (irt.get(NULL) != kInvalidIndirectRefObject) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 283 | ALOGE("null lookup succeeded"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 284 | goto bail; |
| 285 | } |
| 286 | |
| 287 | DBUG_MSG("+++ START stale lookup\n"); |
| 288 | iref0 = irt.add(cookie, obj0); |
| 289 | irt.remove(cookie, iref0); |
| 290 | if (irt.get(iref0) != kInvalidIndirectRefObject) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 291 | ALOGE("stale lookup succeeded"); |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 292 | goto bail; |
| 293 | } |
| 294 | |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 295 | /* |
| 296 | * Test table overflow. |
| 297 | */ |
| 298 | DBUG_MSG("+++ START overflow\n"); |
| 299 | int i; |
| 300 | for (i = 0; i < kTableMax; i++) { |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 301 | manyRefs[i] = irt.add(cookie, obj0); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 302 | if (manyRefs[i] == NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 303 | ALOGE("Failed adding %d of %d", i, kTableMax); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 304 | goto bail; |
| 305 | } |
| 306 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 307 | if (irt.add(cookie, obj0) != NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 308 | ALOGE("Table overflow succeeded"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 309 | goto bail; |
| 310 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 311 | if (irt.capacity() != (size_t)kTableMax) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 312 | ALOGE("Expected %d entries, found %d", kTableMax, irt.capacity()); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 313 | goto bail; |
| 314 | } |
Jeff Brown | 5552e62 | 2011-10-26 17:04:54 -0700 | [diff] [blame] | 315 | irt.dump("table with 20 entries, all filled"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 316 | for (i = 0; i < kTableMax-1; i++) { |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 317 | if (!irt.remove(cookie, manyRefs[i])) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 318 | ALOGE("multi-remove failed at %d", i); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 319 | goto bail; |
| 320 | } |
| 321 | } |
Jeff Brown | 5552e62 | 2011-10-26 17:04:54 -0700 | [diff] [blame] | 322 | irt.dump("table with 20 entries, 19 of them holes"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 323 | /* because of removal order, should have 20 entries, 19 of them holes */ |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 324 | if (irt.capacity() != (size_t)kTableMax) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 325 | ALOGE("Expected %d entries (with holes), found %d", |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 326 | kTableMax, irt.capacity()); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 327 | goto bail; |
| 328 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 329 | if (!irt.remove(cookie, manyRefs[kTableMax-1])) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 330 | ALOGE("multi-remove final failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 331 | goto bail; |
| 332 | } |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 333 | if (irt.capacity() != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 334 | ALOGE("multi-del not empty"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 335 | goto bail; |
| 336 | } |
| 337 | |
Jeff Brown | 5552e62 | 2011-10-26 17:04:54 -0700 | [diff] [blame] | 338 | /* Done */ |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 339 | DBUG_MSG("+++ basic test complete\n"); |
| 340 | result = true; |
| 341 | |
| 342 | bail: |
Elliott Hughes | ce09683 | 2011-06-20 17:50:41 -0700 | [diff] [blame] | 343 | irt.destroy(); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 344 | return result; |
| 345 | } |
| 346 | |
Jeff Brown | 476157d | 2011-10-26 18:41:12 -0700 | [diff] [blame] | 347 | static bool performanceTest() |
| 348 | { |
| 349 | static const int kTableMax = 100; |
| 350 | IndirectRefTable irt; |
| 351 | IndirectRef manyRefs[kTableMax]; |
| 352 | ClassObject* clazz = dvmFindClass("Ljava/lang/Object;", NULL); |
| 353 | Object* obj0 = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 354 | const u4 cookie = IRT_FIRST_SEGMENT; |
| 355 | const int kLoops = 100000; |
| 356 | Stopwatch stopwatch; |
| 357 | |
| 358 | DBUG_MSG("+++ START performance\n"); |
| 359 | |
| 360 | if (!irt.init(kTableMax, kTableMax, kIndirectKindGlobal)) { |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | stopwatch.reset(); |
| 365 | for (int loop = 0; loop < kLoops; loop++) { |
| 366 | for (int i = 0; i < kTableMax; i++) { |
| 367 | manyRefs[i] = irt.add(cookie, obj0); |
| 368 | } |
| 369 | for (int i = 0; i < kTableMax; i++) { |
| 370 | irt.remove(cookie, manyRefs[i]); |
| 371 | } |
| 372 | } |
| 373 | DBUG_MSG("Add/remove %d objects FIFO order, %d iterations, %0.3fms / iteration", |
| 374 | kTableMax, kLoops, stopwatch.elapsedSeconds() * 1000 / kLoops); |
| 375 | |
| 376 | stopwatch.reset(); |
| 377 | for (int loop = 0; loop < kLoops; loop++) { |
| 378 | for (int i = 0; i < kTableMax; i++) { |
| 379 | manyRefs[i] = irt.add(cookie, obj0); |
| 380 | } |
| 381 | for (int i = kTableMax; i-- > 0; ) { |
| 382 | irt.remove(cookie, manyRefs[i]); |
| 383 | } |
| 384 | } |
| 385 | DBUG_MSG("Add/remove %d objects LIFO order, %d iterations, %0.3fms / iteration", |
| 386 | kTableMax, kLoops, stopwatch.elapsedSeconds() * 1000 / kLoops); |
| 387 | |
| 388 | for (int i = 0; i < kTableMax; i++) { |
| 389 | manyRefs[i] = irt.add(cookie, obj0); |
| 390 | } |
| 391 | stopwatch.reset(); |
| 392 | for (int loop = 0; loop < kLoops; loop++) { |
| 393 | for (int i = 0; i < kTableMax; i++) { |
| 394 | irt.get(manyRefs[i]); |
| 395 | } |
| 396 | } |
| 397 | DBUG_MSG("Get %d objects, %d iterations, %0.3fms / iteration", |
| 398 | kTableMax, kLoops, stopwatch.elapsedSeconds() * 1000 / kLoops); |
| 399 | for (int i = kTableMax; i-- > 0; ) { |
| 400 | irt.remove(cookie, manyRefs[i]); |
| 401 | } |
| 402 | |
| 403 | irt.destroy(); |
| 404 | return true; |
| 405 | } |
| 406 | |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 407 | /* |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 408 | * Some quick tests. |
| 409 | */ |
Carl Shapiro | 1e1433e | 2011-04-20 16:51:38 -0700 | [diff] [blame] | 410 | bool dvmTestIndirectRefTable() |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 411 | { |
| 412 | if (!basicTest()) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 413 | ALOGE("IRT basic test failed"); |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 414 | return false; |
| 415 | } |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 416 | |
Jeff Brown | 476157d | 2011-10-26 18:41:12 -0700 | [diff] [blame] | 417 | if (!performanceTest()) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 418 | ALOGE("IRT performance test failed"); |
Jeff Brown | 476157d | 2011-10-26 18:41:12 -0700 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | |
Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 422 | return true; |
| 423 | } |
| 424 | |
| 425 | #endif /*NDEBUG*/ |