| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore; |
| 7 |
|
| 8 |
import java.util.BitSet; |
| 9 |
|
| 10 |
import org.h2.util.MathUtils; |
| 11 |
|
| 12 |
/** |
| 13 |
* A free space bit set. |
| 14 |
*/ |
| 15 |
public class FreeSpaceBitSet { |
| 16 |
|
| 17 |
private static final boolean DETAILED_INFO = false; |
| 18 |
|
| 19 |
/** |
| 20 |
* The first usable block. |
| 21 |
*/ |
| 22 |
private final int firstFreeBlock; |
| 23 |
|
| 24 |
/** |
| 25 |
* The block size in bytes. |
| 26 |
*/ |
| 27 |
private final int blockSize; |
| 28 |
|
| 29 |
/** |
| 30 |
* The bit set. |
| 31 |
*/ |
| 32 |
private final BitSet set = new BitSet(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Left-shifting register, which holds outcomes of recent allocations. Only |
| 36 |
* allocations done in "reuseSpace" mode are recorded here. For example, |
| 37 |
* rightmost bit set to 1 means that last allocation failed to find a hole |
| 38 |
* big enough, and next bit set to 0 means that previous allocation request |
| 39 |
* have found one. |
| 40 |
*/ |
| 41 |
private int failureFlags; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Create a new free space map. |
| 46 |
* |
| 47 |
* @param firstFreeBlock the first free block |
| 48 |
* @param blockSize the block size |
| 49 |
*/ |
| 50 |
public FreeSpaceBitSet(int firstFreeBlock, int blockSize) { |
| 51 |
this.firstFreeBlock = firstFreeBlock; |
| 52 |
this.blockSize = blockSize; |
| 53 |
clear(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Reset the list. |
| 58 |
*/ |
| 59 |
public void clear() { |
| 60 |
set.clear(); |
| 61 |
set.set(0, firstFreeBlock); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check whether one of the blocks is in use. |
| 66 |
* |
| 67 |
* @param pos the position in bytes |
| 68 |
* @param length the number of bytes |
| 69 |
* @return true if a block is in use |
| 70 |
*/ |
| 71 |
public boolean isUsed(long pos, int length) { |
| 72 |
int start = getBlock(pos); |
| 73 |
int blocks = getBlockCount(length); |
| 74 |
for (int i = start; i < start + blocks; i++) { |
| 75 |
if (!set.get(i)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check whether one of the blocks is free. |
| 84 |
* |
| 85 |
* @param pos the position in bytes |
| 86 |
* @param length the number of bytes |
| 87 |
* @return true if a block is free |
| 88 |
*/ |
| 89 |
public boolean isFree(long pos, int length) { |
| 90 |
int start = getBlock(pos); |
| 91 |
int blocks = getBlockCount(length); |
| 92 |
for (int i = start; i < start + blocks; i++) { |
| 93 |
if (set.get(i)) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Allocate a number of blocks and mark them as used. |
| 102 |
* |
| 103 |
* @param length the number of bytes to allocate |
| 104 |
* @return the start position in bytes |
| 105 |
*/ |
| 106 |
public long allocate(int length) { |
| 107 |
return allocate(length, 0, 0); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Allocate a number of blocks and mark them as used. |
| 112 |
* |
| 113 |
* @param length the number of bytes to allocate |
| 114 |
* @param reservedLow start block index of the reserved area (inclusive) |
| 115 |
* @param reservedHigh end block index of the reserved area (exclusive), |
| 116 |
* special value -1 means beginning of the infinite free area |
| 117 |
* @return the start position in bytes |
| 118 |
*/ |
| 119 |
long allocate(int length, long reservedLow, long reservedHigh) { |
| 120 |
return getPos(allocate(getBlockCount(length), (int)reservedLow, (int)reservedHigh, true)); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Calculate starting position of the prospective allocation. |
| 125 |
* |
| 126 |
* @param blocks the number of blocks to allocate |
| 127 |
* @param reservedLow start block index of the reserved area (inclusive) |
| 128 |
* @param reservedHigh end block index of the reserved area (exclusive), |
| 129 |
* special value -1 means beginning of the infinite free area |
| 130 |
* @return the starting block index |
| 131 |
*/ |
| 132 |
long predictAllocation(int blocks, long reservedLow, long reservedHigh) { |
| 133 |
return allocate(blocks, (int)reservedLow, (int)reservedHigh, false); |
| 134 |
} |
| 135 |
|
| 136 |
boolean isFragmented() { |
| 137 |
return Integer.bitCount(failureFlags & 0x0F) > 1; |
| 138 |
} |
| 139 |
|
| 140 |
private int allocate(int blocks, int reservedLow, int reservedHigh, boolean allocate) { |
| 141 |
int freeBlocksTotal = 0; |
| 142 |
for (int i = 0;;) { |
| 143 |
int start = set.nextClearBit(i); |
| 144 |
int end = set.nextSetBit(start + 1); |
| 145 |
int freeBlocks = end - start; |
| 146 |
if (end < 0 || freeBlocks >= blocks) { |
| 147 |
if ((reservedHigh < 0 || start < reservedHigh) && start + blocks > reservedLow) { // overlap detected |
| 148 |
if (reservedHigh < 0) { |
| 149 |
start = getAfterLastBlock(); |
| 150 |
end = -1; |
| 151 |
} else { |
| 152 |
i = reservedHigh; |
| 153 |
continue; |
| 154 |
} |
| 155 |
} |
| 156 |
assert set.nextSetBit(start) == -1 || set.nextSetBit(start) >= start + blocks : |
| 157 |
"Double alloc: " + Integer.toHexString(start) + "/" + Integer.toHexString(blocks) + " " + this; |
| 158 |
if (allocate) { |
| 159 |
set.set(start, start + blocks); |
| 160 |
} else { |
| 161 |
failureFlags <<= 1; |
| 162 |
if (end < 0 && freeBlocksTotal > 4 * blocks) { |
| 163 |
failureFlags |= 1; |
| 164 |
} |
| 165 |
} |
| 166 |
return start; |
| 167 |
} |
| 168 |
freeBlocksTotal += freeBlocks; |
| 169 |
i = end; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Mark the space as in use. |
| 175 |
* |
| 176 |
* @param pos the position in bytes |
| 177 |
* @param length the number of bytes |
| 178 |
*/ |
| 179 |
public void markUsed(long pos, int length) { |
| 180 |
int start = getBlock(pos); |
| 181 |
int blocks = getBlockCount(length); |
| 182 |
// this is not an assert because we get called during file opening |
| 183 |
if (set.nextSetBit(start) != -1 && set.nextSetBit(start) < start + blocks ) { |
| 184 |
throw DataUtils.newMVStoreException( |
| 185 |
DataUtils.ERROR_FILE_CORRUPT, |
| 186 |
"Double mark: " + Integer.toHexString(start) + |
| 187 |
"/" + Integer.toHexString(blocks) + " " + this); |
| 188 |
} |
| 189 |
set.set(start, start + blocks); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Mark the space as free. |
| 194 |
* |
| 195 |
* @param pos the position in bytes |
| 196 |
* @param length the number of bytes |
| 197 |
*/ |
| 198 |
public void free(long pos, int length) { |
| 199 |
int start = getBlock(pos); |
| 200 |
int blocks = getBlockCount(length); |
| 201 |
assert set.nextClearBit(start) >= start + blocks : |
| 202 |
"Double free: " + Integer.toHexString(start) + "/" + Integer.toHexString(blocks) + " " + this; |
| 203 |
set.clear(start, start + blocks); |
| 204 |
} |
| 205 |
|
| 206 |
private long getPos(int block) { |
| 207 |
return (long) block * (long) blockSize; |
| 208 |
} |
| 209 |
|
| 210 |
private int getBlock(long pos) { |
| 211 |
return (int) (pos / blockSize); |
| 212 |
} |
| 213 |
|
| 214 |
private int getBlockCount(int length) { |
| 215 |
return MathUtils.roundUpInt(length, blockSize) / blockSize; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get the fill rate of the space in percent. The value 0 means the space is |
| 220 |
* completely free, and 100 means it is completely full. |
| 221 |
* |
| 222 |
* @return the fill rate (0 - 100) |
| 223 |
*/ |
| 224 |
int getFillRate() { |
| 225 |
int usedBlocks = set.cardinality() - firstFreeBlock; |
| 226 |
int totalBlocks = set.length() - firstFreeBlock; |
| 227 |
return totalBlocks == 0 ? 0 : (int)((100L * usedBlocks + totalBlocks - 1) / totalBlocks); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get the position of the first free space. |
| 232 |
* |
| 233 |
* @return the position. |
| 234 |
*/ |
| 235 |
long getFirstFree() { |
| 236 |
return getPos(set.nextClearBit(0)); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get the position of the last (infinite) free space. |
| 241 |
* |
| 242 |
* @return the position. |
| 243 |
*/ |
| 244 |
long getLastFree() { |
| 245 |
return getPos(getAfterLastBlock()); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Get the index of the first block after last occupied one. |
| 250 |
* It marks the beginning of the last (infinite) free space. |
| 251 |
* |
| 252 |
* @return block index |
| 253 |
*/ |
| 254 |
int getAfterLastBlock() { |
| 255 |
return set.previousSetBit(set.size() - 1) + 1; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Calculates relative "priority" for chunk to be moved. |
| 260 |
* |
| 261 |
* @param block where chunk starts |
| 262 |
* @return priority, bigger number indicate that chunk need to be moved sooner |
| 263 |
*/ |
| 264 |
int getMovePriority(int block) { |
| 265 |
// The most desirable chunks to move are the ones sitting within |
| 266 |
// a relatively short span of occupied blocks which is surrounded |
| 267 |
// from both sides by relatively long free spans |
| 268 |
int prevEnd = set.previousClearBit(block); |
| 269 |
int freeSize; |
| 270 |
if (prevEnd < 0) { |
| 271 |
prevEnd = firstFreeBlock; |
| 272 |
freeSize = 0; |
| 273 |
} else { |
| 274 |
freeSize = prevEnd - set.previousSetBit(prevEnd); |
| 275 |
} |
| 276 |
|
| 277 |
int nextStart = set.nextClearBit(block); |
| 278 |
int nextEnd = set.nextSetBit(nextStart); |
| 279 |
if (nextEnd >= 0) { |
| 280 |
freeSize += nextEnd - nextStart; |
| 281 |
} |
| 282 |
return (nextStart - prevEnd - 1) * 1000 / (freeSize + 1); |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
public String toString() { |
| 287 |
StringBuilder buff = new StringBuilder(); |
| 288 |
if (DETAILED_INFO) { |
| 289 |
int onCount = 0, offCount = 0; |
| 290 |
int on = 0; |
| 291 |
for (int i = 0; i < set.length(); i++) { |
| 292 |
if (set.get(i)) { |
| 293 |
onCount++; |
| 294 |
on++; |
| 295 |
} else { |
| 296 |
offCount++; |
| 297 |
} |
| 298 |
if ((i & 1023) == 1023) { |
| 299 |
buff.append(String.format("%3x", on)).append(' '); |
| 300 |
on = 0; |
| 301 |
} |
| 302 |
} |
| 303 |
buff.append('\n') |
| 304 |
.append(" on ").append(onCount).append(" off ").append(offCount) |
| 305 |
.append(' ').append(100 * onCount / (onCount+offCount)).append("% used "); |
| 306 |
} |
| 307 |
buff.append('['); |
| 308 |
for (int i = 0;;) { |
| 309 |
if (i > 0) { |
| 310 |
buff.append(", "); |
| 311 |
} |
| 312 |
int start = set.nextClearBit(i); |
| 313 |
buff.append(Integer.toHexString(start)).append('-'); |
| 314 |
int end = set.nextSetBit(start + 1); |
| 315 |
if (end < 0) { |
| 316 |
break; |
| 317 |
} |
| 318 |
buff.append(Integer.toHexString(end - 1)); |
| 319 |
i = end + 1; |
| 320 |
} |
| 321 |
buff.append(']'); |
| 322 |
return buff.toString(); |
| 323 |
} |
| 324 |
} |