Skip to content

Commit 39a8caa

Browse files
committed
MDEV-17441 - InnoDB transition to C++11 atomics
buf_page_t::buf_fix_count transition to Atomic_counter.
1 parent c66db37 commit 39a8caa

File tree

7 files changed

+39
-104
lines changed

7 files changed

+39
-104
lines changed

include/my_counter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ template <typename Type> class Atomic_counter
2828
Type sub(Type i) { return m_counter.fetch_sub(i, std::memory_order_relaxed); }
2929

3030
public:
31+
Atomic_counter(const Atomic_counter<Type> &rhs)
32+
{ m_counter.store(rhs, std::memory_order_relaxed); }
3133
Atomic_counter(Type val): m_counter(val) {}
3234
Atomic_counter() {}
3335

storage/innobase/buf/buf0buf.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,7 +3404,7 @@ buf_pool_watch_set(
34043404
}
34053405

34063406
/* Add to an existing watch. */
3407-
buf_block_fix(bpage);
3407+
bpage->fix();
34083408
return(NULL);
34093409
}
34103410

@@ -3544,7 +3544,7 @@ void buf_pool_watch_unset(const page_id_t page_id)
35443544
increments buf_fix_count. */
35453545
bpage = buf_page_hash_get_low(buf_pool, page_id);
35463546

