~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2010-08-12 20:27:32 UTC
  • mto: (1720.1.5 build)
  • mto: This revision was merged to the branch mainline in revision 1722.
  • Revision ID: mordred@inaugust.com-20100812202732-9kzchbkvkyki4n3u
Merged libdrizzle directly into tree.

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.
26
18
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
27
19
 
28
20
You should have received a copy of the GNU General Public License along with
29
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
30
 
St, Fifth Floor, Boston, MA 02110-1301 USA
 
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
22
Place, Suite 330, Boston, MA 02111-1307 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
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
49
 
 
50
***********************************************************************/
33
51
 
34
52
/**************************************************//**
35
53
@file srv/srv0srv.c
60
78
/* Dummy comment */
61
79
#include "srv0srv.h"
62
80
 
63
 
#include <drizzled/error.h>
64
 
#include <drizzled/errmsg_print.h>
65
 
 
66
81
#include "ut0mem.h"
67
82
#include "ut0ut.h"
68
83
#include "os0proc.h"
71
86
#include "sync0sync.h"
72
87
#include "thr0loc.h"
73
88
#include "que0que.h"
 
89
#include "srv0que.h"
74
90
#include "log0recv.h"
75
91
#include "pars0pars.h"
76
92
#include "usr0sess.h"
86
102
#include "row0mysql.h"
87
103
#include "ha_prototypes.h"
88
104
#include "trx0i_s.h"
89
 
#include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
90
105
 
91
106
/* This is set to TRUE if the MySQL user has set it in MySQL; currently
92
107
affects only FOREIGN KEY definition parsing */
103
118
in microseconds, in order to reduce the lagging of the purge thread. */
104
119
UNIV_INTERN ulint       srv_dml_needed_delay = 0;
105
120
 
106
 
UNIV_INTERN ibool       srv_lock_timeout_active = FALSE;
107
 
UNIV_INTERN ibool       srv_monitor_active = FALSE;
 
121
UNIV_INTERN ibool       srv_lock_timeout_and_monitor_active = FALSE;
108
122
UNIV_INTERN ibool       srv_error_monitor_active = FALSE;
109
123
 
110
124
UNIV_INTERN const char* srv_main_thread_op_info = "";
111
125
 
 
126
/** Prefix used by MySQL to indicate pre-5.1 table name encoding */
 
127
UNIV_INTERN const char  srv_mysql50_table_name_prefix[9] = "#mysql50#";
 
128
 
112
129
/* Server parameters which are read from the initfile */
113
130
 
114
131
/* The following three are dir paths which are catenated before file
127
144
/** Whether to check file format during startup.  A value of
128
145
DICT_TF_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
129
146
set it to the highest format we support. */
130
 
UNIV_INTERN ulint       srv_max_file_format_at_startup = DICT_TF_FORMAT_MAX;
 
147
UNIV_INTERN ulint       srv_check_file_format_at_startup = DICT_TF_FORMAT_MAX;
131
148
 
132
149
#if DICT_TF_FORMAT_51
133
150
# error "DICT_TF_FORMAT_51 must be 0!"
136
153
on duplicate key checking and foreign key checking */
137
154
UNIV_INTERN ibool       srv_locks_unsafe_for_binlog = FALSE;
138
155
 
139
 
/* If this flag is TRUE, then we will use the native aio of the
140
 
OS (provided we compiled Innobase with it in), otherwise we will
141
 
use simulated aio we build below with threads.
142
 
Currently we support native aio on windows and linux */
143
 
UNIV_INTERN my_bool     srv_use_native_aio = TRUE;
144
 
 
145
 
#ifdef __WIN__
146
 
/* Windows native condition variables. We use runtime loading / function
147
 
pointers, because they are not available on Windows Server 2003 and
148
 
Windows XP/2000.
149
 
 
150
 
We use condition for events on Windows if possible, even if os_event
151
 
resembles Windows kernel event object well API-wise. The reason is
152
 
performance, kernel objects are heavyweights and WaitForSingleObject() is a
153
 
performance killer causing calling thread to context switch. Besides, Innodb
154
 
is preallocating large number (often millions) of os_events. With kernel event
155
 
objects it takes a big chunk out of non-paged pool, which is better suited
156
 
for tasks like IO than for storing idle event objects. */
157
 
UNIV_INTERN ibool       srv_use_native_conditions = FALSE;
158
 
#endif /* __WIN__ */
159
 
 
160
156
UNIV_INTERN ulint       srv_n_data_files = 0;
161
157
UNIV_INTERN char**      srv_data_file_names = NULL;
162
158
/* size in database pages */
191
187
the checkpoints. */
192
188
UNIV_INTERN bool        srv_adaptive_flushing   = TRUE;
193
189
 
194
 
/** Maximum number of times allowed to conditionally acquire
195
 
mutex before switching to blocking wait on the mutex */
196
 
#define MAX_MUTEX_NOWAIT        20
197
 
 
198
 
/** Check whether the number of failed nonblocking mutex
199
 
acquisition attempts exceeds maximum allowed value. If so,
200
 
srv_printf_innodb_monitor() will request mutex acquisition
201
 
with mutex_enter(), which will wait until it gets the mutex. */
202
 
#define MUTEX_NOWAIT(mutex_skipped)     ((mutex_skipped) < MAX_MUTEX_NOWAIT)
203
 
 
204
 
/** The sort order table of the MySQL latin1_swedish_ci character set
 
190
/* The sort order table of the MySQL latin1_swedish_ci character set
205
191
collation */
206
192
#if defined(BUILD_DRIZZLE)
207
 
const byte      srv_latin1_ordering[256]        /* The sort order table of the latin1
 
193
UNIV_INTERN const byte  srv_latin1_ordering[256]        /* The sort order table of the latin1
208
194
                                        character set. The following table is
209
195
                                        the MySQL order as of Feb 10th, 2002 */
