| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2006, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file buf/buf0buddy.cc |
| 29 |
Binary buddy allocator for compressed pages |
| 30 |
|
| 31 |
Created December 2006 by Marko Makela |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "buf0buddy.h" |
| 35 |
|
| 36 |
#include "buf0buf.h" |
| 37 |
#include "buf0flu.h" |
| 38 |
#include "buf0lru.h" |
| 39 |
#include "dict0dict.h" |
| 40 |
|
| 41 |
#include "page0zip.h" |
| 42 |
|
| 43 |
/** When freeing a buf we attempt to coalesce by looking at its buddy |
| 44 |
and deciding whether it is free or not. To ascertain if the buddy is |
| 45 |
free we look for BUF_BUDDY_STAMP_FREE at BUF_BUDDY_STAMP_OFFSET |
| 46 |
within the buddy. The question is how we can be sure that it is |
| 47 |
safe to look at BUF_BUDDY_STAMP_OFFSET. |
| 48 |
The answer lies in following invariants: |
| 49 |
* All blocks allocated by buddy allocator are used for compressed |
| 50 |
page frame. |
| 51 |
* A compressed table always have space_id < dict_sys_t::s_log_space_id |
| 52 |
* BUF_BUDDY_STAMP_OFFSET always points to the space_id field in |
| 53 |
a frame. |
| 54 |
-- The above is true because we look at these fields when the |
| 55 |
corresponding buddy block is free which implies that: |
| 56 |
- The block we are looking at must have an address aligned at |
| 57 |
the same size that its free buddy has. For example, if we have |
| 58 |
a free block of 8K then its buddy's address must be aligned at |
| 59 |
8K as well. |
| 60 |
- It is possible that the block we are looking at may have been |
| 61 |
further divided into smaller sized blocks but its starting |
| 62 |
address must still remain the start of a page frame i.e.: it |
| 63 |
cannot be middle of a block. For example, if we have a free |
| 64 |
block of size 8K then its buddy may be divided into blocks |
| 65 |
of, say, 1K, 1K, 2K, 4K but the buddy's address will still be |
| 66 |
the starting address of first 1K compressed page. |
| 67 |
- What is important to note is that for any given block, the |
| 68 |
buddy's address cannot be in the middle of a larger block i.e.: |
| 69 |
in above example, our 8K block cannot have a buddy whose address |
| 70 |
is aligned on 8K but it is part of a larger 16K block. |
| 71 |
*/ |
| 72 |
|
| 73 |
/** Offset within buf_buddy_free_t where free or non_free stamps |
| 74 |
are written.*/ |
| 75 |
constexpr uint32_t BUF_BUDDY_STAMP_OFFSET = FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID; |
| 76 |
|
| 77 |
/** Value that we stamp on all buffers that are currently on the zip_free |
| 78 |
list. This value is stamped at BUF_BUDDY_STAMP_OFFSET offset */ |
| 79 |
constexpr uint64_t BUF_BUDDY_STAMP_FREE = dict_sys_t::s_log_space_id; |
| 80 |
|
| 81 |
/** Stamp value for non-free buffers. Will be overwritten by a non-zero |
| 82 |
value by the consumer of the block */ |
| 83 |
[[maybe_unused]] constexpr uint64_t BUF_BUDDY_STAMP_NONFREE = 0XFFFFFFFFUL; |
| 84 |
|
| 85 |
/** Return type of buf_buddy_is_free() */ |
| 86 |
enum buf_buddy_state_t { |
| 87 |
BUF_BUDDY_STATE_FREE, /*!< If the buddy to completely free */ |
| 88 |
BUF_BUDDY_STATE_USED, /*!< Buddy currently in used */ |
| 89 |
BUF_BUDDY_STATE_PARTIALLY_USED /*!< Some sub-blocks in the buddy |
| 90 |
are in use */ |
| 91 |
}; |
| 92 |
|
| 93 |
#ifdef UNIV_DEBUG_VALGRIND |
| 94 |
/** Invalidate memory area that we won't access while page is free */ |
| 95 |
static inline void buf_buddy_mem_invalid( |
| 96 |
buf_buddy_free_t *buf, /*!< in: block to check */ |
| 97 |
ulint i) /*!< in: index of zip_free[] */ |
| 98 |
{ |
| 99 |
const size_t size = BUF_BUDDY_LOW << i; |
| 100 |
ut_ad(i <= BUF_BUDDY_SIZES); |
| 101 |
|
| 102 |
UNIV_MEM_ASSERT_W(buf, size); |
| 103 |
UNIV_MEM_INVALID(buf, size); |
| 104 |
} |
| 105 |
#else /* UNIV_DEBUG_VALGRIND */ |
| 106 |
static inline void buf_buddy_mem_invalid(buf_buddy_free_t *, ulint i) { |
| 107 |
ut_ad(i <= BUF_BUDDY_SIZES); |
| 108 |
} |
| 109 |
#endif /* UNIV_DEBUG_VALGRIND */ |
| 110 |
|
| 111 |
/** Check if a buddy is stamped free. |
| 112 |
@return whether the buddy is free */ |
| 113 |
[[nodiscard]] static inline bool buf_buddy_stamp_is_free( |
| 114 |
const buf_buddy_free_t *buf) /*!< in: block to check */ |
| 115 |
{ |
| 116 |
return (mach_read_from_4(buf->stamp.bytes + BUF_BUDDY_STAMP_OFFSET) == |
| 117 |
BUF_BUDDY_STAMP_FREE); |
| 118 |
} |
| 119 |
|
| 120 |
/** Stamps a buddy free. */ |
| 121 |
static inline void buf_buddy_stamp_free( |
| 122 |
buf_buddy_free_t *buf, /*!< in/out: block to stamp */ |
| 123 |
ulint i) /*!< in: block size */ |
| 124 |
{ |
| 125 |
ut_d(memset(&buf->stamp, static_cast<int>(i), BUF_BUDDY_LOW << i)); |
| 126 |
buf_buddy_mem_invalid(buf, i); |
| 127 |
mach_write_to_4(buf->stamp.bytes + BUF_BUDDY_STAMP_OFFSET, |
| 128 |
BUF_BUDDY_STAMP_FREE); |
| 129 |
buf->stamp.size = i; |
| 130 |
} |
| 131 |
|
| 132 |
/** Stamps a buddy nonfree. |
| 133 |
@param[in,out] buf block to stamp |
| 134 |
@param[in] i block size */ |
| 135 |
static inline void buf_buddy_stamp_nonfree(buf_buddy_free_t *buf, ulint i) { |
| 136 |
buf_buddy_mem_invalid(buf, i); |
| 137 |
memset(buf->stamp.bytes + BUF_BUDDY_STAMP_OFFSET, 0xff, 4); |
| 138 |
} |
| 139 |
|
| 140 |
/** Get the offset of the buddy of a compressed page frame. |
| 141 |
@return the buddy relative of page */ |
| 142 |
static inline void *buf_buddy_get(byte *page, /*!< in: compressed page */ |
| 143 |
ulint size) /*!< in: page size in bytes */ |
| 144 |
{ |
| 145 |
ut_ad(ut_is_2pow(size)); |
| 146 |
ut_ad(size >= BUF_BUDDY_LOW); |
| 147 |
static_assert(BUF_BUDDY_LOW <= UNIV_ZIP_SIZE_MIN); |
| 148 |
ut_ad(size < BUF_BUDDY_HIGH); |
| 149 |
ut_ad(BUF_BUDDY_HIGH == UNIV_PAGE_SIZE); |
| 150 |
ut_ad(!ut_align_offset(page, size)); |
| 151 |
|
| 152 |
if (((ulint)page) & size) { |
| 153 |
return (page - size); |
| 154 |
} else { |
| 155 |
return (page + size); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
#ifdef UNIV_DEBUG |
| 160 |
/** Validate a given zip_free list. */ |
| 161 |
struct CheckZipFree { |
| 162 |
CheckZipFree(ulint i) : m_i(i) {} |
| 163 |
|
| 164 |
void operator()(const buf_buddy_free_t *elem) const { |
| 165 |
ut_a(buf_buddy_stamp_is_free(elem)); |
| 166 |
ut_a(elem->stamp.size <= m_i); |
| 167 |
} |
| 168 |
|
| 169 |
ulint m_i; |
| 170 |
}; |
| 171 |
|
| 172 |
/** Validate a buddy list. |
| 173 |
@param[in] buf_pool buffer pool instance |
| 174 |
@param[in] i buddy size to validate */ |
| 175 |
static void buf_buddy_list_validate(const buf_pool_t *buf_pool, ulint i) { |
| 176 |
CheckZipFree check(i); |
| 177 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 178 |
ut_list_validate(buf_pool->zip_free[i], check); |
| 179 |
} |
| 180 |
|
| 181 |
/** Debug function to validate that a buffer is indeed free i.e.: in the |
| 182 |
zip_free[]. |
| 183 |
@param[in] buf_pool buffer pool instance |
| 184 |
@param[in] buf block to check |
| 185 |
@param[in] i index of buf_pool->zip_free[] |
| 186 |
@return true if free */ |
| 187 |
static inline bool buf_buddy_check_free(buf_pool_t *buf_pool, |
| 188 |
const buf_buddy_free_t *buf, ulint i) { |
| 189 |
const ulint size = BUF_BUDDY_LOW << i; |
| 190 |
|
| 191 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 192 |
ut_ad(!ut_align_offset(buf, size)); |
| 193 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 194 |
|
| 195 |
for (auto itr : buf_pool->zip_free[i]) { |
| 196 |
if (itr == buf) return true; |
| 197 |
} |
| 198 |
|
| 199 |
return false; |
| 200 |
} |
| 201 |
#endif /* UNIV_DEBUG */ |
| 202 |
|
| 203 |
/** Checks if a buf is free i.e.: in the zip_free[]. |
| 204 |
@retval BUF_BUDDY_STATE_FREE if fully free |
| 205 |
@retval BUF_BUDDY_STATE_USED if currently in use |
| 206 |
@retval BUF_BUDDY_STATE_PARTIALLY_USED if partially in use. */ |
| 207 |
[[nodiscard]] static buf_buddy_state_t buf_buddy_is_free( |
| 208 |
buf_buddy_free_t *buf, /*!< in: block to check */ |
| 209 |
ulint i) /*!< in: index of |
| 210 |
buf_pool->zip_free[] */ |
| 211 |
{ |
| 212 |
#ifdef UNIV_DEBUG |
| 213 |
const ulint size = BUF_BUDDY_LOW << i; |
| 214 |
ut_ad(!ut_align_offset(buf, size)); |
| 215 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 216 |
#endif /* UNIV_DEBUG */ |
| 217 |
|
| 218 |
/* We assume that all memory from buf_buddy_alloc() |
| 219 |
is used for compressed page frames. */ |
| 220 |
|
| 221 |
/* We look inside the allocated objects returned by |
| 222 |
buf_buddy_alloc() and assume that each block is a compressed |
| 223 |
page that contains one of the following in space_id. |
| 224 |
* BUF_BUDDY_STAMP_FREE if the block is in a zip_free list or |
| 225 |
* BUF_BUDDY_STAMP_NONFREE if the block has been allocated but |
| 226 |
not initialized yet or |
| 227 |
* A valid space_id of a compressed tablespace |
| 228 |
|
| 229 |
The call below attempts to read from free memory. The memory |
| 230 |
is "owned" by the buddy allocator (and it has been allocated |
| 231 |
from the buffer pool), so there is nothing wrong about this. */ |
| 232 |
if (!buf_buddy_stamp_is_free(buf)) { |
| 233 |
return (BUF_BUDDY_STATE_USED); |
| 234 |
} |
| 235 |
|
| 236 |
/* A block may be free but a fragment of it may still be in use. |
| 237 |
To guard against that we write the free block size in terms of |
| 238 |
zip_free index at start of stamped block. Note that we can |
| 239 |
safely rely on this value only if the buf is free. */ |
| 240 |
ut_ad(buf->stamp.size <= i); |
| 241 |
return (buf->stamp.size == i ? BUF_BUDDY_STATE_FREE |
| 242 |
: BUF_BUDDY_STATE_PARTIALLY_USED); |
| 243 |
} |
| 244 |
|
| 245 |
/** Add a block to the head of the appropriate buddy free list. |
| 246 |
@param[in] buf_pool buffer pool instance |
| 247 |
@param[in,out] buf block to be freed |
| 248 |
@param[in] i index of buf_pool->zip_free[] */ |
| 249 |
static inline void buf_buddy_add_to_free(buf_pool_t *buf_pool, |
| 250 |
buf_buddy_free_t *buf, ulint i) { |
| 251 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 252 |
ut_ad(buf_pool->zip_free[i].first_element != buf); |
| 253 |
|
| 254 |
buf_buddy_stamp_free(buf, i); |
| 255 |
UT_LIST_ADD_FIRST(buf_pool->zip_free[i], buf); |
| 256 |
ut_d(buf_buddy_list_validate(buf_pool, i)); |
| 257 |
} |
| 258 |
|
| 259 |
/** Remove a block from the appropriate buddy free list. |
| 260 |
@param[in] buf_pool buffer pool instance |
| 261 |
@param[in,out] buf block to be freed |
| 262 |
@param[in] i index of buf_pool->zip_free[] */ |
| 263 |
static inline void buf_buddy_remove_from_free(buf_pool_t *buf_pool, |
| 264 |
buf_buddy_free_t *buf, ulint i) { |
| 265 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 266 |
ut_ad(buf_buddy_check_free(buf_pool, buf, i)); |
| 267 |
|
| 268 |
UT_LIST_REMOVE(buf_pool->zip_free[i], buf); |
| 269 |
buf_buddy_stamp_nonfree(buf, i); |
| 270 |
} |
| 271 |
|
| 272 |
/** Try to allocate a block from buf_pool->zip_free[]. |
| 273 |
@param[in] buf_pool buffer pool instance |
| 274 |
@param[in] i index of buf_pool->zip_free[] |
| 275 |
@return allocated block, or NULL if buf_pool->zip_free[] was empty */ |
| 276 |
static buf_buddy_free_t *buf_buddy_alloc_zip(buf_pool_t *buf_pool, ulint i) { |
| 277 |
buf_buddy_free_t *buf; |
| 278 |
|
| 279 |
ut_a(i < BUF_BUDDY_SIZES); |
| 280 |
ut_a(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 281 |
|
| 282 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 283 |
ut_d(buf_buddy_list_validate(buf_pool, i)); |
| 284 |
|
| 285 |
buf = UT_LIST_GET_FIRST(buf_pool->zip_free[i]); |
| 286 |
|
| 287 |
if (buf_get_withdraw_depth(buf_pool)) { |
| 288 |
while (buf != nullptr && |
| 289 |
buf_frame_will_withdrawn(buf_pool, reinterpret_cast<byte *>(buf))) { |
| 290 |
/* This should be withdrawn, not to be allocated */ |
| 291 |
buf = UT_LIST_GET_NEXT(list, buf); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (buf) { |
| 296 |
buf_buddy_remove_from_free(buf_pool, buf, i); |
| 297 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 298 |
|
| 299 |
} else if (i + 1 < BUF_BUDDY_SIZES) { |
| 300 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 301 |
/* Attempt to split. */ |
| 302 |
buf = buf_buddy_alloc_zip(buf_pool, i + 1); |
| 303 |
|
| 304 |
if (buf) { |
| 305 |
byte *allocated_block = buf->stamp.bytes; |
| 306 |
buf_buddy_free_t *buddy = reinterpret_cast<buf_buddy_free_t *>( |
| 307 |
allocated_block + (BUF_BUDDY_LOW << i)); |
| 308 |
|
| 309 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 310 |
ut_ad(!buf_pool_contains_zip(buf_pool, buddy)); |
| 311 |
buf_buddy_add_to_free(buf_pool, buddy, i); |
| 312 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 313 |
} |
| 314 |
} else { |
| 315 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 316 |
} |
| 317 |
|
| 318 |
if (buf) { |
| 319 |
/* Trash the page other than the BUF_BUDDY_STAMP_NONFREE. */ |
| 320 |
UNIV_MEM_TRASH(buf, ~i, BUF_BUDDY_STAMP_OFFSET); |
| 321 |
UNIV_MEM_TRASH(BUF_BUDDY_STAMP_OFFSET + 4 + buf->stamp.bytes, ~i, |
| 322 |
(BUF_BUDDY_LOW << i) - (BUF_BUDDY_STAMP_OFFSET + 4)); |
| 323 |
ut_ad(mach_read_from_4(buf->stamp.bytes + BUF_BUDDY_STAMP_OFFSET) == |
| 324 |
BUF_BUDDY_STAMP_NONFREE); |
| 325 |
} |
| 326 |
|
| 327 |
return (buf); |
| 328 |
} |
| 329 |
|
| 330 |
/** Deallocate a buffer frame of UNIV_PAGE_SIZE. |
| 331 |
@param[in] buf_pool buffer pool instance |
| 332 |
@param[in] buf buffer frame to deallocate */ |
| 333 |
static void buf_buddy_block_free(buf_pool_t *buf_pool, void *buf) { |
| 334 |
const auto hash_value = buf_pool_hash_zip_frame(buf); |
| 335 |
buf_page_t *bpage; |
| 336 |
|
| 337 |
ut_ad(!mutex_own(&buf_pool->zip_mutex)); |
| 338 |
ut_a(!ut_align_offset(buf, UNIV_PAGE_SIZE)); |
| 339 |
|
| 340 |
mutex_enter(&buf_pool->zip_hash_mutex); |
| 341 |
|
| 342 |
HASH_SEARCH(hash, buf_pool->zip_hash, hash_value, buf_page_t *, bpage, |
| 343 |
ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_MEMORY && |
| 344 |
bpage->in_zip_hash && !bpage->in_page_hash), |
| 345 |
((buf_block_t *)bpage)->frame == buf); |
| 346 |
ut_a(bpage); |
| 347 |
ut_a(buf_page_get_state(bpage) == BUF_BLOCK_MEMORY); |
| 348 |
ut_ad(!bpage->in_page_hash); |
| 349 |
ut_ad(bpage->in_zip_hash); |
| 350 |
ut_d(bpage->in_zip_hash = false); |
| 351 |
HASH_DELETE(buf_page_t, hash, buf_pool->zip_hash, hash_value, bpage); |
| 352 |
|
| 353 |
ut_ad(buf_pool->buddy_n_frames > 0); |
| 354 |
ut_d(buf_pool->buddy_n_frames--); |
| 355 |
|
| 356 |
mutex_exit(&buf_pool->zip_hash_mutex); |
| 357 |
|
| 358 |
ut_d(memset(buf, 0, UNIV_PAGE_SIZE)); |
| 359 |
UNIV_MEM_INVALID(buf, UNIV_PAGE_SIZE); |
| 360 |
|
| 361 |
buf_LRU_block_free_non_file_page(reinterpret_cast<buf_block_t *>(bpage)); |
| 362 |
} |
| 363 |
|
| 364 |
/** Allocate a buffer block to the buddy allocator. |
| 365 |
@param[in] block buffer frame to allocate */ |
| 366 |
static void buf_buddy_block_register(buf_block_t *block) { |
| 367 |
buf_pool_t *buf_pool = buf_pool_from_block(block); |
| 368 |
const auto hash_value = buf_pool_hash_zip(block); |
| 369 |
ut_ad(!mutex_own(&buf_pool->zip_mutex)); |
| 370 |
ut_ad(buf_block_get_state(block) == BUF_BLOCK_READY_FOR_USE); |
| 371 |
|
| 372 |
buf_block_set_state(block, BUF_BLOCK_MEMORY); |
| 373 |
|
| 374 |
ut_a(block->frame); |
| 375 |
ut_a(!ut_align_offset(block->frame, UNIV_PAGE_SIZE)); |
| 376 |
|
| 377 |
ut_ad(!block->page.in_page_hash); |
| 378 |
ut_ad(!block->page.in_zip_hash); |
| 379 |
ut_d(block->page.in_zip_hash = true); |
| 380 |
|
| 381 |
mutex_enter(&buf_pool->zip_hash_mutex); |
| 382 |
HASH_INSERT(buf_page_t, hash, buf_pool->zip_hash, hash_value, &block->page); |
| 383 |
|
| 384 |
ut_d(buf_pool->buddy_n_frames++); |
| 385 |
mutex_exit(&buf_pool->zip_hash_mutex); |
| 386 |
} |
| 387 |
|
| 388 |
/** Allocate a block from a bigger object. |
| 389 |
@param[in] buf_pool buffer pool instance |
| 390 |
@param[in] buf a block that is free to use |
| 391 |
@param[in] i index of buf_pool->zip_free[] |
| 392 |
@param[in] j size of buf as an index of buf_pool->zip_free[] |
| 393 |
@return allocated block */ |
| 394 |
static void *buf_buddy_alloc_from(buf_pool_t *buf_pool, void *buf, ulint i, |
| 395 |
ulint j) { |
| 396 |
ulint offs = BUF_BUDDY_LOW << j; |
| 397 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 398 |
ut_ad(j <= BUF_BUDDY_SIZES); |
| 399 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 400 |
ut_ad(j >= i); |
| 401 |
ut_ad(!ut_align_offset(buf, offs)); |
| 402 |
|
| 403 |
/* Add the unused parts of the block to the free lists. */ |
| 404 |
while (j > i) { |
| 405 |
buf_buddy_free_t *zip_buf; |
| 406 |
|
| 407 |
offs >>= 1; |
| 408 |
j--; |
| 409 |
|
| 410 |
zip_buf = reinterpret_cast<buf_buddy_free_t *>( |
| 411 |
reinterpret_cast<byte *>(buf) + offs); |
| 412 |
buf_buddy_add_to_free(buf_pool, zip_buf, j); |
| 413 |
} |
| 414 |
|
| 415 |
buf_buddy_stamp_nonfree(reinterpret_cast<buf_buddy_free_t *>(buf), i); |
| 416 |
return (buf); |
| 417 |
} |
| 418 |
|
| 419 |
/** Allocate a block. |
| 420 |
@param[in,out] buf_pool buffer pool instance |
| 421 |
@param[in] i index of buf_pool->zip_free[] |
| 422 |
or BUF_BUDDY_SIZES |
| 423 |
@return allocated block, never NULL */ |
| 424 |
void *buf_buddy_alloc_low(buf_pool_t *buf_pool, ulint i) { |
| 425 |
buf_block_t *block; |
| 426 |
|
| 427 |
ut_ad(!mutex_own(&buf_pool->zip_mutex)); |
| 428 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 429 |
|
| 430 |
if (i < BUF_BUDDY_SIZES) { |
| 431 |
/* Try to allocate from the buddy system. */ |
| 432 |
block = (buf_block_t *)buf_buddy_alloc_zip(buf_pool, i); |
| 433 |
|
| 434 |
if (block) { |
| 435 |
goto func_exit; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/* Try allocating from the buf_pool->free list. */ |
| 440 |
block = buf_LRU_get_free_only(buf_pool); |
| 441 |
|
| 442 |
if (block) { |
| 443 |
goto alloc_big; |
| 444 |
} |
| 445 |
|
| 446 |
/* Try replacing an uncompressed page in the buffer pool. */ |
| 447 |
block = buf_LRU_get_free_block(buf_pool); |
| 448 |
|
| 449 |
alloc_big: |
| 450 |
buf_buddy_block_register(block); |
| 451 |
|
| 452 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 453 |
block = (buf_block_t *)buf_buddy_alloc_from(buf_pool, block->frame, i, |
| 454 |
BUF_BUDDY_SIZES); |
| 455 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 456 |
|
| 457 |
func_exit: |
| 458 |
buf_pool->buddy_stat[i].used.fetch_add(1); |
| 459 |
return (block); |
| 460 |
} |
| 461 |
|
| 462 |
/** Try to relocate a block. The caller must hold zip_free_mutex, and this |
| 463 |
function will release and lock it again. |
| 464 |
@param[in] buf_pool buffer pool instance |
| 465 |
@param[in] src block to relocate |
| 466 |
@param[in] dst free block to relocated to |
| 467 |
@param[in] i index of buf_pool->zip_free[] |
| 468 |
@param[in] force true if we must relocated always |
| 469 |
@return true if relocated */ |
| 470 |
static bool buf_buddy_relocate(buf_pool_t *buf_pool, void *src, void *dst, |
| 471 |
ulint i, bool force) { |
| 472 |
buf_page_t *bpage; |
| 473 |
const ulint size = BUF_BUDDY_LOW << i; |
| 474 |
space_id_t space; |
| 475 |
page_no_t offset; |
| 476 |
|
| 477 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 478 |
ut_ad(!mutex_own(&buf_pool->zip_mutex)); |
| 479 |
ut_ad(!ut_align_offset(src, size)); |
| 480 |
ut_ad(!ut_align_offset(dst, size)); |
| 481 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 482 |
UNIV_MEM_ASSERT_W(dst, size); |
| 483 |
|
| 484 |
space = |
| 485 |
mach_read_from_4((const byte *)src + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID); |
| 486 |
offset = mach_read_from_4((const byte *)src + FIL_PAGE_OFFSET); |
| 487 |
|
| 488 |
/* Suppress Valgrind warnings about conditional jump |
| 489 |
on uninitialized value. */ |
| 490 |
UNIV_MEM_VALID(&space, sizeof space); |
| 491 |
UNIV_MEM_VALID(&offset, sizeof offset); |
| 492 |
|
| 493 |
ut_ad(space != BUF_BUDDY_STAMP_FREE); |
| 494 |
|
| 495 |
const page_id_t page_id(space, offset); |
| 496 |
|
| 497 |
/* If space,offset is bogus, then we know that the |
| 498 |
buf_page_hash_get_low() call below will return NULL. */ |
| 499 |
if (!force && buf_pool != buf_pool_get(page_id)) { |
| 500 |
return (false); |
| 501 |
} |
| 502 |
|
| 503 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 504 |
|
| 505 |
rw_lock_t *hash_lock = buf_page_hash_lock_get(buf_pool, page_id); |
| 506 |
|
| 507 |
rw_lock_x_lock(hash_lock, UT_LOCATION_HERE); |
| 508 |
|
| 509 |
/* page_hash can be changed. */ |
| 510 |
hash_lock = buf_page_hash_lock_x_confirm(hash_lock, buf_pool, page_id); |
| 511 |
|
| 512 |
bpage = buf_page_hash_get_low(buf_pool, page_id); |
| 513 |
|
| 514 |
if (!bpage || bpage->zip.data != src) { |
| 515 |
/* The block has probably been freshly |
| 516 |
allocated by buf_LRU_get_free_block() but not |
| 517 |
added to buf_pool->page_hash yet. Obviously, |
| 518 |
it cannot be relocated. */ |
| 519 |
|
| 520 |
rw_lock_x_unlock(hash_lock); |
| 521 |
|
| 522 |
if (!force || space != 0 || offset != 0) { |
| 523 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 524 |
return (false); |
| 525 |
} |
| 526 |
|
| 527 |
/* It might be just uninitialized page. |
| 528 |
We should search from LRU list also. */ |
| 529 |
|
| 530 |
/* force is true only when buffer pool resizing, |
| 531 |
in which we hold LRU_list_mutex already, see |
| 532 |
buf_pool_withdraw_blocks(). */ |
| 533 |
ut_ad(force); |
| 534 |
ut_ad(mutex_own(&buf_pool->LRU_list_mutex)); |
| 535 |
|
| 536 |
bpage = UT_LIST_GET_FIRST(buf_pool->LRU); |
| 537 |
while (bpage != nullptr) { |
| 538 |
if (bpage->zip.data == src) { |
| 539 |
hash_lock = buf_page_hash_lock_get(buf_pool, bpage->id); |
| 540 |
rw_lock_x_lock(hash_lock, UT_LOCATION_HERE); |
| 541 |
break; |
| 542 |
} |
| 543 |
bpage = UT_LIST_GET_NEXT(LRU, bpage); |
| 544 |
} |
| 545 |
|
| 546 |
if (bpage == nullptr) { |
| 547 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 548 |
return (false); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if (page_zip_get_size(&bpage->zip) != size) { |
| 553 |
/* The block is of different size. We would |
| 554 |
have to relocate all blocks covered by src. |
| 555 |
For the sake of simplicity, give up. */ |
| 556 |
ut_ad(page_zip_get_size(&bpage->zip) < size); |
| 557 |
|
| 558 |
rw_lock_x_unlock(hash_lock); |
| 559 |
|
| 560 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 561 |
return (false); |
| 562 |
} |
| 563 |
|
| 564 |
/* The block must have been allocated, but it may |
| 565 |
contain uninitialized data. */ |
| 566 |
UNIV_MEM_ASSERT_W(src, size); |
| 567 |
|
| 568 |
BPageMutex *block_mutex = buf_page_get_mutex(bpage); |
| 569 |
|
| 570 |
mutex_enter(block_mutex); |
| 571 |
|
| 572 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 573 |
|
| 574 |
if (buf_page_can_relocate(bpage)) { |
| 575 |
/* Relocate the compressed page. */ |
| 576 |
const auto start_time = std::chrono::steady_clock::now(); |
| 577 |
|
| 578 |
ut_a(bpage->zip.data == src); |
| 579 |
|
| 580 |
memcpy(dst, src, size); |
| 581 |
bpage->zip.data = reinterpret_cast<page_zip_t *>(dst); |
| 582 |
|
| 583 |
rw_lock_x_unlock(hash_lock); |
| 584 |
|
| 585 |
mutex_exit(block_mutex); |
| 586 |
|
| 587 |
buf_buddy_mem_invalid(reinterpret_cast<buf_buddy_free_t *>(src), i); |
| 588 |
|
| 589 |
buf_buddy_stat_t *buddy_stat = &buf_pool->buddy_stat[i]; |
| 590 |
buddy_stat->relocated++; |
| 591 |
buddy_stat->relocated_duration += |
| 592 |
std::chrono::steady_clock::now() - start_time; |
| 593 |
return (true); |
| 594 |
} |
| 595 |
|
| 596 |
rw_lock_x_unlock(hash_lock); |
| 597 |
|
| 598 |
mutex_exit(block_mutex); |
| 599 |
return (false); |
| 600 |
} |
| 601 |
|
| 602 |
/** Deallocate a block. |
| 603 |
@param[in] buf_pool buffer pool instance |
| 604 |
@param[in] buf block to be freed, must not be pointed to |
| 605 |
by the buffer pool |
| 606 |
@param[in] i index of buf_pool->zip_free[], |
| 607 |
or BUF_BUDDY_SIZES |
| 608 |
@param[in] has_zip_free whether has zip_free_mutex */ |
| 609 |
void buf_buddy_free_low(buf_pool_t *buf_pool, void *buf, ulint i, |
| 610 |
bool has_zip_free) { |
| 611 |
buf_buddy_free_t *buddy; |
| 612 |
|
| 613 |
ut_ad(!mutex_own(&buf_pool->zip_mutex)); |
| 614 |
ut_ad(i <= BUF_BUDDY_SIZES); |
| 615 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 616 |
|
| 617 |
if (!has_zip_free) { |
| 618 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 619 |
} |
| 620 |
|
| 621 |
ut_ad(mutex_own(&buf_pool->zip_free_mutex)); |
| 622 |
ut_ad(buf_pool->buddy_stat[i].used > 0); |
| 623 |
buf_pool->buddy_stat[i].used.fetch_sub(1); |
| 624 |
recombine: |
| 625 |
UNIV_MEM_ASSERT_AND_ALLOC(buf, BUF_BUDDY_LOW << i); |
| 626 |
|
| 627 |
if (i == BUF_BUDDY_SIZES) { |
| 628 |
if (!has_zip_free) { |
| 629 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 630 |
} |
| 631 |
buf_buddy_block_free(buf_pool, buf); |
| 632 |
return; |
| 633 |
} |
| 634 |
|
| 635 |
ut_ad(i < BUF_BUDDY_SIZES); |
| 636 |
ut_ad(buf == ut_align_down(buf, BUF_BUDDY_LOW << i)); |
| 637 |
ut_ad(!buf_pool_contains_zip(buf_pool, buf)); |
| 638 |
|
| 639 |
/* Do not recombine blocks if there are few free blocks. |
| 640 |
We may waste up to 15360*max_len bytes to free blocks |
| 641 |
(1024 + 2048 + 4096 + 8192 = 15360) */ |
| 642 |
if (UT_LIST_GET_LEN(buf_pool->zip_free[i]) < 16 && |
| 643 |
buf_pool->curr_size >= buf_pool->old_size) { |
| 644 |
goto func_exit; |
| 645 |
} |
| 646 |
|
| 647 |
/* Try to combine adjacent blocks. */ |
| 648 |
buddy = reinterpret_cast<buf_buddy_free_t *>( |
| 649 |
buf_buddy_get(reinterpret_cast<byte *>(buf), BUF_BUDDY_LOW << i)); |
| 650 |
|
| 651 |
switch (buf_buddy_is_free(buddy, i)) { |
| 652 |
case BUF_BUDDY_STATE_FREE: |
| 653 |
/* The buddy is free: recombine */ |
| 654 |
buf_buddy_remove_from_free(buf_pool, buddy, i); |
| 655 |
buddy_is_free: |
| 656 |
ut_ad(!buf_pool_contains_zip(buf_pool, buddy)); |
| 657 |
i++; |
| 658 |
buf = ut_align_down(buf, BUF_BUDDY_LOW << i); |
| 659 |
|
| 660 |
goto recombine; |
| 661 |
|
| 662 |
case BUF_BUDDY_STATE_USED: |
| 663 |
ut_d(buf_buddy_list_validate(buf_pool, i)); |
| 664 |
|
| 665 |
/* The buddy is not free. Is there a free block of |
| 666 |
this size? */ |
| 667 |
if (buf_buddy_free_t *zip_buf = |
| 668 |
UT_LIST_GET_FIRST(buf_pool->zip_free[i])) { |
| 669 |
/* Remove the block from the free list, because |
| 670 |
a successful buf_buddy_relocate() will overwrite |
| 671 |
zip_free->list. */ |
| 672 |
buf_buddy_remove_from_free(buf_pool, zip_buf, i); |
| 673 |
|
| 674 |
/* Try to relocate the buddy of buf to the free |
| 675 |
block. */ |
| 676 |
if (buf_buddy_relocate(buf_pool, buddy, zip_buf, i, false)) { |
| 677 |
goto buddy_is_free; |
| 678 |
} |
| 679 |
|
| 680 |
buf_buddy_add_to_free(buf_pool, zip_buf, i); |
| 681 |
} |
| 682 |
|
| 683 |
break; |
| 684 |
case BUF_BUDDY_STATE_PARTIALLY_USED: |
| 685 |
/* Some sub-blocks in the buddy are still in use. |
| 686 |
Relocation will fail. No need to try. */ |
| 687 |
break; |
| 688 |
} |
| 689 |
|
| 690 |
func_exit: |
| 691 |
/* Free the block to the buddy list. */ |
| 692 |
buf_buddy_add_to_free(buf_pool, reinterpret_cast<buf_buddy_free_t *>(buf), i); |
| 693 |
if (!has_zip_free) { |
| 694 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
/** Try to reallocate a block. |
| 699 |
@param[in] buf_pool buffer pool instance |
| 700 |
@param[in] buf block to be reallocated, must be pointed |
| 701 |
to by the buffer pool |
| 702 |
@param[in] size block size, up to UNIV_PAGE_SIZE |
| 703 |
@retval true if succeeded or if failed because the block was fixed |
| 704 |
@retval false if failed because of no free blocks. */ |
| 705 |
bool buf_buddy_realloc(buf_pool_t *buf_pool, void *buf, ulint size) { |
| 706 |
buf_block_t *block = nullptr; |
| 707 |
ulint i = buf_buddy_get_slot(size); |
| 708 |
|
| 709 |
ut_ad(!mutex_own(&buf_pool->zip_mutex)); |
| 710 |
ut_ad(i <= BUF_BUDDY_SIZES); |
| 711 |
ut_ad(i >= buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)); |
| 712 |
|
| 713 |
if (i < BUF_BUDDY_SIZES) { |
| 714 |
/* Try to allocate from the buddy system. */ |
| 715 |
block = reinterpret_cast<buf_block_t *>(buf_buddy_alloc_zip(buf_pool, i)); |
| 716 |
} |
| 717 |
|
| 718 |
if (block == nullptr) { |
| 719 |
/* Try allocating from the buf_pool->free list if it is not empty. This |
| 720 |
method is executed during withdrawing phase of BufferPool resize only. It is |
| 721 |
better to not block other user threads as much as possible. So, the main |
| 722 |
strategy is to passively reserve and use blocks that are already on the free |
| 723 |
list. Otherwise, if we were to call `buf_LRU_get_free_block` instead of |
| 724 |
`buf_LRU_get_free_only`, we would have to release the LRU mutex before the |
| 725 |
call and this would cause a need to break the reallocation loop in |
| 726 |
`buf_pool_withdraw_blocks`, which would render withdrawing even more |
| 727 |
inefficient. */ |
| 728 |
block = buf_LRU_get_free_only(buf_pool); |
| 729 |
|
| 730 |
if (block == nullptr) { |
| 731 |
return (false); /* free_list was not enough */ |
| 732 |
} |
| 733 |
|
| 734 |
buf_buddy_block_register(block); |
| 735 |
|
| 736 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 737 |
block = reinterpret_cast<buf_block_t *>( |
| 738 |
buf_buddy_alloc_from(buf_pool, block->frame, i, BUF_BUDDY_SIZES)); |
| 739 |
} else { |
| 740 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 741 |
} |
| 742 |
|
| 743 |
buf_pool->buddy_stat[i].used.fetch_add(1); |
| 744 |
|
| 745 |
/* Try to relocate the buddy of buf to the free block. */ |
| 746 |
if (buf_buddy_relocate(buf_pool, buf, block, i, true)) { |
| 747 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 748 |
/* succeeded */ |
| 749 |
buf_buddy_free_low(buf_pool, buf, i, false); |
| 750 |
return (true); |
| 751 |
} |
| 752 |
|
| 753 |
/* failed */ |
| 754 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 755 |
buf_buddy_free_low(buf_pool, block, i, false); |
| 756 |
|
| 757 |
return (false); |
| 758 |
} |
| 759 |
|
| 760 |
/** Combine all pairs of free buddies. |
| 761 |
@param[in] buf_pool buffer pool instance */ |
| 762 |
void buf_buddy_condense_free(buf_pool_t *buf_pool) { |
| 763 |
mutex_enter(&buf_pool->zip_free_mutex); |
| 764 |
ut_ad(buf_pool->curr_size < buf_pool->old_size); |
| 765 |
|
| 766 |
for (ulint i = 0; i < UT_ARR_SIZE(buf_pool->zip_free); ++i) { |
| 767 |
buf_buddy_free_t *buf = UT_LIST_GET_FIRST(buf_pool->zip_free[i]); |
| 768 |
|
| 769 |
/* seek to withdraw target */ |
| 770 |
while (buf != nullptr && |
| 771 |
!buf_frame_will_withdrawn(buf_pool, reinterpret_cast<byte *>(buf))) { |
| 772 |
buf = UT_LIST_GET_NEXT(list, buf); |
| 773 |
} |
| 774 |
|
| 775 |
while (buf != nullptr) { |
| 776 |
buf_buddy_free_t *next = UT_LIST_GET_NEXT(list, buf); |
| 777 |
|
| 778 |
buf_buddy_free_t *buddy = reinterpret_cast<buf_buddy_free_t *>( |
| 779 |
buf_buddy_get(reinterpret_cast<byte *>(buf), BUF_BUDDY_LOW << i)); |
| 780 |
|
| 781 |
/* seek to the next withdraw target */ |
| 782 |
while (true) { |
| 783 |
while (next != nullptr && |
| 784 |
!buf_frame_will_withdrawn(buf_pool, |
| 785 |
reinterpret_cast<byte *>(next))) { |
| 786 |
next = UT_LIST_GET_NEXT(list, next); |
| 787 |
} |
| 788 |
|
| 789 |
if (buddy != next) { |
| 790 |
break; |
| 791 |
} |
| 792 |
|
| 793 |
next = UT_LIST_GET_NEXT(list, next); |
| 794 |
} |
| 795 |
|
| 796 |
if (buf_buddy_is_free(buddy, i) == BUF_BUDDY_STATE_FREE) { |
| 797 |
/* Both buf and buddy are free. |
| 798 |
Try to combine them. */ |
| 799 |
buf_buddy_remove_from_free(buf_pool, buf, i); |
| 800 |
buf_pool->buddy_stat[i].used.fetch_add(1); |
| 801 |
|
| 802 |
buf_buddy_free_low(buf_pool, buf, i, true); |
| 803 |
} |
| 804 |
|
| 805 |
buf = next; |
| 806 |
} |
| 807 |
} |
| 808 |
mutex_exit(&buf_pool->zip_free_mutex); |
| 809 |
} |