~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/srv/srv0srv.c

  • Committer: Lee Bieber
  • Date: 2010-11-07 19:34:48 UTC
  • mfrom: (1910.1.2 build)
  • Revision ID: kalebral@gmail.com-20101107193448-64kdu912qej354sh
Merge Stewart - including adapting and expanding the "differences from mysql" page from the wiki.
Merge Stewart - fix bug 668143: drizzleslap with --commit runs second iteration data load in a transaction

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, 2009 Google Inc.
5
 
Copyright (C) 2009, Percona Inc.
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
4
Copyright (c) 2008, 2009 Google Inc.
6
5
 
7
6
Portions of this file contain modifications contributed and copyrighted by
8
7
Google, Inc. Those modifications are gratefully acknowledged and are described
10
9
incorporated with their permission, and subject to the conditions contained in
11
10
the file COPYING.Google.
12
11
 
13
 
Portions of this file contain modifications contributed and copyrighted
14
 
by Percona Inc.. Those modifications are
15
 
gratefully acknowledged and are described briefly in the InnoDB
16
 
documentation. The contributions by Percona Inc. are incorporated with
17
 
their permission, and subject to the conditions contained in the file
18
 
COPYING.Percona.
19
 
 
20
12
This program is free software; you can redistribute it and/or modify it under
21
13
the terms of the GNU General Public License as published by the Free Software
22
14
Foundation; version 2 of the License.
30
22
St, Fifth Floor, Boston, MA 02110-1301 USA
31
23
 
32
24
*****************************************************************************/
 
25
/***********************************************************************
 
26
 
 
27
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
28
Copyright (c) 2009, Percona Inc.
 
29
 
 
30
Portions of this file contain modifications contributed and copyrighted
 
31
by Percona Inc.. Those modifications are
 
32
gratefully acknowledged and are described briefly in the InnoDB
 
33
documentation. The contributions by Percona Inc. are incorporated with
 
34
their permission, and subject to the conditions contained in the file
 
35
COPYING.Percona.
 
36
 
 
37
This program is free software; you can redistribute it and/or modify it
 
38
under the terms of the GNU General Public License as published by the
 
39
Free Software Foundation; version 2 of the License.
 
40
 
 
41
This program is distributed in the hope that it will be useful, but
 
42
WITHOUT ANY WARRANTY; without even the implied warranty of
 
43
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 
44
Public License for more details.
 
45
 
 
46
You should have received a copy of the GNU General Public License along
 
47
with this program; if not, write to the Free Software Foundation, Inc.,
 
48
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
49
 
 
50
***********************************************************************/
33
51
 
34
52
/**************************************************//**
35
53
@file srv/srv0srv.c
68
86
#include "sync0sync.h"
69
87
#include "thr0loc.h"
70
88
#include "que0que.h"
 
89
#include "srv0que.h"
71
90
#include "log0recv.h"
72
91
#include "pars0pars.h"
73
92
#include "usr0sess.h"
100
119
in microseconds, in order to reduce the lagging of the purge thread. */
101
120
UNIV_INTERN ulint       srv_dml_needed_delay = 0;
102
121
 
103
 
UNIV_INTERN ibool       srv_lock_timeout_active = FALSE;
104
 
UNIV_INTERN ibool       srv_monitor_active = FALSE;
 
122
UNIV_INTERN ibool       srv_lock_timeout_and_monitor_active = FALSE;
105
123
UNIV_INTERN ibool       srv_error_monitor_active = FALSE;
106
124
 
107
125
UNIV_INTERN const char* srv_main_thread_op_info = "";
108
126
 
 
127
/** Prefix used by MySQL to indicate pre-5.1 table name encoding */
 
128
UNIV_INTERN const char  srv_mysql50_table_name_prefix[9] = "#mysql50#";
 
129
 
109
130
/* Server parameters which are read from the initfile */
110
131
 
111
132
/* The following three are dir paths which are catenated before file
124
145
/** Whether to check file format during startup.  A value of
125
146
DICT_TF_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
126
147
set it to the highest format we support. */
127
 
UNIV_INTERN ulint       srv_max_file_format_at_startup = DICT_TF_FORMAT_MAX;
 
148
UNIV_INTERN ulint       srv_check_file_format_at_startup = DICT_TF_FORMAT_MAX;
128
149
 
129
150
#if DICT_TF_FORMAT_51
130
151
# error "DICT_TF_FORMAT_51 must be 0!"
133
154
on duplicate key checking and foreign key checking */
134
155
UNIV_INTERN ibool       srv_locks_unsafe_for_binlog = FALSE;
135
156
 
136
 
/* If this flag is TRUE, then we will use the native aio of the
137
 
OS (provided we compiled Innobase with it in), otherwise we will
138
 
use simulated aio we build below with threads.
139
 
Currently we support native aio on windows and linux */
140
 
UNIV_INTERN my_bool     srv_use_native_aio = TRUE;
141
 
 
142
 
#ifdef __WIN__
143
 
/* Windows native condition variables. We use runtime loading / function
144
 
pointers, because they are not available on Windows Server 2003 and
145
 
Windows XP/2000.
146
 
 
147
 
We use condition for events on Windows if possible, even if os_event
148
 
resembles Windows kernel event object well API-wise. The reason is
149
 
performance, kernel objects are heavyweights and WaitForSingleObject() is a
150
 
performance killer causing calling thread to context switch. Besides, Innodb
151
 
is preallocating large number (often millions) of os_events. With kernel event
152
 
objects it takes a big chunk out of non-paged pool, which is better suited
153
 
for tasks like IO than for storing idle event objects. */
154
 
UNIV_INTERN ibool       srv_use_native_conditions = FALSE;
155
 
#endif /* __WIN__ */
156
 
 
157
157
UNIV_INTERN ulint       srv_n_data_files = 0;
158
158
UNIV_INTERN char**      srv_data_file_names = NULL;
159
159
/* size in database pages */
188
188
the checkpoints. */
189
189
UNIV_INTERN bool        srv_adaptive_flushing   = TRUE;
190
190
 
191
 
/** Maximum number of times allowed to conditionally acquire
192
 
mutex before switching to blocking wait on the mutex */
193
 
#define MAX_MUTEX_NOWAIT        20
194
 
 
195
 
/** Check whether the number of failed nonblocking mutex
196
 
acquisition attempts exceeds maximum allowed value. If so,
197
 
srv_printf_innodb_monitor() will request mutex acquisition
198
 
with mutex_enter(), which will wait until it gets the mutex. */
199
 
#define MUTEX_NOWAIT(mutex_skipped)     ((mutex_skipped) < MAX_MUTEX_NOWAIT)
200
 
 
201
 
/** The sort order table of the MySQL latin1_swedish_ci character set
 
191
/* The sort order table of the MySQL latin1_swedish_ci character set
202
192
collation */
203
193
#if defined(BUILD_DRIZZLE)
204
 
const byte      srv_latin1_ordering[256]        /* The sort order table of the latin1
 
194
UNIV_INTERN const byte  srv_latin1_ordering[256]        /* The sort order table of the latin1
205
195
                                        character set. The following table is
206
196
                                        the MySQL order as of Feb 10th, 2002 */