210
196
= {
250
236
UNIV_INTERN my_bool     srv_use_sys_malloc      = TRUE;
251
237
/* requested size in kilobytes */
252
238
UNIV_INTERN ulint       srv_buf_pool_size       = ULINT_MAX;
253
 
/* requested number of buffer pool instances */
254
 
UNIV_INTERN ulint       srv_buf_pool_instances  = 1;
255
239
/* previously requested size */
256
240
UNIV_INTERN ulint       srv_buf_pool_old_size;
257
241
/* current size in kilobytes */
303
287
 
304
288
UNIV_INTERN ulong       srv_max_buf_pool_modified_pct   = 75;
305
289
 
306
 
/* the number of purge threads to use from the worker pool (currently 0 or 1).*/
307
 
UNIV_INTERN ulong srv_n_purge_threads = 0;
308
 
 
309
 
/* the number of records to purge in one batch */
310
 
UNIV_INTERN ulong srv_purge_batch_size = 20;
311
 
 
312
290
/* variable counts amount of data read in total (in bytes) */
313
291
UNIV_INTERN ulint srv_data_read = 0;
314
292
 
355
333
reading of a disk page */
356
334
UNIV_INTERN ulint srv_buf_pool_reads = 0;
357
335
 
 
336
/** Number of sequential read-aheads */
 
337
UNIV_INTERN ulint srv_read_ahead_seq = 0;
 
338
 
 
339
/** Number of random read-aheads */
 
340
UNIV_INTERN ulint srv_read_ahead_rnd = 0;
 
341
 
358
342
/* structure to pass status variables to MySQL */
359
343
UNIV_INTERN export_struc export_vars;
360
344
 
431
415
UNIV_INTERN ibool       srv_use_doublewrite_buf = TRUE;
432
416
UNIV_INTERN ibool       srv_use_checksums = TRUE;
433
417
 
 
418
UNIV_INTERN ibool       srv_set_thread_priorities = TRUE;
 
419
UNIV_INTERN int srv_query_thread_priority = 0;
 
420
 
434
421
UNIV_INTERN ulong       srv_replication_delay           = 0;
435
422
 
436
423
/*-------------------------------------------*/
463
450
UNIV_INTERN ib_int64_t  srv_n_lock_wait_time            = 0;
464
451
UNIV_INTERN ulint               srv_n_lock_max_wait_time        = 0;
465
452
 
466
 
UNIV_INTERN ulint               srv_truncated_status_writes     = 0;
467
453
 
468
454
/*
469
455
  Set the following to 0 if you want InnoDB to write messages on
487
473
 
488
474
/* Mutex for locking srv_monitor_file */
489
475
UNIV_INTERN mutex_t     srv_monitor_file_mutex;
490
 
 
491
 
#ifdef UNIV_PFS_MUTEX
492
 
/* Key to register kernel_mutex with performance schema */
493
 
UNIV_INTERN mysql_pfs_key_t     kernel_mutex_key;
494
 
/* Key to protect writing the commit_id to the sys header */
495
 
UNIV_INTERN mysql_pfs_key_t     commit_id_mutex_key;
496
 
/* Key to register srv_innodb_monitor_mutex with performance schema */
497
 
UNIV_INTERN mysql_pfs_key_t     srv_innodb_monitor_mutex_key;
498
 
/* Key to register srv_monitor_file_mutex with performance schema */
499
 
UNIV_INTERN mysql_pfs_key_t     srv_monitor_file_mutex_key;
500
 
/* Key to register srv_dict_tmpfile_mutex with performance schema */
501
 
UNIV_INTERN mysql_pfs_key_t     srv_dict_tmpfile_mutex_key;
502
 
/* Key to register the mutex with performance schema */
503
 
UNIV_INTERN mysql_pfs_key_t     srv_misc_tmpfile_mutex_key;
504
 
#endif /* UNIV_PFS_MUTEX */
505
 
 
506
476
/* Temporary file for innodb monitor output */
507
477
UNIV_INTERN FILE*       srv_monitor_file;
508
478
/* Mutex for locking srv_dict_tmpfile.
535
505
static ulint   srv_main_flush_loops             = 0;
536
506
/* Log writes involving flush. */
537
507
static ulint   srv_log_writes_and_flush         = 0;
 
508
/* Log writes not including flush. */
 
509
static ulint   srv_log_buffer_writes            = 0;
538
510
 
539
511
/* This is only ever touched by the master thread. It records the
540
512
time when the last flush of log file has happened. The master
736
708
/* Table for MySQL threads where they will be suspended to wait for locks */
737
709
UNIV_INTERN srv_slot_t* srv_mysql_table = NULL;
738
710
 
739
 
UNIV_INTERN os_event_t  srv_timeout_event;
740
 
 
741
 
UNIV_INTERN os_event_t  srv_monitor_event;
742
 
 
743
 
UNIV_INTERN os_event_t  srv_error_event;
744
 
 
745
711
UNIV_INTERN os_event_t  srv_lock_timeout_thread_event;
746
712
 
747
713
UNIV_INTERN srv_sys_t*  srv_sys = NULL;
751
717
UNIV_INTERN byte        srv_pad1[64];
752
718
/* mutex protecting the server, trx structs, query threads, and lock table */
753
719
UNIV_INTERN mutex_t*    kernel_mutex_temp;
754
 
/* mutex protecting the sys header for writing the commit id */
755
 
UNIV_INTERN mutex_t*    commit_id_mutex_temp;
756
 
 
757
720
/* padding to prevent other memory update hotspots from residing on
758
721
the same memory cache line */
759
722
UNIV_INTERN byte        srv_pad2[64];
772
735
static ulint    srv_meter_foreground[SRV_MASTER + 1];
773
736
#endif
774
737
 
 
738
/* The following values give info about the activity going on in
 
739
the database. They are protected by the server mutex. The arrays
 
740
are indexed by the type of the thread. */
 
741
 
 
742
UNIV_INTERN ulint       srv_n_threads_active[SRV_MASTER + 1];
 
743
UNIV_INTERN ulint       srv_n_threads[SRV_MASTER + 1];
 
744
 
775
745
/***********************************************************************
776
746
Prints counters for work done by srv_master_thread. */
777
747
static
785
755
                srv_main_1_second_loops, srv_main_sleeps,
786
756
                srv_main_10_second_loops, srv_main_background_loops,
787
757
                srv_main_flush_loops);
788
 
        fprintf(file, "srv_master_thread log flush and writes: %lu\n",
789
 
                      srv_log_writes_and_flush);
 
758
        fprintf(file, "srv_master_thread log flush and writes: %lu "
 
759
                      " log writes only: %lu\n",
 
760
                      srv_log_writes_and_flush, srv_log_buffer_writes);
790
761
}
791
762
 
792
 
/* The following values give info about the activity going on in
793
 
the database. They are protected by the server mutex. The arrays
794
 
are indexed by the type of the thread. */
795
 
 
796
 
UNIV_INTERN ulint       srv_n_threads_active[SRV_MASTER + 1];
797
 
UNIV_INTERN ulint       srv_n_threads[SRV_MASTER + 1];
798
 
 
799
763
/*********************************************************************//**
800
764
Sets the info describing an i/o thread current state. */
801
765
UNIV_INTERN
915
879
 
916
880
        slot = srv_table_get_nth_slot(slot_no);
917
881
 
918
 
        type = static_cast<srv_thread_type>(slot->type);
 
882
        type = slot->type;
919
883
 
920
884
        ut_ad(type >= SRV_WORKER);
921
885
        ut_ad(type <= SRV_MASTER);
958
922
 
959
923
                slot = srv_table_get_nth_slot(i);
960
924
 
961
 
                if (slot->in_use &&
962
 
                    (static_cast<srv_thread_type>(slot->type) == type) &&
963
 
                    slot->suspended) {
 
925
                if (slot->in_use && slot->type == type && slot->suspended) {
964
926
 
965
927
                        slot->suspended = FALSE;
966
928
 
1005
967
 
1006
968
        slot = srv_table_get_nth_slot(slot_no);
1007
969
 
1008
 
        type = static_cast<srv_thread_type>(slot->type);
 
970
        type = slot->type;
1009
971
 
1010
972
        ut_ad(type >= SRV_WORKER);
1011
973
        ut_ad(type <= SRV_MASTER);
1026
988
        srv_slot_t*             slot;
1027
989
        ulint                   i;
1028
990
 
1029
 
        srv_sys = static_cast<srv_sys_t *>(mem_alloc(sizeof(srv_sys_t)));
1030
 
 
1031
 
        kernel_mutex_temp = static_cast<ib_mutex_t *>(mem_alloc(sizeof(mutex_t)));
1032
 
        mutex_create(kernel_mutex_key, &kernel_mutex, SYNC_KERNEL);
1033
 
 
1034
 
        commit_id_mutex_temp = static_cast<ib_mutex_t *>(mem_alloc(sizeof(mutex_t)));
1035
 
        mutex_create(commit_id_mutex_key, &commit_id_mutex, SYNC_COMMIT_ID_LOCK);
1036
 
 
1037
 
        mutex_create(srv_innodb_monitor_mutex_key,
1038
 
                     &srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
1039
 
 
1040
 
        srv_sys->threads = static_cast<srv_table_t *>(mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t)));
 
991
        srv_sys = mem_alloc(sizeof(srv_sys_t));
 
992
 
 
993
        kernel_mutex_temp = mem_alloc(sizeof(mutex_t));
 
994
        mutex_create(&kernel_mutex, SYNC_KERNEL);
 
995
 
 
996
        mutex_create(&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
 
997
 
 
998
        srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1041
999
 
1042
1000
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1043
1001
                slot = srv_table_get_nth_slot(i);
1047
1005
                ut_a(slot->event);
1048
1006
        }
1049
1007
 
1050
 
        srv_mysql_table = static_cast<srv_slot_t *>(mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t)));
 
1008
        srv_mysql_table = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1051
1009
 
1052
1010
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1053
1011
                slot = srv_mysql_table + i;
1057
1015
                ut_a(slot->event);
1058
1016
        }
1059
1017
 
1060
 
        srv_error_event = os_event_create(NULL);
1061
 
 
1062
 
        srv_timeout_event = os_event_create(NULL);
1063
 
 
1064
 
        srv_monitor_event = os_event_create(NULL);
1065
 
 
1066
1018
        srv_lock_timeout_thread_event = os_event_create(NULL);
1067
1019
 
1068
1020
        for (i = 0; i < SRV_MASTER + 1; i++) {
1089
1041
 
1090
1042
        UT_LIST_INIT(srv_conc_queue);
1091
1043
 
1092
 
        srv_conc_slots = static_cast<srv_conc_slot_t *>(mem_alloc(OS_THREAD_MAX_N * sizeof(srv_conc_slot_t)));
 
1044
        srv_conc_slots = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_conc_slot_t));
1093
1045
 
