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