207
197
= {
247
237
UNIV_INTERN my_bool     srv_use_sys_malloc      = TRUE;
248
238
/* requested size in kilobytes */
249
239
UNIV_INTERN ulint       srv_buf_pool_size       = ULINT_MAX;
250
 
/* requested number of buffer pool instances */
251
 
UNIV_INTERN ulint       srv_buf_pool_instances  = 1;
252
240
/* previously requested size */
253
241
UNIV_INTERN ulint       srv_buf_pool_old_size;
254
242
/* current size in kilobytes */
300
288
 
301
289
UNIV_INTERN ulong       srv_max_buf_pool_modified_pct   = 75;
302
290
 
303
 
/* the number of purge threads to use from the worker pool (currently 0 or 1).*/
304
 
UNIV_INTERN ulong srv_n_purge_threads = 0;
305
 
 
306
 
/* the number of records to purge in one batch */
307
 
UNIV_INTERN ulong srv_purge_batch_size = 20;
308
 
 
309
291
/* variable counts amount of data read in total (in bytes) */
310
292
UNIV_INTERN ulint srv_data_read = 0;
311
293
 
460
442
UNIV_INTERN ib_int64_t  srv_n_lock_wait_time            = 0;
461
443
UNIV_INTERN ulint               srv_n_lock_max_wait_time        = 0;
462
444
 
463
 
UNIV_INTERN ulint               srv_truncated_status_writes     = 0;
464
445
 
465
446
/*
466
447
  Set the following to 0 if you want InnoDB to write messages on
484
465
 
485
466
/* Mutex for locking srv_monitor_file */
486
467
UNIV_INTERN mutex_t     srv_monitor_file_mutex;
487
 
 
488
 
#ifdef UNIV_PFS_MUTEX
489
 
/* Key to register kernel_mutex with performance schema */
490
 
UNIV_INTERN mysql_pfs_key_t     kernel_mutex_key;
491
 
/* Key to protect writing the commit_id to the sys header */
492
 
UNIV_INTERN mysql_pfs_key_t     commit_id_mutex_key;
493
 
/* Key to register srv_innodb_monitor_mutex with performance schema */
494
 
UNIV_INTERN mysql_pfs_key_t     srv_innodb_monitor_mutex_key;
495
 
/* Key to register srv_monitor_file_mutex with performance schema */
496
 
UNIV_INTERN mysql_pfs_key_t     srv_monitor_file_mutex_key;
497
 
/* Key to register srv_dict_tmpfile_mutex with performance schema */
498
 
UNIV_INTERN mysql_pfs_key_t     srv_dict_tmpfile_mutex_key;
499
 
/* Key to register the mutex with performance schema */
500
 
UNIV_INTERN mysql_pfs_key_t     srv_misc_tmpfile_mutex_key;
501
 
#endif /* UNIV_PFS_MUTEX */
502
 
 
503
468
/* Temporary file for innodb monitor output */
504
469
UNIV_INTERN FILE*       srv_monitor_file;
505
470
/* Mutex for locking srv_dict_tmpfile.
733
698
/* Table for MySQL threads where they will be suspended to wait for locks */
734
699
UNIV_INTERN srv_slot_t* srv_mysql_table = NULL;
735
700
 
736
 
UNIV_INTERN os_event_t  srv_timeout_event;
737
 
 
738
 
UNIV_INTERN os_event_t  srv_monitor_event;
739
 
 
740
 
UNIV_INTERN os_event_t  srv_error_event;
741
 
 
742
701
UNIV_INTERN os_event_t  srv_lock_timeout_thread_event;
743
702
 
744
703
UNIV_INTERN srv_sys_t*  srv_sys = NULL;
748
707
UNIV_INTERN byte        srv_pad1[64];
749
708
/* mutex protecting the server, trx structs, query threads, and lock table */
750
709
UNIV_INTERN mutex_t*    kernel_mutex_temp;
751
 
/* mutex protecting the sys header for writing the commit id */
752
 
UNIV_INTERN mutex_t*    commit_id_mutex_temp;
753
 
 
754
710
/* padding to prevent other memory update hotspots from residing on
755
711
the same memory cache line */
756
712
UNIV_INTERN byte        srv_pad2[64];
769
725
static ulint    srv_meter_foreground[SRV_MASTER + 1];
770
726
#endif
771
727
 
 
728
/* The following values give info about the activity going on in
 
729
the database. They are protected by the server mutex. The arrays
 
730
are indexed by the type of the thread. */
 
731
 
 
732
UNIV_INTERN ulint       srv_n_threads_active[SRV_MASTER + 1];
 
733
UNIV_INTERN ulint       srv_n_threads[SRV_MASTER + 1];
 
734
 
772
735
/***********************************************************************
773
736
Prints counters for work done by srv_master_thread. */
774
737
static
786
749
                      srv_log_writes_and_flush);
787
750
}
788
751
 
789
 
/* The following values give info about the activity going on in
790
 
the database. They are protected by the server mutex. The arrays
791
 
are indexed by the type of the thread. */
792
 
 
793
 
UNIV_INTERN ulint       srv_n_threads_active[SRV_MASTER + 1];
794
 
UNIV_INTERN ulint       srv_n_threads[SRV_MASTER + 1];
795
 
 
796
752
/*********************************************************************//**
797
753
Sets the info describing an i/o thread current state. */
798
754
UNIV_INTERN
912
868
 
913
869
        slot = srv_table_get_nth_slot(slot_no);
914
870
 
915
 
        type = static_cast<srv_thread_type>(slot->type);
 
871
        type = slot->type;
916
872
 
917
873
        ut_ad(type >= SRV_WORKER);
918
874
        ut_ad(type <= SRV_MASTER);
955
911
 
956
912
                slot = srv_table_get_nth_slot(i);
957
913
 
958
 
                if (slot->in_use &&
959
 
                    (static_cast<srv_thread_type>(slot->type) == type) &&
960
 
                    slot->suspended) {
 
914
                if (slot->in_use && slot->type == type && slot->suspended) {
961
915
 
962
916
                        slot->suspended = FALSE;
963
917
 
1002
956
 
1003
957
        slot = srv_table_get_nth_slot(slot_no);
1004
958
 
1005
 
        type = static_cast<srv_thread_type>(slot->type);
 
959
        type = slot->type;
1006
960
 
1007
961
        ut_ad(type >= SRV_WORKER);
1008
962
        ut_ad(type <= SRV_MASTER);
1023
977
        srv_slot_t*             slot;
1024
978
        ulint                   i;
1025
979
 
1026
 
        srv_sys = static_cast<srv_sys_t *>(mem_alloc(sizeof(srv_sys_t)));
1027
 
 
1028
 
        kernel_mutex_temp = static_cast<ib_mutex_t *>(mem_alloc(sizeof(mutex_t)));
1029
 
        mutex_create(kernel_mutex_key, &kernel_mutex, SYNC_KERNEL);
1030
 
 
1031
 
        commit_id_mutex_temp = static_cast<ib_mutex_t *>(mem_alloc(sizeof(mutex_t)));
1032
 
        mutex_create(commit_id_mutex_key, &commit_id_mutex, SYNC_COMMIT_ID_LOCK);
1033
 
 
1034
 
        mutex_create(srv_innodb_monitor_mutex_key,
1035
 
                     &srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
1036
 
 
1037
 
        srv_sys->threads = static_cast<srv_table_t *>(mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t)));
 
980
        srv_sys = mem_alloc(sizeof(srv_sys_t));
 
981
 
 
982
        kernel_mutex_temp = mem_alloc(sizeof(mutex_t));
 
983
        mutex_create(&kernel_mutex, SYNC_KERNEL);
 
984
 
 
985
        mutex_create(&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
 
986
 
 
987
        srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1038
988
 
1039
989
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1040
990
                slot = srv_table_get_nth_slot(i);
1044
994
                ut_a(slot->event);
1045
995
        }
1046
996
 
1047
 
        srv_mysql_table = static_cast<srv_slot_t *>(mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t)));
 
997
        srv_mysql_table = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1048
998
 
1049
999
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1050
1000
                slot = srv_mysql_table + i;
1054
1004
                ut_a(slot->event);
1055
1005
        }
1056
1006
 
1057
 
        srv_error_event = os_event_create(NULL);
1058
 
 
1059
 
        srv_timeout_event = os_event_create(NULL);
1060
 
 
1061
 
        srv_monitor_event = os_event_create(NULL);
1062
 
 
1063
1007
        srv_lock_timeout_thread_event = os_event_create(NULL);
1064
1008
 
1065
1009
        for (i = 0; i < SRV_MASTER + 1; i++) {
1086
1030
 
1087
1031
        UT_LIST_INIT(srv_conc_queue);
1088
1032
 
1089
 
        srv_conc_slots = static_cast<srv_conc_slot_t *>(mem_alloc(OS_THREAD_MAX_N * sizeof(srv_conc_slot_t)));
 
1033
        srv_conc_slots = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_conc_slot_t));