1094
1046
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1095
1047
                conc_slot = srv_conc_slots + i;
1103
1055
}
1104
1056
 
1105
1057
/*********************************************************************//**
1106
 
Frees the data structures created in srv_init(). */
 
1058
Frees the OS fast mutex created in srv_init(). */
1107
1059
UNIV_INTERN
1108
1060
void
1109
1061
srv_free(void)
1110
1062
/*==========*/
1111
1063
{
1112
1064
        os_fast_mutex_free(&srv_conc_mutex);
1113
 
        mem_free(srv_conc_slots);
1114
 
        srv_conc_slots = NULL;
1115
 
 
1116
 
        mem_free(srv_sys->threads);
1117
 
        mem_free(srv_sys);
1118
 
        srv_sys = NULL;
1119
 
 
1120
 
        mem_free(kernel_mutex_temp);
1121
 
        kernel_mutex_temp = NULL;
1122
 
        mem_free(srv_mysql_table);
1123
 
        srv_mysql_table = NULL;
1124
 
 
1125
 
        mem_free(commit_id_mutex_temp);
1126
 
        commit_id_mutex_temp = NULL;
1127
 
 
1128
 
        trx_i_s_cache_free(trx_i_s_cache);
1129
1065
}
1130
1066
 
1131
1067
/*********************************************************************//**
1137
1073
/*==================*/
1138
1074
{
1139
1075
        ut_mem_init();
1140
 
        /* Reset the system variables in the recovery module. */
1141
 
        recv_sys_var_init();
1142
1076
        os_sync_init();
1143
1077
        sync_init();
1144
1078
        mem_init(srv_mem_pool_size);
1645
1579
                row_mysql_unfreeze_data_dictionary(trx);
1646
1580
                break;
1647
1581
        case RW_X_LATCH:
1648
 
                /* There should never be a lock wait when the
1649
 
                dictionary latch is reserved in X mode.  Dictionary
1650
 
                transactions should only acquire locks on dictionary
1651
 
                tables, not other tables. All access to dictionary
1652
 
                tables should be covered by dictionary
1653
 
                transactions. */
1654
 
                ut_print_timestamp(stderr);
1655
 
                fputs("  InnoDB: Error: dict X latch held in "
1656
 
                      "srv_suspend_mysql_thread\n", stderr);
1657
 
                /* This should never occur. This incorrect handling
1658
 
                was added in the early development of
1659
 
                ha_innobase::add_index() in InnoDB Plugin 1.0. */
1660
1582
                /* Release fast index creation latch */
1661
1583
                row_mysql_unlock_data_dictionary(trx);
1662
1584
                break;
1676
1598
                row_mysql_freeze_data_dictionary(trx);
1677
1599
                break;
1678
1600
        case RW_X_LATCH:
1679
 
                /* This should never occur. This incorrect handling
1680
 
                was added in the early development of
1681
 
                ha_innobase::add_index() in InnoDB Plugin 1.0. */
1682
1601
                row_mysql_lock_data_dictionary(trx);
1683
1602
                break;
1684
1603
        }
1715
1634
                    start_time != -1 && finish_time != -1) {
1716
1635
                        srv_n_lock_max_wait_time = diff_time;
1717
1636
                }
1718
 
 
1719
 
                /* Record the lock wait time for this thread */
1720
 
                thd_set_lock_wait_time(trx->mysql_thd, diff_time);
1721
1637
        }
1722
1638
 
1723
1639
        if (trx->was_chosen_as_deadlock_victim) {
1739
1655
 
1740
1656
                trx->error_state = DB_LOCK_WAIT_TIMEOUT;
1741
1657
        }
1742
 
 
1743
 
        if (trx_is_interrupted(trx)) {
1744
 
 
1745
 
                trx->error_state = DB_INTERRUPTED;
1746
 
        }
1747
1658
}
1748
1659
 
1749
1660
/********************************************************************//**
1795
1706
 
1796
1707
        log_refresh_stats();
1797
1708
 
1798
 
        buf_refresh_io_stats_all();
 
1709
        buf_refresh_io_stats();
1799
1710
 
1800
1711
        srv_n_rows_inserted_old = srv_n_rows_inserted;
1801
1712
        srv_n_rows_updated_old = srv_n_rows_updated;
1806
1717
}
1807
1718
 
1808
1719
/******************************************************************//**
1809
 
Outputs to a file the output of the InnoDB Monitor.
1810
 
@return FALSE if not all information printed
1811
 
due to failure to obtain necessary mutex */
 
1720
Outputs to a file the output of the InnoDB Monitor. */
1812
1721
UNIV_INTERN
1813
 
ibool
 
1722
void
1814
1723
srv_printf_innodb_monitor(
1815
1724
/*======================*/
1816
1725
        FILE*   file,           /*!< in: output stream */
1817
 
        ibool   nowait,         /*!< in: whether to wait for kernel mutex */
1818
1726
        ulint*  trx_start,      /*!< out: file position of the start of
1819
1727
                                the list of active transactions */
1820
1728
        ulint*  trx_end)        /*!< out: file position of the end of
1823
1731
        double  time_elapsed;
1824
1732
        time_t  current_time;
1825
1733
        ulint   n_reserved;
1826
 
        ibool   ret;
1827
1734
 
1828
1735
        mutex_enter(&srv_innodb_monitor_mutex);
1829
1736
 
1847
1754
                "Per second averages calculated from the last %lu seconds\n",
1848
1755
                (ulong)time_elapsed);
1849
1756
 
1850
 
        fputs("-----------------\n"
1851
 
              "BACKGROUND THREAD\n"
1852
 
              "-----------------\n", file);
 
1757
        fputs("----------\n"
 
1758
                "BACKGROUND THREAD\n"
 
1759
                "----------\n", file);
1853
1760
        srv_print_master_thread_info(file);
1854
1761
 
1855
1762
        fputs("----------\n"
1873
1780
 
1874
1781
        mutex_exit(&dict_foreign_err_mutex);
1875
1782
 
1876
 
        /* Only if lock_print_info_summary proceeds correctly,
1877
 
        before we call the lock_print_info_all_transactions
1878
 
        to print all the lock information. */
1879
 
        ret = lock_print_info_summary(file, nowait);
1880
 
 
1881
 
        if (ret) {
1882
 
                if (trx_start) {
1883
 
                        long    t = ftell(file);
1884
 
                        if (t < 0) {
1885
 
                                *trx_start = ULINT_UNDEFINED;
1886
 
                        } else {
1887
 
                                *trx_start = (ulint) t;
1888
 
                        }
1889
 
                }
1890
 
                lock_print_info_all_transactions(file);
1891
 
                if (trx_end) {
1892
 
                        long    t = ftell(file);
1893
 
                        if (t < 0) {
1894
 
                                *trx_end = ULINT_UNDEFINED;
1895
 
                        } else {
1896
 
                                *trx_end = (ulint) t;
1897
 
                        }
1898
 
                }
1899
 
        }
1900
 
 
 
1783
        lock_print_info_summary(file);
 
1784
        if (trx_start) {
 
1785
                long    t = ftell(file);
 
1786
                if (t < 0) {
 
1787
                        *trx_start = ULINT_UNDEFINED;
 
1788
                } else {
 
1789
                        *trx_start = (ulint) t;
 
1790
                }
 
1791
        }
 
1792
        lock_print_info_all_transactions(file);
 
1793
        if (trx_end) {
 
1794
                long    t = ftell(file);
 
1795
                if (t < 0) {
 
1796
                        *trx_end = ULINT_UNDEFINED;
 
1797
                } else {
 
1798
                        *trx_end = (ulint) t;
 
1799
                }
 
1800
        }
1901
1801
        fputs("--------\n"
1902
1802
              "FILE I/O\n"
1903
1803
              "--------\n", file);
1945
1845
                (ulong) srv_conc_n_waiting_threads);
1946
1846
 
1947
1847
        fprintf(file, "%lu read views open inside InnoDB\n",
1948
 
                static_cast<ulint>(UT_LIST_GET_LEN(trx_sys->view_list)));
 
1848
                UT_LIST_GET_LEN(trx_sys->view_list));
1949
1849
 
1950
1850
        n_reserved = fil_space_get_n_reserved_extents(0);
1951
1851
        if (n_reserved > 0) {
1995
1895
              "============================\n", file);
1996
1896
        mutex_exit(&srv_innodb_monitor_mutex);
1997
1897
        fflush(file);
