~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/buf/buf0buf.c

  • Committer: Andrew Hutchings
  • Date: 2010-11-09 13:38:01 UTC
  • mto: (1919.1.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 1920.
  • Revision ID: andrew@linuxjedi.co.uk-20101109133801-byjzsao76346395x
Add FLUSH GLOBAL STATUS; command
Clears the global status variables for the server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
 
Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
4
 
Copyright (C) 2008, Google Inc.
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
4
Copyright (c) 2008, Google Inc.
5
5
 
6
6
Portions of this file contain modifications contributed and copyrighted by
7
7
Google, Inc. Those modifications are gratefully acknowledged and are described
52
52
#include "log0recv.h"
53
53
#include "page0zip.h"
54
54
 
55
 
#include <drizzled/errmsg_print.h>
56
 
 
57
55
/*
58
56
                IMPLEMENTATION OF THE BUFFER POOL
59
57
                =================================
86
84
The buffer buf_pool contains a single mutex which protects all the
87
85
control data structures of the buf_pool. The content of a buffer frame is
88
86
protected by a separate read-write lock in its control block, though.
89
 
These locks can be locked and unlocked without owning the buf_pool->mutex.
 
87
These locks can be locked and unlocked without owning the buf_pool mutex.
90
88
The OS events in the buf_pool struct can be waited for without owning the
91
 
buf_pool->mutex.
 
89
buf_pool mutex.
92
90
 
93
 
The buf_pool->mutex is a hot-spot in main memory, causing a lot of
 
91
The buf_pool mutex is a hot-spot in main memory, causing a lot of
94
92
memory bus traffic on multiprocessor systems when processors
95
93
alternately access the mutex. On our Pentium, the mutex is accessed
96
94
maybe every 10 microseconds. We gave up the solution to have mutexes
97
95
for each control block, for instance, because it seemed to be
98
96
complicated.
99
97
 
100
 
A solution to reduce mutex contention of the buf_pool->mutex is to
 
98
A solution to reduce mutex contention of the buf_pool mutex is to
101
99
create a separate mutex for the page hash table. On Pentium,
102
100
accessing the hash table takes 2 microseconds, about half
103
 
of the total buf_pool->mutex hold time.
 
101
of the total buf_pool mutex hold time.
104
102
 
105
103
                Control blocks
106
104
                --------------
155
153
which we can use when we want to artificially age a page in the
156
154
buf_pool. This is used if we know that some page is not needed
157
155
again for some time: we insert the block right after the pointer,
158
 
causing it to be replaced sooner than would normally be the case.
 
156
causing it to be replaced sooner than would noramlly be the case.
159
157
Currently this aging mechanism is used for read-ahead mechanism
160
158
of pages, and it can also be used when there is a scan of a full
161
159
table which cannot fit in the memory. Putting the pages near the
162
 
end of the LRU list, we make sure that most of the buf_pool stays
163
 
in the main memory, undisturbed.
 
160
of the LRU list, we make sure that most of the buf_pool stays in the
 
161
main memory, undisturbed.
164
162
 
165
163
The unzip_LRU list contains a subset of the common LRU list.  The
166
164
blocks on the unzip_LRU list hold a compressed file page and the
174
172
holding file pages that have been modified in the memory
175
173
but not written to disk yet. The block with the oldest modification
176
174
which has not yet been written to disk is at the end of the chain.
177
 
The access to this list is protected by buf_pool->flush_list_mutex.
178
175
 
179
176
The chain of unmodified compressed blocks (buf_pool->zip_clean)
180
177
contains the control blocks (buf_page_t) of those compressed pages
245
242
#ifndef UNIV_HOTBACKUP
246
243
/** Value in microseconds */
247
244
static const int WAIT_FOR_READ  = 5000;
248
 
/** Number of attemtps made to read in a page in the buffer pool */
249
 
static const ulint BUF_PAGE_READ_MAX_RETRIES = 100;
250
 
 
251
 
/** The buffer pools of the database */
252
 
UNIV_INTERN buf_pool_t* buf_pool_ptr;
 
245
 
 
246
/** The buffer buf_pool of the database */
 
247
UNIV_INTERN buf_pool_t* buf_pool = NULL;
 
248
 
 
249
/** mutex protecting the buffer pool struct and control blocks, except the
 
250
read-write lock in them */
 
251
UNIV_INTERN mutex_t             buf_pool_mutex;
 
252
/** mutex protecting the control blocks of compressed-only pages
 
253
(of type buf_page_t, not buf_block_t) */
 
254
UNIV_INTERN mutex_t             buf_pool_zip_mutex;
253
255
 
254
256
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
255
257
static ulint    buf_dbg_counter = 0; /*!< This is used to insert validation
256
 
                                        operations in execution in the
 
258
                                        operations in excution in the
257
259
                                        debug version */
 
260
/** Flag to forbid the release of the buffer pool mutex.
 
261
Protected by buf_pool_mutex. */
 
262
UNIV_INTERN ulint               buf_pool_mutex_exit_forbidden = 0;
258
263
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
259
264
#ifdef UNIV_DEBUG
260
265
/** If this is set TRUE, the program prints info whenever
262
267
UNIV_INTERN ibool               buf_debug_prints = FALSE;
263
268
#endif /* UNIV_DEBUG */
264
269
 
265
 
#ifdef UNIV_PFS_RWLOCK
266
 
/* Keys to register buffer block related rwlocks and mutexes with
267
 
performance schema */
268
 
UNIV_INTERN mysql_pfs_key_t     buf_block_lock_key;
269
 
# ifdef UNIV_SYNC_DEBUG
270
 
UNIV_INTERN mysql_pfs_key_t     buf_block_debug_latch_key;
271
 
# endif /* UNIV_SYNC_DEBUG */
272
 
#endif /* UNIV_PFS_RWLOCK */
273
 
 
274
 
#ifdef UNIV_PFS_MUTEX
275
 
UNIV_INTERN mysql_pfs_key_t     buffer_block_mutex_key;
276
 
UNIV_INTERN mysql_pfs_key_t     buf_pool_mutex_key;
277
 
UNIV_INTERN mysql_pfs_key_t     buf_pool_zip_mutex_key;
278
 
UNIV_INTERN mysql_pfs_key_t     flush_list_mutex_key;
279
 
#endif /* UNIV_PFS_MUTEX */
280
 
 
281
 
#if defined UNIV_PFS_MUTEX || defined UNIV_PFS_RWLOCK
282
 
# ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK
283
 
 
284
 
/* Buffer block mutexes and rwlocks can be registered
285
 
in one group rather than individually. If PFS_GROUP_BUFFER_SYNC
286
 
is defined, register buffer block mutex and rwlock
287
 
in one group after their initialization. */
288
 
#  define PFS_GROUP_BUFFER_SYNC
289
 
 
290
 
/* This define caps the number of mutexes/rwlocks can
291
 
be registered with performance schema. Developers can
292
 
modify this define if necessary. Please note, this would
293
 
be effective only if PFS_GROUP_BUFFER_SYNC is defined. */
294
 
#  define PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER    ULINT_MAX
295
 
 
296
 
# endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */
297
 
#endif /* UNIV_PFS_MUTEX || UNIV_PFS_RWLOCK */
298
 
 
299
270
/** A chunk of buffers.  The buffer pool is allocated in chunks. */
300
271
struct buf_chunk_struct{
301
272
        ulint           mem_size;       /*!< allocated size of the chunk */
307
278
#endif /* !UNIV_HOTBACKUP */
308
279
 
309
280
/********************************************************************//**
310
 
Gets the smallest oldest_modification lsn for any page in the pool. Returns
311
 
zero if all modified pages have been flushed to disk.
312
 
@return oldest modification in pool, zero if none */
313
 
UNIV_INTERN
314
 
ib_uint64_t
315
 
buf_pool_get_oldest_modification(void)
316
 
/*==================================*/
317
 
{
318
 
        ulint           i;
319
 
        buf_page_t*     bpage;
320
 
        ib_uint64_t     lsn = 0;
321
 
        ib_uint64_t     oldest_lsn = 0;
322
 
 
323
 
        /* When we traverse all the flush lists we don't want another
324
 
        thread to add a dirty page to any flush list. */
325
 
        log_flush_order_mutex_enter();
326
 
 
327
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
328
 
                buf_pool_t*     buf_pool;
329
 
 
330
 
                buf_pool = buf_pool_from_array(i);
331
 
 
332
 
                buf_flush_list_mutex_enter(buf_pool);
333
 
 
334
 
                bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
335
 
 
336
 
                if (bpage != NULL) {
337
 
                        ut_ad(bpage->in_flush_list);
338
 
                        lsn = bpage->oldest_modification;
339
 
                }
340
 
 
341
 
                buf_flush_list_mutex_exit(buf_pool);
342
 
 
343
 
                if (!oldest_lsn || oldest_lsn > lsn) {
344
 
                        oldest_lsn = lsn;
345
 
                }
346
 
        }
347
 
 
348
 
        log_flush_order_mutex_exit();
349
 
 
350
 
        /* The returned answer may be out of date: the flush_list can
351
 
        change after the mutex has been released. */
352
 
 
353
 
        return(oldest_lsn);
354
 
}
355
 
 
356
 
/********************************************************************//**
357
 
Get total buffer pool statistics. */
358
 
UNIV_INTERN
359
 
void
360
 
buf_get_total_list_len(
361
 
/*===================*/
362
 
        ulint*          LRU_len,        /*!< out: length of all LRU lists */
363
 
        ulint*          free_len,       /*!< out: length of all free lists */
364
 
        ulint*          flush_list_len) /*!< out: length of all flush lists */
365
 
{
366
 
        ulint           i;
367
 
 
368
 
        *LRU_len = 0;
369
 
        *free_len = 0;
370
 
        *flush_list_len = 0;
371
 
 
372
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
373
 
                buf_pool_t*     buf_pool;
374
 
 
375
 
                buf_pool = buf_pool_from_array(i);
376
 
                *LRU_len += UT_LIST_GET_LEN(buf_pool->LRU);
377
 
                *free_len += UT_LIST_GET_LEN(buf_pool->free);
378
 
                *flush_list_len += UT_LIST_GET_LEN(buf_pool->flush_list);
379
 
        }
380
 
}
381
 
 
382
 
/********************************************************************//**
383
 
Get total buffer pool statistics. */
384
 
UNIV_INTERN
385
 
void
386
 
buf_get_total_stat(
387
 
/*===============*/
388
 
        buf_pool_stat_t*        tot_stat)       /*!< out: buffer pool stats */
389
 
{
390
 
        ulint                   i;
391
 
 
392
 
        memset(tot_stat, 0, sizeof(*tot_stat));
393
 
 
394
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
395
 
                buf_pool_stat_t*buf_stat;
396
 
                buf_pool_t*     buf_pool;
397
 
 
398
 
                buf_pool = buf_pool_from_array(i);
399
 
 
400
 
                buf_stat = &buf_pool->stat;
401
 
                tot_stat->n_page_gets += buf_stat->n_page_gets;
402
 
                tot_stat->n_pages_read += buf_stat->n_pages_read;
403
 
                tot_stat->n_pages_written += buf_stat->n_pages_written;
404
 
                tot_stat->n_pages_created += buf_stat->n_pages_created;
405
 
                tot_stat->n_ra_pages_read += buf_stat->n_ra_pages_read;
406
 
                tot_stat->n_ra_pages_evicted += buf_stat->n_ra_pages_evicted;
407
 
                tot_stat->n_pages_made_young += buf_stat->n_pages_made_young;
408
 
 
409
 
                tot_stat->n_pages_not_made_young +=
410
 
                        buf_stat->n_pages_not_made_young;
411
 
        }
412
 
}
413
 
 
414
 
/********************************************************************//**
415
 
Allocates a buffer block.
416
 
@return own: the allocated block, in state BUF_BLOCK_MEMORY */
417
 
UNIV_INTERN
418
 
buf_block_t*
419
 
buf_block_alloc(
420
 
/*============*/
421
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
422
 
        ulint           zip_size)       /*!< in: compressed page size in bytes,
423
 
                                        or 0 if uncompressed tablespace */
424
 
{
425
 
        buf_block_t*    block;
426
 
        ulint           index;
427
 
        static ulint    buf_pool_index;
428
 
 
429
 
        if (buf_pool == NULL) {
430
 
                /* We are allocating memory from any buffer pool, ensure
431
 
                we spread the grace on all buffer pool instances. */
432
 
                index = buf_pool_index++ % srv_buf_pool_instances;
433
 
                buf_pool = buf_pool_from_array(index);
434
 
        }
435
 
 
436
 
        block = buf_LRU_get_free_block(buf_pool, zip_size);
437
 
 
438
 
        buf_block_set_state(block, BUF_BLOCK_MEMORY);
439
 
 
440
 
        return(block);
441
 
}
442
 
 
443
 
/********************************************************************//**
444
281
Calculates a page checksum which is stored to the page when it is written
445
282
to a file. Note that we must be careful to calculate the same value on
446
283
32-bit and 64-bit architectures.
524
361
                ib_uint64_t     current_lsn;
525
362
 
526
363
                if (log_peek_lsn(&current_lsn)
527
 
                    && UNIV_UNLIKELY
528
 
                    (current_lsn
529
 
                     < mach_read_from_8(read_buf + FIL_PAGE_LSN))) {
 
364
                    && current_lsn < mach_read_ull(read_buf + FIL_PAGE_LSN)) {
530
365
                        ut_print_timestamp(stderr);
531
366
 
532
 
                        drizzled::errmsg_printf(drizzled::error::INFO,
533
 
                                                "InnoDB: Error: page %lu log sequence number %"PRIu64". "
534
 
                                                "InnoDB: is in the future! Current system log sequence number %"PRIu64". "
535
 
                                                "Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. See "
536
 
                                                " " REFMAN "forcing-recovery.html for more information. ",
537
 
                                                (ulong) mach_read_from_4(read_buf
538
 
                                                                         + FIL_PAGE_OFFSET),
539
 
                                                mach_read_from_8(read_buf + FIL_PAGE_LSN),
540
 
                                                current_lsn);
 
367
                        fprintf(stderr,
 
368
                                "  InnoDB: Error: page %lu log sequence number"
 
369
                                " %"PRIu64"\n"
 
370
                                "InnoDB: is in the future! Current system "
 
371
                                "log sequence number %"PRIu64".\n"
 
372
                                "InnoDB: Your database may be corrupt or "
 
373
                                "you may have copied the InnoDB\n"
 
374
                                "InnoDB: tablespace but not the InnoDB "
 
375
                                "log files. See\n"
 
376
                                "InnoDB: " REFMAN "forcing-recovery.html\n"
 
377
                                "InnoDB: for more information.\n",
 
378
                                (ulong) mach_read_from_4(read_buf
 
379
                                                         + FIL_PAGE_OFFSET),
 
380
                                mach_read_ull(read_buf + FIL_PAGE_LSN),
 
381
                                current_lsn);
541
382
                }
542
383
        }
543
384
#endif
733
574
#endif /* !UNIV_HOTBACKUP */
734
575
 
735
576
        switch (fil_page_get_type(read_buf)) {
736
 
                index_id_t      index_id;
737
577
        case FIL_PAGE_INDEX:
738
 
                index_id = btr_page_get_index_id(read_buf);
739
578
                fprintf(stderr,
740
579
                        "InnoDB: Page may be an index page where"
741
 
                        " index id is %llu\n",
742
 
                        (ullint) index_id);
 
580
                        " index id is %lu %lu\n",
 
581
                        (ulong) ut_dulint_get_high(
 
582
                                btr_page_get_index_id(read_buf)),
 
583
                        (ulong) ut_dulint_get_low(
 
584
                                btr_page_get_index_id(read_buf)));
743
585
#ifndef UNIV_HOTBACKUP
744
 
                index = dict_index_find_on_id_low(index_id);
 
586
                index = dict_index_find_on_id_low(
 
587
                        btr_page_get_index_id(read_buf));
745
588
                if (index) {
746
589
                        fputs("InnoDB: (", stderr);
747
590
                        dict_index_name_print(stderr, NULL, index);
793
636
}
794
637
 
795
638
#ifndef UNIV_HOTBACKUP
796
 
 
797
 
# ifdef PFS_GROUP_BUFFER_SYNC
798
 
/********************************************************************//**
799
 
This function registers mutexes and rwlocks in buffer blocks with
800
 
performance schema. If PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER is
801
 
defined to be a value less than chunk->size, then only mutexes
802
 
and rwlocks in the first PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER
803
 
blocks are registered. */
804
 
static
805
 
void
806
 
pfs_register_buffer_block(
807
 
/*======================*/
808
 
        buf_chunk_t*    chunk)          /*!< in/out: chunk of buffers */
809
 
{
810
 
        ulint           i;
811
 
        ulint           num_to_register;
812
 
        buf_block_t*    block;
813
 
 
814
 
        block = chunk->blocks;
815
 
 
816
 
        num_to_register = ut_min(chunk->size,
817
 
                                 PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER);
818
 
 
819
 
        for (i = 0; i < num_to_register; i++) {
820
 
                mutex_t*        mutex;
821
 
                rw_lock_t*      rwlock;
822
 
 
823
 
#  ifdef UNIV_PFS_MUTEX
824
 
                mutex = &block->mutex;
825
 
                ut_a(!mutex->pfs_psi);
826
 
                mutex->pfs_psi = (PSI_server)
827
 
                        ? PSI_server->init_mutex(buffer_block_mutex_key, mutex)
828
 
                        : NULL;
829
 
#  endif /* UNIV_PFS_MUTEX */
830
 
 
831
 
#  ifdef UNIV_PFS_RWLOCK
832
 
                rwlock = &block->lock;
833
 
                ut_a(!rwlock->pfs_psi);
834
 
                rwlock->pfs_psi = (PSI_server)
835
 
                        ? PSI_server->init_rwlock(buf_block_lock_key, rwlock)
836
 
                        : NULL;
837
 
#  endif /* UNIV_PFS_RWLOCK */
838
 
                block++;
839
 
        }
840
 
}
841
 
# endif /* PFS_GROUP_BUFFER_SYNC */
842
 
 
843
639
/********************************************************************//**
844
640
Initializes a buffer control block when the buf_pool is created. */
845
641
static
846
642
void
847
643
buf_block_init(
848
644
/*===========*/
849
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
850
 
        buf_block_t*    block,          /*!< in: pointer to control block */
851
 
        byte*           frame)          /*!< in: pointer to buffer frame */
 
645
        buf_block_t*    block,  /*!< in: pointer to control block */
 
646
        byte*           frame)  /*!< in: pointer to buffer frame */