1090
1034
 
1091
1035
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1092
1036
                conc_slot = srv_conc_slots + i;
1119
1063
        mem_free(srv_mysql_table);
1120
1064
        srv_mysql_table = NULL;
1121
1065
 
1122
 
        mem_free(commit_id_mutex_temp);
1123
 
        commit_id_mutex_temp = NULL;
1124
 
 
1125
1066
        trx_i_s_cache_free(trx_i_s_cache);
1126
1067
}
1127
1068
 
1642
1583
                row_mysql_unfreeze_data_dictionary(trx);
1643
1584
                break;
1644
1585
        case RW_X_LATCH:
1645
 
                /* There should never be a lock wait when the
1646
 
                dictionary latch is reserved in X mode.  Dictionary
1647
 
                transactions should only acquire locks on dictionary
1648
 
                tables, not other tables. All access to dictionary
1649
 
                tables should be covered by dictionary
1650
 
                transactions. */
1651
 
                ut_print_timestamp(stderr);
1652
 
                fputs("  InnoDB: Error: dict X latch held in "
1653
 
                      "srv_suspend_mysql_thread\n", stderr);
1654
 
                /* This should never occur. This incorrect handling
1655
 
                was added in the early development of
1656
 
                ha_innobase::add_index() in InnoDB Plugin 1.0. */
1657
1586
                /* Release fast index creation latch */
1658
1587
                row_mysql_unlock_data_dictionary(trx);
1659
1588
                break;
1673
1602
                row_mysql_freeze_data_dictionary(trx);
1674
1603
                break;
1675
1604
        case RW_X_LATCH:
1676
 
                /* This should never occur. This incorrect handling
1677
 
                was added in the early development of
1678
 
                ha_innobase::add_index() in InnoDB Plugin 1.0. */
1679
1605
                row_mysql_lock_data_dictionary(trx);
1680
1606
                break;
1681
1607
        }
1712
1638
                    start_time != -1 && finish_time != -1) {
1713
1639
                        srv_n_lock_max_wait_time = diff_time;
1714
1640
                }
1715
 
 
1716
 
                /* Record the lock wait time for this thread */
1717
 
                thd_set_lock_wait_time(trx->mysql_thd, diff_time);
1718
1641
        }
1719
1642
 
1720
1643
        if (trx->was_chosen_as_deadlock_victim) {
1736
1659
 
1737
1660
                trx->error_state = DB_LOCK_WAIT_TIMEOUT;
1738
1661
        }
1739
 
 
1740
 
        if (trx_is_interrupted(trx)) {
1741
 
 
1742
 
                trx->error_state = DB_INTERRUPTED;
1743
 
        }
1744
1662
}
1745
1663
 
1746
1664
/********************************************************************//**
1792
1710
 
1793
1711
        log_refresh_stats();
1794
1712
 
1795
 
        buf_refresh_io_stats_all();
 
1713
        buf_refresh_io_stats();
1796
1714
 
1797
1715
        srv_n_rows_inserted_old = srv_n_rows_inserted;
1798
1716
        srv_n_rows_updated_old = srv_n_rows_updated;
1803
1721
}
1804
1722
 
1805
1723
/******************************************************************//**
1806
 
Outputs to a file the output of the InnoDB Monitor.
1807
 
@return FALSE if not all information printed
1808
 
due to failure to obtain necessary mutex */
 
1724
Outputs to a file the output of the InnoDB Monitor. */
1809
1725
UNIV_INTERN
1810
 
ibool
 
1726
void
1811
1727
srv_printf_innodb_monitor(
1812
1728
/*======================*/
1813
1729
        FILE*   file,           /*!< in: output stream */
1814
 
        ibool   nowait,         /*!< in: whether to wait for kernel mutex */
1815
1730
        ulint*  trx_start,      /*!< out: file position of the start of
1816
1731
                                the list of active transactions */
1817
1732
        ulint*  trx_end)        /*!< out: file position of the end of
1820
1735
        double  time_elapsed;
1821
1736
        time_t  current_time;
1822
1737
        ulint   n_reserved;
1823
 
        ibool   ret;
1824
1738
 
1825
1739
        mutex_enter(&srv_innodb_monitor_mutex);
1826
1740
 
1844
1758
                "Per second averages calculated from the last %lu seconds\n",
1845
1759
                (ulong)time_elapsed);
1846
1760
 
1847
 
        fputs("-----------------\n"
1848
 
              "BACKGROUND THREAD\n"
1849
 
              "-----------------\n", file);
 
1761
        fputs("----------\n"
 
1762
                "BACKGROUND THREAD\n"
 
1763
                "----------\n", file);
1850
1764
        srv_print_master_thread_info(file);
1851
1765
 
1852
1766
        fputs("----------\n"
1870
1784
 
1871
1785
        mutex_exit(&dict_foreign_err_mutex);
1872
1786
 
1873
 
        /* Only if lock_print_info_summary proceeds correctly,
1874
 
        before we call the lock_print_info_all_transactions
1875
 
        to print all the lock information. */
1876
 
        ret = lock_print_info_summary(file, nowait);
1877
 
 
1878
 
        if (ret) {
1879
 
                if (trx_start) {
1880
 
                        long    t = ftell(file);
1881
 
                        if (t < 0) {
1882
 
                                *trx_start = ULINT_UNDEFINED;
1883
 
                        } else {
1884
 
                                *trx_start = (ulint) t;
1885
 
                        }
1886
 
                }
1887
 
                lock_print_info_all_transactions(file);
1888
 
                if (trx_end) {
1889
 
                        long    t = ftell(file);
1890
 
                        if (t < 0) {
1891
 
                                *trx_end = ULINT_UNDEFINED;
1892
 
                        } else {
1893
 
                                *trx_end = (ulint) t;
1894
 
                        }
1895
 
                }
1896
 
        }
1897
 
 
 
1787
        lock_print_info_summary(file);
 
1788
        if (trx_start) {
 
1789
                long    t = ftell(file);
 
1790
                if (t < 0) {
 
1791
                        *trx_start = ULINT_UNDEFINED;
 
1792
                } else {
 
1793
                        *trx_start = (ulint) t;
 
1794
                }
 
1795
        }
 
1796
        lock_print_info_all_transactions(file);
 
1797
        if (trx_end) {
 
1798
                long    t = ftell(file);
 
1799
                if (t < 0) {
 
1800
                        *trx_end = ULINT_UNDEFINED;
 
1801
                } else {
 
1802
                        *trx_end = (ulint) t;
 
1803
                }
 
1804
        }
1898
1805
        fputs("--------\n"
1899
1806
              "FILE I/O\n"
1900
1807
              "--------\n", file);
1942
1849
                (ulong) srv_conc_n_waiting_threads);
1943
1850
 
1944
1851
        fprintf(file, "%lu read views open inside InnoDB\n",
1945
 
                static_cast<ulint>(UT_LIST_GET_LEN(trx_sys->view_list)));
 
1852
                UT_LIST_GET_LEN(trx_sys->view_list));
1946
1853
 
1947
1854
        n_reserved = fil_space_get_n_reserved_extents(0);
1948
1855
        if (n_reserved > 0) {
1992
1899
              "============================\n", file);
1993
1900
        mutex_exit(&srv_innodb_monitor_mutex);
1994
1901
        fflush(file);
1995
 
 
1996
 
        return(ret);
1997
1902
}
1998
1903
 