3547-
if (buf_block_unfix(bpage) == 0
3547+
if (bpage->unfix() == 0
35483548
&& buf_pool_watch_is_sentinel(buf_pool, bpage)) {
35493549
buf_pool_watch_remove(buf_pool, bpage);
35503550
}
@@ -3777,7 +3777,7 @@ buf_page_get_zip(
37773777

37783778
case BUF_BLOCK_ZIP_PAGE:
37793779
case BUF_BLOCK_ZIP_DIRTY:
3780-
buf_block_fix(bpage);
3780+
bpage->fix();
37813781
block_mutex = &buf_pool->zip_mutex;
37823782
mutex_enter(block_mutex);
37833783
goto got_block;
@@ -4296,10 +4296,10 @@ buf_page_get_gen(
42964296
= buf_page_get_mutex(
42974297
&fix_block->page);
42984298
mutex_enter(fix_mutex);
4299-
buf_block_fix(fix_block);
4299+
fix_block->fix();
43004300
mutex_exit(fix_mutex);
43014301
} else {
4302-
buf_block_fix(fix_block);
4302+
fix_block->fix();
43034303
}
43044304

43054305
/* Now safe to release page_hash mutex */
@@ -4406,10 +4406,10 @@ buf_page_get_gen(
44064406
BPageMutex* fix_mutex = buf_page_get_mutex(
44074407
&fix_block->page);
44084408
mutex_enter(fix_mutex);
4409-
buf_block_fix(fix_block);
4409+
fix_block->fix();
44104410
mutex_exit(fix_mutex);
44114411
} else {
4412-
buf_block_fix(fix_block);
4412+
fix_block->fix();
44134413
}
44144414

44154415
/* Now safe to release page_hash mutex */
@@ -4432,7 +4432,7 @@ buf_page_get_gen(
44324432
/* The page is being read to buffer pool,
44334433
but we cannot wait around for the read to
44344434
complete. */
4435-
buf_block_unfix(fix_block);
4435+
fix_block->unfix();
44364436

44374437
return(NULL);
44384438
}
@@ -4448,7 +4448,7 @@ buf_page_get_gen(
44484448
/* This suggests that the page is being flushed.
44494449
Avoid returning reference to this page.
44504450
Instead wait for the flush action to complete. */
4451-
buf_block_unfix(fix_block);
4451+
fix_block->unfix();
44524452
os_thread_sleep(WAIT_FOR_WRITE);
44534453
goto loop;
44544454
}
@@ -4457,7 +4457,7 @@ buf_page_get_gen(
44574457
evict_from_pool:
44584458
ut_ad(!fix_block->page.oldest_modification);
44594459
buf_pool_mutex_enter(buf_pool);
4460-
buf_block_unfix(fix_block);
4460+
fix_block->unfix();
44614461

44624462
if (!buf_LRU_free_page(&fix_block->page, true)) {
44634463
ut_ad(0);
@@ -4475,7 +4475,7 @@ buf_page_get_gen(
44754475
adaptive hash index. There cannot be an
44764476
adaptive hash index for a compressed-only
44774477
page, so do not bother decompressing the page. */
4478-
buf_block_unfix(fix_block);
4478+
fix_block->unfix();
44794479

44804480
return(NULL);
44814481
}
@@ -4489,7 +4489,7 @@ buf_page_get_gen(
44894489
/* This condition often occurs when the buffer
44904490
is not buffer-fixed, but I/O-fixed by
44914491
buf_page_init_for_read(). */
4492-
buf_block_unfix(fix_block);
4492+
fix_block->unfix();
44934493

44944494
/* The block is buffer-fixed or I/O-fixed.
44954495
Try again later. */
@@ -4518,7 +4518,7 @@ buf_page_get_gen(
45184518
/* Buffer-fixing prevents the page_hash from changing. */
45194519
ut_ad(bpage == buf_page_hash_get_low(buf_pool, page_id));
45204520

4521-
buf_block_unfix(fix_block);
4521+
fix_block->unfix();
45224522

45234523
buf_page_mutex_enter(block);
45244524
mutex_enter(&buf_pool->zip_mutex);
@@ -4610,7 +4610,7 @@ buf_page_get_gen(
46104610
buf_page_mutex_exit(fix_block);
46114611

46124612
--buf_pool->n_pend_unzip;
4613-
buf_block_unfix(fix_block);
4613+
fix_block->unfix();
46144614
buf_pool_mutex_exit(buf_pool);
46154615
rw_lock_x_unlock(&fix_block->lock);
46164616

@@ -4673,7 +4673,7 @@ buf_page_get_gen(
46734673

46744674
buf_pool_mutex_enter(buf_pool);
46754675

4676-
buf_block_unfix(fix_block);
4676+
fix_block->unfix();
46774677

46784678
/* Now we are only holding the buf_pool->mutex,
46794679
not block->mutex or hash_lock. Blocks cannot be
@@ -4732,7 +4732,7 @@ buf_page_get_gen(
47324732

47334733
buf_page_mutex_exit(fix_block);
47344734

4735-
buf_block_fix(fix_block);
4735+
fix_block->fix();
47364736

47374737
/* Failed to evict the page; change it directly */
47384738

@@ -5233,7 +5233,7 @@ buf_page_init(
52335233

52345234
ut_a(buf_fix_count > 0);
52355235

5236-
my_atomic_add32((int32*) &block->page.buf_fix_count, buf_fix_count);
5236+
block->page.buf_fix_count += buf_fix_count;
52375237

52385238
buf_pool_watch_remove(buf_pool, hash_page);
52395239
} else {
@@ -5474,7 +5474,7 @@ buf_page_init_for_read(
54745474

54755475
ut_a(buf_fix_count > 0);
54765476

5477-
my_atomic_add32((int32*) &bpage->buf_fix_count, buf_fix_count);
5477+
bpage->buf_fix_count += buf_fix_count;
54785478

54795479
ut_ad(buf_pool_watch_is_sentinel(buf_pool, watch_page));
54805480
buf_pool_watch_remove(buf_pool, watch_page);

storage/innobase/buf/buf0lru.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2459,7 +2459,7 @@ buf_LRU_print_instance(
24592459

24602460
if (bpage->buf_fix_count) {
24612461
fprintf(stderr, "buffix count %u ",
2462-
bpage->buf_fix_count);
2462+
uint32_t(bpage->buf_fix_count));
24632463
}
24642464

24652465
if (buf_page_get_io_fix(bpage)) {

storage/innobase/include/buf0buf.h

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -665,37 +665,6 @@ buf_block_buf_fix_inc_func(
665665
buf_block_t* block) /*!< in/out: block to bufferfix */
666666
MY_ATTRIBUTE((nonnull));
667667

668-
/** Increments the bufferfix count.
669-
@param[in,out] bpage block to bufferfix
670-
@return the count */
671-
UNIV_INLINE
672-
ulint
673-
buf_block_fix(
674-
buf_page_t* bpage);
675-
676-
/** Increments the bufferfix count.
677-
@param[in,out] block block to bufferfix
678-
@return the count */
679-
UNIV_INLINE
680-
ulint
681-
buf_block_fix(
682-
buf_block_t* block);
683-
684-
/** Decrements the bufferfix count.
685-
@param[in,out] bpage block to bufferunfix
686-
@return the remaining buffer-fix count */
687-
UNIV_INLINE
688-
ulint
689-
buf_block_unfix(
690-
buf_page_t* bpage);
691-
/** Decrements the bufferfix count.
692-
@param[in,out] block block to bufferunfix
693-
@return the remaining buffer-fix count */
694-
UNIV_INLINE
695-
ulint
696-
buf_block_unfix(
697-
buf_block_t* block);
698-
699668
# ifdef UNIV_DEBUG
700669
/** Increments the bufferfix count.
701670
@param[in,out] b block to bufferfix
@@ -1490,7 +1459,7 @@ class buf_page_t {
14901459
page_size_t size;
14911460

14921461
/** Count of how manyfold this block is currently bufferfixed. */
1493-
ib_uint32_t buf_fix_count;
1462+
Atomic_counter<uint32_t> buf_fix_count;
14941463

14951464
/** type of pending I/O operation; also protected by
14961465
buf_pool->mutex for writes only */
@@ -1645,6 +1614,14 @@ class buf_page_t {
16451614
protected by buf_pool->zip_mutex
16461615
or buf_block_t::mutex. */
16471616
# endif /* UNIV_DEBUG */
1617+
1618+
void fix() { buf_fix_count++; }
1619+
uint32_t unfix()
1620+
{
1621+
uint32_t count= buf_fix_count--;
1622+
ut_ad(count != 0);
1623+
return count - 1;
1624+
}
16481625
};
16491626

16501627
/** The buffer control block structure */
@@ -1808,6 +1785,9 @@ struct buf_block_t{
18081785
and accessed; we introduce this new
18091786
mutex in InnoDB-5.1 to relieve
18101787
contention on the buffer pool mutex */
1788+
1789+
void fix() { page.fix(); }
1790+
uint32_t unfix() { return page.unfix(); }
18111791
};
18121792

18131793
/** Check if a buf_block_t object is in a valid state

storage/innobase/include/buf0buf.ic

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -945,28 +945,6 @@ buf_block_get_modify_clock(
945945
return(block->modify_clock);
946946
}
947947

948-
/** Increments the bufferfix count.
949-
@param[in,out] bpage block to bufferfix
950-
@return the count */
951-
UNIV_INLINE
952-
ulint
953-
buf_block_fix(
954-
buf_page_t* bpage)
955-
{
956-
return uint32(my_atomic_add32((int32*) &bpage->buf_fix_count, 1) + 1);
957-
}
958-
959-
/** Increments the bufferfix count.
960-
@param[in,out] block block to bufferfix
961-
@return the count */
962-
UNIV_INLINE
963-
ulint
964-
buf_block_fix(
965-
buf_block_t* block)
966-
{
967-
return(buf_block_fix(&block->page));
968-
}
969-
970948
/*******************************************************************//**
971949
Increments the bufferfix count. */
972950
UNIV_INLINE
@@ -990,32 +968,7 @@ buf_block_buf_fix_inc_func(
990968
}
991969
#endif /* UNIV_DEBUG */
992970

993-
buf_block_fix(block);
994-
}
995-
996-
/** Decrements the bufferfix count.
997-
@param[in,out] bpage block to bufferunfix
998-
@return the remaining buffer-fix count */
999-
UNIV_INLINE
1000-
ulint
1001-
buf_block_unfix(
1002-
buf_page_t* bpage)
1003-
{
1004-
uint32 count = uint32(my_atomic_add32((int32*) &bpage->buf_fix_count,
1005-
-1));
1006-
ut_ad(count != 0);
1007-
return count - 1;
1008-
}
1009-
1010-
/** Decrements the bufferfix count.
1011-
@param[in,out] block block to bufferunfix
1012-
@return the remaining buffer-fix count */
1013-
UNIV_INLINE
1014-
ulint
1015-
buf_block_unfix(
1016-
buf_block_t* block)
1017-
{
1018-
return(buf_block_unfix(&block->page));
971+
block->fix();
1019972
}
1020973

1021974
/*******************************************************************//**
@@ -1026,7 +979,7 @@ buf_block_buf_fix_dec(
1026979
/*==================*/
1027980
buf_block_t* block) /*!< in/out: block to bufferunfix */
1028981
{
1029-
buf_block_unfix(block);
982+
block->unfix();
1030983

1031984
#ifdef UNIV_DEBUG
1032985
/* No debug latch is acquired if block belongs to system temporary.
@@ -1283,7 +1236,7 @@ buf_page_release_zip(
12831236
/* Fall through */
12841237
case BUF_BLOCK_ZIP_PAGE:
12851238
case BUF_BLOCK_ZIP_DIRTY:
1286-
buf_block_unfix(reinterpret_cast<buf_block_t*>(bpage));
1239+
reinterpret_cast<buf_block_t*>(bpage)->unfix();
12871240
return;
12881241

12891242
case BUF_BLOCK_POOL_WATCH:

storage/innobase/include/mtr0mtr.ic

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mtr_t::release_block_at_savepoint(
170170

171171
ut_a(slot->object == block);
172172

173-
buf_block_unfix(reinterpret_cast<buf_block_t*>(block));
173+
reinterpret_cast<buf_block_t*>(block)->unfix();
174174

175175
buf_page_release_latch(block, slot->type);
176176

storage/innobase/mtr/mtr0mtr.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ memo_slot_release(mtr_memo_slot_t* slot)
191191

192192
block = reinterpret_cast<buf_block_t*>(slot->object);
193193

194-
buf_block_unfix(block);
194+
block->unfix();
195195
buf_page_release_latch(block, slot->type);
196196
break;
197197
}
@@ -228,7 +228,7 @@ memo_block_unfix(mtr_memo_slot_t* slot)
228228
case MTR_MEMO_PAGE_S_FIX:
229229
case MTR_MEMO_PAGE_X_FIX:
230230
case MTR_MEMO_PAGE_SX_FIX: {
231-
buf_block_unfix(reinterpret_cast<buf_block_t*>(slot->object));
231+
reinterpret_cast<buf_block_t*>(slot->object)->unfix();
232232
break;
233233
}
234234

0 commit comments

Comments
 (0)