852
647
{
853
648
        UNIV_MEM_DESC(frame, UNIV_PAGE_SIZE, block);
854
649
 
855
650
        block->frame = frame;
856
651
 
857
 
        block->page.buf_pool_index = buf_pool_index(buf_pool);
858
652
        block->page.state = BUF_BLOCK_NOT_USED;
859
653
        block->page.buf_fix_count = 0;
860
654
        block->page.io_fix = BUF_IO_NONE;
868
662
        block->check_index_page_at_flush = FALSE;
869
663
        block->index = NULL;
870
664
 
871
 
        block->is_hashed = FALSE;
872
 
 
873
665
#ifdef UNIV_DEBUG
874
666
        block->page.in_page_hash = FALSE;
875
667
        block->page.in_zip_hash = FALSE;
883
675
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
884
676
        page_zip_des_init(&block->page.zip);
885
677
 
886
 
#if defined PFS_SKIP_BUFFER_MUTEX_RWLOCK || defined PFS_GROUP_BUFFER_SYNC
887
 
        /* If PFS_SKIP_BUFFER_MUTEX_RWLOCK is defined, skip registration
888
 
        of buffer block mutex/rwlock with performance schema. If
889
 
        PFS_GROUP_BUFFER_SYNC is defined, skip the registration
890
 
        since buffer block mutex/rwlock will be registered later in
891
 
        pfs_register_buffer_block() */
892
 
 
893
 
        mutex_create(PFS_NOT_INSTRUMENTED, &block->mutex, SYNC_BUF_BLOCK);
894
 
        rw_lock_create(PFS_NOT_INSTRUMENTED, &block->lock, SYNC_LEVEL_VARYING);
895
 
#else /* PFS_SKIP_BUFFER_MUTEX_RWLOCK || PFS_GROUP_BUFFER_SYNC */
896
 
        mutex_create(buffer_block_mutex_key, &block->mutex, SYNC_BUF_BLOCK);
897
 
        rw_lock_create(buf_block_lock_key, &block->lock, SYNC_LEVEL_VARYING);
898
 
#endif /* PFS_SKIP_BUFFER_MUTEX_RWLOCK || PFS_GROUP_BUFFER_SYNC */
899
 
 
 
678
        mutex_create(&block->mutex, SYNC_BUF_BLOCK);
 
679
 
 
680
        rw_lock_create(&block->lock, SYNC_LEVEL_VARYING);
900
681
        ut_ad(rw_lock_validate(&(block->lock)));
901
682
 
902
683
#ifdef UNIV_SYNC_DEBUG
903
 
        rw_lock_create(buf_block_debug_latch_key,
904
 
                       &block->debug_latch, SYNC_NO_ORDER_CHECK);
 
684
        rw_lock_create(&block->debug_latch, SYNC_NO_ORDER_CHECK);
905
685
#endif /* UNIV_SYNC_DEBUG */
906
686
}
907
687
 
912
692
buf_chunk_t*
913
693
buf_chunk_init(
914
694
/*===========*/
915
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
916
695
        buf_chunk_t*    chunk,          /*!< out: chunk of buffers */
917
696
        ulint           mem_size)       /*!< in: requested size in bytes */
918
697
{
937
716
 
938
717
        /* Allocate the block descriptors from
939
718
        the start of the memory block. */
940
 
        chunk->blocks = static_cast<buf_block_struct *>(chunk->mem);
 
719
        chunk->blocks = chunk->mem;
941
720
 
942
721
        /* Align a pointer to the first frame.  Note that when
943
722
        os_large_page_size is smaller than UNIV_PAGE_SIZE,
944
723
        we may allocate one fewer block than requested.  When
945
724
        it is bigger, we may allocate more blocks than requested. */
946
725
 
947
 
        frame = static_cast<unsigned char *>(ut_align(chunk->mem, UNIV_PAGE_SIZE));
 
726
        frame = ut_align(chunk->mem, UNIV_PAGE_SIZE);
948
727
        chunk->size = chunk->mem_size / UNIV_PAGE_SIZE
949
728
                - (frame != chunk->mem);
950
729
 
968
747
 
969
748
        for (i = chunk->size; i--; ) {
970
749
 
971
 
                buf_block_init(buf_pool, block, frame);
 
750
                buf_block_init(block, frame);
972
751
 
973
752
#ifdef HAVE_VALGRIND
974
753
                /* Wipe contents of frame to eliminate a Purify warning */
976
755
#endif
977
756
                /* Add the block to the free list */
978
757
                UT_LIST_ADD_LAST(list, buf_pool->free, (&block->page));
979
 
 
980
758
                ut_d(block->page.in_free_list = TRUE);
981
 
                ut_ad(buf_pool_from_block(block) == buf_pool);
982
759
 
983
760
                block++;
984
761
                frame += UNIV_PAGE_SIZE;
985
762
        }
986
763
 
987
 
#ifdef PFS_GROUP_BUFFER_SYNC
988
 
        pfs_register_buffer_block(chunk);
989
 
#endif
990
764
        return(chunk);
991
765
}
992
766
 
1005
779
        buf_block_t*    block;
1006
780
        ulint           i;
1007
781
 
 
782
        ut_ad(buf_pool);
 
783
        ut_ad(buf_pool_mutex_own());
 
784
 
1008
785
        block = chunk->blocks;
1009
786
 
1010
787
        for (i = chunk->size; i--; block++) {
1025
802
buf_block_t*
1026
803
buf_pool_contains_zip(
1027
804
/*==================*/
1028
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
1029
 
        const void*     data)           /*!< in: pointer to compressed page */
 
805
        const void*     data)   /*!< in: pointer to compressed page */
1030
806
{
1031
807
        ulint           n;
1032
808
        buf_chunk_t*    chunk = buf_pool->chunks;
1033
809
 
1034
 
        ut_ad(buf_pool);
1035
 
        ut_ad(buf_pool_mutex_own(buf_pool));
1036
810
        for (n = buf_pool->n_chunks; n--; chunk++) {
1037
 
 
1038
811
                buf_block_t* block = buf_chunk_contains_zip(chunk, data);
1039
812
 
1040
813
                if (block) {
1058
831
        buf_block_t*    block;
1059
832
        ulint           i;
1060
833
 
 
834
        ut_ad(buf_pool);
 
835
        ut_ad(buf_pool_mutex_own());
 
836
 
1061
837
        block = chunk->blocks;
1062
838
 
1063
839
        for (i = chunk->size; i--; block++) {
1107
883
        const buf_block_t*      block;
1108
884
        ulint                   i;
1109
885
 
 
886
        ut_ad(buf_pool);
 
887
        ut_ad(buf_pool_mutex_own());
 
888
 
1110
889
        block = chunk->blocks;
1111
890
 
1112
891
        for (i = chunk->size; i--; block++) {
1126
905
void
1127
906
buf_chunk_free(
1128
907
/*===========*/
1129
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
1130
908
        buf_chunk_t*    chunk)          /*!< out: chunk of buffers */
1131
909
{
1132
910
        buf_block_t*            block;
1133
911
        const buf_block_t*      block_end;
1134
912
 
1135
 
        ut_ad(buf_pool_mutex_own(buf_pool));
 
913
        ut_ad(buf_pool_mutex_own());
1136
914
 
1137
915
        block_end = chunk->blocks + chunk->size;
1138
916
 
1160
938
}
1161
939
 
1162
940
/********************************************************************//**
1163
 
Set buffer pool size variables after resizing it */
1164
 
static
1165
 
void
1166
 
buf_pool_set_sizes(void)
1167
 
/*====================*/
1168
 
{
1169
 
        ulint   i;
1170
 
        ulint   curr_size = 0;
1171
 
 
1172
 
        buf_pool_mutex_enter_all();
1173
 
 
1174
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
1175
 
                buf_pool_t*     buf_pool;
1176
 
 
1177
 
                buf_pool = buf_pool_from_array(i);
1178
 
                curr_size += buf_pool->curr_pool_size;
1179
 
        }
1180
 
 
1181
 
        srv_buf_pool_curr_size = curr_size;
1182
 
        srv_buf_pool_old_size = srv_buf_pool_size;
1183
 
 
1184
 
        buf_pool_mutex_exit_all();
1185
 
}
1186
 
 
1187
 
/********************************************************************//**
1188
 
Initialize a buffer pool instance.
1189
 
@return DB_SUCCESS if all goes well. */
1190
 
static
1191
 
ulint
1192
 
buf_pool_init_instance(
1193
 
/*===================*/
1194
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
1195
 
        ulint           buf_pool_size,  /*!< in: size in bytes */
1196
 
        ulint           instance_no)    /*!< in: id of the instance */
1197
 
{
 
941
Creates the buffer pool.
 
942
@return own: buf_pool object, NULL if not enough memory or error */
 
943
UNIV_INTERN
 
944
buf_pool_t*
 
945
buf_pool_init(void)
 
946
/*===============*/
 
947
{
 
948
        buf_chunk_t*    chunk;
1198
949
        ulint           i;
1199
 
        buf_chunk_t*    chunk;
 
950
 
 
951
        buf_pool = mem_zalloc(sizeof(buf_pool_t));
1200
952
 
1201
953
        /* 1. Initialize general fields
1202
954
        ------------------------------- */
1203
 
        mutex_create(buf_pool_mutex_key,
1204
 
                     &buf_pool->mutex, SYNC_BUF_POOL);
1205
 
        mutex_create(buf_pool_zip_mutex_key,
1206
 
                     &buf_pool->zip_mutex, SYNC_BUF_BLOCK);
1207
 
 
1208
 
        buf_pool_mutex_enter(buf_pool);
1209
 
 
1210
 
        if (buf_pool_size > 0) {
1211
 
                buf_pool->n_chunks = 1;
1212
 
                void *chunk_ptr= mem_zalloc((sizeof *chunk));
1213
 
                buf_pool->chunks = chunk = static_cast<buf_chunk_t *>(chunk_ptr);
1214
 
 
1215
 
                UT_LIST_INIT(buf_pool->free);
1216
 
 
1217
 
                if (!buf_chunk_init(buf_pool, chunk, buf_pool_size)) {
1218
 
                        mem_free(chunk);
1219
 
                        mem_free(buf_pool);
1220
 
 
1221
 
                        buf_pool_mutex_exit(buf_pool);
1222
 
 
1223
 
                        return(DB_ERROR);
1224
 
                }
1225
 
 
1226
 
                buf_pool->instance_no = instance_no;
1227
 
                buf_pool->old_pool_size = buf_pool_size;
1228
 
                buf_pool->curr_size = chunk->size;
1229
 
                buf_pool->curr_pool_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
1230
 
 
1231
 
                buf_pool->page_hash = hash_create(2 * buf_pool->curr_size);
1232
 
                buf_pool->zip_hash = hash_create(2 * buf_pool->curr_size);
1233
 
                
1234
 
                buf_pool->last_printout_time = ut_time();
 
955
        mutex_create(&buf_pool_mutex, SYNC_BUF_POOL);
 
956
        mutex_create(&buf_pool_zip_mutex, SYNC_BUF_BLOCK);
 
957
 
 
958
        buf_pool_mutex_enter();
 
959
 
 
960
        buf_pool->n_chunks = 1;
 
961
        buf_pool->chunks = chunk = mem_alloc(sizeof *chunk);
 
962
 
 
963
        UT_LIST_INIT(buf_pool->free);
 
964
 
 
965
        if (!buf_chunk_init(chunk, srv_buf_pool_size)) {
 
966
                mem_free(chunk);
 
967
                mem_free(buf_pool);
 
968
                buf_pool = NULL;
 
969
                return(NULL);
1235
970
        }
 
971
 
 
972
        srv_buf_pool_old_size = srv_buf_pool_size;
 
973
        buf_pool->curr_size = chunk->size;
 
974
        srv_buf_pool_curr_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
 
975
 
 
976
        buf_pool->page_hash = hash_create(2 * buf_pool->curr_size);
 
977
        buf_pool->zip_hash = hash_create(2 * buf_pool->curr_size);
 
978
 
 
979
        buf_pool->last_printout_time = time(NULL);
 
980
 
1236
981
        /* 2. Initialize flushing fields
1237
982
        -------------------------------- */
1238
983
 
1239
 
        mutex_create(flush_list_mutex_key, &buf_pool->flush_list_mutex,
1240
 
                     SYNC_BUF_FLUSH_LIST);
1241
 
 
1242
984
        for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
1243
985
                buf_pool->no_flush[i] = os_event_create(NULL);
1244
986
        }
1245
987
 
1246
988
        /* 3. Initialize LRU fields
1247
989
        --------------------------- */
1248
 
 
1249
 
        /* All fields are initialized by mem_zalloc(). */
1250
 
 
1251
 
        buf_pool_mutex_exit(buf_pool);
1252
 
 
1253
 
        return(DB_SUCCESS);
 
990
        /* All fields are initialized by mem_zalloc(). */
 
991
 
 
992
        buf_pool_mutex_exit();
 
993
 
 
994
        btr_search_sys_create(buf_pool->curr_size
 
995
                              * UNIV_PAGE_SIZE / sizeof(void*) / 64);
 
996
 
 
997
        /* 4. Initialize the buddy allocator fields */
 
998
        /* All fields are initialized by mem_zalloc(). */
 
999
 
 
1000
        return(buf_pool);
1254
1001
}
1255
1002
 
1256
1003
/********************************************************************//**
1257
 
free one buffer pool instance */
1258
 
static
 
1004
Frees the buffer pool at shutdown.  This must not be invoked before
 
1005
freeing all mutexes. */
 
1006
UNIV_INTERN
1259
1007
void
1260
 
buf_pool_free_instance(
1261
 
/*===================*/
1262
 
        buf_pool_t*     buf_pool)       /* in,own: buffer pool instance
1263
 
                                        to free */
 
1008
buf_pool_free(void)
 
1009
/*===============*/
1264
1010
{
1265
1011
        buf_chunk_t*    chunk;
1266
1012
        buf_chunk_t*    chunks;
1277
1023
        mem_free(buf_pool->chunks);
1278
1024
        hash_table_free(buf_pool->page_hash);
1279
1025
        hash_table_free(buf_pool->zip_hash);
1280
 
}
1281
 
 
1282
 
/********************************************************************//**
1283
 
Creates the buffer pool.
1284
 
@return DB_SUCCESS if success, DB_ERROR if not enough memory or error */
1285
 
UNIV_INTERN
1286
 
ulint
1287
 
buf_pool_init(
1288
 
/*==========*/
1289
 
        ulint   total_size,     /*!< in: size of the total pool in bytes */
1290
 
        ulint   n_instances)    /*!< in: number of instances */
1291
 
{
1292
 
        ulint           i;
1293
 
        const ulint     size    = total_size / n_instances;
1294
 
 
1295
 
        ut_ad(n_instances > 0);
1296
 
        ut_ad(n_instances <= MAX_BUFFER_POOLS);
1297
 
        ut_ad(n_instances == srv_buf_pool_instances);
1298
 
 
1299
 
        /* We create an extra buffer pool instance, this instance is used
1300
 
        for flushing the flush lists, to keep track of n_flush for all
1301
 
        the buffer pools and also used as a waiting object during flushing. */
1302
 
        void *buf_pool_void_ptr= mem_zalloc(n_instances * sizeof *buf_pool_ptr);
1303
 
        buf_pool_ptr = static_cast<buf_pool_struct *>(buf_pool_void_ptr);
1304
 
 
1305
 
        for (i = 0; i < n_instances; i++) {
1306
 
                buf_pool_t*     ptr     = &buf_pool_ptr[i];
1307
 
 
1308
 
                if (buf_pool_init_instance(ptr, size, i) != DB_SUCCESS) {
1309
 
 
1310
 
                        /* Free all the instances created so far. */
1311
 
                        buf_pool_free(i);
1312
 
 
1313
 
                        return(DB_ERROR);
1314
 
                }
1315
 
        }
1316
 
 
1317
 
        buf_pool_set_sizes();
1318
 
        buf_LRU_old_ratio_update(100 * 3/ 8, FALSE);
1319
 
 
1320
 
        btr_search_sys_create(buf_pool_get_curr_size() / sizeof(void*) / 64);
1321
 
 
1322
 
        return(DB_SUCCESS);
1323
 
}
1324
 
 
1325
 
/********************************************************************//**
1326
 
Frees the buffer pool at shutdown.  This must not be invoked before
1327
 
freeing all mutexes. */
1328
 
UNIV_INTERN
1329
 
void
1330
 
buf_pool_free(
1331
 
/*==========*/
1332
 
        ulint   n_instances)    /*!< in: numbere of instances to free */
1333
 
{
1334
 
        ulint   i;
1335
 
 
1336
 
        for (i = 0; i < n_instances; i++) {
1337
 
                buf_pool_free_instance(buf_pool_from_array(i));
1338
 
        }
1339
 
 
1340
 
        mem_free(buf_pool_ptr);
1341
 
        buf_pool_ptr = NULL;
1342
 
}
1343
 
 
1344
 
/********************************************************************//**
1345
 
Drops adaptive hash index for a buffer pool instance. */
1346
 
static
1347
 
void
1348
 
buf_pool_drop_hash_index_instance(
1349
 
/*==============================*/
1350
 
        buf_pool_t*     buf_pool,               /*!< in: buffer pool instance */
1351
 
        ibool*          released_search_latch)  /*!< out: flag for signalling
1352
 
                                                whether the search latch was
1353
 
                                                released */
1354
 
{
1355
 
        buf_chunk_t*    chunks  = buf_pool->chunks;
1356
 
        buf_chunk_t*    chunk   = chunks + buf_pool->n_chunks;
1357
 
 
1358
 
        while (--chunk >= chunks) {
1359
 
                ulint           i;
1360
 
                buf_block_t*    block   = chunk->blocks;
1361
 
 
1362
 
                for (i = chunk->size; i--; block++) {
1363
 
                        /* block->is_hashed cannot be modified
1364
 
                        when we have an x-latch on btr_search_latch;
1365
 
                        see the comment in buf0buf.h */
1366
 
                        
1367
 
                        if (!block->is_hashed) {
1368
 
                                continue;
1369
 
                        }
1370
 
                        
1371
 
                        /* To follow the latching order, we
1372
 
                        have to release btr_search_latch
1373
 
                        before acquiring block->latch. */
1374
 
                        rw_lock_x_unlock(&btr_search_latch);
1375
 
                        /* When we release the search latch,
1376
 
                        we must rescan all blocks, because
1377
 
                        some may become hashed again. */
1378
 
                        *released_search_latch = TRUE;
1379
 
                        
1380
 
                        rw_lock_x_lock(&block->lock);
1381
 
                        
1382
 
                        /* This should be guaranteed by the
1383
 
                        callers, which will be holding
1384
 
                        btr_search_enabled_mutex. */
1385
 
                        ut_ad(!btr_search_enabled);
1386
 
                        
1387
 
                        /* Because we did not buffer-fix the
1388
 
                        block by calling buf_block_get_gen(),
1389
 
                        it is possible that the block has been
1390
 
                        allocated for some other use after
1391
 
                        btr_search_latch was released above.
1392
 
                        We do not care which file page the
1393
 
                        block is mapped to.  All we want to do
1394
 
                        is to drop any hash entries referring
1395
 
                        to the page. */
1396
 
                        
1397
 
                        /* It is possible that
1398
 
                        block->page.state != BUF_FILE_PAGE.
1399
 
                        Even that does not matter, because
1400
 
                        btr_search_drop_page_hash_index() will
1401
 
                        check block->is_hashed before doing
1402
 
                        anything.  block->is_hashed can only
1403
 
                        be set on uncompressed file pages. */
1404
 
                        
1405
 
                        btr_search_drop_page_hash_index(block);
1406
 
                        
1407
 
                        rw_lock_x_unlock(&block->lock);
1408
 
                        
1409
 
                        rw_lock_x_lock(&btr_search_latch);
1410
 
                        
1411
 
                        ut_ad(!btr_search_enabled);
1412
 
                }
1413
 
        }
1414
 
}
1415
 
 
 
1026
        mem_free(buf_pool);
 
1027
        buf_pool = NULL;
 
1028
}
 
1029
 
1416
1030
/********************************************************************//**
1417
1031
Drops the adaptive hash index.  To prevent a livelock, this function
1418
1032
is only to be called while holding btr_search_latch and while
1430
1044
        ut_ad(!btr_search_enabled);
1431
1045
 
1432
1046
        do {
1433
 
                ulint   i;
 
1047
                buf_chunk_t*    chunks  = buf_pool->chunks;
 
1048
                buf_chunk_t*    chunk   = chunks + buf_pool->n_chunks;
1434
1049
 
1435
1050
                released_search_latch = FALSE;
1436
1051
 
1437
 
                for (i = 0; i < srv_buf_pool_instances; i++) {
1438
 
                        buf_pool_t*     buf_pool;
1439
 
 
1440
 
                        buf_pool = buf_pool_from_array(i);
1441
 
 
1442
 
                        buf_pool_drop_hash_index_instance(
1443
 
                                buf_pool, &released_search_latch);
 
1052
                while (--chunk >= chunks) {
 
1053
                        buf_block_t*    block   = chunk->blocks;
 
1054
                        ulint           i       = chunk->size;
 
1055
 
 
1056
                        for (; i--; block++) {
 
1057
                                /* block->is_hashed cannot be modified
 
1058
                                when we have an x-latch on btr_search_latch;
 
1059
                                see the comment in buf0buf.h */
 
1060
 
 
1061
                                if (!block->is_hashed) {
 
1062
                                        continue;
 
1063
                                }
 
1064
 
 
1065
                                /* To follow the latching order, we
 
1066
                                have to release btr_search_latch
 
1067
                                before acquiring block->latch. */
 
1068
                                rw_lock_x_unlock(&btr_search_latch);
 
1069
                                /* When we release the search latch,
 
1070
                                we must rescan all blocks, because
 
1071
                                some may become hashed again. */
 
1072
                                released_search_latch = TRUE;
 
1073
 
 
1074
                                rw_lock_x_lock(&block->lock);
 
1075
 
 
1076
                                /* This should be guaranteed by the
 
1077
                                callers, which will be holding
 
1078
                                btr_search_enabled_mutex. */
 
1079
                                ut_ad(!btr_search_enabled);
 
1080
 
 
1081
                                /* Because we did not buffer-fix the
 
1082
                                block by calling buf_block_get_gen(),
 
1083
                                it is possible that the block has been
 
1084
                                allocated for some other use after
 
1085
                                btr_search_latch was released above.
 
1086
                                We do not care which file page the
 
1087
                                block is mapped to.  All we want to do
 
1088
                                is to drop any hash entries referring
 
1089
                                to the page. */
 
1090
 
 
1091
                                /* It is possible that
 
1092
                                block->page.state != BUF_FILE_PAGE.
 
1093
                                Even that does not matter, because
 
1094
                                btr_search_drop_page_hash_index() will
 
1095
                                check block->is_hashed before doing
 
1096
                                anything.  block->is_hashed can only
 
1097
                                be set on uncompressed file pages. */
 
1098
 
 
1099
                                btr_search_drop_page_hash_index(block);
 
1100
 
 
1101
                                rw_lock_x_unlock(&block->lock);
 
1102
 
 
1103
                                rw_lock_x_lock(&btr_search_latch);
 
1104
 
 
1105
                                ut_ad(!btr_search_enabled);
 
1106
                        }
1444
1107
                }
1445
 
 
1446
1108
        } while (released_search_latch);
1447
1109
}
1448
1110
 