1999
1904
/******************************************************************//**
2003
1908
srv_export_innodb_status(void)
2004
1909
/*==========================*/
2005
1910
{
2006
 
        buf_pool_stat_t stat;
2007
 
        ulint           LRU_len;
2008
 
        ulint           free_len;
2009
 
        ulint           flush_list_len;
2010
 
 
2011
 
        buf_get_total_stat(&stat);
2012
 
        buf_get_total_list_len(&LRU_len, &free_len, &flush_list_len);
2013
 
 
2014
1911
        mutex_enter(&srv_innodb_monitor_mutex);
2015
1912
 
2016
1913
        export_vars.innodb_data_pending_reads
2025
1922
        export_vars.innodb_data_reads = os_n_file_reads;
2026
1923
        export_vars.innodb_data_writes = os_n_file_writes;
2027
1924
        export_vars.innodb_data_written = srv_data_written;
2028
 
        export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
 
1925
        export_vars.innodb_buffer_pool_read_requests = buf_pool->stat.n_page_gets;
2029
1926
        export_vars.innodb_buffer_pool_write_requests
2030
1927
                = srv_buf_pool_write_requests;
2031
1928
        export_vars.innodb_buffer_pool_wait_free = srv_buf_pool_wait_free;
2032
1929
        export_vars.innodb_buffer_pool_pages_flushed = srv_buf_pool_flushed;
2033
1930
        export_vars.innodb_buffer_pool_reads = srv_buf_pool_reads;
2034
1931
        export_vars.innodb_buffer_pool_read_ahead
2035
 
                = stat.n_ra_pages_read;
 
1932
                = buf_pool->stat.n_ra_pages_read;
2036
1933
        export_vars.innodb_buffer_pool_read_ahead_evicted
2037
 
                = stat.n_ra_pages_evicted;
2038
 
        export_vars.innodb_buffer_pool_pages_data = LRU_len;
2039
 
        export_vars.innodb_buffer_pool_pages_dirty = flush_list_len;
2040
 
        export_vars.innodb_buffer_pool_pages_free = free_len;
 
1934
                = buf_pool->stat.n_ra_pages_evicted;
 
1935
        export_vars.innodb_buffer_pool_pages_data
 
1936
                = UT_LIST_GET_LEN(buf_pool->LRU);
 
1937
        export_vars.innodb_buffer_pool_pages_dirty
 
1938
                = UT_LIST_GET_LEN(buf_pool->flush_list);
 
1939
        export_vars.innodb_buffer_pool_pages_free
 
1940
                = UT_LIST_GET_LEN(buf_pool->free);
2041
1941
#ifdef UNIV_DEBUG
2042
1942
        export_vars.innodb_buffer_pool_pages_latched
2043
1943
                = buf_get_latched_pages_number();
2044
1944
#endif /* UNIV_DEBUG */
2045
 
        export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
 
1945
        export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
2046
1946
 
2047
 
        export_vars.innodb_buffer_pool_pages_misc
2048
 
                = buf_pool_get_n_pages() - LRU_len - free_len;
 
1947
        export_vars.innodb_buffer_pool_pages_misc = buf_pool->curr_size
 
1948
                - UT_LIST_GET_LEN(buf_pool->LRU)
 
1949
                - UT_LIST_GET_LEN(buf_pool->free);
2049
1950
#ifdef HAVE_ATOMIC_BUILTINS
2050
1951
        export_vars.innodb_have_atomic_builtins = 1;
2051
1952
#else
2061
1962
        export_vars.innodb_log_writes = srv_log_writes;
2062
1963
        export_vars.innodb_dblwr_pages_written = srv_dblwr_pages_written;
2063
1964
        export_vars.innodb_dblwr_writes = srv_dblwr_writes;
2064
 
        export_vars.innodb_pages_created = stat.n_pages_created;
2065
 
        export_vars.innodb_pages_read = stat.n_pages_read;
2066
 
        export_vars.innodb_pages_written = stat.n_pages_written;
 
1965
        export_vars.innodb_pages_created = buf_pool->stat.n_pages_created;
 
1966
        export_vars.innodb_pages_read = buf_pool->stat.n_pages_read;
 
1967
        export_vars.innodb_pages_written = buf_pool->stat.n_pages_written;
2067
1968
        export_vars.innodb_row_lock_waits = srv_n_lock_wait_count;
2068
1969
        export_vars.innodb_row_lock_current_waits
2069
1970
                = srv_n_lock_wait_current_count;
2080
1981
        export_vars.innodb_rows_inserted = srv_n_rows_inserted;
2081
1982
        export_vars.innodb_rows_updated = srv_n_rows_updated;
2082
1983
        export_vars.innodb_rows_deleted = srv_n_rows_deleted;
2083
 
        export_vars.innodb_truncated_status_writes = srv_truncated_status_writes;
2084
1984
 
2085
1985
        mutex_exit(&srv_innodb_monitor_mutex);
2086
1986
}
2087
1987
 
2088
1988
/*********************************************************************//**
2089
 
A thread which prints the info output by various InnoDB monitors.
 
1989
A thread which wakes up threads whose lock wait may have lasted too long.
 
1990
This also prints the info output by various InnoDB monitors.
2090
1991
@return a dummy parameter */
2091
1992
UNIV_INTERN
2092
1993
os_thread_ret_t
2093
 
srv_monitor_thread(
2094
 
/*===============*/
2095
 
        void*   /*arg __attribute__((unused))*/)
 
1994
srv_lock_timeout_and_monitor_thread(
 
1995
/*================================*/
 
1996
        void*   arg __attribute__((unused)))
2096
1997
                        /*!< in: a dummy parameter required by
2097
1998
                        os_thread_create */