1998
 
 
1999
 
        return(ret);
2000
1898
}
2001
1899
 
2002
1900
/******************************************************************//**
2006
1904
srv_export_innodb_status(void)
2007
1905
/*==========================*/
2008
1906
{
2009
 
        buf_pool_stat_t stat;
2010
 
        ulint           LRU_len;
2011
 
        ulint           free_len;
2012
 
        ulint           flush_list_len;
2013
 
 
2014
 
        buf_get_total_stat(&stat);
2015
 
        buf_get_total_list_len(&LRU_len, &free_len, &flush_list_len);
2016
 
 
2017
1907
        mutex_enter(&srv_innodb_monitor_mutex);
2018
1908
 
2019
1909
        export_vars.innodb_data_pending_reads
2028
1918
        export_vars.innodb_data_reads = os_n_file_reads;
2029
1919
        export_vars.innodb_data_writes = os_n_file_writes;
2030
1920
        export_vars.innodb_data_written = srv_data_written;
2031
 
        export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
 
1921
        export_vars.innodb_buffer_pool_read_requests = buf_pool->n_page_gets;
2032
1922
        export_vars.innodb_buffer_pool_write_requests
2033
1923
                = srv_buf_pool_write_requests;
2034
1924
        export_vars.innodb_buffer_pool_wait_free = srv_buf_pool_wait_free;
2035
1925
        export_vars.innodb_buffer_pool_pages_flushed = srv_buf_pool_flushed;
2036
1926
        export_vars.innodb_buffer_pool_reads = srv_buf_pool_reads;
2037
 
        export_vars.innodb_buffer_pool_read_ahead
2038
 
                = stat.n_ra_pages_read;
2039
 
        export_vars.innodb_buffer_pool_read_ahead_evicted
2040
 
                = stat.n_ra_pages_evicted;
2041
 
        export_vars.innodb_buffer_pool_pages_data = LRU_len;
2042
 
        export_vars.innodb_buffer_pool_pages_dirty = flush_list_len;
2043
 
        export_vars.innodb_buffer_pool_pages_free = free_len;
 
1927
        export_vars.innodb_buffer_pool_read_ahead_rnd = srv_read_ahead_rnd;
 
1928
        export_vars.innodb_buffer_pool_read_ahead_seq = srv_read_ahead_seq;
 
1929
        export_vars.innodb_buffer_pool_pages_data
 
1930
                = UT_LIST_GET_LEN(buf_pool->LRU);
 
1931
        export_vars.innodb_buffer_pool_pages_dirty
 
1932
                = UT_LIST_GET_LEN(buf_pool->flush_list);
 
1933
        export_vars.innodb_buffer_pool_pages_free
 
1934
                = UT_LIST_GET_LEN(buf_pool->free);
2044
1935
#ifdef UNIV_DEBUG
2045
1936
        export_vars.innodb_buffer_pool_pages_latched
2046
1937
                = buf_get_latched_pages_number();
2047
1938
#endif /* UNIV_DEBUG */
2048
 
        export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
 
1939
        export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
2049
1940
 
2050
 
        export_vars.innodb_buffer_pool_pages_misc
2051
 
                = buf_pool_get_n_pages() - LRU_len - free_len;
 
1941
        export_vars.innodb_buffer_pool_pages_misc = buf_pool->curr_size
 
1942
                - UT_LIST_GET_LEN(buf_pool->LRU)
 
1943
                - UT_LIST_GET_LEN(buf_pool->free);
2052
1944
#ifdef HAVE_ATOMIC_BUILTINS
2053
1945
        export_vars.innodb_have_atomic_builtins = 1;
2054
1946
#else
2064
1956
        export_vars.innodb_log_writes = srv_log_writes;
2065
1957
        export_vars.innodb_dblwr_pages_written = srv_dblwr_pages_written;
2066
1958
        export_vars.innodb_dblwr_writes = srv_dblwr_writes;
2067
 
        export_vars.innodb_pages_created = stat.n_pages_created;
2068
 
        export_vars.innodb_pages_read = stat.n_pages_read;
2069
 
        export_vars.innodb_pages_written = stat.n_pages_written;
 
1959
        export_vars.innodb_pages_created = buf_pool->n_pages_created;
 
1960
        export_vars.innodb_pages_read = buf_pool->n_pages_read;
 
1961
        export_vars.innodb_pages_written = buf_pool->n_pages_written;
2070
1962
        export_vars.innodb_row_lock_waits = srv_n_lock_wait_count;
2071
1963
        export_vars.innodb_row_lock_current_waits
2072
1964
                = srv_n_lock_wait_current_count;
2083
1975
        export_vars.innodb_rows_inserted = srv_n_rows_inserted;
2084
1976
        export_vars.innodb_rows_updated = srv_n_rows_updated;
2085
1977
        export_vars.innodb_rows_deleted = srv_n_rows_deleted;
2086
 
        export_vars.innodb_truncated_status_writes = srv_truncated_status_writes;
2087
1978
 
2088
1979
        mutex_exit(&srv_innodb_monitor_mutex);
2089
1980
}
2090
1981
 
2091
1982
/*********************************************************************//**
2092
 
A thread which prints the info output by various InnoDB monitors.
 
1983
A thread which wakes up threads whose lock wait may have lasted too long.
 
1984
This also prints the info output by various InnoDB monitors.
2093
1985
@return a dummy parameter */
2094
1986
UNIV_INTERN
2095
1987
os_thread_ret_t
2096
 
srv_monitor_thread(
2097
 
/*===============*/
2098
 
        void*   /*arg __attribute__((unused))*/)
 
1988
srv_lock_timeout_and_monitor_thread(
 
1989
/*================================*/
 
1990
        void*   arg __attribute__((unused)))
2099
1991
                        /*!< in: a dummy parameter required by
2100
1992
                        os_thread_create */