1461
1123
{
1462
1124
        buf_page_t*     b;
1463
1125
        ulint           fold;
1464
 
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
1465
1126
 
1466
 
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1127
        ut_ad(buf_pool_mutex_own());
1467
1128
        ut_ad(mutex_own(buf_page_get_mutex(bpage)));
1468
1129
        ut_a(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
1469
1130
        ut_a(bpage->buf_fix_count == 0);
1470
1131
        ut_ad(bpage->in_LRU_list);
1471
1132
        ut_ad(!bpage->in_zip_hash);
1472
1133
        ut_ad(bpage->in_page_hash);
1473
 
        ut_ad(bpage == buf_page_hash_get(buf_pool,
1474
 
                                         bpage->space, bpage->offset));
1475
 
        ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
 
1134
        ut_ad(bpage == buf_page_hash_get(bpage->space, bpage->offset));
1476
1135
#ifdef UNIV_DEBUG
1477
1136
        switch (buf_page_get_state(bpage)) {
1478
1137
        case BUF_BLOCK_ZIP_FREE:
1528
1187
 
1529
1188
        HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage);
1530
1189
        HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold, dpage);
 
1190
 
 
1191
        UNIV_MEM_INVALID(bpage, sizeof *bpage);
1531
1192
}
1532
1193
 
1533
1194
/********************************************************************//**
1534
 
Shrinks a buffer pool instance. */
 
1195
Shrinks the buffer pool. */
1535
1196
static
1536
1197
void
1537
 
buf_pool_shrink_instance(
1538
 
/*=====================*/
1539
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
1540
 
        ulint           chunk_size)     /*!< in: number of pages to remove */
 
1198
buf_pool_shrink(
 
1199
/*============*/
 
1200
        ulint   chunk_size)     /*!< in: number of pages to remove */
1541
1201
{
1542
1202
        buf_chunk_t*    chunks;
1543
1203
        buf_chunk_t*    chunk;
1546
1206
        buf_chunk_t*    max_chunk;
1547
1207
        buf_chunk_t*    max_free_chunk;
1548
1208
 
1549
 
        ut_ad(!buf_pool_mutex_own(buf_pool));
 
1209
        ut_ad(!buf_pool_mutex_own());
1550
1210
 
1551
1211
try_again:
1552
1212
        btr_search_disable(); /* Empty the adaptive hash index again */
1553
 
        buf_pool_mutex_enter(buf_pool);
 
1213
        buf_pool_mutex_enter();
1554
1214
 
1555
1215
shrink_again:
1556
1216
        if (buf_pool->n_chunks <= 1) {
1613
1273
 
1614
1274
                        mutex_enter(&block->mutex);
1615
1275
                        /* The following calls will temporarily
1616
 
                        release block->mutex and buf_pool->mutex.
 
1276
                        release block->mutex and buf_pool_mutex.
1617
1277
                        Therefore, we have to always retry,
1618
1278
                        even if !dirty && !nonfree. */
1619
1279
 
1629
1289
                        mutex_exit(&block->mutex);
1630
1290
                }
1631
1291
 
1632
 
                buf_pool_mutex_exit(buf_pool);
 
1292
                buf_pool_mutex_exit();
1633
1293
 
1634
1294
                /* Request for a flush of the chunk if it helps.
1635
1295
                Do not flush if there are non-free blocks, since
1638
1298
                        /* Avoid busy-waiting. */
1639
1299
                        os_thread_sleep(100000);
1640
1300
                } else if (dirty
1641
 
                           && buf_flush_LRU(buf_pool, dirty)
1642
 
                              == ULINT_UNDEFINED) {
 
1301
                           && buf_flush_batch(BUF_FLUSH_LRU, dirty, 0)
 
1302
                           == ULINT_UNDEFINED) {
1643
1303
 
1644
 
                        buf_flush_wait_batch_end(buf_pool, BUF_FLUSH_LRU);
 
1304
                        buf_flush_wait_batch_end(BUF_FLUSH_LRU);
1645
1305
                }
1646
1306
 
1647
1307
                goto try_again;
1650
1310
        max_size = max_free_size;
1651
1311
        max_chunk = max_free_chunk;
1652
1312
 
1653
 
        buf_pool->old_pool_size = buf_pool->curr_pool_size;
 
1313
        srv_buf_pool_old_size = srv_buf_pool_size;
1654
1314
 
1655
1315
        /* Rewrite buf_pool->chunks.  Copy everything but max_chunk. */
1656
 
        chunks = static_cast<buf_chunk_t *>(mem_alloc((buf_pool->n_chunks - 1) * sizeof *chunks));
 
1316
        chunks = mem_alloc((buf_pool->n_chunks - 1) * sizeof *chunks);
1657
1317
        memcpy(chunks, buf_pool->chunks,
1658
1318
               (max_chunk - buf_pool->chunks) * sizeof *chunks);
1659
1319
        memcpy(chunks + (max_chunk - buf_pool->chunks),
1662
1322
               - (max_chunk + 1));
1663
1323
        ut_a(buf_pool->curr_size > max_chunk->size);
1664
1324
        buf_pool->curr_size -= max_chunk->size;
1665
 
        buf_pool->curr_pool_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
 
1325
        srv_buf_pool_curr_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
1666
1326
        chunk_size -= max_chunk->size;
1667
 
        buf_chunk_free(buf_pool, max_chunk);
 
1327
        buf_chunk_free(max_chunk);
1668
1328
        mem_free(buf_pool->chunks);
1669
1329
        buf_pool->chunks = chunks;
1670
1330
        buf_pool->n_chunks--;
1674
1334
 
1675
1335
                goto shrink_again;
1676
1336
        }
1677
 
        goto func_exit;
1678
1337
 
1679
1338
func_done:
1680
 
        buf_pool->old_pool_size = buf_pool->curr_pool_size;
 
1339
        srv_buf_pool_old_size = srv_buf_pool_size;
1681
1340
func_exit:
1682
 
        buf_pool_mutex_exit(buf_pool);
 
1341
        buf_pool_mutex_exit();
1683
1342
        btr_search_enable();
1684
1343
}
1685
1344
 
1686
1345
/********************************************************************//**
1687
 
Shrinks the buffer pool. */
1688
 
static
1689
 
void
1690
 
buf_pool_shrink(
1691
 
/*============*/
1692
 
        ulint   chunk_size)     /*!< in: number of pages to remove */
1693
 
{
1694
 
        ulint   i;
1695
 
 
1696
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
1697
 
                buf_pool_t*     buf_pool;
1698
 
                ulint           instance_chunk_size;
1699
 
 
1700
 
                instance_chunk_size = chunk_size / srv_buf_pool_instances;
1701
 
                buf_pool = buf_pool_from_array(i);
1702
 
                buf_pool_shrink_instance(buf_pool, instance_chunk_size);
1703
 
        }
1704
 
 
1705
 
        buf_pool_set_sizes();
1706
 
}
1707
 
 
1708
 
/********************************************************************//**
1709
 
Rebuild buf_pool->page_hash for a buffer pool instance. */
1710
 
static
1711
 
void
1712
 
buf_pool_page_hash_rebuild_instance(
1713
 
/*================================*/
1714
 
        buf_pool_t*     buf_pool)               /*!< in: buffer pool instance */
 
1346
Rebuild buf_pool->page_hash. */
 
1347
static
 
1348
void
 
1349
buf_pool_page_hash_rebuild(void)
 
1350
/*============================*/
1715
1351
{
1716
1352
        ulint           i;
1717
 
        buf_page_t*     b;
1718
 
        buf_chunk_t*    chunk;
1719
1353
        ulint           n_chunks;
 
1354
        buf_chunk_t*    chunk;
 
1355
        hash_table_t*   page_hash;
1720
1356
        hash_table_t*   zip_hash;
1721
 
        hash_table_t*   page_hash;
 
1357
        buf_page_t*     b;
1722
1358
 
1723
 
        buf_pool_mutex_enter(buf_pool);
 
1359
        buf_pool_mutex_enter();
1724
1360
 
1725
1361
        /* Free, create, and populate the hash table. */
1726
1362
        hash_table_free(buf_pool->page_hash);
1773
1409
                            buf_page_address_fold(b->space, b->offset), b);
1774
1410
        }
1775
1411
 
1776
 
        buf_flush_list_mutex_enter(buf_pool);
1777
1412
        for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
1778
1413
             b = UT_LIST_GET_NEXT(list, b)) {
1779
1414
                ut_ad(b->in_flush_list);
1801
1436
                }
1802
1437
        }
1803
1438
 
1804
 
        buf_flush_list_mutex_exit(buf_pool);
1805
 
        buf_pool_mutex_exit(buf_pool);
1806
 
}
1807
 
 
1808
 
/********************************************************************
1809
 
Determine if a block is a sentinel for a buffer pool watch.
1810
 
@return TRUE if a sentinel for a buffer pool watch, FALSE if not */
1811
 
UNIV_INTERN
1812
 
ibool
1813
 
buf_pool_watch_is_sentinel(
1814
 
/*=======================*/
1815
 
        buf_pool_t*             buf_pool,       /*!< buffer pool instance */
1816
 
        const buf_page_t*       bpage)          /*!< in: block */
1817
 
{
1818
 
        ut_ad(buf_page_in_file(bpage));
1819
 
 
1820
 
        if (bpage < &buf_pool->watch[0]
1821
 
            || bpage >= &buf_pool->watch[BUF_POOL_WATCH_SIZE]) {
1822
 
 
1823
 
                ut_ad(buf_page_get_state(bpage) != BUF_BLOCK_ZIP_PAGE
1824
 
                      || bpage->zip.data != NULL);
1825
 
 
1826
 
                return(FALSE);
1827
 
        }
1828
 
 
1829
 
        ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_PAGE);
1830
 
        ut_ad(!bpage->in_zip_hash);
1831
 
        ut_ad(bpage->in_page_hash);
1832
 
        ut_ad(bpage->zip.data == NULL);
1833
 
        ut_ad(bpage->buf_fix_count > 0);
1834
 
        return(TRUE);
1835
 
}
1836
 
 
1837
 
/****************************************************************//**
1838
 
Add watch for the given page to be read in. Caller must have the buffer pool
1839
 
mutex reserved.
1840
 
@return NULL if watch set, block if the page is in the buffer pool */
1841
 
UNIV_INTERN
1842
 
buf_page_t*
1843
 
buf_pool_watch_set(
1844
 
/*===============*/
1845
 
        ulint   space,  /*!< in: space id */
1846
 
        ulint   offset, /*!< in: page number */
1847
 
        ulint   fold)   /*!< in: buf_page_address_fold(space, offset) */
1848
 
{
1849
 
        buf_page_t*     bpage;
1850
 
        ulint           i;
1851
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
1852
 
 
1853
 
        ut_ad(buf_pool_mutex_own(buf_pool));
1854
 
 
1855
 
        bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
1856
 
 
1857
 
        if (UNIV_LIKELY_NULL(bpage)) {
1858
 
                if (!buf_pool_watch_is_sentinel(buf_pool, bpage)) {
1859
 
                        /* The page was loaded meanwhile. */
1860
 
                        return(bpage);
1861
 
                }
1862
 
                /* Add to an existing watch. */
1863
 
                bpage->buf_fix_count++;
1864
 
                return(NULL);
1865
 
        }
1866
 
 
1867
 
        for (i = 0; i < BUF_POOL_WATCH_SIZE; i++) {
1868
 
                bpage = &buf_pool->watch[i];
1869
 
 
1870
 
                ut_ad(bpage->access_time == 0);
1871
 
                ut_ad(bpage->newest_modification == 0);
1872
 
                ut_ad(bpage->oldest_modification == 0);
1873
 
                ut_ad(bpage->zip.data == NULL);
1874
 
                ut_ad(!bpage->in_zip_hash);
1875
 
 
1876
 
                switch (bpage->state) {
1877
 
                case BUF_BLOCK_POOL_WATCH:
1878
 
                        ut_ad(!bpage->in_page_hash);
1879
 
                        ut_ad(bpage->buf_fix_count == 0);
1880
 
 
1881
 
                        /* bpage is pointing to buf_pool->watch[],
1882
 
                        which is protected by buf_pool->mutex.
1883
 
                        Normally, buf_page_t objects are protected by
1884
 
                        buf_block_t::mutex or buf_pool->zip_mutex or both. */
1885
 
 
1886
 
                        bpage->state = BUF_BLOCK_ZIP_PAGE;
1887
 
                        bpage->space = space;
1888
 
                        bpage->offset = offset;
1889
 
                        bpage->buf_fix_count = 1;
1890
 
 
1891
 
                        ut_d(bpage->in_page_hash = TRUE);
1892
 
                        HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
1893
 
                                    fold, bpage);
1894
 
                        return(NULL);
1895
 
                case BUF_BLOCK_ZIP_PAGE:
1896
 
                        ut_ad(bpage->in_page_hash);
1897
 
                        ut_ad(bpage->buf_fix_count > 0);
1898
 
                        break;
1899
 
                default:
1900
 
                        ut_error;
1901
 
                }
1902
 
        }
1903
 
 
1904
 
        /* Allocation failed.  Either the maximum number of purge
1905
 
        threads should never exceed BUF_POOL_WATCH_SIZE, or this code
1906
 
        should be modified to return a special non-NULL value and the
1907
 
        caller should purge the record directly. */
1908
 
        ut_error;
1909
 
 
1910
 
        /* Fix compiler warning */
1911
 
        return(NULL);
1912
 
}
1913
 
 
1914
 
/********************************************************************//**
1915
 
Rebuild buf_pool->page_hash. */
1916
 
static
1917
 
void
1918
 
buf_pool_page_hash_rebuild(void)
1919
 