2098
1999
{
2099
 
        ib_int64_t      sig_count;
 
2000
        srv_slot_t*     slot;
2100
2001
        double          time_elapsed;
2101
2002
        time_t          current_time;
2102
2003
        time_t          last_table_monitor_time;
2103
2004
        time_t          last_tablespace_monitor_time;
2104
2005
        time_t          last_monitor_time;
2105
 
        ulint           mutex_skipped;
2106
 
        ibool           last_srv_print_monitor;
 
2006
        ibool           some_waits;
 
2007
        double          wait_time;
 
2008
        ulint           i;
2107
2009
 
2108
2010
#ifdef UNIV_DEBUG_THREAD_CREATION
2109
2011
        fprintf(stderr, "Lock timeout thread starts, id %lu\n",
2110
2012
                os_thread_pf(os_thread_get_curr_id()));
2111
2013
#endif
2112
 
 
2113
 
#ifdef UNIV_PFS_THREAD
2114
 
        pfs_register_thread(srv_monitor_thread_key);
2115
 
#endif
2116
 
 
2117
 
        srv_last_monitor_time = ut_time();
2118
 
        last_table_monitor_time = ut_time();
2119
 
        last_tablespace_monitor_time = ut_time();
2120
 
        last_monitor_time = ut_time();
2121
 
        mutex_skipped = 0;
2122
 
        last_srv_print_monitor = srv_print_innodb_monitor;
 
2014
        UT_NOT_USED(arg);
 
2015
        srv_last_monitor_time = time(NULL);
 
2016
        last_table_monitor_time = time(NULL);
 
2017
        last_tablespace_monitor_time = time(NULL);
 
2018
        last_monitor_time = time(NULL);
2123
2019
loop:
2124
 
        srv_monitor_active = TRUE;
2125
 
 
2126
 
        /* Wake up every 5 seconds to see if we need to print
2127
 
        monitor information or if signalled at shutdown. */
2128
 
 
2129
 
        sig_count = os_event_reset(srv_monitor_event);
2130
 
 
2131
 
        os_event_wait_time_low(srv_monitor_event, 5000000, sig_count);
2132
 
 
2133
 
        current_time = ut_time();
 
2020
        srv_lock_timeout_and_monitor_active = TRUE;
 
2021
 
 
2022
        /* When someone is waiting for a lock, we wake up every second
 
2023
        and check if a timeout has passed for a lock wait */
 
2024
 
 
2025
        os_thread_sleep(1000000);
 
2026
 
 
2027
        current_time = time(NULL);
2134
2028
 
2135
2029
        time_elapsed = difftime(current_time, last_monitor_time);
2136
2030
 
2137
2031
        if (time_elapsed > 15) {
2138
 
                last_monitor_time = ut_time();
 
2032
                last_monitor_time = time(NULL);
2139
2033
 
2140
2034
                if (srv_print_innodb_monitor) {
2141
 
                        /* Reset mutex_skipped counter everytime
2142
 
                        srv_print_innodb_monitor changes. This is to
2143
 
                        ensure we will not be blocked by kernel_mutex
2144
 
                        for short duration information printing,
2145
 
                        such as requested by sync_array_print_long_waits() */
2146
 
                        if (!last_srv_print_monitor) {
2147
 
                                mutex_skipped = 0;
2148
 
                                last_srv_print_monitor = TRUE;
2149
 
                        }
2150
 
 
2151
 
                        if (!srv_printf_innodb_monitor(stderr,
2152
 
                                                MUTEX_NOWAIT(mutex_skipped),
2153
 
                                                NULL, NULL)) {
2154
 
                                mutex_skipped++;
2155
 
                        } else {
2156
 
                                /* Reset the counter */
2157
 
                                mutex_skipped = 0;
2158
 
                        }
2159
 
                } else {
2160
 
                        last_srv_print_monitor = FALSE;
 
2035
                        srv_printf_innodb_monitor(stderr, NULL, NULL);
2161
2036
                }
2162
2037
 
2163
 
 
2164
2038
                if (srv_innodb_status) {
2165
2039
                        mutex_enter(&srv_monitor_file_mutex);
2166
2040
                        rewind(srv_monitor_file);
2167
 
                        if (!srv_printf_innodb_monitor(srv_monitor_file,
2168
 
                                                MUTEX_NOWAIT(mutex_skipped),
2169
 
                                                NULL, NULL)) {
2170
 
                                mutex_skipped++;
2171
 
                        } else {
2172
 
                                mutex_skipped = 0;
2173
 
                        }
2174
 
 
 
2041
                        srv_printf_innodb_monitor(srv_monitor_file, NULL,
 
2042
                                                  NULL);
2175
2043
                        os_file_set_eof(srv_monitor_file);
2176
2044
                        mutex_exit(&srv_monitor_file_mutex);
2177
2045
                }
2179
2047
                if (srv_print_innodb_tablespace_monitor
2180
2048
                    && difftime(current_time,
2181
2049
                                last_tablespace_monitor_time) > 60) {
2182
 
                        last_tablespace_monitor_time = ut_time();
 
2050
                        last_tablespace_monitor_time = time(NULL);
2183
2051
 
2184
2052
                        fputs("========================"
2185
2053
                              "========================\n",
2205
2073
                if (srv_print_innodb_table_monitor
2206
2074
                    && difftime(current_time, last_table_monitor_time) > 60) {
2207
2075
 
2208
 
                        last_table_monitor_time = ut_time();
 
2076
                        last_table_monitor_time = time(NULL);
2209
2077
 
2210
2078
                        fputs("===========================================\n",
2211
2079
                              stderr);
2224
2092
                }
2225
2093
        }
2226
2094
 
2227
 
        if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
2228
 
                goto exit_func;
2229
 
        }
2230
 
 
2231
 
        if (srv_print_innodb_monitor
2232
 
            || srv_print_innodb_lock_monitor
2233
 
            || srv_print_innodb_tablespace_monitor
2234
 
            || srv_print_innodb_table_monitor) {
2235
 
                goto loop;
2236
 
        }
2237
 
 
2238
 
        srv_monitor_active = FALSE;
2239
 
 
2240
 
        goto loop;
2241
 
 
2242
 
exit_func:
2243
 
        srv_monitor_active = FALSE;
2244
 
 
2245
 
        /* We count the number of threads in os_thread_exit(). A created
2246
 
        thread should always use that to exit and not use return() to exit. */
2247
 
 
2248
 
        os_thread_exit(NULL);
2249
 
 
2250
 
        OS_THREAD_DUMMY_RETURN;
2251
 
}
2252
 
 
2253
 
/*********************************************************************//**
2254
 
A thread which wakes up threads whose lock wait may have lasted too long.
2255
 
@return a dummy parameter */
2256
 
UNIV_INTERN
2257
 
os_thread_ret_t
2258
 
srv_lock_timeout_thread(
2259
 
/*====================*/
2260
 
        void*   /*arg __attribute__((unused))*/)
2261
 
                        /* in: a dummy parameter required by
2262
 
                        os_thread_create */
2263
 
{
2264
 
        srv_slot_t*     slot;
2265
 
        ibool           some_waits;
2266
 
        double          wait_time;
2267
 
        ulint           i;
2268
 
        ib_int64_t      sig_count;
2269
 
 
2270
 
#ifdef UNIV_PFS_THREAD
2271
 
        pfs_register_thread(srv_lock_timeout_thread_key);
2272
 
#endif
2273
 
 
2274
 
loop:
2275
 
 
2276
 
        /* When someone is waiting for a lock, we wake up every second
2277
 
        and check if a timeout has passed for a lock wait */
2278
 
 
2279
 
        sig_count = os_event_reset(srv_timeout_event);
2280
 
 
2281
 
        os_event_wait_time_low(srv_timeout_event, 1000000, sig_count);
2282
 
 
2283
 
        srv_lock_timeout_active = TRUE;
2284
 
 
2285
2095
        mutex_enter(&kernel_mutex);
2286
2096
 
2287
2097
        some_waits = FALSE;
2305
2115
                        lock_wait_timeout = thd_lock_wait_timeout(
2306
2116
                                trx->mysql_thd);
2307
2117
 
2308
 
                        if (trx_is_interrupted(trx)
2309
 
                            || (lock_wait_timeout < 100000000
2310
 
                                && (wait_time > (double) lock_wait_timeout
2311
 
                                    || wait_time < 0))) {
 
2118
                        if (lock_wait_timeout < 100000000
 
2119
                            && (wait_time > (double) lock_wait_timeout
 
2120
                                || wait_time < 0)) {
2312
2121
 
2313
2122
                                /* Timeout exceeded or a wrap-around in system
2314
2123
                                time counter: cancel the lock request queued
2333
2142
                goto exit_func;
2334
2143
        }
2335
2144
 
2336
 
        if (some_waits) {
 
2145
        if (some_waits || srv_print_innodb_monitor
 
2146
            || srv_print_innodb_lock_monitor
 
2147
            || srv_print_innodb_tablespace_monitor
 
2148
            || srv_print_innodb_table_monitor) {
2337
2149
                goto loop;
2338
2150
        }
2339
2151
 
2340
 
        srv_lock_timeout_active = FALSE;
 
2152
        /* No one was waiting for a lock and no monitor was active:
 
2153
        suspend this thread */
 
2154
 
 
2155
        srv_lock_timeout_and_monitor_active = FALSE;
2341
2156
 
2342
2157
#if 0
2343
2158
        /* The following synchronisation is disabled, since
2347
2162
        goto loop;
2348
2163
 
2349
2164
exit_func:
2350
 
        srv_lock_timeout_active = FALSE;
 
2165
        srv_lock_timeout_and_monitor_active = FALSE;
2351
2166
 
2352
2167
        /* We count the number of threads in os_thread_exit(). A created
2353
2168
        thread should always use that to exit and not use return() to exit. */
2365
2180
os_thread_ret_t
2366
2181
srv_error_monitor_thread(
2367
2182
/*=====================*/
2368
 
        void*   /*arg __attribute__((unused))*/)
 
2183
        void*   arg __attribute__((unused)))
2369
2184
                        /*!< in: a dummy parameter required by
2370
2185
                        os_thread_create */
2371
2186
{
2373
2188
        ulint           fatal_cnt       = 0;
2374
2189
        ib_uint64_t     old_lsn;
2375
2190
        ib_uint64_t     new_lsn;
2376
 
        ib_int64_t      sig_count;
2377
2191
 
2378
2192
        old_lsn = srv_start_lsn;
2379
2193
 
2381
2195
        fprintf(stderr, "Error monitor thread starts, id %lu\n",
2382
2196
                os_thread_pf(os_thread_get_curr_id()));
2383
2197
#endif
2384
 
 
2385
 
#ifdef UNIV_PFS_THREAD
2386
 
        pfs_register_thread(srv_error_monitor_thread_key);
2387
 
#endif
2388
 
 
2389
2198
loop:
2390
2199
        srv_error_monitor_active = TRUE;
2391
2200
 
2449
2258
 
2450
2259
        fflush(stderr);
2451
2260
 
2452
 
        sig_count = os_event_reset(srv_error_event);
2453
 
 
2454
 
        os_event_wait_time_low(srv_error_event, 1000000, sig_count);
 
2261
        os_thread_sleep(1000000);
2455
2262
 
2456
2263
        if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
2457
2264
 
2468
2275
        OS_THREAD_DUMMY_RETURN;
2469
2276
}
2470
2277
 
2471
 
/**********************************************************************//**
2472
 
Check whether any background thread is active.
2473
 
@return FALSE if all are are suspended or have exited. */
2474
 
UNIV_INTERN
2475
 
ibool
2476
 
srv_is_any_background_thread_active(void)
2477
 
/*=====================================*/
2478
 
{
2479
 
        ulint   i;
2480
 
        ibool   ret = FALSE;
2481
 
 
2482
 
        mutex_enter(&kernel_mutex);
2483
 
 
2484
 
        for (i = SRV_COM; i <= SRV_MASTER; ++i) {
2485
 
                if (srv_n_threads_active[i] != 0) {
2486
 
                        ret = TRUE;
2487
 
                        break;
2488
 
                }
2489
 
        }
2490
 
 
2491
 
        mutex_exit(&kernel_mutex);
2492
 
 
2493
 
        return(ret);
2494
 
}
2495
 
 
2496
2278
/*******************************************************************//**
2497
2279
Tells the InnoDB server that there has been activity in the database
2498
2280
and wakes up the master thread if it is suspended (not sleeping). Used
2499
2281
in the MySQL interface. Note that there is a small chance that the master
2500
 
thread stays suspended (we do not protect our operation with the
2501
 
srv_sys_t->mutex, for performance reasons). */
 
2282
thread stays suspended (we do not protect our operation with the kernel
 
2283
mutex, for performace reasons). */
2502
2284
UNIV_INTERN
2503
2285
void
2504
2286
srv_active_wake_master_thread(void)
2517
2299
}
2518
2300
 
2519
2301
/*******************************************************************//**
2520
 
Tells the purge thread that there has been activity in the database
2521
 
and wakes up the purge thread if it is suspended (not sleeping).  Note
2522
 
that there is a small chance that the purge thread stays suspended
2523
 
(we do not protect our operation with the kernel mutex, for
2524
 
performace reasons). */
2525
 
UNIV_INTERN
2526
 
void
2527
 
srv_wake_purge_thread_if_not_active(void)
2528
 
/*=====================================*/
2529
 
{
2530
 
        ut_ad(!mutex_own(&kernel_mutex));
2531
 
 
2532
 
        if (srv_n_purge_threads > 0
2533
 
            && srv_n_threads_active[SRV_WORKER] == 0) {
2534
 
 
2535
 
                mutex_enter(&kernel_mutex);
2536
 
 
2537
 
                srv_release_threads(SRV_WORKER, 1);
2538
 
 
2539
 
                mutex_exit(&kernel_mutex);
2540
 
        }
2541
 
}
2542
 
 
2543
 
/*******************************************************************//**
2544
2302
Wakes up the master thread if it is suspended or being suspended. */
2545
2303
UNIV_INTERN
2546
2304
void
2556
2314
        mutex_exit(&kernel_mutex);
2557
2315
}
2558
2316
 
2559
 
/*******************************************************************//**
2560
 
Wakes up the purge thread if it's not already awake. */
2561
 
UNIV_INTERN
2562
 
void
2563
 
srv_wake_purge_thread(void)
2564
 
/*=======================*/
2565
 
{
2566
 
        ut_ad(!mutex_own(&kernel_mutex));
2567
 
 
2568
 
        if (srv_n_purge_threads > 0) {
2569
 
 
2570
 
                mutex_enter(&kernel_mutex);
2571
 
 
2572
 
                srv_release_threads(SRV_WORKER, 1);
2573
 
 
2574
 
                mutex_exit(&kernel_mutex);
2575
 
        }
2576
 
}
2577
 
 
2578
2317
/**********************************************************************
2579
2318
The master thread is tasked to ensure that flush of log file happens
2580
2319
once every second in the background. This is to ensure that not more
2595
2334
        }
2596
2335
}
2597
2336
 
2598
 
/********************************************************************//**
2599
 
Do a full purge, reconfigure the purge sub-system if a dynamic
2600
 
change is detected. */
2601
 
static
2602
 
void
2603
 
srv_master_do_purge(void)
2604
 
/*=====================*/
2605
 
{
2606
 
        ulint   n_pages_purged;
2607
 
 
2608
 
        ut_ad(!mutex_own(&kernel_mutex));
2609
 
 
2610
 
        ut_a(srv_n_purge_threads == 0);
2611
 
 
2612
 
        do {
2613
 
                /* Check for shutdown and change in purge config. */
2614
 
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2615
 
                        /* Nothing to purge. */
2616
 
                        n_pages_purged = 0;
2617
 
                } else {
2618
 
                        n_pages_purged = trx_purge(srv_purge_batch_size);
2619
 
                }
2620
 
 
2621
 
                srv_sync_log_buffer_in_background();
2622
 
 
2623
 
        } while (n_pages_purged > 0);
2624
 
}
2625
 
 
2626
2337
/*********************************************************************//**
2627
2338
The master thread controlling the server.
2628
2339
@return a dummy parameter */
2630
2341
os_thread_ret_t
2631
2342
srv_master_thread(
2632
2343
/*==============*/
2633
 
        void*   /*arg __attribute__((unused))*/)
 