2101
1993
{
2102
 
        ib_int64_t      sig_count;
 
1994
        srv_slot_t*     slot;
2103
1995
        double          time_elapsed;
2104
1996
        time_t          current_time;
2105
1997
        time_t          last_table_monitor_time;
2106
1998
        time_t          last_tablespace_monitor_time;
2107
1999
        time_t          last_monitor_time;
2108
 
        ulint           mutex_skipped;
2109
 
        ibool           last_srv_print_monitor;
 
2000
        ibool           some_waits;
 
2001
        double          wait_time;
 
2002
        ulint           i;
2110
2003
 
2111
2004
#ifdef UNIV_DEBUG_THREAD_CREATION
2112
2005
        fprintf(stderr, "Lock timeout thread starts, id %lu\n",
2113
2006
                os_thread_pf(os_thread_get_curr_id()));
2114
2007
#endif
2115
 
 
2116
 
#ifdef UNIV_PFS_THREAD
2117
 
        pfs_register_thread(srv_monitor_thread_key);
2118
 
#endif
2119
 
 
2120
 
        srv_last_monitor_time = ut_time();
2121
 
        last_table_monitor_time = ut_time();
2122
 
        last_tablespace_monitor_time = ut_time();
2123
 
        last_monitor_time = ut_time();
2124
 
        mutex_skipped = 0;
2125
 
        last_srv_print_monitor = srv_print_innodb_monitor;
 
2008
        UT_NOT_USED(arg);
 
2009
        srv_last_monitor_time = time(NULL);
 
2010
        last_table_monitor_time = time(NULL);
 
2011
        last_tablespace_monitor_time = time(NULL);
 
2012
        last_monitor_time = time(NULL);
2126
2013
loop:
2127
 
        srv_monitor_active = TRUE;
2128
 
 
2129
 
        /* Wake up every 5 seconds to see if we need to print
2130
 
        monitor information or if signalled at shutdown. */
2131
 
 
2132
 
        sig_count = os_event_reset(srv_monitor_event);
2133
 
 
2134
 
        os_event_wait_time_low(srv_monitor_event, 5000000, sig_count);
2135
 
 
2136
 
        current_time = ut_time();
 
2014
        srv_lock_timeout_and_monitor_active = TRUE;
 
2015
 
 
2016
        /* When someone is waiting for a lock, we wake up every second
 
2017
        and check if a timeout has passed for a lock wait */
 
2018
 
 
2019
        os_thread_sleep(1000000);
 
2020
 
 
2021
        current_time = time(NULL);
2137
2022
 
2138
2023
        time_elapsed = difftime(current_time, last_monitor_time);
2139
2024
 
2140
2025
        if (time_elapsed > 15) {
2141
 
                last_monitor_time = ut_time();
 
2026
                last_monitor_time = time(NULL);
2142
2027
 
2143
2028
                if (srv_print_innodb_monitor) {
2144
 
                        /* Reset mutex_skipped counter everytime
2145
 
                        srv_print_innodb_monitor changes. This is to
2146
 
                        ensure we will not be blocked by kernel_mutex
2147
 
                        for short duration information printing,
2148
 
                        such as requested by sync_array_print_long_waits() */
2149
 
                        if (!last_srv_print_monitor) {
2150
 
                                mutex_skipped = 0;
2151
 
                                last_srv_print_monitor = TRUE;
2152
 
                        }
2153
 
 
2154
 
                        if (!srv_printf_innodb_monitor(stderr,
2155
 
                                                MUTEX_NOWAIT(mutex_skipped),
2156
 
                                                NULL, NULL)) {
2157
 
                                mutex_skipped++;
2158
 
                        } else {
2159
 
                                /* Reset the counter */
2160
 
                                mutex_skipped = 0;
2161
 
                        }
2162
 
                } else {
2163
 
                        last_srv_print_monitor = FALSE;
 
2029
                        srv_printf_innodb_monitor(stderr, NULL, NULL);
2164
2030
                }
2165
2031
 
2166
 
 
2167
2032
                if (srv_innodb_status) {
2168
2033
                        mutex_enter(&srv_monitor_file_mutex);
2169
2034
                        rewind(srv_monitor_file);
2170
 
                        if (!srv_printf_innodb_monitor(srv_monitor_file,
2171
 
                                                MUTEX_NOWAIT(mutex_skipped),
2172
 
                                                NULL, NULL)) {
2173
 
                                mutex_skipped++;
2174
 
                        } else {
2175
 
                                mutex_skipped = 0;
2176
 
                        }
2177
 
 
 
2035
                        srv_printf_innodb_monitor(srv_monitor_file, NULL,
 
2036
                                                  NULL);
2178
2037
                        os_file_set_eof(srv_monitor_file);
2179
2038
                        mutex_exit(&srv_monitor_file_mutex);
2180
2039
                }
2182
2041
                if (srv_print_innodb_tablespace_monitor
2183
2042
                    && difftime(current_time,
2184
2043
                                last_tablespace_monitor_time) > 60) {
2185
 
                        last_tablespace_monitor_time = ut_time();
 
2044
                        last_tablespace_monitor_time = time(NULL);
2186
2045
 
2187
2046
                        fputs("========================"
2188
2047
                              "========================\n",
2208
2067
                if (srv_print_innodb_table_monitor
2209
2068
                    && difftime(current_time, last_table_monitor_time) > 60) {
2210
2069
 
2211
 
                        last_table_monitor_time = ut_time();
 
2070
                        last_table_monitor_time = time(NULL);
2212
2071
 
2213
2072
                        fputs("===========================================\n",
2214
2073
                              stderr);
2227
2086
                }
2228
2087
        }
2229
2088
 
2230
 
        if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
2231
 
                goto exit_func;
2232
 
        }
2233
 
 
2234
 
        if (srv_print_innodb_monitor
2235
 
            || srv_print_innodb_lock_monitor
2236
 
            || srv_print_innodb_tablespace_monitor
2237
 
            || srv_print_innodb_table_monitor) {
2238
 
                goto loop;
2239
 
        }
2240
 
 
2241
 
        srv_monitor_active = FALSE;
2242
 
 
2243
 
        goto loop;
2244
 
 
2245
 
exit_func:
2246
 
        srv_monitor_active = FALSE;
2247
 
 
2248
 
        /* We count the number of threads in os_thread_exit(). A created
2249
 
        thread should always use that to exit and not use return() to exit. */
2250
 
 
2251
 
        os_thread_exit(NULL);
2252
 
 
2253
 
        OS_THREAD_DUMMY_RETURN;
2254
 
}
2255
 
 
2256
 
/*********************************************************************//**
2257
 
A thread which wakes up threads whose lock wait may have lasted too long.
2258
 
@return a dummy parameter */
2259
 
UNIV_INTERN
2260
 
os_thread_ret_t
2261
 
srv_lock_timeout_thread(
2262
 
/*====================*/
2263
 
        void*   /*arg __attribute__((unused))*/)
2264
 
                        /* in: a dummy parameter required by
2265
 
                        os_thread_create */
2266
 
{
2267
 
        srv_slot_t*     slot;
2268
 
        ibool           some_waits;
2269
 
        double          wait_time;
2270
 
        ulint           i;
2271
 
        ib_int64_t      sig_count;
2272
 
 
2273
 
#ifdef UNIV_PFS_THREAD
2274
 
        pfs_register_thread(srv_lock_timeout_thread_key);
2275
 
#endif
2276
 
 
2277
 
loop:
2278
 
 
2279
 
        /* When someone is waiting for a lock, we wake up every second
2280
 
        and check if a timeout has passed for a lock wait */
2281
 
 
2282
 
        sig_count = os_event_reset(srv_timeout_event);
2283
 
 
2284
 
        os_event_wait_time_low(srv_timeout_event, 1000000, sig_count);
2285
 
 
2286
 
        srv_lock_timeout_active = TRUE;
2287
 
 
2288
2089
        mutex_enter(&kernel_mutex);
2289
2090
 
2290
2091
        some_waits = FALSE;
2308
2109
                        lock_wait_timeout = thd_lock_wait_timeout(
2309
2110
                                trx->mysql_thd);
2310
2111
 
2311
 
                        if (trx_is_interrupted(trx)
2312
 
                            || (lock_wait_timeout < 100000000
2313
 
                                && (wait_time > (double) lock_wait_timeout
2314
 
                                    || wait_time < 0))) {
 
2112
                        if (lock_wait_timeout < 100000000
 
2113
                            && (wait_time > (double) lock_wait_timeout
 
2114
                                || wait_time < 0)) {
2315
2115
 
2316
2116
                                /* Timeout exceeded or a wrap-around in system
2317
2117
                                time counter: cancel the lock request queued
2336
2136
                goto exit_func;
2337
2137
        }
2338
2138
 
2339
 
        if (some_waits) {
 
2139
        if (some_waits || srv_print_innodb_monitor
 
2140
            || srv_print_innodb_lock_monitor
 
2141
            || srv_print_innodb_tablespace_monitor
 
2142
            || srv_print_innodb_table_monitor) {
2340
2143
                goto loop;
2341
2144
        }
2342
2145
 
2343
 
        srv_lock_timeout_active = FALSE;
 
2146
        /* No one was waiting for a lock and no monitor was active:
 
2147
        suspend this thread */
 
2148
 
 
2149
        srv_lock_timeout_and_monitor_active = FALSE;
2344
2150
 
2345
2151
#if 0
2346
2152
        /* The following synchronisation is disabled, since
2350
2156
        goto loop;
2351
2157
 
2352
2158
exit_func:
2353
 
        srv_lock_timeout_active = FALSE;
 
2159
        srv_lock_timeout_and_monitor_active = FALSE;
2354
2160
 
2355
2161
        /* We count the number of threads in os_thread_exit(). A created
2356
2162
        thread should always use that to exit and not use return() to exit. */
2368
2174
os_thread_ret_t
2369
2175
srv_error_monitor_thread(
2370
2176
/*=====================*/
2371
 
        void*   /*arg __attribute__((unused))*/)
 
2177
        void*   arg __attribute__((unused)))
2372
2178
                        /*!< in: a dummy parameter required by
2373
2179
                        os_thread_create */
2374
2180
{
2376
2182
        ulint           fatal_cnt       = 0;
2377
2183
        ib_uint64_t     old_lsn;
2378
2184
        ib_uint64_t     new_lsn;
2379
 
        ib_int64_t      sig_count;
2380
2185
 
2381
2186
        old_lsn = srv_start_lsn;
2382
2187
 
2384
2189
        fprintf(stderr, "Error monitor thread starts, id %lu\n",
2385
2190
                os_thread_pf(os_thread_get_curr_id()));
2386
2191
#endif
2387
 
 
2388
 
#ifdef UNIV_PFS_THREAD
2389
 
        pfs_register_thread(srv_error_monitor_thread_key);
2390
 
#endif
2391
 
 
2392
2192
loop:
2393
2193
        srv_error_monitor_active = TRUE;
2394
2194
 
2398
2198
        new_lsn = log_get_lsn();
2399
2199
 
2400
2200
        if (new_lsn < old_lsn) {
2401
 
          drizzled::errmsg_printf(drizzled::error::INFO,
2402
 
                                  "InnoDB: Error: old log sequence number %"PRIu64" was greater than the new log sequence number %"PRIu64"!"
2403
 
                                  "InnoDB: Please submit a bug report to http://bugs.launchpad.net/drizzle",
2404
 
                                  old_lsn, new_lsn);
 
2201
                ut_print_timestamp(stderr);
 
2202
                fprintf(stderr,
 
2203
                        "  InnoDB: Error: old log sequence number %"PRIu64""
 
2204
                        " was greater\n"
 
2205
                        "InnoDB: than the new log sequence number %"PRIu64"!\n"
 
2206
                        "InnoDB: Please submit a bug report"
 
2207
                        " to http://bugs.mysql.com\n",
 
2208
                        old_lsn, new_lsn);
2405
2209
        }
2406
2210
 
2407
2211
        old_lsn = new_lsn;
2448
2252
 
2449
2253
        fflush(stderr);
2450
2254
 
2451
 
        sig_count = os_event_reset(srv_error_event);
2452
 
 
2453
 
        os_event_wait_time_low(srv_error_event, 1000000, sig_count);
 
2255
        os_thread_sleep(1000000);
2454
2256
 
2455
2257
        if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
2456
2258
 
2467
2269
        OS_THREAD_DUMMY_RETURN;
2468
2270
}
2469
2271
 
2470
 
/**********************************************************************//**
2471
 
Check whether any background thread is active.
2472
 
@return FALSE if all are are suspended or have exited. */
2473
 
UNIV_INTERN
2474
 
ibool
2475
 
srv_is_any_background_thread_active(void)
2476
 
/*=====================================*/
2477
 
{
2478
 
        ulint   i;
2479
 
        ibool   ret = FALSE;
2480
 
 
2481
 
        mutex_enter(&kernel_mutex);
2482
 
 
2483
 
        for (i = SRV_COM; i <= SRV_MASTER; ++i) {
2484
 
                if (srv_n_threads_active[i] != 0) {
2485
 
                        ret = TRUE;
2486
 
                        break;
2487
 
                }
2488
 
        }
2489
 
 
2490
 
        mutex_exit(&kernel_mutex);
2491
 
 
2492
 
        return(ret);
2493
 
}
2494
 
 
2495
2272
/*******************************************************************//**
2496
2273
Tells the InnoDB server that there has been activity in the database
2497
2274
and wakes up the master thread if it is suspended (not sleeping). Used
2498
2275
in the MySQL interface. Note that there is a small chance that the master
2499
 
thread stays suspended (we do not protect our operation with the
2500
 
srv_sys_t->mutex, for performance reasons). */
 
2276
thread stays suspended (we do not protect our operation with the kernel
 
2277
mutex, for performace reasons). */
2501
2278
UNIV_INTERN
2502
2279
void
2503
2280
srv_active_wake_master_thread(void)
2516
2293
}
2517
2294
 
2518
2295
/*******************************************************************//**
2519
 
Tells the purge thread that there has been activity in the database
2520
 
and wakes up the purge thread if it is suspended (not sleeping).  Note
2521
 
that there is a small chance that the purge thread stays suspended
2522
 
(we do not protect our operation with the kernel mutex, for
2523
 
performace reasons). */
2524
 
UNIV_INTERN
2525
 
void
2526
 
srv_wake_purge_thread_if_not_active(void)
2527
 
/*=====================================*/
2528
 
{
2529
 
        ut_ad(!mutex_own(&kernel_mutex));
2530
 
 
2531
 
        if (srv_n_purge_threads > 0
2532
 
            && srv_n_threads_active[SRV_WORKER] == 0) {
2533
 
 
2534
 
                mutex_enter(&kernel_mutex);
2535
 
 
2536
 
                srv_release_threads(SRV_WORKER, 1);
2537
 
 
2538
 
                mutex_exit(&kernel_mutex);
2539
 
        }
2540
 
}
2541
 
 
2542
 
/*******************************************************************//**
2543
2296
Wakes up the master thread if it is suspended or being suspended. */
2544
2297
UNIV_INTERN
2545
2298
void
2555
2308
        mutex_exit(&kernel_mutex);
2556
2309
}
2557
2310
 
2558
 
/*******************************************************************//**
2559
 
Wakes up the purge thread if it's not already awake. */
2560
 
UNIV_INTERN
2561
 
void
2562
 
srv_wake_purge_thread(void)
2563
 
/*=======================*/
2564
 
{
2565
 
        ut_ad(!mutex_own(&kernel_mutex));
2566
 
 
2567
 
        if (srv_n_purge_threads > 0) {
2568
 
 
2569
 
                mutex_enter(&kernel_mutex);
2570
 
 
2571
 
                srv_release_threads(SRV_WORKER, 1);
2572
 
 
2573
 
                mutex_exit(&kernel_mutex);
2574
 
        }
2575
 
}
2576
 
 
2577
2311
/**********************************************************************
2578
2312
The master thread is tasked to ensure that flush of log file happens
2579
2313
once every second in the background. This is to ensure that not more
2591
2325
                log_buffer_sync_in_background(TRUE);
2592
2326
                srv_last_log_flush_time = current_time;
2593
2327
                srv_log_writes_and_flush++;
 
2328
        } else {
 
2329
                /* Actually we don't need to write logs here.
 
2330
                We are just being extra safe here by forcing
 
2331
                the log buffer to log file. */
 
2332
                log_buffer_sync_in_background(FALSE);
 
2333
                srv_log_buffer_writes++;
2594
2334
        }