/*============================*/
1920
 
{
1921
 
        ulint   i;
1922
 
 
1923
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
1924
 
                buf_pool_page_hash_rebuild_instance(buf_pool_from_array(i));
1925
 
        }
1926
 
}
1927
 
 
1928
 
/********************************************************************//**
1929
 
Increase the buffer pool size of one buffer pool instance. */
1930
 
static
1931
 
void
1932
 
buf_pool_increase_instance(
1933
 
/*=======================*/
1934
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instane */
1935
 
        ulint           change_size)    /*!< in: new size of the pool */
1936
 
{
1937
 
        buf_chunk_t*    chunks;
1938
 
        buf_chunk_t*    chunk;
1939
 
 
1940
 
        buf_pool_mutex_enter(buf_pool);
1941
 
        chunks = static_cast<buf_chunk_t *>(mem_alloc((buf_pool->n_chunks + 1) * sizeof *chunks));
1942
 
 
1943
 
        memcpy(chunks, buf_pool->chunks, buf_pool->n_chunks * sizeof *chunks);
1944
 
 
1945
 
        chunk = &chunks[buf_pool->n_chunks];
1946
 
 
1947
 
        if (!buf_chunk_init(buf_pool, chunk, change_size)) {
1948
 
                mem_free(chunks);
1949
 
        } else {
1950
 
                buf_pool->old_pool_size = buf_pool->curr_pool_size;
1951
 
                buf_pool->curr_size += chunk->size;
1952
 
                buf_pool->curr_pool_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
1953
 
                mem_free(buf_pool->chunks);
1954
 
                buf_pool->chunks = chunks;
1955
 
                buf_pool->n_chunks++;
1956
 
        }
1957
 
 
1958
 
        buf_pool_mutex_exit(buf_pool);
1959
 
}
1960
 
 
1961
 
/********************************************************************//**
1962
 
Increase the buffer pool size. */
1963
 
static
1964
 
void
1965
 
buf_pool_increase(
1966
 
/*==============*/
1967
 
        ulint   change_size)
1968
 
{
1969
 
        ulint   i;
1970
 
 
1971
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
1972
 
                buf_pool_increase_instance(
1973
 
                        buf_pool_from_array(i),
1974
 
                        change_size / srv_buf_pool_instances);
1975
 
        }
1976
 
 
1977
 
        buf_pool_set_sizes();
 
1439
        buf_pool_mutex_exit();
1978
1440
}
1979
1441
 
1980
1442
/********************************************************************//**
1984
1446
buf_pool_resize(void)
1985
1447
/*=================*/
1986
1448
{
1987
 
        ulint   change_size;
1988
 
        ulint   min_change_size = 1048576 * srv_buf_pool_instances;
1989
 
 
1990
 
        buf_pool_mutex_enter_all();
1991
 
  
1992
 
        if (srv_buf_pool_old_size == srv_buf_pool_size) {
1993
 
  
1994
 
                buf_pool_mutex_exit_all();
1995
 
 
1996
 
                return;
1997
 
 
1998
 
        } else if (srv_buf_pool_curr_size + min_change_size
1999
 
                   > srv_buf_pool_size) {
2000
 
  
2001
 
                change_size = (srv_buf_pool_curr_size - srv_buf_pool_size)
2002
 
                            / UNIV_PAGE_SIZE;
2003
 
 
2004
 
                buf_pool_mutex_exit_all();
2005
 
  
2006
 
                /* Disable adaptive hash indexes and empty the index
2007
 
                in order to free up memory in the buffer pool chunks. */
2008
 
                buf_pool_shrink(change_size);
2009
 
 
2010
 
        } else if (srv_buf_pool_curr_size + min_change_size
2011
 
                   < srv_buf_pool_size) {
2012
 
 
2013
 
                /* Enlarge the buffer pool by at least one megabyte */
2014
 
  
2015
 
                change_size = srv_buf_pool_size - srv_buf_pool_curr_size;
2016
 
 
2017
 
                buf_pool_mutex_exit_all();
2018
 
 
2019
 
                buf_pool_increase(change_size);
2020
 
        } else {
2021
 
                srv_buf_pool_size = srv_buf_pool_old_size;
2022
 
 
2023
 
                buf_pool_mutex_exit_all();
2024
 
 
 
1449
        buf_pool_mutex_enter();
 
1450
 
 
1451
        if (srv_buf_pool_old_size == srv_buf_pool_size) {
 
1452
 
 
1453
                buf_pool_mutex_exit();
2025
1454
                return;
2026
1455
        }
2027
 
  
2028
 
        buf_pool_page_hash_rebuild();
2029
 
}
2030
 
 
2031
 
/****************************************************************//**
2032
 
Remove the sentinel block for the watch before replacing it with a real block.
2033
 
buf_page_watch_clear() or buf_page_watch_occurred() will notice that
2034
 
the block has been replaced with the real block.
2035
 
@return reference count, to be added to the replacement block */
2036
 
static
2037
 
void
2038
 
buf_pool_watch_remove(
2039
 
/*==================*/
2040
 
        buf_pool_t*     buf_pool,       /*!< buffer pool instance */
2041
 
        ulint           fold,           /*!< in: buf_page_address_fold(
2042
 
                                        space, offset) */
2043
 
        buf_page_t*     watch)          /*!< in/out: sentinel for watch */
2044
 
{
2045
 
        ut_ad(buf_pool_mutex_own(buf_pool));
2046
 
 
2047
 
        HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, watch);
2048
 
        ut_d(watch->in_page_hash = FALSE);
2049
 
        watch->buf_fix_count = 0;
2050
 
        watch->state = BUF_BLOCK_POOL_WATCH;
2051
 
}
2052
 
 
2053
 
/****************************************************************//**
2054
 
Stop watching if the page has been read in.
2055
 
buf_pool_watch_set(space,offset) must have returned NULL before. */
2056
 
UNIV_INTERN
2057
 
void
2058
 
buf_pool_watch_unset(
2059
 
/*=================*/
2060
 
        ulint   space,  /*!< in: space id */
2061
 
        ulint   offset) /*!< in: page number */
2062
 
{
2063
 
        buf_page_t*     bpage;
2064
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2065
 
        ulint           fold = buf_page_address_fold(space, offset);
2066
 
 
2067
 
        buf_pool_mutex_enter(buf_pool);
2068
 
        bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
2069
 
        /* The page must exist because buf_pool_watch_set()
2070
 
        increments buf_fix_count. */
2071
 
        ut_a(bpage);
2072
 
 
2073
 
        if (UNIV_UNLIKELY(!buf_pool_watch_is_sentinel(buf_pool, bpage))) {
2074
 
                mutex_t* mutex = buf_page_get_mutex(bpage);
2075
 
 
2076
 
                mutex_enter(mutex);
2077
 
                ut_a(bpage->buf_fix_count > 0);
2078
 
                bpage->buf_fix_count--;
2079
 
                mutex_exit(mutex);
2080
 
        } else {
2081
 
                ut_a(bpage->buf_fix_count > 0);
2082
 
 
2083
 
                if (UNIV_LIKELY(!--bpage->buf_fix_count)) {
2084
 
                        buf_pool_watch_remove(buf_pool, fold, bpage);
 
1456
 
 
1457
        if (srv_buf_pool_curr_size + 1048576 > srv_buf_pool_size) {
 
1458
 
 
1459
                buf_pool_mutex_exit();
 
1460
 
 
1461
                /* Disable adaptive hash indexes and empty the index
 
1462
                in order to free up memory in the buffer pool chunks. */
 
1463
                buf_pool_shrink((srv_buf_pool_curr_size - srv_buf_pool_size)
 
1464
                                / UNIV_PAGE_SIZE);
 
1465
        } else if (srv_buf_pool_curr_size + 1048576 < srv_buf_pool_size) {
 
1466
 
 
1467
                /* Enlarge the buffer pool by at least one megabyte */
 
1468
 
 
1469
                ulint           mem_size
 
1470
                        = srv_buf_pool_size - srv_buf_pool_curr_size;
 
1471
                buf_chunk_t*    chunks;
 
1472
                buf_chunk_t*    chunk;
 
1473
 
 
1474
                chunks = mem_alloc((buf_pool->n_chunks + 1) * sizeof *chunks);
 
1475
 
 
1476
                memcpy(chunks, buf_pool->chunks, buf_pool->n_chunks
 
1477
                       * sizeof *chunks);
 
1478
 
 
1479
                chunk = &chunks[buf_pool->n_chunks];
 
1480
 
 
1481
                if (!buf_chunk_init(chunk, mem_size)) {
 
1482
                        mem_free(chunks);
 
1483
                } else {
 
1484
                        buf_pool->curr_size += chunk->size;
 
1485
                        srv_buf_pool_curr_size = buf_pool->curr_size
 
1486
                                * UNIV_PAGE_SIZE;
 
1487
                        mem_free(buf_pool->chunks);
 
1488
                        buf_pool->chunks = chunks;
 
1489
                        buf_pool->n_chunks++;
2085
1490
                }
 
1491
 
 
1492
                srv_buf_pool_old_size = srv_buf_pool_size;
 
1493
                buf_pool_mutex_exit();
2086
1494
        }
2087
1495
 
2088
 
        buf_pool_mutex_exit(buf_pool);
2089
 
}
2090
 
 
2091
 
/****************************************************************//**
2092
 
Check if the page has been read in.
2093
 
This may only be called after buf_pool_watch_set(space,offset)
2094
 
has returned NULL and before invoking buf_pool_watch_unset(space,offset).
2095
 
@return FALSE if the given page was not read in, TRUE if it was */
2096
 
UNIV_INTERN
2097
 
ibool
2098
 
buf_pool_watch_occurred(
2099
 
/*====================*/
2100
 
        ulint   space,  /*!< in: space id */
2101
 
        ulint   offset) /*!< in: page number */
2102
 
{
2103
 
        ibool           ret;
2104
 
        buf_page_t*     bpage;
2105
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2106
 
        ulint           fold    = buf_page_address_fold(space, offset);
2107
 
 
2108
 
        buf_pool_mutex_enter(buf_pool);
2109
 
 
2110
 
        bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
2111
 
        /* The page must exist because buf_pool_watch_set()
2112
 
        increments buf_fix_count. */
2113
 
        ut_a(bpage);
2114
 
        ret = !buf_pool_watch_is_sentinel(buf_pool, bpage);
2115
 
        buf_pool_mutex_exit(buf_pool);
2116
 
 
2117
 
        return(ret);
 
1496
        buf_pool_page_hash_rebuild();
2118
1497
}
2119
1498
 
2120
1499
/********************************************************************//**
2127
1506
/*================*/
2128
1507
        buf_page_t*     bpage)  /*!< in: buffer block of a file page */
2129
1508
{
2130
 
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
2131
 
 
2132
 
        buf_pool_mutex_enter(buf_pool);
 
1509
        buf_pool_mutex_enter();
2133
1510
 
2134
1511
        ut_a(buf_page_in_file(bpage));
2135
1512
 
2136
1513
        buf_LRU_make_block_young(bpage);
2137
1514
 
2138
 
        buf_pool_mutex_exit(buf_pool);
 
1515
        buf_pool_mutex_exit();
2139
1516
}
2140
1517
 
2141
1518
/********************************************************************//**
2153
1530
                                        read under mutex protection,
2154
1531
                                        or 0 if unknown */
2155
1532
{
2156
 
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
2157
 
 
2158
 
        ut_ad(!buf_pool_mutex_own(buf_pool));
 
1533
        ut_ad(!buf_pool_mutex_own());
2159
1534
        ut_a(buf_page_in_file(bpage));
2160
1535
 
2161
1536
        if (buf_page_peek_if_too_old(bpage)) {
2162
 
                buf_pool_mutex_enter(buf_pool);
 
1537
                buf_pool_mutex_enter();
2163
1538
                buf_LRU_make_block_young(bpage);
2164
 
                buf_pool_mutex_exit(buf_pool);
 
1539
                buf_pool_mutex_exit();
2165
1540
        } else if (!access_time) {
2166
1541
                ulint   time_ms = ut_time_ms();
2167
 
                buf_pool_mutex_enter(buf_pool);
 
1542
                buf_pool_mutex_enter();
2168
1543
                buf_page_set_accessed(bpage, time_ms);
2169
 
                buf_pool_mutex_exit(buf_pool);
 
1544
                buf_pool_mutex_exit();
2170
1545
        }
2171
1546
}
2172
1547
 
2181
1556
        ulint   offset) /*!< in: page number */
2182
1557
{
2183
1558
        buf_block_t*    block;
2184
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2185
 
 
2186
 
        buf_pool_mutex_enter(buf_pool);
2187
 
 
2188
 
        block = (buf_block_t*) buf_page_hash_get(buf_pool, space, offset);
 
1559
 
 
1560
        buf_pool_mutex_enter();
 
1561
 
 
1562
        block = (buf_block_t*) buf_page_hash_get(space, offset);
2189
1563
 
2190
1564
        if (block && buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE) {
2191
 
                ut_ad(!buf_pool_watch_is_sentinel(buf_pool, &block->page));
2192
1565
                block->check_index_page_at_flush = FALSE;
2193
1566
        }
2194
1567
 
2195
 
        buf_pool_mutex_exit(buf_pool);
 
1568
        buf_pool_mutex_exit();
2196
1569
}
2197
1570
 
2198
1571
/********************************************************************//**
2209
1582
{
2210
1583
        buf_block_t*    block;
2211
1584
        ibool           is_hashed;
2212
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2213
 
 
2214
 
        buf_pool_mutex_enter(buf_pool);
2215
 
 
2216
 
        block = (buf_block_t*) buf_page_hash_get(buf_pool, space, offset);
 
1585
 
 
1586
        buf_pool_mutex_enter();
 
1587
 
 
1588
        block = (buf_block_t*) buf_page_hash_get(space, offset);
2217
1589
 
2218
1590
        if (!block || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
2219
1591
                is_hashed = FALSE;
2220
1592
        } else {
2221
 
                ut_ad(!buf_pool_watch_is_sentinel(buf_pool, &block->page));
2222
1593
                is_hashed = block->is_hashed;
2223
1594
        }
2224
1595
 
2225
 
        buf_pool_mutex_exit(buf_pool);
 
1596
        buf_pool_mutex_exit();
2226
1597
 
2227
1598
        return(is_hashed);
2228
1599
}
2242
1613
        ulint   offset) /*!< in: page number */
2243
1614
{
2244
1615
        buf_page_t*     bpage;
2245
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2246
 
 
2247
 
        buf_pool_mutex_enter(buf_pool);
2248
 
 
2249
 
        bpage = buf_page_hash_get(buf_pool, space, offset);
 
1616
 
 
1617
        buf_pool_mutex_enter();
 
1618
 
 
1619
        bpage = buf_page_hash_get(space, offset);
2250
1620
 
2251
1621
        if (bpage) {
2252
 
                ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
2253
1622
                bpage->file_page_was_freed = TRUE;
2254
1623
        }
2255
1624
 
2256
 
        buf_pool_mutex_exit(buf_pool);
 
1625
        buf_pool_mutex_exit();
2257
1626
 
2258
1627
        return(bpage);
2259
1628
}
2272
1641
        ulint   offset) /*!< in: page number */
2273
1642
{
2274
1643
        buf_page_t*     bpage;
2275
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2276
 
 
2277
 
        buf_pool_mutex_enter(buf_pool);
2278
 
 
2279
 
        bpage = buf_page_hash_get(buf_pool, space, offset);
 
1644
 
 
1645
        buf_pool_mutex_enter();
 
1646
 
 
1647
        bpage = buf_page_hash_get(space, offset);
2280
1648
 
2281
1649
        if (bpage) {
2282
 
                ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
2283
1650
                bpage->file_page_was_freed = FALSE;
2284
1651
        }
2285
1652
 
2286
 
        buf_pool_mutex_exit(buf_pool);
 
1653
        buf_pool_mutex_exit();
2287
1654
 
2288
1655
        return(bpage);
2289
1656
}
2310
1677
        mutex_t*        block_mutex;
2311
1678
        ibool           must_read;
2312
1679
        unsigned        access_time;
2313
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2314
1680
 
2315
1681
#ifndef UNIV_LOG_DEBUG
2316
1682
        ut_ad(!ibuf_inside());
2318
1684
        buf_pool->stat.n_page_gets++;
2319
1685
 