2344
        void*   arg __attribute__((unused)))
2634
2345
                        /*!< in: a dummy parameter required by
2635
2346
                        os_thread_create */
2636
2347
{
2637
 
        buf_pool_stat_t buf_stat;
2638
2348
        os_event_t      event;
2639
2349
        ulint           old_activity_count;
2640
2350
        ulint           n_pages_purged  = 0;
2646
2356
        ulint           n_ios_old;
2647
2357
        ulint           n_ios_very_old;
2648
2358
        ulint           n_pend_ios;
2649
 
        ulint           next_itr_time;
 
2359
        ibool           skip_sleep      = FALSE;
2650
2360
        ulint           i;
2651
2361
 
2652
2362
#ifdef UNIV_DEBUG_THREAD_CREATION
2653
2363
        fprintf(stderr, "Master thread starts, id %lu\n",
2654
2364
                os_thread_pf(os_thread_get_curr_id()));
2655
2365
#endif
2656
 
 
2657
 
#ifdef UNIV_PFS_THREAD
2658
 
        pfs_register_thread(srv_master_thread_key);
2659
 
#endif
2660
 
 
2661
2366
        srv_main_thread_process_no = os_proc_get_number();
2662
2367
        srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
2663
2368
 
2676
2381
 
2677
2382
        srv_main_thread_op_info = "reserving kernel mutex";
2678
2383
 
2679
 
        buf_get_total_stat(&buf_stat);
2680
 
        n_ios_very_old = log_sys->n_log_ios + buf_stat.n_pages_read
2681
 
                + buf_stat.n_pages_written;
 
2384
        n_ios_very_old = log_sys->n_log_ios + buf_pool->stat.n_pages_read
 
2385
                + buf_pool->stat.n_pages_written;
2682
2386
        mutex_enter(&kernel_mutex);
2683
2387
 
2684
2388
        /* Store the user activity counter at the start of this loop */
2695
2399
        when there is database activity */
2696
2400
 
2697
2401
        srv_last_log_flush_time = time(NULL);
2698
 
 
2699
 
        /* Sleep for 1 second on entrying the for loop below the first time. */
2700
 
        next_itr_time = ut_time_ms() + 1000;
 
2402
        skip_sleep = FALSE;
2701
2403
 
2702
2404
        for (i = 0; i < 10; i++) {
2703
 
                ulint   cur_time = ut_time_ms();
 
2405
                n_ios_old = log_sys->n_log_ios + buf_pool->stat.n_pages_read
 
2406
                        + buf_pool->stat.n_pages_written;
 
2407
                srv_main_thread_op_info = "sleeping";
 
2408
                srv_main_1_second_loops++;
 
2409
 
 
2410
                if (!skip_sleep) {
 
2411
 
 
2412
                        os_thread_sleep(1000000);
 
2413
                        srv_main_sleeps++;
 
2414
                }
 
2415
 
 
2416
                skip_sleep = FALSE;
2704
2417
 
2705
2418
                /* ALTER TABLE in MySQL requires on Unix that the table handler
2706
2419
                can drop tables lazily after there no longer are SELECT
2717
2430
                        goto background_loop;
2718
2431
                }
2719
2432
 
2720
 
                buf_get_total_stat(&buf_stat);
2721
 
 
2722
 
                n_ios_old = log_sys->n_log_ios + buf_stat.n_pages_read
2723
 
                        + buf_stat.n_pages_written;
2724
 
 
2725
 
                srv_main_thread_op_info = "sleeping";
2726
 
                srv_main_1_second_loops++;
2727
 
 
2728
 
                if (next_itr_time > cur_time
2729
 
                    && srv_shutdown_state == SRV_SHUTDOWN_NONE) {
2730
 
 
2731
 
                        /* Get sleep interval in micro seconds. We use
2732
 
                        ut_min() to avoid long sleep in case of
2733
 
                        wrap around. */
2734
 
                        os_thread_sleep(ut_min(1000000,
2735
 
                                        (next_itr_time - cur_time)
2736
 
                                         * 1000));
2737
 
                        srv_main_sleeps++;
2738
 
                }
2739
 
 
2740
 
                /* Each iteration should happen at 1 second interval. */
2741
 
                next_itr_time = ut_time_ms() + 1000;
2742
 
 
2743
2433
                /* Flush logs if needed */
2744
2434
                srv_sync_log_buffer_in_background();
2745
2435
 
2747
2437
                log_free_check();
2748
2438
 
2749
2439
                /* If i/os during one second sleep were less than 5% of
2750
 
                capacity, we assume that there is free disk i/o capacity
2751
 
                available, and it makes sense to do an insert buffer merge. */
 
2440
                capacity, we assume that there is free disk i/o capacity
 
2441
                available, and it makes sense to do an insert buffer merge. */
2752
2442
 
2753
 
                buf_get_total_stat(&buf_stat);
2754
2443
                n_pend_ios = buf_get_n_pending_ios()
2755
2444
                        + log_sys->n_pending_writes;
2756
 
                n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2757
 
                        + buf_stat.n_pages_written;
 
2445
                n_ios = log_sys->n_log_ios + buf_pool->stat.n_pages_read
 
2446
                        + buf_pool->stat.n_pages_written;
2758
2447
                if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2759
2448
                    && (n_ios - n_ios_old < SRV_RECENT_IO_ACTIVITY)) {
2760
2449
                        srv_main_thread_op_info = "doing insert buffer merge";
2772
2461
 
2773
2462
                        srv_main_thread_op_info =
2774
2463
                                "flushing buffer pool pages";
2775
 
                        n_pages_flushed = buf_flush_list(
2776
 
                                PCT_IO(100), IB_ULONGLONG_MAX);
2777
 
 
 
2464
                        n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2465
                                                          PCT_IO(100),
 
2466
                                                          IB_ULONGLONG_MAX);
 