2595
2335
}
2596
2336
 
2597
 
/********************************************************************//**
2598
 
Do a full purge, reconfigure the purge sub-system if a dynamic
2599
 
change is detected. */
2600
 
static
2601
 
void
2602
 
srv_master_do_purge(void)
2603
 
/*=====================*/
2604
 
{
2605
 
        ulint   n_pages_purged;
2606
 
 
2607
 
        ut_ad(!mutex_own(&kernel_mutex));
2608
 
 
2609
 
        ut_a(srv_n_purge_threads == 0);
2610
 
 
2611
 
        do {
2612
 
                /* Check for shutdown and change in purge config. */
2613
 
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2614
 
                        /* Nothing to purge. */
2615
 
                        n_pages_purged = 0;
2616
 
                } else {
2617
 
                        n_pages_purged = trx_purge(srv_purge_batch_size);
2618
 
                }
2619
 
 
2620
 
                srv_sync_log_buffer_in_background();
2621
 
 
2622
 
        } while (n_pages_purged > 0);
2623
 
}
2624
 
 
2625
2337
/*********************************************************************//**
2626
2338
The master thread controlling the server.
2627
2339
@return a dummy parameter */
2629
2341
os_thread_ret_t
2630
2342
srv_master_thread(
2631
2343
/*==============*/
2632
 
        void*   /*arg __attribute__((unused))*/)
 
2344
        void*   arg __attribute__((unused)))
2633
2345
                        /*!< in: a dummy parameter required by
2634
2346
                        os_thread_create */