2320
1686
        for (;;) {
2321
 
                buf_pool_mutex_enter(buf_pool);
 
1687
                buf_pool_mutex_enter();
2322
1688
lookup:
2323
 
                bpage = buf_page_hash_get(buf_pool, space, offset);
 
1689
                bpage = buf_page_hash_get(space, offset);
2324
1690
                if (bpage) {
2325
 
                        ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
2326
1691
                        break;
2327
1692
                }
2328
1693
 
2329
1694
                /* Page not in buf_pool: needs to be read from file */
2330
1695
 
2331
 
                buf_pool_mutex_exit(buf_pool);
 
1696
                buf_pool_mutex_exit();
2332
1697
 
2333
1698
                buf_read_page(space, zip_size, offset);
2334
1699
 
2340
1705
        if (UNIV_UNLIKELY(!bpage->zip.data)) {
2341
1706
                /* There is no compressed page. */
2342
1707
err_exit:
2343
 
                buf_pool_mutex_exit(buf_pool);
 
1708
                buf_pool_mutex_exit();
2344
1709
                return(NULL);
2345
1710
        }
2346
1711
 
2347
 
        ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
2348
 
 
2349
1712
        switch (buf_page_get_state(bpage)) {
2350
1713
        case BUF_BLOCK_NOT_USED:
2351
1714
        case BUF_BLOCK_READY_FOR_USE:
2355
1718
                break;
2356
1719
        case BUF_BLOCK_ZIP_PAGE:
2357
1720
        case BUF_BLOCK_ZIP_DIRTY:
2358
 
                block_mutex = &buf_pool->zip_mutex;
 
1721
                block_mutex = &buf_pool_zip_mutex;
2359
1722
                mutex_enter(block_mutex);
2360
1723
                bpage->buf_fix_count++;
2361
1724
                goto got_block;
2383
1746
        must_read = buf_page_get_io_fix(bpage) == BUF_IO_READ;
2384
1747
        access_time = buf_page_is_accessed(bpage);
2385
1748
 
2386
 
        buf_pool_mutex_exit(buf_pool);
 
1749
        buf_pool_mutex_exit();
2387
1750
 
2388
1751
        mutex_exit(block_mutex);
2389
1752
 
2455
1818
        buf_block_t*    block,  /*!< in/out: block */
2456
1819
        ibool           check)  /*!< in: TRUE=verify the page checksum */
2457
1820
{
2458
 
        const byte*     frame           = block->page.zip.data;
2459
 
        ulint           stamp_checksum  = mach_read_from_4(
2460
 
                frame + FIL_PAGE_SPACE_OR_CHKSUM);
 
1821
        const byte* frame = block->page.zip.data;
2461
1822
 
2462
1823
        ut_ad(buf_block_get_zip_size(block));
2463
1824
        ut_a(buf_block_get_space(block) != 0);
2464
1825
 
2465
 
        if (UNIV_LIKELY(check && stamp_checksum != BUF_NO_CHECKSUM_MAGIC)) {
 
1826
        if (UNIV_LIKELY(check)) {
 
1827
                ulint   stamp_checksum  = mach_read_from_4(
 
1828
                        frame + FIL_PAGE_SPACE_OR_CHKSUM);
2466
1829
                ulint   calc_checksum   = page_zip_calc_checksum(
2467
1830
                        frame, page_zip_get_size(&block->page.zip));
2468
1831
 
2513
1876
 
2514
1877
#ifndef UNIV_HOTBACKUP
2515
1878
/*******************************************************************//**
2516
 
Gets the block to whose frame the pointer is pointing to if found
2517
 
in this buffer pool instance.
2518
 
@return pointer to block */
2519
 
static
 
1879
Gets the block to whose frame the pointer is pointing to.
 
1880
@return pointer to block, never NULL */
 
1881
UNIV_INTERN
2520
1882
buf_block_t*
2521
 
buf_block_align_instance(
2522
 
/*=====================*/
2523
 
        buf_pool_t*     buf_pool,       /*!< in: buffer in which the block
2524
 
                                        resides */
2525
 
        const byte*     ptr)            /*!< in: pointer to a frame */
 
1883
buf_block_align(
 
1884
/*============*/
 
1885
        const byte*     ptr)    /*!< in: pointer to a frame */
2526
1886
{
2527
1887
        buf_chunk_t*    chunk;
2528
1888
        ulint           i;
2548
1908
                        ut_ad(block->frame == page_align(ptr));
2549
1909
#ifdef UNIV_DEBUG
2550
1910
                        /* A thread that updates these fields must
2551
 
                        hold buf_pool->mutex and block->mutex.  Acquire
 
1911
                        hold buf_pool_mutex and block->mutex.  Acquire
2552
1912
                        only the latter. */
2553
1913
                        mutex_enter(&block->mutex);
2554
1914
 
2597
1957
                }
2598
1958
        }
2599
1959
 
2600
 
        return(NULL);
2601
 
}
2602
 
 
2603
 
/*******************************************************************//**
2604
 
Gets the block to whose frame the pointer is pointing to.
2605
 
@return pointer to block, never NULL */
2606
 
UNIV_INTERN
2607
 
buf_block_t*
2608
 
buf_block_align(
2609
 
/*============*/
2610
 
        const byte*     ptr)    /*!< in: pointer to a frame */
2611
 
{
2612
 
        ulint           i;
2613
 
 
2614
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
2615
 
                buf_block_t*    block;
2616
 
 
2617
 
                block = buf_block_align_instance(
2618
 
                        buf_pool_from_array(i), ptr);
2619
 
                if (block) {
2620
 
                        return(block);
2621
 
                }
2622
 
        }
2623
 
 
2624
1960
        /* The block should always be found. */
2625
1961
        ut_error;
2626
1962
        return(NULL);
2628
1964
 
2629
1965
/********************************************************************//**
2630
1966
Find out if a pointer belongs to a buf_block_t. It can be a pointer to
2631
 
the buf_block_t itself or a member of it. This functions checks one of
2632
 
the buffer pool instances.
 
1967
the buf_block_t itself or a member of it
2633
1968
@return TRUE if ptr belongs to a buf_block_t struct */
2634
 
static
 
1969
UNIV_INTERN
2635
1970
ibool
2636
 
buf_pointer_is_block_field_instance(
2637
 
/*================================*/
2638
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
2639
 
        const void*     ptr)            /*!< in: pointer not dereferenced */
 
1971
buf_pointer_is_block_field(
 
1972
/*=======================*/
 
1973
        const void*             ptr)    /*!< in: pointer not
 
1974
                                        dereferenced */
2640
1975
{
2641
1976
        const buf_chunk_t*              chunk   = buf_pool->chunks;
2642
1977
        const buf_chunk_t* const        echunk  = chunk + buf_pool->n_chunks;
2657
1992
}
2658
1993
 
2659
1994
/********************************************************************//**
2660
 
Find out if a pointer belongs to a buf_block_t. It can be a pointer to
2661
 
the buf_block_t itself or a member of it
2662
 
@return TRUE if ptr belongs to a buf_block_t struct */
2663
 
UNIV_INTERN
2664
 
ibool
2665
 
buf_pointer_is_block_field(
2666
 
/*=======================*/
2667
 
        const void*     ptr)    /*!< in: pointer not dereferenced */
2668
 
{
2669
 
        ulint   i;
2670
 
 
2671
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
2672
 
                ibool   found;
2673
 
 
2674
 
                found = buf_pointer_is_block_field_instance(
2675
 
                        buf_pool_from_array(i), ptr);
2676
 
                if (found) {
2677
 
                        return(TRUE);
2678
 
                }
2679
 
        }
2680
 
 
2681
 
        return(FALSE);
2682
 
}
2683
 
 
2684
 
/********************************************************************//**
2685
1995
Find out if a buffer block was created by buf_chunk_init().
2686
1996
@return TRUE if "block" has been added to buf_pool->free by buf_chunk_init() */
2687
1997
static
2688
1998
ibool
2689
1999
buf_block_is_uncompressed(
2690
2000
/*======================*/
2691
 
        buf_pool_t*             buf_pool,       /*!< in: buffer pool instance */
2692
 
        const buf_block_t*      block)          /*!< in: pointer to block,
2693
 
                                                not dereferenced */
 
2001
        const buf_block_t*      block)  /*!< in: pointer to block,
 
2002
                                        not dereferenced */
2694
2003
{
2695
 
        ut_ad(buf_pool_mutex_own(buf_pool));
 
2004
        ut_ad(buf_pool_mutex_own());
2696
2005
 
2697
2006
        if (UNIV_UNLIKELY((((ulint) block) % sizeof *block) != 0)) {
2698
2007
                /* The pointer should be aligned. */
2699
2008
                return(FALSE);
2700
2009
        }
2701
2010
 
2702
 
        return(buf_pointer_is_block_field_instance(buf_pool, (void *)block));
 
2011
        return(buf_pointer_is_block_field((void *)block));
2703
2012
}
2704
2013
 
2705
2014
/********************************************************************//**
2716
2025
        ulint           rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
2717
2026
        buf_block_t*    guess,  /*!< in: guessed block or NULL */
2718
2027
        ulint           mode,   /*!< in: BUF_GET, BUF_GET_IF_IN_POOL,
2719
 
                                BUF_GET_NO_LATCH, or
2720
 
                                BUF_GET_IF_IN_POOL_OR_WATCH */
 
2028
                                BUF_GET_NO_LATCH */
2721
2029
        const char*     file,   /*!< in: file name */
2722
2030
        ulint           line,   /*!< in: line where called */
2723
2031
        mtr_t*          mtr)    /*!< in: mini-transaction */