2467
 
 
2468
                        /* If we had to do the flush, it may have taken
 
2469
                        even more than 1 second, and also, there may be more
 
2470
                        to flush. Do not sleep 1 second during the next
 
2471
                        iteration of this loop. */
 
2472
 
 
2473
                        skip_sleep = TRUE;
2778
2474
                } else if (srv_adaptive_flushing) {
2779
2475
 
2780
2476
                        /* Try to keep the rate of flushing of dirty
2787
2483
                                        "flushing buffer pool pages";
2788
2484
                                n_flush = ut_min(PCT_IO(100), n_flush);
2789
2485
                                n_pages_flushed =
2790
 
                                        buf_flush_list(
 
2486
                                        buf_flush_batch(
 
2487
                                                BUF_FLUSH_LIST,
2791
2488
                                                n_flush,
2792
2489
                                                IB_ULONGLONG_MAX);
 
2490
                                skip_sleep = TRUE;
2793
2491
                        }
2794
2492
                }
2795
2493
 
2819
2517
        loop above requests writes for that case. The writes done here
2820
2518
        are not required, and may be disabled. */
2821
2519
 
2822
 
        buf_get_total_stat(&buf_stat);
2823
2520
        n_pend_ios = buf_get_n_pending_ios() + log_sys->n_pending_writes;
2824
 
        n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2825
 
                + buf_stat.n_pages_written;
 
2521
        n_ios = log_sys->n_log_ios + buf_pool->stat.n_pages_read
 
2522
                + buf_pool->stat.n_pages_written;
2826
2523
 
2827
2524
        srv_main_10_second_loops++;
2828
2525
        if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2829
2526
            && (n_ios - n_ios_very_old < SRV_PAST_IO_ACTIVITY)) {
2830
2527
 
2831
2528
                srv_main_thread_op_info = "flushing buffer pool pages";
2832
 
                buf_flush_list(PCT_IO(100), IB_ULONGLONG_MAX);
 
2529
                buf_flush_batch(BUF_FLUSH_LIST, PCT_IO(100),
 
2530
                                IB_ULONGLONG_MAX);
2833
2531
 
2834
2532
                /* Flush logs if needed */
2835
2533
                srv_sync_log_buffer_in_background();
2844
2542
        /* Flush logs if needed */
2845
2543
        srv_sync_log_buffer_in_background();
2846
2544
 
2847
 
        if (srv_n_purge_threads == 0) {
2848
 
                srv_main_thread_op_info = "master purging";
2849
 
 
2850
 
                srv_master_do_purge();
 
2545
        /* We run a full purge every 10 seconds, even if the server
 
2546
        were active */
 
2547
        do {
2851
2548
 
2852
2549
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2853
2550
 
2854
2551
                        goto background_loop;
2855
2552
                }
2856
 
        }
 
2553
 
 
2554
                srv_main_thread_op_info = "purging";
 
2555
                n_pages_purged = trx_purge();
 
2556
 
 
2557
                /* Flush logs if needed */
 
2558
                srv_sync_log_buffer_in_background();
 
2559
 
 
2560
        } while (n_pages_purged);
2857
2561
 
2858
2562
        srv_main_thread_op_info = "flushing buffer pool pages";
2859
2563
 
2865
2569
                (> 70 %), we assume we can afford reserving the disk(s) for
2866
2570
                the time it requires to flush 100 pages */
2867
2571
 
2868
 
                n_pages_flushed = buf_flush_list(
2869
 
                        PCT_IO(100), IB_ULONGLONG_MAX);
 
2572
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2573
                                                  PCT_IO(100),
 
2574
                                                  IB_ULONGLONG_MAX);
2870
2575
        } else {
2871
2576
                /* Otherwise, we only flush a small number of pages so that
2872
2577
                we do not unnecessarily use much disk i/o capacity from
2873
2578
                other work */
2874
2579
 
2875
 
                n_pages_flushed = buf_flush_list(
2876
 
                          PCT_IO(10), IB_ULONGLONG_MAX);
 
2580
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2581
                                                  PCT_IO(10),
 
2582
                                                  IB_ULONGLONG_MAX);
2877
2583
        }
2878
2584
 
2879
2585
        srv_main_thread_op_info = "making checkpoint";
2917
2623
                MySQL tries to drop a table while there are still open handles
2918
2624
                to it and we had to put it to the background drop queue.) */
2919
2625
 
2920
 
                if (srv_shutdown_state == SRV_SHUTDOWN_NONE) {
2921
 
                        os_thread_sleep(100000);
 
2626
                os_thread_sleep(100000);
 
2627
        }
 
