| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 "Dalvik.h" |
| 18 | #include "HeapBitmap.h" |
| 19 | #include "clz.h" |
| Carl Shapiro | f1461cb | 2010-07-29 19:51:53 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> /* for PROT_* */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | #define LIKELY(exp) (__builtin_expect((exp) != 0, true)) |
| 23 | #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false)) |
| 24 | |
| 25 | /* |
| 26 | * Initialize a HeapBitmap so that it points to a bitmap large |
| 27 | * enough to cover a heap at <base> of <maxSize> bytes, where |
| 28 | * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned. |
| 29 | */ |
| 30 | bool |
| 31 | dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize, |
| 32 | const char *name) |
| 33 | { |
| 34 | void *bits; |
| 35 | size_t bitsLen; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | |
| 37 | assert(hb != NULL); |
| Carl Shapiro | 3db1ad3 | 2010-07-12 16:09:54 -0700 | [diff] [blame] | 38 | assert(name != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | bitsLen = HB_OFFSET_TO_INDEX(maxSize) * sizeof(*hb->bits); |
| Carl Shapiro | 3db1ad3 | 2010-07-12 16:09:54 -0700 | [diff] [blame] | 40 | bits = dvmAllocRegion(bitsLen, PROT_READ | PROT_WRITE, name); |
| 41 | if (bits == NULL) { |
| 42 | LOGE("Could not mmap %zd-byte ashmem region '%s'", bitsLen, name); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 43 | return false; |
| 44 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 45 | hb->bits = bits; |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 46 | hb->bitsLen = hb->allocLen = bitsLen; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 47 | hb->base = (uintptr_t)base; |
| 48 | hb->max = hb->base - 1; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
| 52 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | * Clean up any resources associated with the bitmap. |
| 54 | */ |
| 55 | void |
| 56 | dvmHeapBitmapDelete(HeapBitmap *hb) |
| 57 | { |
| 58 | assert(hb != NULL); |
| 59 | |
| 60 | if (hb->bits != NULL) { |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 61 | munmap((char *)hb->bits, hb->allocLen); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 62 | } |
| 63 | memset(hb, 0, sizeof(*hb)); |
| 64 | } |
| 65 | |
| 66 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 67 | * Fill the bitmap with zeroes. Returns the bitmap's memory to |
| 68 | * the system as a side-effect. |
| 69 | */ |
| 70 | void |
| 71 | dvmHeapBitmapZero(HeapBitmap *hb) |
| 72 | { |
| 73 | assert(hb != NULL); |
| 74 | |
| 75 | if (hb->bits != NULL) { |
| 76 | /* This returns the memory to the system. |
| 77 | * Successive page faults will return zeroed memory. |
| 78 | */ |
| 79 | madvise(hb->bits, hb->bitsLen, MADV_DONTNEED); |
| 80 | hb->max = hb->base - 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Walk through the bitmaps in increasing address order, and find the |
| 86 | * object pointers that correspond to places where the bitmaps differ. |
| 87 | * Call <callback> zero or more times with lists of these object pointers. |
| 88 | * |
| 89 | * The <finger> argument to the callback indicates the next-highest |
| 90 | * address that hasn't been visited yet; setting bits for objects whose |
| 91 | * addresses are less than <finger> are not guaranteed to be seen by |
| 92 | * the current XorWalk. <finger> will be set to ULONG_MAX when the |
| 93 | * end of the bitmap is reached. |
| 94 | */ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 95 | void dvmHeapBitmapXorWalk(const HeapBitmap *hb1, const HeapBitmap *hb2, |
| 96 | BitmapCallback *callback, void *callbackArg) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | { |
| 98 | static const size_t kPointerBufSize = 128; |
| 99 | void *pointerBuf[kPointerBufSize]; |
| 100 | void **pb = pointerBuf; |
| 101 | size_t index; |
| 102 | size_t i; |
| 103 | |
| 104 | #define FLUSH_POINTERBUF(finger_) \ |
| 105 | do { \ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 106 | (*callback)(pb - pointerBuf, (void **)pointerBuf, \ |
| 107 | (void *)(finger_), callbackArg); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 108 | pb = pointerBuf; \ |
| 109 | } while (false) |
| 110 | |
| 111 | #define DECODE_BITS(hb_, bits_, update_index_) \ |
| 112 | do { \ |
| 113 | if (UNLIKELY(bits_ != 0)) { \ |
| 114 | static const unsigned long kHighBit = \ |
| 115 | (unsigned long)1 << (HB_BITS_PER_WORD - 1); \ |
| 116 | const uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + hb_->base; \ |
| 117 | /*TODO: hold onto ptrBase so we can shrink max later if possible */ \ |
| 118 | /*TODO: see if this is likely or unlikely */ \ |
| 119 | while (bits_ != 0) { \ |
| 120 | const int rshift = CLZ(bits_); \ |
| 121 | bits_ &= ~(kHighBit >> rshift); \ |
| 122 | *pb++ = (void *)(ptrBase + rshift * HB_OBJECT_ALIGNMENT); \ |
| 123 | } \ |
| 124 | /* Make sure that there are always enough slots available */ \ |
| 125 | /* for an entire word of 1s. */ \ |
| 126 | if (kPointerBufSize - (pb - pointerBuf) < HB_BITS_PER_WORD) { \ |
| 127 | FLUSH_POINTERBUF(ptrBase + \ |
| 128 | HB_BITS_PER_WORD * HB_OBJECT_ALIGNMENT); \ |
| 129 | if (update_index_) { \ |
| 130 | /* The callback may have caused hb_->max to grow. */ \ |
| 131 | index = HB_OFFSET_TO_INDEX(hb_->max - hb_->base); \ |
| 132 | } \ |
| 133 | } \ |
| 134 | } \ |
| 135 | } while (false) |
| 136 | |
| 137 | assert(hb1 != NULL); |
| 138 | assert(hb1->bits != NULL); |
| 139 | assert(hb2 != NULL); |
| 140 | assert(hb2->bits != NULL); |
| 141 | assert(callback != NULL); |
| 142 | |
| 143 | if (hb1->base != hb2->base) { |
| 144 | LOGW("dvmHeapBitmapXorWalk: bitmaps cover different heaps " |
| 145 | "(0x%08x != 0x%08x)\n", |
| 146 | (uintptr_t)hb1->base, (uintptr_t)hb2->base); |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 147 | return; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 148 | } |
| 149 | if (hb1->bitsLen != hb2->bitsLen) { |
| 150 | LOGW("dvmHeapBitmapXorWalk: size of bitmaps differ (%zd != %zd)\n", |
| 151 | hb1->bitsLen, hb2->bitsLen); |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 152 | return; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 153 | } |
| 154 | if (hb1->max < hb1->base && hb2->max < hb2->base) { |
| 155 | /* Easy case; both are obviously empty. |
| 156 | */ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 157 | return; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* First, walk along the section of the bitmaps that may be the same. |
| 161 | */ |
| 162 | if (hb1->max >= hb1->base && hb2->max >= hb2->base) { |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 163 | unsigned long *p1, *p2; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | uintptr_t offset; |
| 165 | |
| 166 | offset = ((hb1->max < hb2->max) ? hb1->max : hb2->max) - hb1->base; |
| 167 | //TODO: keep track of which (and whether) one is longer for later |
| 168 | index = HB_OFFSET_TO_INDEX(offset); |
| 169 | |
| 170 | p1 = hb1->bits; |
| 171 | p2 = hb2->bits; |
| 172 | for (i = 0; i <= index; i++) { |
| 173 | //TODO: unroll this. pile up a few in locals? |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 174 | unsigned long diff = *p1++ ^ *p2++; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 175 | DECODE_BITS(hb1, diff, false); |
| 176 | //BUG: if the callback was called, either max could have changed. |
| 177 | } |
| 178 | /* The next index to look at. |
| 179 | */ |
| 180 | index++; |
| 181 | } else { |
| 182 | /* One of the bitmaps is empty. |
| 183 | */ |
| 184 | index = 0; |
| 185 | } |
| 186 | |
| 187 | /* If one bitmap's max is larger, walk through the rest of the |
| 188 | * set bits. |
| 189 | */ |
| 190 | const HeapBitmap *longHb; |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 191 | unsigned long *p; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 192 | //TODO: may be the same size, in which case this is wasted work |
| 193 | longHb = (hb1->max > hb2->max) ? hb1 : hb2; |
| 194 | i = index; |
| 195 | index = HB_OFFSET_TO_INDEX(longHb->max - longHb->base); |
| 196 | p = longHb->bits + i; |
| 197 | for (/* i = i */; i <= index; i++) { |
| 198 | //TODO: unroll this |
| 199 | unsigned long bits = *p++; |
| 200 | DECODE_BITS(longHb, bits, true); |
| 201 | } |
| 202 | |
| 203 | if (pb > pointerBuf) { |
| Barry Hayes | 73f3e6f | 2010-07-15 08:58:10 -0700 | [diff] [blame] | 204 | /* Set the finger to the end of the heap (rather than |
| 205 | * longHb->max) so that the callback doesn't expect to be |
| 206 | * called again if it happens to change the current max. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 207 | */ |
| Barry Hayes | 73f3e6f | 2010-07-15 08:58:10 -0700 | [diff] [blame] | 208 | uintptr_t finalFinger = longHb->base + HB_MAX_OFFSET(longHb); |
| 209 | FLUSH_POINTERBUF(finalFinger); |
| 210 | assert(finalFinger > longHb->max); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 211 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 212 | #undef FLUSH_POINTERBUF |
| 213 | #undef DECODE_BITS |
| 214 | } |
| 215 | |
| 216 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 217 | * Similar to dvmHeapBitmapXorWalk(), but visit the set bits |
| 218 | * in a single bitmap. |
| 219 | */ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 220 | void dvmHeapBitmapWalk(const HeapBitmap *hb, |
| 221 | BitmapCallback *callback, void *callbackArg) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | { |
| 223 | /* Create an empty bitmap with the same extent as <hb>. |
| 224 | * Don't actually allocate any memory. |
| 225 | */ |
| 226 | HeapBitmap emptyHb = *hb; |
| 227 | emptyHb.max = emptyHb.base - 1; // empty |
| 228 | emptyHb.bits = (void *)1; // non-NULL but intentionally bad |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame^] | 229 | dvmHeapBitmapXorWalk(hb, &emptyHb, callback, callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 230 | } |