2724
2032
{
2725
2033
        buf_block_t*    block;
2726
 
        ulint           fold;
2727
2034
        unsigned        access_time;
2728
2035
        ulint           fix_type;
2729
2036
        ibool           must_read;
2730
 
        ulint           retries = 0;
2731
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
2732
2037
 
2733
2038
        ut_ad(mtr);
2734
 
        ut_ad(mtr->state == MTR_ACTIVE);
2735
2039
        ut_ad((rw_latch == RW_S_LATCH)
2736
2040
              || (rw_latch == RW_X_LATCH)
2737
2041
              || (rw_latch == RW_NO_LATCH));
2738
2042
        ut_ad((mode != BUF_GET_NO_LATCH) || (rw_latch == RW_NO_LATCH));
2739
 
        ut_ad(mode == BUF_GET
2740
 
              || mode == BUF_GET_IF_IN_POOL
2741
 
              || mode == BUF_GET_NO_LATCH
2742
 
              || mode == BUF_GET_IF_IN_POOL_OR_WATCH);
 
2043
        ut_ad((mode == BUF_GET) || (mode == BUF_GET_IF_IN_POOL)
 
2044
              || (mode == BUF_GET_NO_LATCH));
2743
2045
        ut_ad(zip_size == fil_space_get_zip_size(space));
2744
2046
        ut_ad(ut_is_2pow(zip_size));
2745
2047
#ifndef UNIV_LOG_DEBUG
2746
2048
        ut_ad(!ibuf_inside() || ibuf_page(space, zip_size, offset, NULL));
2747
2049
#endif
2748
2050
        buf_pool->stat.n_page_gets++;
2749
 
        fold = buf_page_address_fold(space, offset);
2750
2051
loop:
2751
2052
        block = guess;
2752
 
        buf_pool_mutex_enter(buf_pool);
 
2053
        buf_pool_mutex_enter();
2753
2054
 
2754
2055
        if (block) {
2755
2056
                /* If the guess is a compressed page descriptor that
2760
2061
                the guess may be pointing to a buffer pool chunk that
2761
2062
                has been released when resizing the buffer pool. */
2762
2063
 
2763
 
                if (!buf_block_is_uncompressed(buf_pool, block)
 
2064
                if (!buf_block_is_uncompressed(block)
2764
2065
                    || offset != block->page.offset
2765
2066
                    || space != block->page.space
2766
2067
                    || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
2773
2074
        }
2774
2075
 
2775
2076
        if (block == NULL) {
2776
 
                block = (buf_block_t*) buf_page_hash_get_low(
2777
 
                        buf_pool, space, offset, fold);
 
2077
                block = (buf_block_t*) buf_page_hash_get(space, offset);
2778
2078
        }
2779
2079
 
2780
2080
loop2:
2781
 
        if (block && buf_pool_watch_is_sentinel(buf_pool, &block->page)) {
2782
 
                block = NULL;
2783
 
        }
2784
 
 
2785
2081
        if (block == NULL) {
2786
2082
                /* Page not in buf_pool: needs to be read from file */
2787
2083
 
2788
 
                if (mode == BUF_GET_IF_IN_POOL_OR_WATCH) {
2789
 
                        block = (buf_block_t*) buf_pool_watch_set(
2790
 
                                space, offset, fold);
2791
 
 
2792
 
                        if (UNIV_LIKELY_NULL(block)) {
2793
 
 
2794
 
                                goto got_block;
2795
 
                        }
2796
 
                }
2797
 
 
2798
 
                buf_pool_mutex_exit(buf_pool);
2799
 
 
2800
 
                if (mode == BUF_GET_IF_IN_POOL
2801
 
                    || mode == BUF_GET_IF_IN_POOL_OR_WATCH) {
 
2084
                buf_pool_mutex_exit();
 
2085
 
 
2086
                if (mode == BUF_GET_IF_IN_POOL) {
2802
2087
 
2803
2088
                        return(NULL);
2804
2089
                }
2805
2090
 
2806
 
                if (buf_read_page(space, zip_size, offset)) {
2807
 
                        retries = 0;
2808
 
                } else if (retries < BUF_PAGE_READ_MAX_RETRIES) {
2809
 
                        ++retries;
2810
 
                } else {
2811
 
                        fprintf(stderr, "InnoDB: Error: Unable"
2812
 
                                " to read tablespace %lu page no"
2813
 
                                " %lu into the buffer pool after"
2814
 
                                " %lu attempts\n"
2815
 
                                "InnoDB: The most probable cause"
2816
 
                                " of this error may be that the"
2817
 
                                " table has been corrupted.\n"
2818
 
                                "InnoDB: You can try to fix this"
2819
 
                                " problem by using"
2820
 
                                " innodb_force_recovery.\n"
2821
 
                                "InnoDB: Please see reference manual"
2822
 
                                " for more details.\n"
2823
 
                                "InnoDB: Aborting...\n",
2824
 
                                space, offset,
2825
 
                                BUF_PAGE_READ_MAX_RETRIES);
2826
 
 
2827
 
                        ut_error;
2828
 
                }
 
2091
                buf_read_page(space, zip_size, offset);
2829
2092
 
2830
2093
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
2831
2094
                ut_a(++buf_dbg_counter % 37 || buf_validate());
2833
2096
                goto loop;
2834
2097
        }
2835
2098
 
2836
 
got_block:
2837
2099
        ut_ad(page_zip_get_size(&block->page.zip) == zip_size);
2838
2100
 
2839
2101
        must_read = buf_block_get_io_fix(block) == BUF_IO_READ;
2840
2102
 
2841
2103
        if (must_read && mode == BUF_GET_IF_IN_POOL) {
2842
 
 
2843
 
                /* The page is being read to buffer pool,
2844
 
                but we cannot wait around for the read to
2845
 
                complete. */
2846
 
                buf_pool_mutex_exit(buf_pool);
 
2104
                /* The page is only being read to buffer */
 
2105
                buf_pool_mutex_exit();
2847
2106
 
2848
2107
                return(NULL);
2849
2108
        }
2859
2118
        case BUF_BLOCK_ZIP_DIRTY:
2860
2119
                bpage = &block->page;
2861
2120
                /* Protect bpage->buf_fix_count. */
2862
 
                mutex_enter(&buf_pool->zip_mutex);
 
2121
                mutex_enter(&buf_pool_zip_mutex);
2863
2122
 
2864
2123
                if (bpage->buf_fix_count
2865
2124
                    || buf_page_get_io_fix(bpage) != BUF_IO_NONE) {
2866
2125
                        /* This condition often occurs when the buffer
2867
2126
                        is not buffer-fixed, but I/O-fixed by
2868
2127
                        buf_page_init_for_read(). */
2869
 
                        mutex_exit(&buf_pool->zip_mutex);
 
2128
                        mutex_exit(&buf_pool_zip_mutex);
2870
2129
wait_until_unfixed:
2871
2130
                        /* The block is buffer-fixed or I/O-fixed.
2872
2131
                        Try again later. */
2873
 
                        buf_pool_mutex_exit(buf_pool);
 
2132
                        buf_pool_mutex_exit();
2874
2133
                        os_thread_sleep(WAIT_FOR_READ);
2875
 
  
 
2134
 
2876
2135
                        goto loop;
2877
2136
                }
2878
2137
 
2879
2138
                /* Allocate an uncompressed page. */
2880
 
                buf_pool_mutex_exit(buf_pool);
2881
 
                mutex_exit(&buf_pool->zip_mutex);
 
2139
                buf_pool_mutex_exit();
 
2140
                mutex_exit(&buf_pool_zip_mutex);
2882
2141
 
2883
 
                block = buf_LRU_get_free_block(buf_pool, 0);
 
2142
                block = buf_LRU_get_free_block(0);
2884
2143
                ut_a(block);
2885
2144
 
2886
 
                buf_pool_mutex_enter(buf_pool);
 
2145
                buf_pool_mutex_enter();
2887
2146
                mutex_enter(&block->mutex);
2888
2147
 
2889
2148
                {
2890
 
                        buf_page_t*     hash_bpage;
2891
 
 
2892
 
                        hash_bpage = buf_page_hash_get_low(
2893
 
                                buf_pool, space, offset, fold);
 
2149
                        buf_page_t*     hash_bpage
 
2150
                                = buf_page_hash_get(space, offset);
2894
2151
 
2895
2152
                        if (UNIV_UNLIKELY(bpage != hash_bpage)) {
2896
2153
                                /* The buf_pool->page_hash was modified
2897
 
                                while buf_pool->mutex was released.
 
2154
                                while buf_pool_mutex was released.
2898
2155
                                Free the block that was allocated. */
2899
2156
 
2900
2157
                                buf_LRU_block_free_non_file_page(block);
2910
2167
                     || buf_page_get_io_fix(bpage) != BUF_IO_NONE)) {
2911
2168
 
2912
2169
                        /* The block was buffer-fixed or I/O-fixed
2913
 
                        while buf_pool->mutex was not held by this thread.
 
2170
                        while buf_pool_mutex was not held by this thread.
2914
2171
                        Free the block that was allocated and try again.
2915
2172
                        This should be extremely unlikely. */
2916
2173
 
2923
2180
                /* Move the compressed page from bpage to block,
2924
2181
                and uncompress it. */
2925
2182
 
2926
 
                mutex_enter(&buf_pool->zip_mutex);
 
2183
                mutex_enter(&buf_pool_zip_mutex);
2927
2184
 
2928
2185
                buf_relocate(bpage, &block->page);
2929
2186
                buf_block_init_low(block);
2939
2196
                        ut_ad(!block->page.in_flush_list);
2940
2197
                } else {
2941
2198
                        /* Relocate buf_pool->flush_list. */
2942
 
                        buf_flush_relocate_on_flush_list(bpage,
2943
 
                                                         &block->page);
 
2199
                        buf_page_t*     b;
 
2200
 
 
2201
                        b = UT_LIST_GET_PREV(list, &block->page);
 
2202
                        ut_ad(block->page.in_flush_list);
 
2203
                        UT_LIST_REMOVE(list, buf_pool->flush_list,
 
2204
                                       &block->page);
 
2205
 
 
2206
                        if (b) {
 
2207
                                UT_LIST_INSERT_AFTER(
 
2208
                                        list, buf_pool->flush_list, b,
 
2209
                                        &block->page);
 
2210
                        } else {
 
2211
                                UT_LIST_ADD_FIRST(
 
2212
                                        list, buf_pool->flush_list,
 
2213
                                        &block->page);
 
2214
                        }
2944
2215
                }
2945
2216
 
2946
2217
                /* Buffer-fix, I/O-fix, and X-latch the block
2953
2224
 
2954
2225
                block->page.buf_fix_count = 1;
2955
2226
                buf_block_set_io_fix(block, BUF_IO_READ);
2956
 
                rw_lock_x_lock_func(&block->lock, 0, file, line);
2957
 
 
2958
 
                UNIV_MEM_INVALID(bpage, sizeof *bpage);
2959
 
 
 
2227
                rw_lock_x_lock(&block->lock);
2960
2228
                mutex_exit(&block->mutex);
2961
 
                mutex_exit(&buf_pool->zip_mutex);
 
2229
                mutex_exit(&buf_pool_zip_mutex);
2962
2230
                buf_pool->n_pend_unzip++;
2963
2231
 
2964
 
                buf_buddy_free(buf_pool, bpage, sizeof *bpage);
 
2232
                buf_buddy_free(bpage, sizeof *bpage);
2965
2233
 
2966
 
                buf_pool_mutex_exit(buf_pool);
 
2234
                buf_pool_mutex_exit();
2967
2235
 
2968
2236
                /* Decompress the page and apply buffered operations
2969
 
                while not holding buf_pool->mutex or block->mutex. */
 
2237
                while not holding buf_pool_mutex or block->mutex. */
2970
2238
                success = buf_zip_decompress(block, srv_use_checksums);
2971
 
                ut_a(success);
2972
2239
 
2973
 
                if (UNIV_LIKELY(!recv_no_ibuf_operations)) {
 
2240
                if (UNIV_LIKELY(success)) {
2974
2241
                        ibuf_merge_or_delete_for_page(block, space, offset,
2975
2242
                                                      zip_size, TRUE);
2976
2243
                }
2977
2244
 
2978
2245
                /* Unfix and unlatch the block. */
2979
 
                buf_pool_mutex_enter(buf_pool);
 
2246
                buf_pool_mutex_enter();
2980
2247
                mutex_enter(&block->mutex);
2981
2248
                block->page.buf_fix_count--;
2982
2249
                buf_block_set_io_fix(block, BUF_IO_NONE);
2984
2251
                buf_pool->n_pend_unzip--;
2985
2252
                rw_lock_x_unlock(&block->lock);
2986
2253
 
 
2254
                if (UNIV_UNLIKELY(!success)) {
 
2255
 
 
2256
                        buf_pool_mutex_exit();
 
2257
                        return(NULL);
 
2258
                }
 
2259
 
2987
2260
                break;
2988
2261
 
2989
2262
        case BUF_BLOCK_ZIP_FREE:
2998
2271
        ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
2999
2272
 
3000
2273
        mutex_enter(&block->mutex);
3001
 
#if UNIV_WORD_SIZE == 4
3002
 
        /* On 32-bit systems, there is no padding in buf_page_t.  On
3003
 
        other systems, Valgrind could complain about uninitialized pad
3004
 
        bytes. */
3005
2274
        UNIV_MEM_ASSERT_RW(&block->page, sizeof block->page);
3006
 
#endif
3007
 
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
3008
 
        if ((mode == BUF_GET_IF_IN_POOL || mode == BUF_GET_IF_IN_POOL_OR_WATCH)
3009
 
            && ibuf_debug) {
3010
 
                /* Try to evict the block from the buffer pool, to use the
3011
 
                insert buffer (change buffer) as much as possible. */
3012
 
 
3013
 
                if (buf_LRU_free_block(&block->page, TRUE, NULL)
3014
 
                    == BUF_LRU_FREED) {
3015
 
                        mutex_exit(&block->mutex);
3016
 
                        if (mode == BUF_GET_IF_IN_POOL_OR_WATCH) {
3017
 
                                /* Set the watch, as it would have
3018
 
                                been set if the page were not in the
3019
 
                                buffer pool in the first place. */
3020
 
                                block = (buf_block_t*) buf_pool_watch_set(
3021
 
                                        space, offset, fold);
3022
 
 
3023
 
                                if (UNIV_LIKELY_NULL(block)) {
3024
 
 
3025
 
                                        /* The page entered the buffer
3026
 
                                        pool for some reason. Try to
3027
 
                                        evict it again. */
3028
 
                                        goto got_block;
3029
 
                                }
3030
 
                        }
3031
 
                        buf_pool_mutex_exit(buf_pool);
3032
 
                        fprintf(stderr,
3033
 
                                "innodb_change_buffering_debug evict %u %u\n",
3034
 
                                (unsigned) space, (unsigned) offset);
3035
 
                        return(NULL);
3036
 
                } else if (buf_flush_page_try(buf_pool, block)) {
3037
 
                        fprintf(stderr,
3038
 
                                "innodb_change_buffering_debug flush %u %u\n",
3039
 
                                (unsigned) space, (unsigned) offset);
3040
 
                        guess = block;
3041
 
                        goto loop;
3042
 
                }
3043
 
 
3044
 
                /* Failed to evict the page; change it directly */
3045
 
        }
3046
 
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
3047
2275
 
3048
2276
        buf_block_buf_fix_inc(block, file, line);
3049
2277
 
3053
2281
 
3054
2282
        access_time = buf_page_is_accessed(&block->page);
3055
2283
 
3056
 
        buf_pool_mutex_exit(buf_pool);
 
2284
        buf_pool_mutex_exit();
3057
2285
 
3058
2286
        buf_page_set_accessed_make_young(&block->page, access_time);
3059
2287
 
3128
2356
@return TRUE if success */
3129
2357
UNIV_INTERN
3130
2358
ibool
3131
 
buf_page_optimistic_get(
3132
 
/*====================*/
 
2359
buf_page_optimistic_get_func(
 
2360
/*=========================*/
3133
2361
        ulint           rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
3134
2362
        buf_block_t*    block,  /*!< in: guessed buffer block */
3135
2363
        ib_uint64_t     modify_clock,/*!< in: modify clock value if mode is
3138
2366
        ulint           line,   /*!< in: line where called */
3139
2367
        mtr_t*          mtr)    /*!< in: mini-transaction */
3140
2368
{
3141
 
        buf_pool_t*     buf_pool;
3142
2369
        unsigned        access_time;
3143
2370
        ibool           success;
3144
2371
        ulint           fix_type;
3145
2372
 
3146
 
        ut_ad(block);
3147
 
        ut_ad(mtr);
3148
 
        ut_ad(mtr->state == MTR_ACTIVE);
 
2373
        ut_ad(mtr && block);
3149
2374
        ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
3150
2375
 
3151
2376
        mutex_enter(&block->mutex);
3232
2457
        ut_a(ibuf_count_get(buf_block_get_space(block),
3233
2458
                            buf_block_get_page_no(block)) == 0);
3234
2459
#endif
3235
 
        buf_pool = buf_pool_from_block(block);
3236
2460
        buf_pool->stat.n_page_gets++;
3237
2461
 
3238
2462
        return(TRUE);
3254
2478
        ulint           line,   /*!< in: line where called */
3255
2479
        mtr_t*          mtr)    /*!< in: mini-transaction */
3256
2480
{
3257
 
        buf_pool_t*     buf_pool;
3258
2481
        ibool           success;
3259
2482
        ulint           fix_type;
3260
2483
 
3261
2484
        ut_ad(mtr);
3262
 
        ut_ad(mtr->state == MTR_ACTIVE);
3263
2485
        ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
3264
2486
 
3265
2487
        mutex_enter(&block->mutex);
3283
2505
 
3284
2506
        mutex_exit(&block->mutex);
3285
2507
 
3286
 
        buf_pool = buf_pool_from_block(block);
3287
 
 
3288
2508
        if (mode == BUF_MAKE_YOUNG && buf_page_peek_if_too_old(&block->page)) {
3289
 
                buf_pool_mutex_enter(buf_pool);
 
2509
                buf_pool_mutex_enter();
3290
2510
                buf_LRU_make_block_young(&block->page);
3291
 
                buf_pool_mutex_exit(buf_pool);
 
2511
                buf_pool_mutex_exit();
3292
2512
        } else if (!buf_page_is_accessed(&block->page)) {
3293
2513
                /* Above, we do a dirty read on purpose, to avoid
3294
2514
                mutex contention.  The field buf_page_t::access_time
3296
2516
                field must be protected by mutex, however. */
3297
2517
                ulint   time_ms = ut_time_ms();
3298
2518
 
3299
 
                buf_pool_mutex_enter(buf_pool);
 
2519
                buf_pool_mutex_enter();
3300
2520
                buf_page_set_accessed(&block->page, time_ms);
3301
 
                buf_pool_mutex_exit(buf_pool);
 
2521
                buf_pool_mutex_exit();
3302
2522
        }
3303
2523
 
3304
2524
        ut_ad(!ibuf_inside() || (mode == BUF_KEEP_OLD));
3360
2580
        buf_block_t*    block;
3361
2581
        ibool           success;
3362
2582
        ulint           fix_type;
3363
 
        buf_pool_t*     buf_pool = buf_pool_get(space_id, page_no);
3364
 
 
3365
 
        ut_ad(mtr);
3366
 
        ut_ad(mtr->state == MTR_ACTIVE);
3367
 
 
3368
 
        buf_pool_mutex_enter(buf_pool);
3369
 
        block = buf_block_hash_get(buf_pool, space_id, page_no);
3370
 
 
3371
 
        if (!block || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
3372
 
                buf_pool_mutex_exit(buf_pool);
 
2583
 
 
2584
        buf_pool_mutex_enter();
 
2585
        block = buf_block_hash_get(space_id, page_no);
 
2586
 
 
2587
        if (!block) {
 
2588
                buf_pool_mutex_exit();
3373
2589
                return(NULL);
3374
2590
        }
3375
2591
 
3376
 
        ut_ad(!buf_pool_watch_is_sentinel(buf_pool, &block->page));
3377
 
 
3378
2592
        mutex_enter(&block->mutex);
3379
 
        buf_pool_mutex_exit(buf_pool);
 
2593
        buf_pool_mutex_exit();
3380
2594
 
3381
2595
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
3382
2596
        ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
3459
2673
        ulint           space,  /*!< in: space id */
3460
2674
        ulint           offset, /*!< in: offset of the page within space
3461
2675
                                in units of a page */
3462
 
        ulint           fold,   /*!< in: buf_page_address_fold(space,offset) */
3463
2676
        buf_block_t*    block)  /*!< in: block to init */
3464
2677
{
3465
2678
        buf_page_t*     hash_page;
3466
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
3467
2679
 
3468
 
        ut_ad(buf_pool_mutex_own(buf_pool));
 
2680
        ut_ad(buf_pool_mutex_own());
3469
2681
        ut_ad(mutex_own(&(block->mutex)));
3470
2682
        ut_a(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE);
3471
2683
 
3483
2695
 
3484
2696
        buf_block_init_low(block);
3485
2697
 
3486
 
        block->lock_hash_val = lock_rec_hash(space, offset);
3487
 
 
3488
 
        buf_page_init_low(&block->page);
 
2698
        block->lock_hash_val    = lock_rec_hash(space, offset);
3489
2699
 
3490
2700
        /* Insert into the hash table of file pages */
3491
2701
 
3492
 
        hash_page = buf_page_hash_get_low(buf_pool, space, offset, fold);
3493
 
 
3494
 
        if (UNIV_LIKELY(!hash_page)) {
3495
 
        } else if (buf_pool_watch_is_sentinel(buf_pool, hash_page)) {
3496
 
                /* Preserve the reference count. */
3497
 
                ulint   buf_fix_count = hash_page->buf_fix_count;
3498
 
 
3499
 
                ut_a(buf_fix_count > 0);
3500
 
                block->page.buf_fix_count += buf_fix_count;
3501
 
                buf_pool_watch_remove(buf_pool, fold, hash_page);
3502
 
        } else {
 
2702
        hash_page = buf_page_hash_get(space, offset);
 
2703
 
 
2704
        if (UNIV_LIKELY_NULL(hash_page)) {
3503
2705
                fprintf(stderr,
3504
2706
                        "InnoDB: Error: page %lu %lu already found"
3505
2707
                        " in the hash table: %p, %p\n",
3508
2710
                        (const void*) hash_page, (const void*) block);
3509
2711
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
3510
2712
                mutex_exit(&block->mutex);
3511
 
                buf_pool_mutex_exit(buf_pool);
 
2713
                buf_pool_mutex_exit();
3512
2714
                buf_print();
3513
2715
                buf_LRU_print();
3514
2716
                buf_validate();
3517
2719
                ut_error;
3518
2720
        }
3519
2721
 
 
2722
        buf_page_init_low(&block->page);
 
2723
 
3520
2724
        ut_ad(!block->page.in_zip_hash);
3521
2725
        ut_ad(!block->page.in_page_hash);
3522
2726
        ut_d(block->page.in_page_hash = TRUE);
3523
2727
        HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
3524
 
                    fold, &block->page);
 
2728
                    buf_page_address_fold(space, offset), &block->page);
3525
2729
}
3526
2730
 
3527
2731
/********************************************************************//**
3543
2747
        ulint           space,  /*!< in: space id */
3544
2748
        ulint           zip_size,/*!< in: compressed page size, or 0 */
3545
2749
        ibool           unzip,  /*!< in: TRUE=request uncompressed page */
3546
 
        ib_int64_t      tablespace_version,
3547
 
                                /*!< in: prevents reading from a wrong
 
2750
        ib_int64_t      tablespace_version,/*!< in: prevents reading from a wrong
3548
2751
                                version of the tablespace in case we have done
3549
2752
                                DISCARD + IMPORT */
3550
2753
        ulint           offset) /*!< in: page number */
3551
2754
{
3552
2755
        buf_block_t*    block;
3553
 
        buf_page_t*     bpage   = NULL;
3554
 
        buf_page_t*     watch_page;
 
2756
        buf_page_t*     bpage;
3555
2757
        mtr_t           mtr;
3556
 
        ulint           fold;
3557
2758
        ibool           lru     = FALSE;
3558
2759
        void*           data;
3559
 
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
3560
2760
 
3561
2761
        ut_ad(buf_pool);
3562
2762
 
3585
2785
            && UNIV_LIKELY(!recv_recovery_is_on())) {
3586
2786
                block = NULL;
3587
2787
        } else {
3588
 
                block = buf_LRU_get_free_block(buf_pool, 0);
 
2788
                block = buf_LRU_get_free_block(0);
3589
2789
                ut_ad(block);
3590
 
                ut_ad(buf_pool_from_block(block) == buf_pool);
3591
2790
        }
3592
2791
 
3593
 
        fold = buf_page_address_fold(space, offset);
3594
 
 
3595
 
        buf_pool_mutex_enter(buf_pool);
3596
 
 
3597
 
        watch_page = buf_page_hash_get_low(buf_pool, space, offset, fold);
3598
 
        if (watch_page && !buf_pool_watch_is_sentinel(buf_pool, watch_page)) {
 
2792
        buf_pool_mutex_enter();
 
2793
 
 
2794
        if (buf_page_hash_get(space, offset)) {
3599
2795
                /* The page is already in the buffer pool. */
3600
 
                watch_page = NULL;
3601
2796
err_exit:
3602
2797
                if (block) {
3603
2798
                        mutex_enter(&block->mutex);
3621
2816
        if (block) {
3622
2817
                bpage = &block->page;
3623
2818
                mutex_enter(&block->mutex);
3624
 
 
3625
 
                ut_ad(buf_pool_from_bpage(bpage) == buf_pool);
3626
 
 
3627
 
                buf_page_init(space, offset, fold, block);
 
2819
                buf_page_init(space, offset, block);
3628
2820
 
3629
2821
                /* The block must be put to the LRU list, to the old blocks */
3630
2822
                buf_LRU_add_block(bpage, TRUE/* to old blocks */);
3644
2836
                if (UNIV_UNLIKELY(zip_size)) {
3645
2837
                        page_zip_set_size(&block->page.zip, zip_size);
3646
2838
 
3647
 
                        /* buf_pool->mutex may be released and
 
2839
                        /* buf_pool_mutex may be released and
3648
2840
                        reacquired by buf_buddy_alloc().  Thus, we
3649
2841
                        must release block->mutex in order not to
3650
2842
                        break the latching order in the reacquisition
3651
 
                        of buf_pool->mutex.  We also must defer this
 
2843
                        of buf_pool_mutex.  We also must defer this
3652
2844
                        operation until after the block descriptor has
3653
2845
                        been added to buf_pool->LRU and
3654
2846
                        buf_pool->page_hash. */
3655
2847
                        mutex_exit(&block->mutex);
3656
 
                        data = buf_buddy_alloc(buf_pool, zip_size, &lru);
 
2848
                        data = buf_buddy_alloc(zip_size, &lru);
3657
2849
                        mutex_enter(&block->mutex);
3658
 
                        block->page.zip.data = static_cast<unsigned char *>(data);
 
2850
                        block->page.zip.data = data;
3659
2851
 
3660
2852
                        /* To maintain the invariant
3661
2853
                        block->in_unzip_LRU_list
3677
2869
                control block (bpage), in order to avoid the
3678
2870
                invocation of buf_buddy_relocate_block() on
3679
2871
                uninitialized data. */
3680
 
                data = buf_buddy_alloc(buf_pool, zip_size, &lru);
3681
 
                bpage = static_cast<buf_page_struct *>(buf_buddy_alloc(buf_pool, sizeof *bpage, &lru));
3682
 
 
3683
 
                /* Initialize the buf_pool pointer. */
3684
 
                bpage->buf_pool_index = buf_pool_index(buf_pool);
 
2872
                data = buf_buddy_alloc(zip_size, &lru);
 
2873
                bpage = buf_buddy_alloc(sizeof *bpage, &lru);
3685
2874
 
3686
2875
                /* If buf_buddy_alloc() allocated storage from the LRU list,
3687
 
                it released and reacquired buf_pool->mutex.  Thus, we must
 
2876
                it released and reacquired buf_pool_mutex.  Thus, we must
3688
2877
                check the page_hash again, as it may have been modified. */
3689
 
                if (UNIV_UNLIKELY(lru)) {
3690
 
 
3691
 
                        watch_page = buf_page_hash_get_low(
3692
 
                                buf_pool, space, offset, fold);
3693
 
 
3694
 
                        if (watch_page
3695
 
                            && !buf_pool_watch_is_sentinel(buf_pool,
3696
 
                                                           watch_page)) {
3697
 
 
3698
 
                                /* The block was added by some other thread. */
3699
 
                                watch_page = NULL;
3700
 
                                buf_buddy_free(buf_pool, bpage, sizeof *bpage);
3701
 
                                buf_buddy_free(buf_pool, data, zip_size);
3702
 
 
3703
 
                                bpage = NULL;
3704
 
                                goto func_exit;
3705
 
                        }
 
2878
                if (UNIV_UNLIKELY(lru)
 
2879
                    && UNIV_LIKELY_NULL(buf_page_hash_get(space, offset))) {
 
2880
 
 
2881
                        /* The block was added by some other thread. */
 
2882
                        buf_buddy_free(bpage, sizeof *bpage);
 
2883
                        buf_buddy_free(data, zip_size);
 
2884
 
 
2885
                        bpage = NULL;
 
2886
                        goto func_exit;
3706
2887
                }
3707
2888
 
3708
2889
                page_zip_des_init(&bpage->zip);
3709
2890
                page_zip_set_size(&bpage->zip, zip_size);
3710
 
                bpage->zip.data = static_cast<unsigned char *>(data);
 
2891
                bpage->zip.data = data;
3711
2892
 
3712
 
                mutex_enter(&buf_pool->zip_mutex);
 
2893
                mutex_enter(&buf_pool_zip_mutex);
3713
2894
                UNIV_MEM_DESC(bpage->zip.data,
3714
2895
                              page_zip_get_size(&bpage->zip), bpage);
3715
 
 
3716
2896
                buf_page_init_low(bpage);
3717
 
 
3718
2897
                bpage->state    = BUF_BLOCK_ZIP_PAGE;
3719
2898
                bpage->space    = space;
3720
2899
                bpage->offset   = offset;
3721
2900
 
3722
 
 
3723
2901
#ifdef UNIV_DEBUG
3724
2902
                bpage->in_page_hash = FALSE;
3725
2903
                bpage->in_zip_hash = FALSE;
3729
2907
#endif /* UNIV_DEBUG */
3730
2908
 
3731
2909
                ut_d(bpage->in_page_hash = TRUE);
3732
 
 
3733
 
                if (UNIV_LIKELY_NULL(watch_page)) {
3734
 
                        /* Preserve the reference count. */
3735
 
                        ulint   buf_fix_count = watch_page->buf_fix_count;
3736
 
                        ut_a(buf_fix_count > 0);
3737
 
                        bpage->buf_fix_count += buf_fix_count;
3738
 
                        ut_ad(buf_pool_watch_is_sentinel(buf_pool, watch_page));
3739
 
                        buf_pool_watch_remove(buf_pool, fold, watch_page);
3740
 
                }
3741
 
 
3742
 
                HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold,
3743
 
                            bpage);
 
2910
                HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
 
2911
                            buf_page_address_fold(space, offset), bpage);
3744
2912
 
3745
2913
                /* The block must be put to the LRU list, to the old blocks */
3746
2914
                buf_LRU_add_block(bpage, TRUE/* to old blocks */);
3748
2916
 
3749
2917
                buf_page_set_io_fix(bpage, BUF_IO_READ);
3750
2918
 
3751
 
                mutex_exit(&buf_pool->zip_mutex);
 
2919
                mutex_exit(&buf_pool_zip_mutex);
3752
2920
        }
3753
2921
 
3754
2922
        buf_pool->n_pend_reads++;
3755
2923
func_exit:
3756
 
        buf_pool_mutex_exit(buf_pool);
 
2924
        buf_pool_mutex_exit();
3757
2925
 
3758
2926
        if (mode == BUF_READ_IBUF_PAGES_ONLY) {
3759
2927
 
3782
2950
{
3783
2951
        buf_frame_t*    frame;
3784
2952
        buf_block_t*    block;
3785
 
        ulint           fold;
3786
2953
        buf_block_t*    free_block      = NULL;
3787
2954
        ulint           time_ms         = ut_time_ms();
3788
 
        buf_pool_t*     buf_pool        = buf_pool_get(space, offset);
3789
2955
 
3790
2956
        ut_ad(mtr);
3791
 
        ut_ad(mtr->state == MTR_ACTIVE);
3792
2957
        ut_ad(space || !zip_size);
3793
2958
 
3794
 
        free_block = buf_LRU_get_free_block(buf_pool, 0);
3795
 
 
3796
 
        fold = buf_page_address_fold(space, offset);
3797
 
 
3798
 
        buf_pool_mutex_enter(buf_pool);
3799
 
 
3800
 
        block = (buf_block_t*) buf_page_hash_get_low(
3801
 
                buf_pool, space, offset, fold);
3802
 
 
3803
 
        if (block
3804
 
            && buf_page_in_file(&block->page)
3805
 
            && !buf_pool_watch_is_sentinel(buf_pool, &block->page)) {
 
2959
        free_block = buf_LRU_get_free_block(0);
 
2960
 
 
2961
        buf_pool_mutex_enter();
 
2962
 
 
2963
        block = (buf_block_t*) buf_page_hash_get(space, offset);
 
2964
 
 
2965
        if (block && buf_page_in_file(&block->page)) {
3806
2966
#ifdef UNIV_IBUF_COUNT_DEBUG
3807
2967
                ut_a(ibuf_count_get(space, offset) == 0);
3808
2968
#endif
3811
2971
#endif /* UNIV_DEBUG_FILE_ACCESSES */
3812
2972
 
3813
2973
                /* Page can be found in buf_pool */
3814
 
                buf_pool_mutex_exit(buf_pool);
 
2974
                buf_pool_mutex_exit();
3815
2975
 
3816
2976
                buf_block_free(free_block);
3817
2977
 
3832
2992
 
3833
2993
        mutex_enter(&block->mutex);
3834
2994
 
3835
 
        buf_page_init(space, offset, fold, block);
 
2995
        buf_page_init(space, offset, block);
3836
2996
 
3837
2997
        /* The block must be put to the LRU list */
3838
2998
        buf_LRU_add_block(&block->page, FALSE);
3845
3005
                ibool   lru;
3846
3006
 
3847
3007
                /* Prevent race conditions during buf_buddy_alloc(),
3848
 
                which may release and reacquire buf_pool->mutex,
 
3008
                which may release and reacquire buf_pool_mutex,
3849
3009
                by IO-fixing and X-latching the block. */
3850
3010
 
3851
3011
                buf_page_set_io_fix(&block->page, BUF_IO_READ);
3853
3013
 
3854
3014
                page_zip_set_size(&block->page.zip, zip_size);
3855
3015
                mutex_exit(&block->mutex);
3856
 
                /* buf_pool->mutex may be released and reacquired by
 
3016
                /* buf_pool_mutex may be released and reacquired by
3857
3017
                buf_buddy_alloc().  Thus, we must release block->mutex
3858
3018
                in order not to break the latching order in
3859
 
                the reacquisition of buf_pool->mutex.  We also must
 
3019
                the reacquisition of buf_pool_mutex.  We also must
3860
3020
                defer this operation until after the block descriptor
3861
3021
                has been added to buf_pool->LRU and buf_pool->page_hash. */
3862
 
                data = buf_buddy_alloc(buf_pool, zip_size, &lru);
 
3022
                data = buf_buddy_alloc(zip_size, &lru);
3863
3023
                mutex_enter(&block->mutex);
3864
 
                block->page.zip.data = static_cast<unsigned char *>(data);
 
3024
                block->page.zip.data = data;
3865
3025
 
3866
3026
                /* To maintain the invariant
3867
3027
                block->in_unzip_LRU_list
3877
3037
 
3878
3038
        buf_page_set_accessed(&block->page, time_ms);
3879
3039
 
3880
 
        buf_pool_mutex_exit(buf_pool);
 
3040
        buf_pool_mutex_exit();
3881
3041
 
3882
3042
        mtr_memo_push(mtr, block, MTR_MEMO_BUF_FIX);
3883
3043
 
3889
3049
        ibuf_merge_or_delete_for_page(NULL, space, offset, zip_size, TRUE);
3890
3050
 
3891
3051
        /* Flush pages from the end of the LRU list if necessary */
3892
 
        buf_flush_free_margin(buf_pool);
 
3052
        buf_flush_free_margin();
3893
3053
 
3894
3054
        frame = block->frame;
3895
3055
 
3925
3085
        buf_page_t*     bpage)  /*!< in: pointer to the block in question */
3926
3086
{
3927
3087
        enum buf_io_fix io_type;
3928
 
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
3929
3088
        const ibool     uncompressed = (buf_page_get_state(bpage)
3930
3089
                                        == BUF_BLOCK_FILE_PAGE);
3931
3090
 
4061
3220
                }
4062
3221
        }
4063
3222
 
4064
 
        buf_pool_mutex_enter(buf_pool);
 
3223
        buf_pool_mutex_enter();
4065
3224
        mutex_enter(buf_page_get_mutex(bpage));
4066
3225
 
4067
3226
#ifdef UNIV_IBUF_COUNT_DEBUG
4125
3284
#endif /* UNIV_DEBUG */
4126
3285
 
4127
3286
        mutex_exit(buf_page_get_mutex(bpage));
4128
 
        buf_pool_mutex_exit(buf_pool);
4129
 
}
4130
 
 
4131
 
/*********************************************************************//**
4132
 
Asserts that all file pages in the buffer are in a replaceable state.
4133
 
@return TRUE */
4134
 
static
4135
 
ibool
4136
 
buf_all_freed_instance(
4137
 
/*===================*/
4138
 
        buf_pool_t*     buf_pool)       /*!< in: buffer pool instancce */
4139
 
{
4140
 
        ulint           i;
4141
 
        buf_chunk_t*    chunk;
4142
 
 
4143
 
        ut_ad(buf_pool);
4144
 
 
4145
 
        buf_pool_mutex_enter(buf_pool);
4146
 
 
4147
 
        chunk = buf_pool->chunks;
4148
 
 
4149
 
        for (i = buf_pool->n_chunks; i--; chunk++) {
4150
 
 
4151
 
                const buf_block_t* block = buf_chunk_not_freed(chunk);
4152
 
 
4153
 
                if (UNIV_LIKELY_NULL(block)) {
4154
 
                        fprintf(stderr,
4155
 
                                "Page %lu %lu still fixed or dirty\n",
4156
 
                                (ulong) block->page.space,
4157
 
                                (ulong) block->page.offset);
4158
 
                        ut_error;
4159
 
                }
4160
 
        }
4161
 
 
4162
 
        buf_pool_mutex_exit(buf_pool);
4163
 
 
4164
 
        return(TRUE);
4165
 
}
4166
 
 
4167
 
/*********************************************************************//**
4168
 
Invalidates file pages in one buffer pool instance */
4169
 
static
 
3287
        buf_pool_mutex_exit();
 
3288
}
 
3289
 
 
3290
/*********************************************************************//**
 
3291
Invalidates the file pages in the buffer pool when an archive recovery is
 
3292
completed. All the file pages buffered must be in a replaceable state when
 
3293
this function is called: not latched and not modified. */
 
3294
UNIV_INTERN
4170
3295
void
4171
 
buf_pool_invalidate_instance(
4172
 
/*=========================*/
4173
 
        buf_pool_t*     buf_pool)       /*!< in: buffer pool instance */
 
3296
buf_pool_invalidate(void)
 
3297
/*=====================*/
4174
3298
{
4175
3299
        ibool           freed;
4176
 
        int     i;
 
3300
        enum buf_flush  i;
4177
3301
 
4178
 
        buf_pool_mutex_enter(buf_pool);
 
3302
        buf_pool_mutex_enter();
4179
3303
 
4180
3304
        for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
4181
3305
 
4191
3315
                pool invalidation to proceed we must ensure there is NO
4192
3316
                write activity happening. */
4193
3317
                if (buf_pool->n_flush[i] > 0) {
4194
 
                        buf_pool_mutex_exit(buf_pool);
4195
 
                        buf_flush_wait_batch_end(buf_pool, static_cast<buf_flush>(i));
4196
 
                        buf_pool_mutex_enter(buf_pool);
 
3318
                        buf_pool_mutex_exit();
 
3319
                        buf_flush_wait_batch_end(i);
 
3320
                        buf_pool_mutex_enter();
4197
3321
                }
4198
3322
        }
4199
3323
 
4200
 
        buf_pool_mutex_exit(buf_pool);
 
3324
        buf_pool_mutex_exit();
4201
3325
 
4202
 
        ut_ad(buf_all_freed_instance(buf_pool));
 
3326
        ut_ad(buf_all_freed());
4203
3327
 
4204
3328
        freed = TRUE;
4205
3329
 
4206
3330
        while (freed) {
4207
 
                freed = buf_LRU_search_and_free_block(buf_pool, 100);
 
3331
                freed = buf_LRU_search_and_free_block(100);
4208
3332
        }
4209
3333
 
4210
 
        buf_pool_mutex_enter(buf_pool);
 
3334
        buf_pool_mutex_enter();
4211
3335
 
4212
3336
        ut_ad(UT_LIST_GET_LEN(buf_pool->LRU) == 0);
4213
3337
        ut_ad(UT_LIST_GET_LEN(buf_pool->unzip_LRU) == 0);
4218
3342
        buf_pool->LRU_flush_ended = 0;
4219
3343
 
4220
3344
        memset(&buf_pool->stat, 0x00, sizeof(buf_pool->stat));
4221
 
        buf_refresh_io_stats(buf_pool);
4222
 
 
4223
 
        buf_pool_mutex_exit(buf_pool);
4224
 
}
4225
 
 
4226
 
/*********************************************************************//**
4227
 
Invalidates the file pages in the buffer pool when an archive recovery is
4228
 
completed. All the file pages buffered must be in a replaceable state when
4229
 
this function is called: not latched and not modified. */
4230
 
UNIV_INTERN
4231
 
void
4232
 
buf_pool_invalidate(void)
4233
 
/*=====================*/
4234
 
{
4235
 
        ulint   i;
4236
 
 
4237
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4238
 
                buf_pool_invalidate_instance(buf_pool_from_array(i));
4239
 
        }
 
3345
        buf_refresh_io_stats();
 
3346
 
 
3347
        buf_pool_mutex_exit();
4240
3348
}
4241
3349
 
4242
3350
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
4243
3351
/*********************************************************************//**
4244
 
Validates data in one buffer pool instance
 
3352
Validates the buffer buf_pool data structure.
4245
3353
@return TRUE */
4246
 
static
 
3354
UNIV_INTERN
4247
3355
ibool
4248
 
buf_pool_validate_instance(
4249
 
/*=======================*/
4250
 
        buf_pool_t*     buf_pool)       /*!< in: buffer pool instance */
 
3356
buf_validate(void)
 
3357
/*==============*/
4251
3358
{
4252
3359
        buf_page_t*     b;
4253
3360
        buf_chunk_t*    chunk;
4262
3369
 
4263
3370
        ut_ad(buf_pool);
4264
3371
 
4265
 
        buf_pool_mutex_enter(buf_pool);
 
3372
        buf_pool_mutex_enter();
4266
3373
 
4267
3374
        chunk = buf_pool->chunks;
4268
3375
 
4287
3394
                                break;
4288
3395
 
4289
3396
                        case BUF_BLOCK_FILE_PAGE:
4290
 
                                ut_a(buf_page_hash_get(buf_pool,
4291
 
                                                       buf_block_get_space(
 
3397
                                ut_a(buf_page_hash_get(buf_block_get_space(
4292
3398
                                                               block),
4293
3399
                                                       buf_block_get_page_no(
4294
3400
                                                               block))
4335
3441
                                }
4336
3442
 
4337
3443
                                n_lru++;
 
3444
 
 
3445
                                if (block->page.oldest_modification > 0) {
 
3446
                                        n_flush++;
 
3447
                                }
 
3448
 
4338
3449
                                break;
4339
3450
 
4340
3451
                        case BUF_BLOCK_NOT_USED:
4352
3463
                }
4353
3464
        }
4354
3465
 
4355
 
        mutex_enter(&buf_pool->zip_mutex);
 
3466
        mutex_enter(&buf_pool_zip_mutex);
4356
3467
 
4357
3468
        /* Check clean compressed-only blocks. */
4358
3469
 
4373
3484
                        ut_error;
4374
3485
                        break;
4375
3486
                }
4376
 
 
4377
 
                /* It is OK to read oldest_modification here because
4378
 
                we have acquired buf_pool->zip_mutex above which acts
4379
 
                as the 'block->mutex' for these bpages. */
4380
3487
                ut_a(!b->oldest_modification);
4381
 
                ut_a(buf_page_hash_get(buf_pool, b->space, b->offset) == b);
 
3488
                ut_a(buf_page_hash_get(b->space, b->offset) == b);
4382
3489
 
4383
3490
                n_lru++;
4384
3491
                n_zip++;
4385
3492
        }
4386
3493
 
4387
 
        /* Check dirty blocks. */
 
3494
        /* Check dirty compressed-only blocks. */
4388
3495
 
4389
 
        buf_flush_list_mutex_enter(buf_pool);
4390
3496
        for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
4391
3497
             b = UT_LIST_GET_NEXT(list, b)) {
4392
3498
                ut_ad(b->in_flush_list);
4393
 
                ut_a(b->oldest_modification);
4394
 
                n_flush++;
4395
3499
 
4396
3500
                switch (buf_page_get_state(b)) {
4397
3501
                case BUF_BLOCK_ZIP_DIRTY:
 
3502
                        ut_a(b->oldest_modification);
4398
3503
                        n_lru++;
 
3504
                        n_flush++;
4399
3505
                        n_zip++;
4400
3506
                        switch (buf_page_get_io_fix(b)) {
4401
3507
                        case BUF_IO_NONE:
4402
3508
                        case BUF_IO_READ:
4403
3509
                                break;
 
3510
 
4404
3511
                        case BUF_IO_WRITE:
4405
3512
                                switch (buf_page_get_flush_type(b)) {
4406
3513
                                case BUF_FLUSH_LRU:
4430
3537
                        ut_error;
4431
3538
                        break;
4432
3539
                }
4433
 
                ut_a(buf_page_hash_get(buf_pool, b->space, b->offset) == b);
 
3540
                ut_a(buf_page_hash_get(b->space, b->offset) == b);
4434
3541
        }
4435
3542
 
4436
 
        ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == n_flush);
4437
 
 
4438
 
        buf_flush_list_mutex_exit(buf_pool);
4439
 
 
4440
 
        mutex_exit(&buf_pool->zip_mutex);
 
3543
        mutex_exit(&buf_pool_zip_mutex);
4441
3544
 
4442
3545
        if (n_lru + n_free > buf_pool->curr_size + n_zip) {
4443
3546
                fprintf(stderr, "n LRU %lu, n free %lu, pool %lu zip %lu\n",
4453
3556
                        (ulong) n_free);
4454
3557
                ut_error;
4455
3558
        }
 
3559
        ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == n_flush);
4456
3560
 
4457
3561
        ut_a(buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE] == n_single_flush);
4458
3562
        ut_a(buf_pool->n_flush[BUF_FLUSH_LIST] == n_list_flush);
4459
3563
        ut_a(buf_pool->n_flush[BUF_FLUSH_LRU] == n_lru_flush);
4460
3564
 
4461
 
        buf_pool_mutex_exit(buf_pool);
 
3565
        buf_pool_mutex_exit();
4462
3566
 
4463
3567
        ut_a(buf_LRU_validate());
4464
 
        ut_a(buf_flush_validate(buf_pool));
4465
 
 
4466
 
        return(TRUE);
4467
 
}
4468
 
 
4469
 
/*********************************************************************//**
4470
 
Validates the buffer buf_pool data structure.
4471
 
@return TRUE */
4472
 
UNIV_INTERN
4473
 
ibool
4474
 
buf_validate(void)
4475
 
/*==============*/
4476
 
{
4477
 
        ulint   i;
4478
 
 
4479
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4480
 
                buf_pool_t*     buf_pool;
4481
 
 
4482
 
                buf_pool = buf_pool_from_array(i);
4483
 
 
4484
 
                buf_pool_validate_instance(buf_pool);
4485
 
        }
4486
 
        return(TRUE);
4487
 
}
4488
 
 
 
3568
        ut_a(buf_flush_validate());
 
3569
 
 
3570
        return(TRUE);
 
3571
}
4489
3572
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
4490
3573
 
4491
3574
#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
4492
3575
/*********************************************************************//**
4493
 
Prints info of the buffer buf_pool data structure for one instance. */
4494
 
static
 
3576
Prints info of the buffer buf_pool data structure. */
 
3577
UNIV_INTERN
4495
3578
void
4496
 
buf_print_instance(
4497
 
/*===============*/
4498
 
        buf_pool_t*     buf_pool)
 
3579
buf_print(void)
 
3580
/*===========*/
4499
3581
{
4500
 
        index_id_t*     index_ids;
 
3582
        dulint*         index_ids;
4501
3583
        ulint*          counts;
4502
3584
        ulint           size;
4503
3585
        ulint           i;
4504
3586
        ulint           j;
4505
 
        index_id_t      id;
 
3587
        dulint          id;
4506
3588
        ulint           n_found;
4507
3589
        buf_chunk_t*    chunk;
4508
3590
        dict_index_t*   index;
4511
3593
 
4512
3594
        size = buf_pool->curr_size;
4513
3595
 
4514
 
        index_ids = mem_alloc(size * sizeof *index_ids);
 
3596
        index_ids = mem_alloc(sizeof(dulint) * size);
4515
3597
        counts = mem_alloc(sizeof(ulint) * size);
4516
3598
 
4517
 
        buf_pool_mutex_enter(buf_pool);
4518
 
        buf_flush_list_mutex_enter(buf_pool);
 
3599
        buf_pool_mutex_enter();
4519
3600
 
4520
3601
        fprintf(stderr,
4521
3602
                "buf_pool size %lu\n"
4542
3623
                (ulong) buf_pool->stat.n_pages_created,
4543
3624
                (ulong) buf_pool->stat.n_pages_written);
4544
3625
 
4545
 
        buf_flush_list_mutex_exit(buf_pool);
4546
 
 
4547
3626
        /* Count the number of blocks belonging to each index in the buffer */
4548
3627
 
4549
3628
        n_found = 0;
4566
3645
 
4567
3646
                                while (j < n_found) {
4568
3647
 
4569
 
                                        if (index_ids[j] == id) {
 
3648
                                        if (ut_dulint_cmp(index_ids[j],
 
3649
                                                          id) == 0) {
4570
3650
                                                counts[j]++;
4571
3651
 
4572
3652
                                                break;
4583
3663
                }
4584
3664
        }
4585
3665
 
4586
 
        buf_pool_mutex_exit(buf_pool);
 
3666
        buf_pool_mutex_exit();
4587
3667
 
4588
3668
        for (i = 0; i < n_found; i++) {
4589
3669
                index = dict_index_get_if_in_cache(index_ids[i]);
4590
3670
 
4591
3671
                fprintf(stderr,
4592
 
                        "Block count for index %llu in buffer is about %lu",
4593
 
                        (ullint) index_ids[i],
 
3672
                        "Block count for index %lu in buffer is about %lu",
 
3673
                        (ulong) ut_dulint_get_low(index_ids[i]),
4594
3674
                        (ulong) counts[i]);
4595
3675
 
4596
3676
                if (index) {
4604
3684
        mem_free(index_ids);
4605
3685
        mem_free(counts);
4606
3686
 
4607
 
        ut_a(buf_pool_validate_instance(buf_pool));
4608
 
}
4609
 
 
4610
 
/*********************************************************************//**
4611
 
Prints info of the buffer buf_pool data structure. */
4612
 
UNIV_INTERN
4613
 
void
4614
 
buf_print(void)
4615
 
/*===========*/
4616
 
{
4617
 
        ulint   i;
4618
 
 
4619
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4620
 
                buf_pool_t*     buf_pool;
4621
 
 
4622
 
                buf_pool = buf_pool_from_array(i);
4623
 
                buf_print_instance(buf_pool);
4624
 
        }
 
3687
        ut_a(buf_validate());
4625
3688
}
4626
3689
#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
4627
3690
 
4631
3694
@return number of latched pages */
4632
3695
UNIV_INTERN
4633
3696
ulint
4634
 
buf_get_latched_pages_number_instance(
4635
 
/*==================================*/
4636
 
        buf_pool_t*     buf_pool)       /*!< in: buffer pool instance */
 
3697
buf_get_latched_pages_number(void)
 
3698
/*==============================*/
4637
3699
{
 
3700
        buf_chunk_t*    chunk;
4638
3701
        buf_page_t*     b;
4639
3702
        ulint           i;
4640
 
        buf_chunk_t*    chunk;
4641
3703
        ulint           fixed_pages_number = 0;
4642
3704
 
4643
 
        buf_pool_mutex_enter(buf_pool);
 
3705
        buf_pool_mutex_enter();
4644
3706
 
4645
3707
        chunk = buf_pool->chunks;
4646
3708
 
4669
3731
                }
4670
3732
        }
4671
3733
 
4672
 
        mutex_enter(&buf_pool->zip_mutex);
 
3734
        mutex_enter(&buf_pool_zip_mutex);
4673
3735
 
4674
3736
        /* Traverse the lists of clean and dirty compressed-only blocks. */
4675
3737
 
4684
3746
                }
4685
3747
        }