2635
2347
{
2636
 
        buf_pool_stat_t buf_stat;
2637
2348
        os_event_t      event;
2638
2349
        ulint           old_activity_count;
2639
2350
        ulint           n_pages_purged  = 0;
2645
2356
        ulint           n_ios_old;
2646
2357
        ulint           n_ios_very_old;
2647
2358
        ulint           n_pend_ios;
2648
 
        ulint           next_itr_time;
 
2359
        ibool           skip_sleep      = FALSE;
2649
2360
        ulint           i;
2650
2361
 
2651
2362
#ifdef UNIV_DEBUG_THREAD_CREATION
2652
2363
        fprintf(stderr, "Master thread starts, id %lu\n",
2653
2364
                os_thread_pf(os_thread_get_curr_id()));
2654
2365
#endif
2655
 
 
2656
 
#ifdef UNIV_PFS_THREAD
2657
 
        pfs_register_thread(srv_master_thread_key);
2658
 
#endif
2659
 
 
2660
2366
        srv_main_thread_process_no = os_proc_get_number();
2661
2367
        srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
2662
2368
 
2675
2381
 
2676
2382
        srv_main_thread_op_info = "reserving kernel mutex";
2677
2383
 
2678
 
        buf_get_total_stat(&buf_stat);
2679
 
        n_ios_very_old = log_sys->n_log_ios + buf_stat.n_pages_read
2680
 
                + buf_stat.n_pages_written;
 
2384
        n_ios_very_old = log_sys->n_log_ios + buf_pool->n_pages_read
 
2385
                + buf_pool->n_pages_written;
2681
2386
        mutex_enter(&kernel_mutex);
2682
2387
 
2683
2388
        /* Store the user activity counter at the start of this loop */
2694
2399
        when there is database activity */
2695
2400
 
2696
2401
        srv_last_log_flush_time = time(NULL);
2697
 
 
2698
 
        /* Sleep for 1 second on entrying the for loop below the first time. */
2699
 
        next_itr_time = ut_time_ms() + 1000;
 
2402
        skip_sleep = FALSE;
2700
2403
 
2701
2404
        for (i = 0; i < 10; i++) {
2702
 
                ulint   cur_time = ut_time_ms();
 
2405
                n_ios_old = log_sys->n_log_ios + buf_pool->n_pages_read
 
2406
                        + buf_pool->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;
2703
2417
 
2704
2418
                /* ALTER TABLE in MySQL requires on Unix that the table handler
2705
2419
                can drop tables lazily after there no longer are SELECT
2716
2430
                        goto background_loop;
2717
2431
                }
2718
2432
 
2719
 
                buf_get_total_stat(&buf_stat);
2720
 
 
2721
 
                n_ios_old = log_sys->n_log_ios + buf_stat.n_pages_read
2722
 
                        + buf_stat.n_pages_written;
2723
 
 
2724
 
                srv_main_thread_op_info = "sleeping";
2725
 
                srv_main_1_second_loops++;
2726
 
 
2727
 
                if (next_itr_time > cur_time
2728
 
                    && srv_shutdown_state == SRV_SHUTDOWN_NONE) {
2729
 
 
2730
 
                        /* Get sleep interval in micro seconds. We use
2731
 
                        ut_min() to avoid long sleep in case of
2732
 
                        wrap around. */
2733
 
                        os_thread_sleep(ut_min(1000000,
2734
 
                                        (next_itr_time - cur_time)
2735
 
                                         * 1000));
2736
 
                        srv_main_sleeps++;
2737
 
                }
2738
 
 
2739
 
                /* Each iteration should happen at 1 second interval. */
2740
 
                next_itr_time = ut_time_ms() + 1000;
2741
 
 
2742
2433
                /* Flush logs if needed */
2743
2434
                srv_sync_log_buffer_in_background();
2744
2435
 
2746
2437
                log_free_check();
2747
2438
 
2748
2439
                /* If i/os during one second sleep were less than 5% of
2749
 
                capacity, we assume that there is free disk i/o capacity
2750
 
                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. */
2751
2442
 
2752
 
                buf_get_total_stat(&buf_stat);
2753
2443
                n_pend_ios = buf_get_n_pending_ios()
2754
2444
                        + log_sys->n_pending_writes;
2755
 
                n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2756
 
                        + buf_stat.n_pages_written;
 
2445
                n_ios = log_sys->n_log_ios + buf_pool->n_pages_read
 
2446
                        + buf_pool->n_pages_written;
2757
2447
                if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2758
2448
                    && (n_ios - n_ios_old < SRV_RECENT_IO_ACTIVITY)) {
2759
2449
                        srv_main_thread_op_info = "doing insert buffer merge";
2769
2459
                        /* Try to keep the number of modified pages in the
2770
2460
                        buffer pool under the limit wished by the user */
2771
2461
 
2772
 
                        srv_main_thread_op_info =
2773
 
                                "flushing buffer pool pages";
2774
 
                        n_pages_flushed = buf_flush_list(
2775
 
                                PCT_IO(100), IB_ULONGLONG_MAX);
2776
 
 
 
2462
                        n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2463
                                                          PCT_IO(100),
 
2464
                                                          IB_ULONGLONG_MAX);
 
2465
 
 
2466
                        /* If we had to do the flush, it may have taken
 
2467
                        even more than 1 second, and also, there may be more
 
2468
                        to flush. Do not sleep 1 second during the next
 
2469
                        iteration of this loop. */
 
2470
 
 
2471
                        skip_sleep = TRUE;
2777
2472
                } else if (srv_adaptive_flushing) {
2778
2473
 
2779
2474
                        /* Try to keep the rate of flushing of dirty
2782
2477
                        ulint n_flush = buf_flush_get_desired_flush_rate();
2783
2478
 
2784
2479
                        if (n_flush) {
2785
 
                                srv_main_thread_op_info =
2786
 
                                        "flushing buffer pool pages";
2787
2480
                                n_flush = ut_min(PCT_IO(100), n_flush);
2788
2481
                                n_pages_flushed =
2789
 
                                        buf_flush_list(
 
2482
                                        buf_flush_batch(
 
2483
                                                BUF_FLUSH_LIST,
2790
2484
                                                n_flush,
2791
2485
                                                IB_ULONGLONG_MAX);
 
2486
                                skip_sleep = TRUE;
2792
2487
                        }
2793
2488
                }
2794
2489
 
2818
2513
        loop above requests writes for that case. The writes done here
2819
2514
        are not required, and may be disabled. */
2820
2515
 
2821
 
        buf_get_total_stat(&buf_stat);
2822
2516
        n_pend_ios = buf_get_n_pending_ios() + log_sys->n_pending_writes;
2823
 
        n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2824
 
                + buf_stat.n_pages_written;
 
2517
        n_ios = log_sys->n_log_ios + buf_pool->n_pages_read
 
2518
                + buf_pool->n_pages_written;
2825
2519
 
2826
2520
        srv_main_10_second_loops++;
2827
2521
        if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2828
2522
            && (n_ios - n_ios_very_old < SRV_PAST_IO_ACTIVITY)) {
2829
2523
 
2830
2524
                srv_main_thread_op_info = "flushing buffer pool pages";
2831
 
                buf_flush_list(PCT_IO(100), IB_ULONGLONG_MAX);
 
2525
                buf_flush_batch(BUF_FLUSH_LIST, PCT_IO(100),
 
2526
                                IB_ULONGLONG_MAX);
2832
2527
 
2833
2528
                /* Flush logs if needed */
2834
2529
                srv_sync_log_buffer_in_background();
2843
2538
        /* Flush logs if needed */
2844
2539
        srv_sync_log_buffer_in_background();
2845
2540
 
2846
 
        if (srv_n_purge_threads == 0) {
2847
 
                srv_main_thread_op_info = "master purging";
2848
 
 
2849
 
                srv_master_do_purge();
 
2541
        /* We run a full purge every 10 seconds, even if the server
 
2542
        were active */
 
2543
        do {
2850
2544
 
2851
2545
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2852
2546
 
2853
2547
                        goto background_loop;
2854
2548
                }
2855
 
        }
 
2549
 
 
2550
                srv_main_thread_op_info = "purging";
 
2551
                n_pages_purged = trx_purge();
 
2552
 
 
2553
                /* Flush logs if needed */
 
2554
                srv_sync_log_buffer_in_background();
 
2555
 
 
2556
        } while (n_pages_purged);
2856
2557
 
2857
2558
        srv_main_thread_op_info = "flushing buffer pool pages";
2858
2559
 
2864
2565
                (> 70 %), we assume we can afford reserving the disk(s) for
2865
2566
                the time it requires to flush 100 pages */
2866
2567
 
2867
 
                n_pages_flushed = buf_flush_list(
2868
 
                        PCT_IO(100), IB_ULONGLONG_MAX);
 
2568
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2569
                                                  PCT_IO(100),
 
2570
                                                  IB_ULONGLONG_MAX);
2869
2571
        } else {
2870
2572
                /* Otherwise, we only flush a small number of pages so that
2871
2573
                we do not unnecessarily use much disk i/o capacity from
2872
2574
                other work */
2873
2575
 
2874
 
                n_pages_flushed = buf_flush_list(
2875
 
                          PCT_IO(10), IB_ULONGLONG_MAX);
 
2576
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2577
                                                  PCT_IO(10),
 
2578
                                                  IB_ULONGLONG_MAX);
2876
2579
        }
2877
2580
 
2878
2581
        srv_main_thread_op_info = "making checkpoint";