2628
 
 
2629
        srv_main_thread_op_info = "purging";
 
2630
 
 
2631
        /* Run a full purge */
 
2632
        do {
 
2633
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
 
2634
 
 
2635
                        break;
2922
2636
                }
2923
 
        }
2924
 
 
2925
 
        if (srv_n_purge_threads == 0) {
2926
 
                srv_main_thread_op_info = "master purging";
2927
 
 
2928
 
                srv_master_do_purge();
2929
 
        }
 
2637
 
 
2638
                srv_main_thread_op_info = "purging";
 
2639
                n_pages_purged = trx_purge();
 
2640
 
 
2641
                /* Flush logs if needed */
 
2642
                srv_sync_log_buffer_in_background();
 
2643
 
 
2644
        } while (n_pages_purged);
2930
2645
 
2931
2646
        srv_main_thread_op_info = "reserving kernel mutex";
2932
2647
 
2944
2659
        } else {
2945
2660
                /* This should do an amount of IO similar to the number of
2946
2661
                dirty pages that will be flushed in the call to
2947
 
                buf_flush_list below. Otherwise, the system favors
 
2662
                buf_flush_batch below. Otherwise, the system favors
2948
2663
                clean pages over cleanup throughput. */
2949
2664
                n_bytes_merged = ibuf_contract_for_n_pages(FALSE,
2950
2665
                                                           PCT_IO(100));
2963
2678
        srv_main_thread_op_info = "flushing buffer pool pages";
2964
2679
        srv_main_flush_loops++;
2965
2680
        if (srv_fast_shutdown < 2) {
2966
 
                n_pages_flushed = buf_flush_list(
2967
 
                          PCT_IO(100), IB_ULONGLONG_MAX);
 
2681
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2682
                                                  PCT_IO(100),
 
2683
                                                  IB_ULONGLONG_MAX);
2968
2684
        } else {
2969
2685
                /* In the fastest shutdown we do not flush the buffer pool
2970
2686
                to data files: we set n_pages_flushed to 0 artificially. */
2982
2698
        mutex_exit(&kernel_mutex);
2983
2699
 
2984
2700
        srv_main_thread_op_info = "waiting for buffer pool flush to end";
2985
 
        buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
 
2701
        buf_flush_wait_batch_end(BUF_FLUSH_LIST);
2986
2702
 
2987
2703
        /* Flush logs if needed */
2988
2704
        srv_sync_log_buffer_in_background();
3069
2785
                already when the event wait ends */
3070
2786
 
3071
2787
                os_thread_exit(NULL);
3072
 
 
3073
2788
        }
3074
2789
 
3075
2790
        /* When there is user activity, InnoDB will set the event and the
3078
2793
        goto loop;
3079
2794
 
3080
2795
 
3081
 
#if !defined(__SUNPRO_C)
 
2796
#if (!defined(__SUNPRO_C) && !defined(__SUNPRO_CC))
3082
2797
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3083
2798
#endif
3084
2799
}
3085
 
 
3086
 
/*********************************************************************//**
3087
 
Asynchronous purge thread.
3088
 
@return a dummy parameter */
3089
 
UNIV_INTERN
3090
 
os_thread_ret_t
3091
 
srv_purge_thread(
3092
 
/*=============*/
3093
 
        void*   /*arg __attribute__((unused))*/)        /*!< in: a dummy parameter
3094
 
                                                required by os_thread_create */
3095
 
{
3096
 
        srv_slot_t*     slot;
3097
 
        ulint           slot_no = ULINT_UNDEFINED;
3098
 
        ulint           n_total_purged = ULINT_UNDEFINED;
3099
 
 
3100
 
        ut_a(srv_n_purge_threads == 1);
3101
 
 
3102
 
#ifdef UNIV_DEBUG_THREAD_CREATION
3103
 
        fprintf(stderr, "InnoDB: Purge thread running, id %lu\n",
3104
 
                os_thread_pf(os_thread_get_curr_id()));
3105
 
#endif /* UNIV_DEBUG_THREAD_CREATION */
3106
 
 
3107
 
        mutex_enter(&kernel_mutex);
3108
 
 
3109
 
        slot_no = srv_table_reserve_slot(SRV_WORKER);
3110
 
 
3111
 
        slot = srv_table_get_nth_slot(slot_no);
3112
 
 
3113
 
        ++srv_n_threads_active[SRV_WORKER];
3114
 
 
3115
 
        mutex_exit(&kernel_mutex);
3116
 
 
3117
 
        while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
3118
 
 
3119
 
                ulint   n_pages_purged;
3120
 
 
3121
 
                /* If there are very few records to purge or the last
3122
 
                purge didn't purge any records then wait for activity.
3123
 
                We peek at the history len without holding any mutex
3124
 
                because in the worst case we will end up waiting for
3125
 
                the next purge event. */
3126
 
                if (trx_sys->rseg_history_len < srv_purge_batch_size
3127
 
                    || n_total_purged == 0) {
3128
 
 
3129
 
                        os_event_t      event;
3130
 
 
3131
 
                        mutex_enter(&kernel_mutex);
3132
 
 
3133
 
                        event = srv_suspend_thread();
3134
 
 
3135
 
                        mutex_exit(&kernel_mutex);
3136
 
 
3137
 
                        os_event_wait(event);
3138
 
                }
3139
 
 
3140
 
                /* Check for shutdown and whether we should do purge at all. */
3141
 
                if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND
3142
 
                    || srv_shutdown_state != 0
3143
 
                    || srv_fast_shutdown) {
3144
 
 
3145
 
                        break;
3146
 
                }
3147
 
 
3148
 
                n_total_purged = 0;
3149
 
 
3150
 
                /* Purge until there are no more records to purge and there is
3151
 
                no change in configuration or server state. */
3152
 
                do {
3153
 
                        n_pages_purged = trx_purge(srv_purge_batch_size);
3154
 
 
3155
 
                        n_total_purged += n_pages_purged;
3156
 
 
3157
 
                } while (n_pages_purged > 0 && !srv_fast_shutdown);
3158
 
 
3159
 
                srv_sync_log_buffer_in_background();
3160
 
        }
3161
 
 
3162
 
        mutex_enter(&kernel_mutex);
3163
 
 
3164
 
        ut_ad(srv_table_get_nth_slot(slot_no) == slot);
3165
 
 
3166
 
        /* Decrement the active count. */
3167
 
        srv_suspend_thread();
3168
 
 
3169
 
        slot->in_use = FALSE;
3170
 
 
3171
 
        /* Free the thread local memory. */
3172
 
        thr_local_free(os_thread_get_curr_id());
3173
 
 
3174
 
        mutex_exit(&kernel_mutex);
3175
 
 
3176
 
#ifdef UNIV_DEBUG_THREAD_CREATION
3177
 
        fprintf(stderr, "InnoDB: Purge thread exiting, id %lu\n",
3178
 
                os_thread_pf(os_thread_get_curr_id()));
3179
 
#endif /* UNIV_DEBUG_THREAD_CREATION */
3180
 
 
3181
 
        /* We count the number of threads in os_thread_exit(). A created
3182
 
        thread should always use that to exit and not use return() to exit. */
3183
 
        os_thread_exit(NULL);
3184
 
 
3185
 
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3186
 
}
3187
 
 
3188
 
/**********************************************************************//**
3189
 
Enqueues a task to server task queue and releases a worker thread, if there
3190
 
is a suspended one. */
3191
 
UNIV_INTERN
3192
 
void
3193
 
srv_que_task_enqueue_low(
3194
 
/*=====================*/
3195
 
        que_thr_t*      thr)    /*!< in: query thread */
3196
 
{
3197
 
        ut_ad(thr);
3198
 
 
3199
 
        mutex_enter(&kernel_mutex);
3200
 
 
3201
 
        UT_LIST_ADD_LAST(queue, srv_sys->tasks, thr);
3202
 
 
3203
 
        srv_release_threads(SRV_WORKER, 1);
3204
 
 
3205
 
        mutex_exit(&kernel_mutex);
3206
 
}