4686
3748
 
4687
 
        buf_flush_list_mutex_enter(buf_pool);
4688
3749
        for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
4689
3750
             b = UT_LIST_GET_NEXT(list, b)) {
4690
3751
                ut_ad(b->in_flush_list);
4710
3771
                }
4711
3772
        }
4712
3773
 
4713
 
        buf_flush_list_mutex_exit(buf_pool);
4714
 
        mutex_exit(&buf_pool->zip_mutex);
4715
 
        buf_pool_mutex_exit(buf_pool);
 
3774
        mutex_exit(&buf_pool_zip_mutex);
 
3775
        buf_pool_mutex_exit();
4716
3776
 
4717
3777
        return(fixed_pages_number);
4718
3778
}
4719
 
 
4720
 
/*********************************************************************//**
4721
 
Returns the number of latched pages in all the buffer pools.
4722
 
@return number of latched pages */
4723
 
UNIV_INTERN
4724
 
ulint
4725
 
buf_get_latched_pages_number(void)
4726
 
/*==============================*/
4727
 
{
4728
 
        ulint   i;
4729
 
        ulint   total_latched_pages = 0;
4730
 
 
4731
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4732
 
                buf_pool_t*     buf_pool;
4733
 
 
4734
 
                buf_pool = buf_pool_from_array(i);
4735
 
 
4736
 
                total_latched_pages += buf_get_latched_pages_number_instance(
4737
 
                        buf_pool);
4738
 
        }