2916
2619
                MySQL tries to drop a table while there are still open handles
2917
2620
                to it and we had to put it to the background drop queue.) */
2918
2621
 
2919
 
                if (srv_shutdown_state == SRV_SHUTDOWN_NONE) {
2920
 
                        os_thread_sleep(100000);
 
2622
                os_thread_sleep(100000);
 
2623
        }
 
2624
 
 
2625
        srv_main_thread_op_info = "purging";
 
2626
 
 
2627
        /* Run a full purge */
 
2628
        do {
 
2629
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
 
2630
 
 
2631
                        break;
2921
2632
                }
2922
 
        }
2923
 
 
2924
 
        if (srv_n_purge_threads == 0) {
2925
 
                srv_main_thread_op_info = "master purging";
2926
 
 
2927
 
                srv_master_do_purge();
2928
 
        }
 
2633
 
 
2634
                srv_main_thread_op_info = "purging";
 
2635
                n_pages_purged = trx_purge();
 
2636
 
 
2637
                /* Flush logs if needed */
 
2638
                srv_sync_log_buffer_in_background();
 
2639
 
 
2640
        } while (n_pages_purged);
2929
2641
 
2930
2642
        srv_main_thread_op_info = "reserving kernel mutex";
2931
2643
 
2943
2655
        } else {
2944
2656
                /* This should do an amount of IO similar to the number of
2945
2657
                dirty pages that will be flushed in the call to
2946
 
                buf_flush_list below. Otherwise, the system favors
 
2658
                buf_flush_batch below. Otherwise, the system favors
2947
2659
                clean pages over cleanup throughput. */
2948
2660
                n_bytes_merged = ibuf_contract_for_n_pages(FALSE,
2949
2661
                                                           PCT_IO(100));
2962
2674
        srv_main_thread_op_info = "flushing buffer pool pages";
2963
2675
        srv_main_flush_loops++;
2964
2676
        if (srv_fast_shutdown < 2) {
2965
 
                n_pages_flushed = buf_flush_list(
2966
 
                          PCT_IO(100), IB_ULONGLONG_MAX);
 
2677
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST,
 
2678
                                                  PCT_IO(100),
 
2679
                                                  IB_ULONGLONG_MAX);
2967
2680
        } else {
2968
2681
                /* In the fastest shutdown we do not flush the buffer pool
2969
2682
                to data files: we set n_pages_flushed to 0 artificially. */
2981
2694
        mutex_exit(&kernel_mutex);
2982
2695
 
2983
2696
        srv_main_thread_op_info = "waiting for buffer pool flush to end";
2984
 
        buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
 
2697
        buf_flush_wait_batch_end(BUF_FLUSH_LIST);
2985
2698
 
2986
2699
        /* Flush logs if needed */
2987
2700
        srv_sync_log_buffer_in_background();
3068
2781
                already when the event wait ends */
3069
2782
 
3070
2783
                os_thread_exit(NULL);
3071
 
 
3072
2784
        }
3073
2785
 
3074
2786
        /* When there is user activity, InnoDB will set the event and the
3077
2789
        goto loop;
3078
2790
 
3079
2791
 
3080
 
#if !defined(__SUNPRO_C)
 
2792
#if (!defined(__SUNPRO_C) && !defined(__SUNPRO_CC))
3081
2793
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3082
2794
#endif
3083
2795
}
3084
 
 
3085
 
/*********************************************************************//**
3086
 
Asynchronous purge thread.
3087
 
@return a dummy parameter */
3088
 
UNIV_INTERN
3089
 
os_thread_ret_t
3090
 
srv_purge_thread(
3091
 
/*=============*/
3092
 
        void*   /*arg __attribute__((unused))*/)        /*!< in: a dummy parameter
3093
 
                                                required by os_thread_create */
3094
 
{
3095
 
        srv_slot_t*     slot;
3096
 
        ulint           slot_no = ULINT_UNDEFINED;
3097
 
        ulint           n_total_purged = ULINT_UNDEFINED;
3098
 
 
3099
 
        ut_a(srv_n_purge_threads == 1);
3100
 
 
3101
 
#ifdef UNIV_DEBUG_THREAD_CREATION
3102
 
        fprintf(stderr, "InnoDB: Purge thread running, id %lu\n",
3103
 
                os_thread_pf(os_thread_get_curr_id()));
3104
 
#endif /* UNIV_DEBUG_THREAD_CREATION */
3105
 
 
3106
 
        mutex_enter(&kernel_mutex);
3107
 
 
3108
 
        slot_no = srv_table_reserve_slot(SRV_WORKER);
3109
 
 
3110
 
        slot = srv_table_get_nth_slot(slot_no);
3111
 
 
3112
 
        ++srv_n_threads_active[SRV_WORKER];
3113
 
 
3114
 
        mutex_exit(&kernel_mutex);
3115
 
 
3116
 
        while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
3117
 
 
3118
 
                ulint   n_pages_purged;
3119
 
 
3120
 
                /* If there are very few records to purge or the last
3121
 
                purge didn't purge any records then wait for activity.
3122
 
                We peek at the history len without holding any mutex
3123
 
                because in the worst case we will end up waiting for
3124
 
                the next purge event. */
3125
 
                if (trx_sys->rseg_history_len < srv_purge_batch_size
3126
 
                    || n_total_purged == 0) {
3127
 
 
3128
 
                        os_event_t      event;
3129
 
 
3130
 
                        mutex_enter(&kernel_mutex);
3131
 
 
3132
 
                        event = srv_suspend_thread();
3133
 
 
3134
 
                        mutex_exit(&kernel_mutex);
3135
 
 
3136
 
                        os_event_wait(event);
3137
 
                }
3138
 
 
3139
 
                /* Check for shutdown and whether we should do purge at all. */
3140
 
                if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND
3141
 
                    || srv_shutdown_state != 0
3142
 
                    || srv_fast_shutdown) {
3143
 
 
3144
 
                        break;
3145
 
                }
3146
 
 
3147
 
                n_total_purged = 0;
3148
 
 
3149
 
                /* Purge until there are no more records to purge and there is
3150
 
                no change in configuration or server state. */
3151
 
                do {
3152
 
                        n_pages_purged = trx_purge(srv_purge_batch_size);
3153
 
 
3154
 
                        n_total_purged += n_pages_purged;
3155
 
 
3156
 
                } while (n_pages_purged > 0 && !srv_fast_shutdown);
3157
 
 
3158
 
                srv_sync_log_buffer_in_background();
3159
 
        }
3160
 
 
3161
 
        mutex_enter(&kernel_mutex);
3162
 
 
3163
 
        ut_ad(srv_table_get_nth_slot(slot_no) == slot);
3164
 
 
3165
 
        /* Decrement the active count. */
3166
 
        srv_suspend_thread();
3167
 
 
3168
 
        slot->in_use = FALSE;
3169
 
 
3170
 
        /* Free the thread local memory. */
3171
 
        thr_local_free(os_thread_get_curr_id());
3172
 
 
3173
 
        mutex_exit(&kernel_mutex);
3174
 
 
3175
 
#ifdef UNIV_DEBUG_THREAD_CREATION
3176
 
        fprintf(stderr, "InnoDB: Purge thread exiting, id %lu\n",
3177
 
                os_thread_pf(os_thread_get_curr_id()));
3178
 
#endif /* UNIV_DEBUG_THREAD_CREATION */
3179
 
 
3180
 
        /* We count the number of threads in os_thread_exit(). A created
3181
 
        thread should always use that to exit and not use return() to exit. */
3182
 
        os_thread_exit(NULL);
3183
 
 
3184
 
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3185
 
}
3186
 
 
3187
 
/**********************************************************************//**
3188
 
Enqueues a task to server task queue and releases a worker thread, if there
3189
 
is a suspended one. */
3190
 
UNIV_INTERN
3191
 
void
3192
 
srv_que_task_enqueue_low(
3193
 
/*=====================*/
3194
 
        que_thr_t*      thr)    /*!< in: query thread */
3195
 
{
3196
 
        ut_ad(thr);
3197
 
 
3198
 
        mutex_enter(&kernel_mutex);
3199
 
 
3200
 
        UT_LIST_ADD_LAST(queue, srv_sys->tasks, thr);
3201
 
 
3202
 
        srv_release_threads(SRV_WORKER, 1);
3203
 
 
3204
 
        mutex_exit(&kernel_mutex);
3205
 
}