4739
 
 
4740
 
        return(total_latched_pages);
4741
 
}
4742
 
 
4743
3779
#endif /* UNIV_DEBUG */
4744
3780
 
4745
3781
/*********************************************************************//**
4750
3786
buf_get_n_pending_ios(void)
4751
3787
/*=======================*/
4752
3788
{
4753
 
        ulint   i;
4754
 
        ulint   pend_ios = 0;
4755
 
 
4756
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4757
 
                buf_pool_t*     buf_pool;
4758
 
 
4759
 
                buf_pool = buf_pool_from_array(i);
4760
 
 
4761
 
                pend_ios +=
4762
 
                        buf_pool->n_pend_reads
4763
 
                        + buf_pool->n_flush[BUF_FLUSH_LRU]
4764
 
                        + buf_pool->n_flush[BUF_FLUSH_LIST]
4765
 
                        + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE];
4766
 
        }
4767
 
 
4768
 
        return(pend_ios);
 
3789
        return(buf_pool->n_pend_reads
 
3790
               + buf_pool->n_flush[BUF_FLUSH_LRU]
 
3791
               + buf_pool->n_flush[BUF_FLUSH_LIST]
 
3792
               + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]);
4769
3793
}
4770
3794
 
4771
3795
/*********************************************************************//**
4777
3801
buf_get_modified_ratio_pct(void)
4778
3802
/*============================*/
4779
3803
{
4780
 
        ulint           ratio;
4781
 
        ulint           lru_len = 0;
4782
 
        ulint           free_len = 0;
4783
 
        ulint           flush_list_len = 0;
4784
 
 
4785
 
        buf_get_total_list_len(&lru_len, &free_len, &flush_list_len);
4786
 
 
4787
 
        ratio = (100 * flush_list_len) / (1 + lru_len + free_len);
4788
 
  
 
3804
        ulint   ratio;
 
3805
 
 
3806
        buf_pool_mutex_enter();
 
3807
 
 
3808
        ratio = (100 * UT_LIST_GET_LEN(buf_pool->flush_list))
 
3809
                / (1 + UT_LIST_GET_LEN(buf_pool->LRU)
 
3810
                   + UT_LIST_GET_LEN(buf_pool->free));
 
3811
 
4789
3812
        /* 1 + is there to avoid division by zero */
4790
3813
 
 
3814
        buf_pool_mutex_exit();
 
3815
 
4791
3816
        return(ratio);
4792
3817
}
4793
3818
 
4794
3819
/*********************************************************************//**
4795
3820
Prints info of the buffer i/o. */
4796
 
static
 
3821
UNIV_INTERN
4797
3822
void
4798
 
buf_print_io_instance(
4799
 
/*==================*/
4800
 
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
4801
 
        FILE*           file)           /*!< in/out: buffer where to print */
 
3823
buf_print_io(
 
3824
/*=========*/
 
3825
        FILE*   file)   /*!< in/out: buffer where to print */
4802
3826
{
4803
3827
        time_t  current_time;
4804
3828
        double  time_elapsed;
4806
3830
 
4807
3831
        ut_ad(buf_pool);
4808
3832
 
4809
 
        buf_pool_mutex_enter(buf_pool);
4810
 
        buf_flush_list_mutex_enter(buf_pool);
 
3833
        buf_pool_mutex_enter();
4811
3834
 
4812
3835
        fprintf(file,
4813
3836
                "Buffer pool size   %lu\n"
4829
3852
                + buf_pool->init_flush[BUF_FLUSH_LIST],
4830
3853
                (ulong) buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]);
4831
3854
 
4832
 
        buf_flush_list_mutex_exit(buf_pool);
4833
 
 
4834
3855
        current_time = time(NULL);
4835
3856
        time_elapsed = 0.001 + difftime(current_time,
4836
3857
                                        buf_pool->last_printout_time);
4861
3882
                 - buf_pool->old_stat.n_pages_written)
4862
3883
                / time_elapsed);
4863
3884
 
4864
 
        n_gets_diff = buf_pool->stat.n_page_gets
4865
 
                    - buf_pool->old_stat.n_page_gets;
 
3885
        n_gets_diff = buf_pool->stat.n_page_gets - buf_pool->old_stat.n_page_gets;
4866
3886
 
4867
3887
        if (n_gets_diff) {
4868
3888
                fprintf(file,
4901
3921
        fprintf(file,
4902
3922
                "LRU len: %lu, unzip_LRU len: %lu\n"
4903
3923
                "I/O sum[%lu]:cur[%lu], unzip sum[%lu]:cur[%lu]\n",
4904
 
                static_cast<ulint>(UT_LIST_GET_LEN(buf_pool->LRU)),
4905
 
                static_cast<ulint>(UT_LIST_GET_LEN(buf_pool->unzip_LRU)),
 
3924
                UT_LIST_GET_LEN(buf_pool->LRU),
 
3925
                UT_LIST_GET_LEN(buf_pool->unzip_LRU),
4906
3926
                buf_LRU_stat_sum.io, buf_LRU_stat_cur.io,
4907
3927
                buf_LRU_stat_sum.unzip, buf_LRU_stat_cur.unzip);
4908
3928
 
4909
 
        buf_refresh_io_stats(buf_pool);
4910
 
        buf_pool_mutex_exit(buf_pool);
4911
 
}
4912
 
 
4913
 
/*********************************************************************//**
4914
 
Prints info of the buffer i/o. */
4915
 
UNIV_INTERN
4916
 
void
4917
 
buf_print_io(
4918
 
/*=========*/
4919
 
        FILE*   file)   /*!< in/out: buffer where to print */
4920
 
{
4921
 
        ulint   i;
4922
 
 
4923
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4924
 
                buf_pool_t*     buf_pool;
4925
 
 
4926
 
                buf_pool = buf_pool_from_array(i);
4927
 
                buf_print_io_instance(buf_pool, file);
4928
 
        }
 
3929
        buf_refresh_io_stats();
 
3930
        buf_pool_mutex_exit();
4929
3931
}
4930
3932
 
4931
3933
/**********************************************************************//**
4932
3934
Refreshes the statistics used to print per-second averages. */
4933
3935
UNIV_INTERN
4934
3936
void
4935
 
buf_refresh_io_stats(
4936
 
/*=================*/
4937
 
        buf_pool_t*     buf_pool)       /*!< in: buffer pool instance */
 
3937
buf_refresh_io_stats(void)
 
3938
/*======================*/
4938
3939
{
4939
 
        buf_pool->last_printout_time = ut_time();
 
3940
        buf_pool->last_printout_time = time(NULL);
4940
3941
        buf_pool->old_stat = buf_pool->stat;
4941
3942
}
4942
3943
 
4943
 
/**********************************************************************//**
4944
 
Refreshes the statistics used to print per-second averages. */
4945
 
UNIV_INTERN
4946
 
void
4947
 
buf_refresh_io_stats_all(void)
4948
 
/*==========================*/
4949
 
{
4950
 
        ulint           i;
4951
 
 
4952
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4953
 
                buf_pool_t*     buf_pool;
4954
 
 
4955
 
                buf_pool = buf_pool_from_array(i);
4956
 
 
4957
 
                buf_refresh_io_stats(buf_pool);
4958
 
        }
4959
 
}
4960
 
 
4961
 
/**********************************************************************//**
4962
 
Check if all pages in all buffer pools are in a replacable state.
4963
 
@return FALSE if not */
 
3944
/*********************************************************************//**
 
3945
Asserts that all file pages in the buffer are in a replaceable state.
 
3946
@return TRUE */
4964
3947
UNIV_INTERN
4965
3948
ibool
4966
3949
buf_all_freed(void)
4967
3950
/*===============*/
4968
3951
{
4969
 
        ulint   i;
4970
 
 
4971
 
        for (i = 0; i < srv_buf_pool_instances; i++) {
4972
 
                buf_pool_t*     buf_pool;
4973
 
 
4974
 
                buf_pool = buf_pool_from_array(i);
4975
 
 
4976
 
                if (!buf_all_freed_instance(buf_pool)) {
4977
 
                        return(FALSE);
 
3952
        buf_chunk_t*    chunk;
 
3953
        ulint           i;
 
3954
 
 
3955
        ut_ad(buf_pool);
 
3956
 
 
3957
        buf_pool_mutex_enter();
 
3958
 
 
3959
        chunk = buf_pool->chunks;
 
3960
 
 
3961
        for (i = buf_pool->n_chunks; i--; chunk++) {
 
3962
 
 
3963
                const buf_block_t* block = buf_chunk_not_freed(chunk);
 
3964
 
 
3965
                if (UNIV_LIKELY_NULL(block)) {
 
3966
                        fprintf(stderr,
 
3967
                                "Page %lu %lu still fixed or dirty\n",
 
3968
                                (ulong) block->page.space,
 
3969
                                (ulong) block->page.offset);
 
3970
                        ut_error;
4978
3971
                }
4979
 
        }
 
3972
        }
 
3973
 
 
3974
        buf_pool_mutex_exit();
4980
3975
 
4981
3976
        return(TRUE);
4982
3977
}
4983
 
  
 
3978
 
4984
3979
/*********************************************************************//**
4985
3980
Checks that there currently are no pending i/o-operations for the buffer
4986
3981
pool.
4990
3985
buf_pool_check_no_pending_io(void)
4991
3986
/*==============================*/
4992
3987
{
4993
 
        ulint           i;
4994
 
        ibool           ret = TRUE;
4995
 
 
4996
 
        buf_pool_mutex_enter_all();
4997
 
 
4998
 
        for (i = 0; i < srv_buf_pool_instances && ret; i++) {
4999
 
                const buf_pool_t*       buf_pool;
5000
 
 
5001
 
                buf_pool = buf_pool_from_array(i);
5002
 
 
5003
 
                if (buf_pool->n_pend_reads
5004
 
                    + buf_pool->n_flush[BUF_FLUSH_LRU]
5005
 
                    + buf_pool->n_flush[BUF_FLUSH_LIST]
5006
 
                    + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {
5007
 
 
5008
 
                        ret = FALSE;
5009
 
                }
 
3988
        ibool   ret;
 
3989
 
 
3990
        buf_pool_mutex_enter();
 
3991
 
 
3992
        if (buf_pool->n_pend_reads + buf_pool->n_flush[BUF_FLUSH_LRU]
 
3993
            + buf_pool->n_flush[BUF_FLUSH_LIST]
 
3994
            + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {
 
3995
                ret = FALSE;
 
3996
        } else {
 
3997
                ret = TRUE;
5010
3998
        }
5011
3999
 
5012
 
        buf_pool_mutex_exit_all();
 
4000
        buf_pool_mutex_exit();
5013
4001
 
5014
4002
        return(ret);
5015
4003
}
5016
4004
 
5017
 
#if 0
5018
 
Code currently not used
5019
4005
/*********************************************************************//**
5020
4006
Gets the current length of the free list of buffer blocks.
5021
4007
@return length of the free list */
5026
4012
{
5027
4013
        ulint   len;
5028
4014
 
5029
 
        buf_pool_mutex_enter(buf_pool);
 
4015
        buf_pool_mutex_enter();
5030
4016
 
5031
4017
        len = UT_LIST_GET_LEN(buf_pool->free);
5032
4018
 
5033
 
        buf_pool_mutex_exit(buf_pool);
 
4019
        buf_pool_mutex_exit();
5034
4020
 
5035
4021
        return(len);
5036
4022
}
5037
 
#endif
5038
 
 
5039
4023
#else /* !UNIV_HOTBACKUP */
5040
4024
/********************************************************************//**
5041
4025
Inits a page to the buffer buf_pool, for use in ibbackup --restore. */