~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2009-01-18 01:49:40 UTC
  • Revision ID: brian@gir-3.local-20090118014940-co9651fk7hla6gqg
Removed unused session param from list_open_tables()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (c) 1995, 2010, Innobase Oy. All Rights Reserved.
4
 
Copyright (c) 2008, 2009 Google Inc.
5
 
Copyright (c) 2009, Percona Inc.
6
 
 
7
 
Portions of this file contain modifications contributed and copyrighted by
8
 
Google, Inc. Those modifications are gratefully acknowledged and are described
9
 
briefly in the InnoDB documentation. The contributions by Google are
10
 
incorporated with their permission, and subject to the conditions contained in
11
 
the file COPYING.Google.
12
 
 
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
 
This program is free software; you can redistribute it and/or modify it under
21
 
the terms of the GNU General Public License as published by the Free Software
22
 
Foundation; version 2 of the License.
23
 
 
24
 
This program is distributed in the hope that it will be useful, but WITHOUT
25
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26
 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
27
 
 
28
 
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
31
 
 
32
 
*****************************************************************************/
33
 
 
34
 
/**************************************************//**
35
 
@file srv/srv0srv.c
 
1
/******************************************************
36
2
The database server main program
37
3
 
38
4
NOTE: SQL Server 7 uses something which the documentation
54
20
Another possibility could be to use some very fast user space
55
21
thread library. This might confuse NT though.
56
22
 
 
23
(c) 1995 Innobase Oy
 
24
 
57
25
Created 10/8/1995 Heikki Tuuri
58
26
*******************************************************/
59
 
 
60
27
/* Dummy comment */
61
28
#include "srv0srv.h"
62
29
 
68
35
#include "sync0sync.h"
69
36
#include "thr0loc.h"
70
37
#include "que0que.h"
 
38
#include "srv0que.h"
71
39
#include "log0recv.h"
72
40
#include "pars0pars.h"
73
41
#include "usr0sess.h"
83
51
#include "row0mysql.h"
84
52
#include "ha_prototypes.h"
85
53
#include "trx0i_s.h"
86
 
#include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
87
54
 
88
55
/* This is set to TRUE if the MySQL user has set it in MySQL; currently
89
56
affects only FOREIGN KEY definition parsing */
100
67
in microseconds, in order to reduce the lagging of the purge thread. */
101
68
UNIV_INTERN ulint       srv_dml_needed_delay = 0;
102
69
 
103
 
UNIV_INTERN ibool       srv_lock_timeout_active = FALSE;
104
 
UNIV_INTERN ibool       srv_monitor_active = FALSE;
 
70
UNIV_INTERN ibool       srv_lock_timeout_and_monitor_active = FALSE;
105
71
UNIV_INTERN ibool       srv_error_monitor_active = FALSE;
106
72
 
107
73
UNIV_INTERN const char* srv_main_thread_op_info = "";
108
74
 
109
 
/** Prefix used by MySQL to indicate pre-5.1 table name encoding */
 
75
/* Prefix used by MySQL to indicate pre-5.1 table name encoding */
110
76
UNIV_INTERN const char  srv_mysql50_table_name_prefix[9] = "#mysql50#";
111
77
 
112
78
/* Server parameters which are read from the initfile */
119
85
UNIV_INTERN char*       srv_arch_dir    = NULL;
120
86
#endif /* UNIV_LOG_ARCHIVE */
121
87
 
122
 
/** store to its own file each table created by an user; data
 
88
/* store to its own file each table created by an user; data
123
89
dictionary tables are in the system tablespace 0 */
124
90
UNIV_INTERN my_bool     srv_file_per_table;
125
 
/** The file format to use on new *.ibd files. */
 
91
/* The file format to use on new *.ibd files. */
126
92
UNIV_INTERN ulint       srv_file_format = 0;
127
 
/** Whether to check file format during startup.  A value of
 
93
/* Whether to check file format during startup a value of 
128
94
DICT_TF_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
129
95
set it to the highest format we support. */
130
 
UNIV_INTERN ulint       srv_max_file_format_at_startup = DICT_TF_FORMAT_MAX;
 
96
UNIV_INTERN ulint       srv_check_file_format_at_startup = DICT_TF_FORMAT_MAX;
131
97
 
132
98
#if DICT_TF_FORMAT_51
133
99
# error "DICT_TF_FORMAT_51 must be 0!"
134
100
#endif
135
 
/** Place locks to records only i.e. do not use next-key locking except
 
101
/* Place locks to records only i.e. do not use next-key locking except
136
102
on duplicate key checking and foreign key checking */
137
103
UNIV_INTERN ibool       srv_locks_unsafe_for_binlog = FALSE;
138
104
 
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
105
UNIV_INTERN ulint       srv_n_data_files = 0;
161
106
UNIV_INTERN char**      srv_data_file_names = NULL;
162
107
/* size in database pages */
169
114
UNIV_INTERN ulint       srv_last_file_size_max  = 0;
170
115
/* If the last data file is auto-extended, we add this
171
116
many pages to it at a time */
172
 
UNIV_INTERN unsigned int srv_auto_extend_increment = 8;
 
117
UNIV_INTERN ulong       srv_auto_extend_increment = 8;
173
118
UNIV_INTERN ulint*      srv_data_file_is_raw_partition = NULL;
174
119
 
175
120
/* If the following is TRUE we do not allow inserts etc. This protects
187
132
UNIV_INTERN ulint       srv_log_buffer_size     = ULINT_MAX;
188
133
UNIV_INTERN ulong       srv_flush_log_at_trx_commit = 1;
189
134
 
190
 
/* Try to flush dirty pages so as to avoid IO bursts at
191
 
the checkpoints. */
192
 
UNIV_INTERN bool        srv_adaptive_flushing   = TRUE;
193
 
 
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
 
135
/* The sort order table of the MySQL latin1_swedish_ci character set
205
136
collation */
206
 
#if defined(BUILD_DRIZZLE)
207
137
UNIV_INTERN const byte  srv_latin1_ordering[256]        /* The sort order table of the latin1
208
138
                                        character set. The following table is
209
139
                                        the MySQL order as of Feb 10th, 2002 */
241
171
, 0x44, 0x4E, 0x4F, 0x4F, 0x4F, 0x4F, 0x5D, 0xF7
242
172
, 0xD8, 0x55, 0x55, 0x55, 0x59, 0x59, 0xDE, 0xFF
243
173
};
244
 
#else
245
 
UNIV_INTERN const byte*        srv_latin1_ordering;
246
 
#endif /* BUILD_DRIZZLE */
247
 
 
248
 
 
249
 
/* use os/external memory allocator */
250
 
UNIV_INTERN my_bool     srv_use_sys_malloc      = TRUE;
 
174
 
 
175
 
251
176
/* requested size in kilobytes */
252
 
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;
 
177
UNIV_INTERN ulong       srv_buf_pool_size       = ULINT_MAX;
255
178
/* previously requested size */
256
 
UNIV_INTERN ulint       srv_buf_pool_old_size;
 
179
UNIV_INTERN ulong       srv_buf_pool_old_size;
257
180
/* current size in kilobytes */
258
 
UNIV_INTERN ulint       srv_buf_pool_curr_size  = 0;
 
181
UNIV_INTERN ulong       srv_buf_pool_curr_size  = 0;
259
182
/* size in bytes */
260
183
UNIV_INTERN ulint       srv_mem_pool_size       = ULINT_MAX;
261
184
UNIV_INTERN ulint       srv_lock_table_size     = ULINT_MAX;
262
185
 
263
 
/* This parameter is deprecated. Use srv_n_io_[read|write]_threads
264
 
instead. */
265
186
UNIV_INTERN ulint       srv_n_file_io_threads   = ULINT_MAX;
266
 
UNIV_INTERN ulint       srv_n_read_io_threads   = ULINT_MAX;
267
 
UNIV_INTERN ulint       srv_n_write_io_threads  = ULINT_MAX;
268
 
 
269
 
/* User settable value of the number of pages that must be present
270
 
in the buffer cache and accessed sequentially for InnoDB to trigger a
271
 
readahead request. */
272
 
UNIV_INTERN ulong       srv_read_ahead_threshold        = 56;
273
187
 
274
188
#ifdef UNIV_LOG_ARCHIVE
275
189
UNIV_INTERN ibool               srv_log_archive_on      = FALSE;
293
207
 
294
208
UNIV_INTERN ulint       srv_max_n_open_files      = 300;
295
209
 
296
 
/* Number of IO operations per second the server can do */
297
 
UNIV_INTERN ulong       srv_io_capacity         = 200;
298
 
 
299
210
/* The InnoDB main thread tries to keep the ratio of modified pages
300
211
in the buffer pool to all database pages in the buffer pool smaller than
301
212
the following number. But it is not guaranteed that the value stays below
302
213
that during a time of heavy update/insert activity. */
303
214
 
304
 
UNIV_INTERN ulong       srv_max_buf_pool_modified_pct   = 75;
305
 
 
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;
 
215
UNIV_INTERN ulong       srv_max_buf_pool_modified_pct   = 90;
311
216
 
312
217
/* variable counts amount of data read in total (in bytes) */
313
218
UNIV_INTERN ulint srv_data_read = 0;
351
256
pool to the disk */
352
257
UNIV_INTERN ulint srv_buf_pool_flushed = 0;
353
258
 
354
 
/** Number of buffer pool reads that led to the
 
259
/* variable to count the number of buffer pool reads that led to the
355
260
reading of a disk page */
356
261
UNIV_INTERN ulint srv_buf_pool_reads = 0;
357
262
 
 
263
/* variable to count the number of sequential read-aheads */
 
264
UNIV_INTERN ulint srv_read_ahead_seq = 0;
 
265
 
 
266
/* variable to count the number of random read-aheads */
 
267
UNIV_INTERN ulint srv_read_ahead_rnd = 0;
 
268
 
358
269
/* structure to pass status variables to MySQL */
359
270
UNIV_INTERN export_struc export_vars;
360
271
 
378
289
concurrency check. */
379
290
 
380
291
UNIV_INTERN ulong       srv_thread_concurrency  = 0;
 
292
UNIV_INTERN ulong       srv_commit_concurrency  = 0;
381
293
 
382
294
/* this mutex protects srv_conc data structures */
383
295
UNIV_INTERN os_fast_mutex_t     srv_conc_mutex;
384
 
/* number of transactions that have declared_to_be_inside_innodb set.
385
 
It used to be a non-error for this value to drop below zero temporarily.
386
 
This is no longer true. We'll, however, keep the lint datatype to add
387
 
assertions to catch any corner cases that we may have missed. */
 
296
/* number of OS threads currently inside InnoDB; it is not an error if
 
297
this drops temporarily below zero because we do not demand that every
 
298
thread increments this, but a thread waiting for a lock decrements
 
299
this temporarily */
388
300
UNIV_INTERN lint        srv_conc_n_threads      = 0;
389
301
/* number of OS threads waiting in the FIFO for a permission to enter
390
302
InnoDB */
392
304
 
393
305
typedef struct srv_conc_slot_struct     srv_conc_slot_t;
394
306
struct srv_conc_slot_struct{
395
 
        os_event_t                      event;          /*!< event to wait */
396
 
        ibool                           reserved;       /*!< TRUE if slot
 
307
        os_event_t                      event;          /* event to wait */
 
308
        ibool                           reserved;       /* TRUE if slot
397
309
                                                        reserved */
398
 
        ibool                           wait_ended;     /*!< TRUE when another
 
310
        ibool                           wait_ended;     /* TRUE when another
399
311
                                                        thread has already set
400
312
                                                        the event and the
401
313
                                                        thread in this slot is
402
314
                                                        free to proceed; but
403
315
                                                        reserved may still be
404
316
                                                        TRUE at that point */
405
 
        UT_LIST_NODE_T(srv_conc_slot_t) srv_conc_queue; /*!< queue node */
 
317
        UT_LIST_NODE_T(srv_conc_slot_t) srv_conc_queue; /* queue node */
406
318
};
407
319
 
408
320
/* queue of threads waiting to get in */
431
343
UNIV_INTERN ibool       srv_use_doublewrite_buf = TRUE;
432
344
UNIV_INTERN ibool       srv_use_checksums = TRUE;
433
345
 
 
346
UNIV_INTERN ibool       srv_set_thread_priorities = TRUE;
 
347
UNIV_INTERN int srv_query_thread_priority = 0;
 
348
 
434
349
UNIV_INTERN ulong       srv_replication_delay           = 0;
435
350
 
436
351
/*-------------------------------------------*/
437
 
UNIV_INTERN ulong       srv_n_spin_wait_rounds  = 30;
 
352
UNIV_INTERN ulong       srv_n_spin_wait_rounds  = 20;
438
353
UNIV_INTERN ulong       srv_n_free_tickets_to_enter = 500;
439
354
UNIV_INTERN ulong       srv_thread_sleep_delay = 10000;
440
 
UNIV_INTERN ulong       srv_spin_wait_delay     = 6;
 
355
UNIV_INTERN ulint       srv_spin_wait_delay     = 5;
441
356
UNIV_INTERN ibool       srv_priority_boost      = TRUE;
442
357
 
443
358
#ifdef UNIV_DEBUG
452
367
UNIV_INTERN ulint               srv_n_rows_updated              = 0;
453
368
UNIV_INTERN ulint               srv_n_rows_deleted              = 0;
454
369
UNIV_INTERN ulint               srv_n_rows_read                 = 0;
455
 
 
 
370
#ifndef UNIV_HOTBACKUP
456
371
static ulint    srv_n_rows_inserted_old         = 0;
457
372
static ulint    srv_n_rows_updated_old          = 0;
458
373
static ulint    srv_n_rows_deleted_old          = 0;
459
374
static ulint    srv_n_rows_read_old             = 0;
 
375
#endif /* !UNIV_HOTBACKUP */
460
376
 
461
377
UNIV_INTERN ulint               srv_n_lock_wait_count           = 0;
462
378
UNIV_INTERN ulint               srv_n_lock_wait_current_count   = 0;
463
379
UNIV_INTERN ib_int64_t  srv_n_lock_wait_time            = 0;
464
380
UNIV_INTERN ulint               srv_n_lock_max_wait_time        = 0;
465
381
 
466
 
UNIV_INTERN ulint               srv_truncated_status_writes     = 0;
467
382
 
468
383
/*
469
384
  Set the following to 0 if you want InnoDB to write messages on
487
402
 
488
403
/* Mutex for locking srv_monitor_file */
489
404
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 register srv_innodb_monitor_mutex with performance schema */
495
 
UNIV_INTERN mysql_pfs_key_t     srv_innodb_monitor_mutex_key;
496
 
/* Key to register srv_monitor_file_mutex with performance schema */
497
 
UNIV_INTERN mysql_pfs_key_t     srv_monitor_file_mutex_key;
498
 
/* Key to register srv_dict_tmpfile_mutex with performance schema */
499
 
UNIV_INTERN mysql_pfs_key_t     srv_dict_tmpfile_mutex_key;
500
 
/* Key to register the mutex with performance schema */
501
 
UNIV_INTERN mysql_pfs_key_t     srv_misc_tmpfile_mutex_key;
502
 
#endif /* UNIV_PFS_MUTEX */
503
 
 
504
405
/* Temporary file for innodb monitor output */
505
406
UNIV_INTERN FILE*       srv_monitor_file;
506
407
/* Mutex for locking srv_dict_tmpfile.
519
420
UNIV_INTERN ulint       srv_main_thread_process_no      = 0;
520
421
UNIV_INTERN ulint       srv_main_thread_id              = 0;
521
422
 
522
 
/* The following count work done by srv_master_thread. */
523
 
 
524
 
/* Iterations by the 'once per second' loop. */
525
 
static ulint   srv_main_1_second_loops          = 0;
526
 
/* Calls to sleep by the 'once per second' loop. */
527
 
static ulint   srv_main_sleeps                  = 0;
528
 
/* Iterations by the 'once per 10 seconds' loop. */
529
 
static ulint   srv_main_10_second_loops         = 0;
530
 
/* Iterations of the loop bounded by the 'background_loop' label. */
531
 
static ulint   srv_main_background_loops        = 0;
532
 
/* Iterations of the loop bounded by the 'flush_loop' label. */
533
 
static ulint   srv_main_flush_loops             = 0;
534
 
/* Log writes involving flush. */
535
 
static ulint   srv_log_writes_and_flush         = 0;
536
 
 
537
 
/* This is only ever touched by the master thread. It records the
538
 
time when the last flush of log file has happened. The master
539
 
thread ensures that we flush the log files at least once per
540
 
second. */
541
 
static time_t   srv_last_log_flush_time;
542
 
 
543
 
/* The master thread performs various tasks based on the current
544
 
state of IO activity and the level of IO utilization is past
545
 
intervals. Following macros define thresholds for these conditions. */
546
 
#define SRV_PEND_IO_THRESHOLD   (PCT_IO(3))
547
 
#define SRV_RECENT_IO_ACTIVITY  (PCT_IO(5))
548
 
#define SRV_PAST_IO_ACTIVITY    (PCT_IO(200))
549
 
 
550
423
/*
551
424
        IMPLEMENTATION OF THE SERVER MAIN PROGRAM
552
425
        =========================================
717
590
 
718
591
/* Thread slot in the thread table */
719
592
struct srv_slot_struct{
720
 
        os_thread_id_t  id;             /*!< thread id */
721
 
        os_thread_t     handle;         /*!< thread handle */
722
 
        unsigned        type:3;         /*!< thread type: user, utility etc. */
723
 
        unsigned        in_use:1;       /*!< TRUE if this slot is in use */
724
 
        unsigned        suspended:1;    /*!< TRUE if the thread is waiting
 
593
        os_thread_id_t  id;             /* thread id */
 
594
        os_thread_t     handle;         /* thread handle */
 
595
        unsigned        type:3;         /* thread type: user, utility etc. */
 
596
        unsigned        in_use:1;       /* TRUE if this slot is in use */
 
597
        unsigned        suspended:1;    /* TRUE if the thread is waiting
725
598
                                        for the event of this slot */
726
 
        ib_time_t       suspend_time;   /*!< time when the thread was
 
599
        ib_time_t       suspend_time;   /* time when the thread was
727
600
                                        suspended */
728
 
        os_event_t      event;          /*!< event used in suspending the
 
601
        os_event_t      event;          /* event used in suspending the
729
602
                                        thread when it has nothing to do */
730
 
        que_thr_t*      thr;            /*!< suspended query thread (only
 
603
        que_thr_t*      thr;            /* suspended query thread (only
731
604
                                        used for MySQL threads) */
732
605
};
733
606
 
761
634
static ulint    srv_meter_foreground[SRV_MASTER + 1];
762
635
#endif
763
636
 
764
 
/***********************************************************************
765
 
Prints counters for work done by srv_master_thread. */
766
 
static
767
 
void
768
 
srv_print_master_thread_info(
769
 
/*=========================*/
770
 
        FILE  *file)    /* in: output stream */
771
 
{
772
 
        fprintf(file, "srv_master_thread loops: %lu 1_second, %lu sleeps, "
773
 
                "%lu 10_second, %lu background, %lu flush\n",
774
 
                srv_main_1_second_loops, srv_main_sleeps,
775
 
                srv_main_10_second_loops, srv_main_background_loops,
776
 
                srv_main_flush_loops);
777
 
        fprintf(file, "srv_master_thread log flush and writes: %lu\n",
778
 
                      srv_log_writes_and_flush);
779
 
}
780
 
 
781
637
/* The following values give info about the activity going on in
782
638
the database. They are protected by the server mutex. The arrays
783
639
are indexed by the type of the thread. */
785
641
UNIV_INTERN ulint       srv_n_threads_active[SRV_MASTER + 1];
786
642
UNIV_INTERN ulint       srv_n_threads[SRV_MASTER + 1];
787
643
 
788
 
/*********************************************************************//**
 
644
/*************************************************************************
789
645
Sets the info describing an i/o thread current state. */
790
646
UNIV_INTERN
791
647
void
792
648
srv_set_io_thread_op_info(
793
649
/*======================*/
794
 
        ulint           i,      /*!< in: the 'segment' of the i/o thread */
795
 
        const char*     str)    /*!< in: constant char string describing the
 
650
        ulint           i,      /* in: the 'segment' of the i/o thread */
 
651
        const char*     str)    /* in: constant char string describing the
796
652
                                state */
797
653
{
798
654
        ut_a(i < SRV_MAX_N_IO_THREADS);
800
656
        srv_io_thread_op_info[i] = str;
801
657
}
802
658
 
803
 
/*********************************************************************//**
 
659
/*************************************************************************
804
660
Accessor function to get pointer to n'th slot in the server thread
805
 
table.
806
 
@return pointer to the slot */
 
661
table. */
807
662
static
808
663
srv_slot_t*
809
664
srv_table_get_nth_slot(
810
665
/*===================*/
811
 
        ulint   index)          /*!< in: index of the slot */
 
666
                                /* out: pointer to the slot */
 
667
        ulint   index)          /* in: index of the slot */
812
668
{
813
669
        ut_a(index < OS_THREAD_MAX_N);
814
670
 
815
671
        return(srv_sys->threads + index);
816
672
}
817
673
 
818
 
/*********************************************************************//**
819
 
Gets the number of threads in the system.
820
 
@return sum of srv_n_threads[] */
 
674
#ifndef UNIV_HOTBACKUP
 
675
/*************************************************************************
 
676
Gets the number of threads in the system. */
821
677
UNIV_INTERN
822
678
ulint
823
679
srv_get_n_threads(void)
838
694
        return(n_threads);
839
695
}
840
696
 
841
 
/*********************************************************************//**
 
697
/*************************************************************************
842
698
Reserves a slot in the thread table for the current thread. Also creates the
843
699
thread local storage struct for the current thread. NOTE! The server mutex
844
 
has to be reserved by the caller!
845
 
@return reserved slot index */
 
700
has to be reserved by the caller! */
846
701
static
847
702
ulint
848
703
srv_table_reserve_slot(
849
704
/*===================*/
850
 
        enum srv_thread_type    type)   /*!< in: type of the thread */
 
705
                                        /* out: reserved slot index */
 
706
        enum srv_thread_type    type)   /* in: type of the thread */
851
707
{
852
708
        srv_slot_t*     slot;
853
709
        ulint           i;
878
734
        return(i);
879
735
}
880
736
 
881
 
/*********************************************************************//**
 
737
/*************************************************************************
882
738
Suspends the calling thread to wait for the event in its thread slot.
883
 
NOTE! The server mutex has to be reserved by the caller!
884
 
@return event for the calling thread to wait */
 
739
NOTE! The server mutex has to be reserved by the caller! */
885
740
static
886
741
os_event_t
887
742
srv_suspend_thread(void)
888
743
/*====================*/
 
744
                        /* out: event for the calling thread to wait */
889
745
{
890
746
        srv_slot_t*             slot;
891
747
        os_event_t              event;
921
777
 
922
778
        return(event);
923
779
}
 
780
#endif /* !UNIV_HOTBACKUP */
924
781
 
925
 
/*********************************************************************//**
 
782
/*************************************************************************
926
783
Releases threads of the type given from suspension in the thread table.
927
 
NOTE! The server mutex has to be reserved by the caller!
928
 
@return number of threads released: this may be less than n if not
929
 
enough threads were suspended at the moment */
 
784
NOTE! The server mutex has to be reserved by the caller! */
930
785
UNIV_INTERN
931
786
ulint
932
787
srv_release_threads(
933
788
/*================*/
934
 
        enum srv_thread_type    type,   /*!< in: thread type */
935
 
        ulint                   n)      /*!< in: number of threads to release */
 
789
                                        /* out: number of threads
 
790
                                        released: this may be < n if
 
791
                                        not enough threads were
 
792
                                        suspended at the moment */
 
793
        enum srv_thread_type    type,   /* in: thread type */
 
794
        ulint                   n)      /* in: number of threads to release */
936
795
{
937
796
        srv_slot_t*     slot;
938
797
        ulint           i;
974
833
        return(count);
975
834
}
976
835
 
977
 
/*********************************************************************//**
978
 
Returns the calling thread type.
979
 
@return SRV_COM, ... */
 
836
/*************************************************************************
 
837
Returns the calling thread type. */
980
838
UNIV_INTERN
981
839
enum srv_thread_type
982
840
srv_get_thread_type(void)
983
841
/*=====================*/
 
842
                        /* out: SRV_COM, ... */
984
843
{
985
844
        ulint                   slot_no;
986
845
        srv_slot_t*             slot;
1002
861
        return(type);
1003
862
}
1004
863
 
1005
 
/*********************************************************************//**
 
864
/*************************************************************************
1006
865
Initializes the server. */
1007
866
UNIV_INTERN
1008
867
void
1011
870
{
1012
871
        srv_conc_slot_t*        conc_slot;
1013
872
        srv_slot_t*             slot;
 
873
        dict_table_t*           table;
1014
874
        ulint                   i;
1015
875
 
1016
876
        srv_sys = mem_alloc(sizeof(srv_sys_t));
1017
877
 
1018
878
        kernel_mutex_temp = mem_alloc(sizeof(mutex_t));
1019
 
        mutex_create(kernel_mutex_key, &kernel_mutex, SYNC_KERNEL);
 
879
        mutex_create(&kernel_mutex, SYNC_KERNEL);
1020
880
 
1021
 
        mutex_create(srv_innodb_monitor_mutex_key,
1022
 
                     &srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
 
881
        mutex_create(&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
1023
882
 
1024
883
        srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1025
884
 
1057
916
 
1058
917
        UT_LIST_INIT(srv_sys->tasks);
1059
918
 
1060
 
        /* Create dummy indexes for infimum and supremum records */
1061
 
 
1062
 
        dict_ind_init();
 
919
        /* create dummy table and index for old-style infimum and supremum */
 
920
        table = dict_mem_table_create("SYS_DUMMY1",
 
921
                                      DICT_HDR_SPACE, 1, 0);
 
922
        dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
 
923
                               DATA_ENGLISH | DATA_NOT_NULL, 8);
 
924
 
 
925
        srv_sys->dummy_ind1 = dict_mem_index_create(
 
926
                "SYS_DUMMY1", "SYS_DUMMY1", DICT_HDR_SPACE, 0, 1);
 
927
        dict_index_add_col(srv_sys->dummy_ind1, table,
 
928
                           dict_table_get_nth_col(table, 0), 0);
 
929
        srv_sys->dummy_ind1->table = table;
 
930
        /* create dummy table and index for new-style infimum and supremum */
 
931
        table = dict_mem_table_create("SYS_DUMMY2",
 
932
                                      DICT_HDR_SPACE, 1, DICT_TF_COMPACT);
 
933
        dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
 
934
                               DATA_ENGLISH | DATA_NOT_NULL, 8);
 
935
        srv_sys->dummy_ind2 = dict_mem_index_create(
 
936
                "SYS_DUMMY2", "SYS_DUMMY2", DICT_HDR_SPACE, 0, 1);
 
937
        dict_index_add_col(srv_sys->dummy_ind2, table,
 
938
                           dict_table_get_nth_col(table, 0), 0);
 
939
        srv_sys->dummy_ind2->table = table;
 
940
 
 
941
        /* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */
 
942
        srv_sys->dummy_ind1->cached = srv_sys->dummy_ind2->cached = TRUE;
1063
943
 
1064
944
        /* Init the server concurrency restriction data structures */
1065
945
 
1080
960
        trx_i_s_cache_init(trx_i_s_cache);
1081
961
}
1082
962
 
1083
 
/*********************************************************************//**
1084
 
Frees the data structures created in srv_init(). */
 
963
/*************************************************************************
 
964
Frees the OS fast mutex created in srv_init(). */
1085
965
UNIV_INTERN
1086
966
void
1087
967
srv_free(void)
1088
968
/*==========*/
1089
969
{
1090
970
        os_fast_mutex_free(&srv_conc_mutex);
1091
 
        mem_free(srv_conc_slots);
1092
 
        srv_conc_slots = NULL;
1093
 
 
1094
 
        mem_free(srv_sys->threads);
1095
 
        mem_free(srv_sys);
1096
 
        srv_sys = NULL;
1097
 
 
1098
 
        mem_free(kernel_mutex_temp);
1099
 
        kernel_mutex_temp = NULL;
1100
 
        mem_free(srv_mysql_table);
1101
 
        srv_mysql_table = NULL;
1102
 
 
1103
 
        trx_i_s_cache_free(trx_i_s_cache);
1104
971
}
1105
972
 
1106
 
/*********************************************************************//**
 
973
/*************************************************************************
1107
974
Initializes the synchronization primitives, memory system, and the thread
1108
975
local storage. */
1109
976
UNIV_INTERN
1111
978
srv_general_init(void)
1112
979
/*==================*/
1113
980
{
1114
 
        ut_mem_init();
1115
 
        /* Reset the system variables in the recovery module. */
1116
 
        recv_sys_var_init();
1117
981
        os_sync_init();
1118
982
        sync_init();
1119
983
        mem_init(srv_mem_pool_size);
1125
989
/* Maximum allowable purge history length.  <=0 means 'infinite'. */
1126
990
UNIV_INTERN ulong       srv_max_purge_lag               = 0;
1127
991
 
1128
 
/*********************************************************************//**
 
992
/*************************************************************************
1129
993
Puts an OS thread to wait if there are too many concurrent threads
1130
994
(>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
1131
995
UNIV_INTERN
1132
996
void
1133
997
srv_conc_enter_innodb(
1134
998
/*==================*/
1135
 
        trx_t*  trx)    /*!< in: transaction object associated with the
 
999
        trx_t*  trx)    /* in: transaction object associated with the
1136
1000
                        thread */
1137
1001
{
1138
1002
        ibool                   has_slept = FALSE;
1172
1036
                return;
1173
1037
        }
1174
1038
 
1175
 
        ut_ad(srv_conc_n_threads >= 0);
1176
 
 
1177
1039
        if (srv_conc_n_threads < (lint)srv_thread_concurrency) {
1178
1040
 
1179
1041
                srv_conc_n_threads++;
1285
1147
        os_fast_mutex_unlock(&srv_conc_mutex);
1286
1148
}
1287
1149
 
1288
 
/*********************************************************************//**
 
1150
/*************************************************************************
1289
1151
This lets a thread enter InnoDB regardless of the number of threads inside
1290
1152
InnoDB. This must be called when a thread ends a lock wait. */
1291
1153
UNIV_INTERN
1292
1154
void
1293
1155
srv_conc_force_enter_innodb(
1294
1156
/*========================*/
1295
 
        trx_t*  trx)    /*!< in: transaction object associated with the
 
1157
        trx_t*  trx)    /* in: transaction object associated with the
1296
1158
                        thread */
1297
1159
{
1298
1160
        if (UNIV_LIKELY(!srv_thread_concurrency)) {
1300
1162
                return;
1301
1163
        }
1302
1164
 
1303
 
        ut_ad(srv_conc_n_threads >= 0);
1304
 
 
1305
1165
        os_fast_mutex_lock(&srv_conc_mutex);
1306
1166
 
1307
1167
        srv_conc_n_threads++;
1311
1171
        os_fast_mutex_unlock(&srv_conc_mutex);
1312
1172
}
1313
1173
 
1314
 
/*********************************************************************//**
 
1174
/*************************************************************************
1315
1175
This must be called when a thread exits InnoDB in a lock wait or at the
1316
1176
end of an SQL statement. */
1317
1177
UNIV_INTERN
1318
1178
void
1319
1179
srv_conc_force_exit_innodb(
1320
1180
/*=======================*/
1321
 
        trx_t*  trx)    /*!< in: transaction object associated with the
 
1181
        trx_t*  trx)    /* in: transaction object associated with the
1322
1182
                        thread */
1323
1183
{
1324
1184
        srv_conc_slot_t*        slot    = NULL;
1325
1185
 
 
1186
        if (UNIV_LIKELY(!srv_thread_concurrency)) {
 
1187
 
 
1188
                return;
 
1189
        }
 
1190
 
1326
1191
        if (trx->mysql_thd != NULL
1327
1192
            && thd_is_replication_slave_thread(trx->mysql_thd)) {
1328
1193
 
1336
1201
 
1337
1202
        os_fast_mutex_lock(&srv_conc_mutex);
1338
1203
 
1339
 
        ut_ad(srv_conc_n_threads > 0);
1340
1204
        srv_conc_n_threads--;
1341
1205
        trx->declared_to_be_inside_innodb = FALSE;
1342
1206
        trx->n_tickets_to_enter_innodb = 0;
1368
1232
        }
1369
1233
}
1370
1234
 
1371
 
/*********************************************************************//**
 
1235
/*************************************************************************
1372
1236
This must be called when a thread exits InnoDB. */
1373
1237
UNIV_INTERN
1374
1238
void
1375
1239
srv_conc_exit_innodb(
1376
1240
/*=================*/
1377
 
        trx_t*  trx)    /*!< in: transaction object associated with the
 
1241
        trx_t*  trx)    /* in: transaction object associated with the
1378
1242
                        thread */
1379
1243
{
1380
1244
        if (trx->n_tickets_to_enter_innodb > 0) {
1393
1257
 
1394
1258
/*========================================================================*/
1395
1259
 
1396
 
/*********************************************************************//**
1397
 
Normalizes init parameter values to use units we use inside InnoDB.
1398
 
@return DB_SUCCESS or error code */
 
1260
/*************************************************************************
 
1261
Normalizes init parameter values to use units we use inside InnoDB. */
1399
1262
static
1400
1263
ulint
1401
1264
srv_normalize_init_values(void)
1402
1265
/*===========================*/
 
1266
                                /* out: DB_SUCCESS or error code */
1403
1267
{
1404
1268
        ulint   n;
1405
1269
        ulint   i;
1423
1287
        return(DB_SUCCESS);
1424
1288
}
1425
1289
 
1426
 
/*********************************************************************//**
1427
 
Boots the InnoDB server.
1428
 
@return DB_SUCCESS or error code */
 
1290
/*************************************************************************
 
1291
Boots the InnoDB server. */
1429
1292
UNIV_INTERN
1430
1293
ulint
1431
1294
srv_boot(void)
1432
1295
/*==========*/
 
1296
                        /* out: DB_SUCCESS or error code */
1433
1297
{
1434
1298
        ulint   err;
1435
1299
 
1454
1318
        return(DB_SUCCESS);
1455
1319
}
1456
1320
 
1457
 
/*********************************************************************//**
 
1321
#ifndef UNIV_HOTBACKUP
 
1322
/*************************************************************************
1458
1323
Reserves a slot in the thread table for the current MySQL OS thread.
1459
 
NOTE! The kernel mutex has to be reserved by the caller!
1460
 
@return reserved slot */
 
1324
NOTE! The kernel mutex has to be reserved by the caller! */
1461
1325
static
1462
1326
srv_slot_t*
1463
1327
srv_table_reserve_slot_for_mysql(void)
1464
1328
/*==================================*/
 
1329
                        /* out: reserved slot */
1465
1330
{
1466
1331
        srv_slot_t*     slot;
1467
1332
        ulint           i;
1518
1383
 
1519
1384
        return(slot);
1520
1385
}
 
1386
#endif /* !UNIV_HOTBACKUP */
1521
1387
 
1522
 
/***************************************************************//**
 
1388
/*******************************************************************
1523
1389
Puts a MySQL OS thread to wait for a lock to be released. If an error
1524
1390
occurs during the wait trx->error_state associated with thr is
1525
1391
!= DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
1529
1395
void
1530
1396
srv_suspend_mysql_thread(
1531
1397
/*=====================*/
1532
 
        que_thr_t*      thr)    /*!< in: query thread associated with the MySQL
 
1398
        que_thr_t*      thr)    /* in: query thread associated with the MySQL
1533
1399
                                OS thread */
1534
1400
{
 
1401
#ifndef UNIV_HOTBACKUP
1535
1402
        srv_slot_t*     slot;
1536
1403
        os_event_t      event;
1537
1404
        double          wait_time;
1620
1487
                row_mysql_unfreeze_data_dictionary(trx);
1621
1488
                break;
1622
1489
        case RW_X_LATCH:
1623
 
                /* There should never be a lock wait when the
1624
 
                dictionary latch is reserved in X mode.  Dictionary
1625
 
                transactions should only acquire locks on dictionary
1626
 
                tables, not other tables. All access to dictionary
1627
 
                tables should be covered by dictionary
1628
 
                transactions. */
1629
 
                ut_print_timestamp(stderr);
1630
 
                fputs("  InnoDB: Error: dict X latch held in "
1631
 
                      "srv_suspend_mysql_thread\n", stderr);
1632
 
                /* This should never occur. This incorrect handling
1633
 
                was added in the early development of
1634
 
                ha_innobase::add_index() in InnoDB Plugin 1.0. */
1635
1490
                /* Release fast index creation latch */
1636
1491
                row_mysql_unlock_data_dictionary(trx);
1637
1492
                break;
1639
1494
 
1640
1495
        ut_a(trx->dict_operation_lock_mode == 0);
1641
1496
 
1642
 
        /* Suspend this thread and wait for the event. */
 
1497
        /* Wait for the release */
1643
1498
 
1644
1499
        os_event_wait(event);
1645
1500
 
1646
 
        /* After resuming, reacquire the data dictionary latch if
1647
 
        necessary. */
1648
 
 
1649
1501
        switch (had_dict_lock) {
1650
1502
        case RW_S_LATCH:
1651
1503
                row_mysql_freeze_data_dictionary(trx);
1652
1504
                break;
1653
1505
        case RW_X_LATCH:
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
1506
                row_mysql_lock_data_dictionary(trx);
1658
1507
                break;
1659
1508
        }
1690
1539
                    start_time != -1 && finish_time != -1) {
1691
1540
                        srv_n_lock_max_wait_time = diff_time;
1692
1541
                }
1693
 
 
1694
 
                /* Record the lock wait time for this thread */
1695
 
                thd_set_lock_wait_time(trx->mysql_thd, diff_time);
1696
1542
        }
1697
1543
 
1698
1544
        if (trx->was_chosen_as_deadlock_victim) {
1714
1560
 
1715
1561
                trx->error_state = DB_LOCK_WAIT_TIMEOUT;
1716
1562
        }
1717
 
 
1718
 
        if (trx_is_interrupted(trx)) {
1719
 
 
1720
 
                trx->error_state = DB_INTERRUPTED;
1721
 
        }
 
1563
#else /* UNIV_HOTBACKUP */
 
1564
        /* This function depends on MySQL code that is not included in
 
1565
        InnoDB Hot Backup builds.  Besides, this function should never
 
1566
        be called in InnoDB Hot Backup. */
 
1567
        ut_error;
 
1568
#endif /* UNIV_HOTBACKUP */
1722
1569
}
1723
1570
 
1724
 
/********************************************************************//**
 
1571
/************************************************************************
1725
1572
Releases a MySQL OS thread waiting for a lock to be released, if the
1726
1573
thread is already suspended. */
1727
1574
UNIV_INTERN
1728
1575
void
1729
1576
srv_release_mysql_thread_if_suspended(
1730
1577
/*==================================*/
1731
 
        que_thr_t*      thr)    /*!< in: query thread associated with the
 
1578
        que_thr_t*      thr)    /* in: query thread associated with the
1732
1579
                                MySQL OS thread  */
1733
1580
{
 
1581
#ifndef UNIV_HOTBACKUP
1734
1582
        srv_slot_t*     slot;
1735
1583
        ulint           i;
1736
1584
 
1750
1598
        }
1751
1599
 
1752
1600
        /* not found */
 
1601
#else /* UNIV_HOTBACKUP */
 
1602
        /* This function depends on MySQL code that is not included in
 
1603
        InnoDB Hot Backup builds.  Besides, this function should never
 
1604
        be called in InnoDB Hot Backup. */
 
1605
        ut_error;
 
1606
#endif /* UNIV_HOTBACKUP */
1753
1607
}
1754
1608
 
1755
 
/******************************************************************//**
 
1609
#ifndef UNIV_HOTBACKUP
 
1610
/**********************************************************************
1756
1611
Refreshes the values used to calculate per-second averages. */
1757
1612
static
1758
1613
void
1770
1625
 
1771
1626
        log_refresh_stats();
1772
1627
 
1773
 
        buf_refresh_io_stats_all();
 
1628
        buf_refresh_io_stats();
1774
1629
 
1775
1630
        srv_n_rows_inserted_old = srv_n_rows_inserted;
1776
1631
        srv_n_rows_updated_old = srv_n_rows_updated;
1780
1635
        mutex_exit(&srv_innodb_monitor_mutex);
1781
1636
}
1782
1637
 
1783
 
/******************************************************************//**
1784
 
Outputs to a file the output of the InnoDB Monitor.
1785
 
@return FALSE if not all information printed
1786
 
due to failure to obtain necessary mutex */
 
1638
/**********************************************************************
 
1639
Outputs to a file the output of the InnoDB Monitor. */
1787
1640
UNIV_INTERN
1788
 
ibool
 
1641
void
1789
1642
srv_printf_innodb_monitor(
1790
1643
/*======================*/
1791
 
        FILE*   file,           /*!< in: output stream */
1792
 
        ibool   nowait,         /*!< in: whether to wait for kernel mutex */
1793
 
        ulint*  trx_start,      /*!< out: file position of the start of
 
1644
        FILE*   file,           /* in: output stream */
 
1645
        ulint*  trx_start,      /* out: file position of the start of
1794
1646
                                the list of active transactions */
1795
 
        ulint*  trx_end)        /*!< out: file position of the end of
 
1647
        ulint*  trx_end)        /* out: file position of the end of
1796
1648
                                the list of active transactions */
1797
1649
{
1798
1650
        double  time_elapsed;
1799
1651
        time_t  current_time;
1800
1652
        ulint   n_reserved;
1801
 
        ibool   ret;
1802
1653
 
1803
1654
        mutex_enter(&srv_innodb_monitor_mutex);
1804
1655
 
1822
1673
                "Per second averages calculated from the last %lu seconds\n",
1823
1674
                (ulong)time_elapsed);
1824
1675
 
1825
 
        fputs("-----------------\n"
1826
 
              "BACKGROUND THREAD\n"
1827
 
              "-----------------\n", file);
1828
 
        srv_print_master_thread_info(file);
1829
 
 
1830
1676
        fputs("----------\n"
1831
1677
              "SEMAPHORES\n"
1832
1678
              "----------\n", file);
1848
1694
 
1849
1695
        mutex_exit(&dict_foreign_err_mutex);
1850
1696
 
1851
 
        /* Only if lock_print_info_summary proceeds correctly,
1852
 
        before we call the lock_print_info_all_transactions
1853
 
        to print all the lock information. */
1854
 
        ret = lock_print_info_summary(file, nowait);
1855
 
 
1856
 
        if (ret) {
1857
 
                if (trx_start) {
1858
 
                        long    t = ftell(file);
1859
 
                        if (t < 0) {
1860
 
                                *trx_start = ULINT_UNDEFINED;
1861
 
                        } else {
1862
 
                                *trx_start = (ulint) t;
1863
 
                        }
1864
 
                }
1865
 
                lock_print_info_all_transactions(file);
1866
 
                if (trx_end) {
1867
 
                        long    t = ftell(file);
1868
 
                        if (t < 0) {
1869
 
                                *trx_end = ULINT_UNDEFINED;
1870
 
                        } else {
1871
 
                                *trx_end = (ulint) t;
1872
 
                        }
1873
 
                }
1874
 
        }
1875
 
 
 
1697
        lock_print_info_summary(file);
 
1698
        if (trx_start) {
 
1699
                long    t = ftell(file);
 
1700
                if (t < 0) {
 
1701
                        *trx_start = ULINT_UNDEFINED;
 
1702
                } else {
 
1703
                        *trx_start = (ulint) t;
 
1704
                }
 
1705
        }
 
1706
        lock_print_info_all_transactions(file);
 
1707
        if (trx_end) {
 
1708
                long    t = ftell(file);
 
1709
                if (t < 0) {
 
1710
                        *trx_end = ULINT_UNDEFINED;
 
1711
                } else {
 
1712
                        *trx_end = (ulint) t;
 
1713
                }
 
1714
        }
1876
1715
        fputs("--------\n"
1877
1716
              "FILE I/O\n"
1878
1717
              "--------\n", file);
1970
1809
              "============================\n", file);
1971
1810
        mutex_exit(&srv_innodb_monitor_mutex);
1972
1811
        fflush(file);
1973
 
 
1974
 
        return(ret);
1975
1812
}
1976
1813
 
1977
 
/******************************************************************//**
 
1814
/**********************************************************************
1978
1815
Function to pass InnoDB status variables to MySQL */
1979
1816
UNIV_INTERN
1980
1817
void
1981
1818
srv_export_innodb_status(void)
1982
 
/*==========================*/
1983
1819
{
1984
 
        buf_pool_stat_t stat;
1985
 
        ulint           LRU_len;
1986
 
        ulint           free_len;
1987
 
        ulint           flush_list_len;
1988
 
 
1989
 
        buf_get_total_stat(&stat);
1990
 
        buf_get_total_list_len(&LRU_len, &free_len, &flush_list_len);
1991
 
 
1992
1820
        mutex_enter(&srv_innodb_monitor_mutex);
1993
1821
 
1994
1822
        export_vars.innodb_data_pending_reads
2003
1831
        export_vars.innodb_data_reads = os_n_file_reads;
2004
1832
        export_vars.innodb_data_writes = os_n_file_writes;
2005
1833
        export_vars.innodb_data_written = srv_data_written;
2006
 
        export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
 
1834
        export_vars.innodb_buffer_pool_read_requests = buf_pool->n_page_gets;
2007
1835
        export_vars.innodb_buffer_pool_write_requests
2008
1836
                = srv_buf_pool_write_requests;
2009
1837
        export_vars.innodb_buffer_pool_wait_free = srv_buf_pool_wait_free;
2010
1838
        export_vars.innodb_buffer_pool_pages_flushed = srv_buf_pool_flushed;
2011
1839
        export_vars.innodb_buffer_pool_reads = srv_buf_pool_reads;
2012
 
        export_vars.innodb_buffer_pool_read_ahead
2013
 
                = stat.n_ra_pages_read;
2014
 
        export_vars.innodb_buffer_pool_read_ahead_evicted
2015
 
                = stat.n_ra_pages_evicted;
2016
 
        export_vars.innodb_buffer_pool_pages_data = LRU_len;
2017
 
        export_vars.innodb_buffer_pool_pages_dirty = flush_list_len;
2018
 
        export_vars.innodb_buffer_pool_pages_free = free_len;
 
1840
        export_vars.innodb_buffer_pool_read_ahead_rnd = srv_read_ahead_rnd;
 
1841
        export_vars.innodb_buffer_pool_read_ahead_seq = srv_read_ahead_seq;
 
1842
        export_vars.innodb_buffer_pool_pages_data
 
1843
                = UT_LIST_GET_LEN(buf_pool->LRU);
 
1844
        export_vars.innodb_buffer_pool_pages_dirty
 
1845
                = UT_LIST_GET_LEN(buf_pool->flush_list);
 
1846
        export_vars.innodb_buffer_pool_pages_free
 
1847
                = UT_LIST_GET_LEN(buf_pool->free);
2019
1848
#ifdef UNIV_DEBUG
2020
1849
        export_vars.innodb_buffer_pool_pages_latched
2021
1850
                = buf_get_latched_pages_number();
2022
1851
#endif /* UNIV_DEBUG */
2023
 
        export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
 
1852
        export_vars.innodb_buffer_pool_pages_total = buf_pool->curr_size;
2024
1853
 
2025
 
        export_vars.innodb_buffer_pool_pages_misc
2026
 
                = buf_pool_get_n_pages() - LRU_len - free_len;
2027
 
#ifdef HAVE_ATOMIC_BUILTINS
2028
 
        export_vars.innodb_have_atomic_builtins = 1;
2029
 
#else
2030
 
        export_vars.innodb_have_atomic_builtins = 0;
2031
 
#endif
 
1854
        export_vars.innodb_buffer_pool_pages_misc = buf_pool->curr_size
 
1855
                - UT_LIST_GET_LEN(buf_pool->LRU)
 
1856
                - UT_LIST_GET_LEN(buf_pool->free);
2032
1857
        export_vars.innodb_page_size = UNIV_PAGE_SIZE;
2033
1858
        export_vars.innodb_log_waits = srv_log_waits;
2034
1859
        export_vars.innodb_os_log_written = srv_os_log_written;
2039
1864
        export_vars.innodb_log_writes = srv_log_writes;
2040
1865
        export_vars.innodb_dblwr_pages_written = srv_dblwr_pages_written;
2041
1866
        export_vars.innodb_dblwr_writes = srv_dblwr_writes;
2042
 
        export_vars.innodb_pages_created = stat.n_pages_created;
2043
 
        export_vars.innodb_pages_read = stat.n_pages_read;
2044
 
        export_vars.innodb_pages_written = stat.n_pages_written;
 
1867
        export_vars.innodb_pages_created = buf_pool->n_pages_created;
 
1868
        export_vars.innodb_pages_read = buf_pool->n_pages_read;
 
1869
        export_vars.innodb_pages_written = buf_pool->n_pages_written;
2045
1870
        export_vars.innodb_row_lock_waits = srv_n_lock_wait_count;
2046
1871
        export_vars.innodb_row_lock_current_waits
2047
1872
                = srv_n_lock_wait_current_count;
2058
1883
        export_vars.innodb_rows_inserted = srv_n_rows_inserted;
2059
1884
        export_vars.innodb_rows_updated = srv_n_rows_updated;
2060
1885
        export_vars.innodb_rows_deleted = srv_n_rows_deleted;
2061
 
        export_vars.innodb_truncated_status_writes = srv_truncated_status_writes;
2062
1886
 
2063
1887
        mutex_exit(&srv_innodb_monitor_mutex);
2064
1888
}
2065
1889
 
2066
 
/*********************************************************************//**
2067
 
A thread which prints the info output by various InnoDB monitors.
2068
 
@return a dummy parameter */
 
1890
/*************************************************************************
 
1891
A thread which wakes up threads whose lock wait may have lasted too long.
 
1892
This also prints the info output by various InnoDB monitors. */
2069
1893
UNIV_INTERN
2070
1894
os_thread_ret_t
2071
 
srv_monitor_thread(
2072
 
/*===============*/
 
1895
srv_lock_timeout_and_monitor_thread(
 
1896
/*================================*/
 
1897
                        /* out: a dummy parameter */
2073
1898
        void*   arg __attribute__((unused)))
2074
 
                        /*!< in: a dummy parameter required by
 
1899
                        /* in: a dummy parameter required by
2075
1900
                        os_thread_create */
2076
1901
{
 
1902
        srv_slot_t*     slot;
2077
1903
        double          time_elapsed;
2078
1904
        time_t          current_time;
2079
1905
        time_t          last_table_monitor_time;
2080
1906
        time_t          last_tablespace_monitor_time;
2081
1907
        time_t          last_monitor_time;
2082
 
        ulint           mutex_skipped;
2083
 
        ibool           last_srv_print_monitor;
 
1908
        ibool           some_waits;
 
1909
        double          wait_time;
 
1910
        ulint           i;
2084
1911
 
2085
1912
#ifdef UNIV_DEBUG_THREAD_CREATION
2086
1913
        fprintf(stderr, "Lock timeout thread starts, id %lu\n",
2087
1914
                os_thread_pf(os_thread_get_curr_id()));
2088
1915
#endif
2089
 
 
2090
 
#ifdef UNIV_PFS_THREAD
2091
 
        pfs_register_thread(srv_monitor_thread_key);
2092
 
#endif
2093
 
 
2094
1916
        UT_NOT_USED(arg);
2095
1917
        srv_last_monitor_time = time(NULL);
2096
1918
        last_table_monitor_time = time(NULL);
2097
1919
        last_tablespace_monitor_time = time(NULL);
2098
1920
        last_monitor_time = time(NULL);
2099
 
        mutex_skipped = 0;
2100
 
        last_srv_print_monitor = srv_print_innodb_monitor;
2101
1921
loop:
2102
 
        srv_monitor_active = TRUE;
2103
 
 
2104
 
        /* Wake up every 5 seconds to see if we need to print
2105
 
        monitor information. */
2106
 
 
2107
 
        os_thread_sleep(5000000);
 
1922
        srv_lock_timeout_and_monitor_active = TRUE;
 
1923
 
 
1924
        /* When someone is waiting for a lock, we wake up every second
 
1925
        and check if a timeout has passed for a lock wait */
 
1926
 
 
1927
        os_thread_sleep(1000000);
2108
1928
 
2109
1929
        current_time = time(NULL);
2110
1930
 
2114
1934
                last_monitor_time = time(NULL);
2115
1935
 
2116
1936
                if (srv_print_innodb_monitor) {
2117
 
                        /* Reset mutex_skipped counter everytime
2118
 
                        srv_print_innodb_monitor changes. This is to
2119
 
                        ensure we will not be blocked by kernel_mutex
2120
 
                        for short duration information printing,
2121
 
                        such as requested by sync_array_print_long_waits() */
2122
 
                        if (!last_srv_print_monitor) {
2123
 
                                mutex_skipped = 0;
2124
 
                                last_srv_print_monitor = TRUE;
2125
 
                        }
2126
 
 
2127
 
                        if (!srv_printf_innodb_monitor(stderr,
2128
 
                                                MUTEX_NOWAIT(mutex_skipped),
2129
 
                                                NULL, NULL)) {
2130
 
                                mutex_skipped++;
2131
 
                        } else {
2132
 
                                /* Reset the counter */
2133
 
                                mutex_skipped = 0;
2134
 
                        }
2135
 
                } else {
2136
 
                        last_srv_print_monitor = FALSE;
 
1937
                        srv_printf_innodb_monitor(stderr, NULL, NULL);
2137
1938
                }
2138
1939
 
2139
 
 
2140
1940
                if (srv_innodb_status) {
2141
1941
                        mutex_enter(&srv_monitor_file_mutex);
2142
1942
                        rewind(srv_monitor_file);
2143
 
                        if (!srv_printf_innodb_monitor(srv_monitor_file,
2144
 
                                                MUTEX_NOWAIT(mutex_skipped),
2145
 
                                                NULL, NULL)) {
2146
 
                                mutex_skipped++;
2147
 
                        } else {
2148
 
                                mutex_skipped = 0;
2149
 
                        }
2150
 
 
 
1943
                        srv_printf_innodb_monitor(srv_monitor_file, NULL,
 
1944
                                                  NULL);
2151
1945
                        os_file_set_eof(srv_monitor_file);
2152
1946
                        mutex_exit(&srv_monitor_file_mutex);
2153
1947
                }
2200
1994
                }
2201
1995
        }
2202
1996
 
2203
 
        if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
2204
 
                goto exit_func;
2205
 
        }
2206
 
 
2207
 
        if (srv_print_innodb_monitor
2208
 
            || srv_print_innodb_lock_monitor
2209
 
            || srv_print_innodb_tablespace_monitor
2210
 
            || srv_print_innodb_table_monitor) {
2211
 
                goto loop;
2212
 
        }
2213
 
 
2214
 
        srv_monitor_active = FALSE;
2215
 
 
2216
 
        goto loop;
2217
 
 
2218
 
exit_func:
2219
 
        srv_monitor_active = FALSE;
2220
 
 
2221
 
        /* We count the number of threads in os_thread_exit(). A created
2222
 
        thread should always use that to exit and not use return() to exit. */
2223
 
 
2224
 
        os_thread_exit(NULL);
2225
 
 
2226
 
        OS_THREAD_DUMMY_RETURN;
2227
 
}
2228
 
 
2229
 
/*********************************************************************//**
2230
 
A thread which wakes up threads whose lock wait may have lasted too long.
2231
 
@return a dummy parameter */
2232
 
UNIV_INTERN
2233
 
os_thread_ret_t
2234
 
srv_lock_timeout_thread(
2235
 
/*====================*/
2236
 
        void*   arg __attribute__((unused)))
2237
 
                        /* in: a dummy parameter required by
2238
 
                        os_thread_create */
2239
 
{
2240
 
        srv_slot_t*     slot;
2241
 
        ibool           some_waits;
2242
 
        double          wait_time;
2243
 
        ulint           i;
2244
 
 
2245
 
#ifdef UNIV_PFS_THREAD
2246
 
        pfs_register_thread(srv_lock_timeout_thread_key);
2247
 
#endif
2248
 
 
2249
 
loop:
2250
 
        /* When someone is waiting for a lock, we wake up every second
2251
 
        and check if a timeout has passed for a lock wait */
2252
 
 
2253
 
        os_thread_sleep(1000000);
2254
 
 
2255
 
        srv_lock_timeout_active = TRUE;
2256
 
 
2257
1997
        mutex_enter(&kernel_mutex);
2258
1998
 
2259
1999
        some_waits = FALSE;
2277
2017
                        lock_wait_timeout = thd_lock_wait_timeout(
2278
2018
                                trx->mysql_thd);
2279
2019
 
2280
 
                        if (trx_is_interrupted(trx)
2281
 
                            || (lock_wait_timeout < 100000000
2282
 
                                && (wait_time > (double) lock_wait_timeout
2283
 
                                    || wait_time < 0))) {
 
2020
                        if (lock_wait_timeout < 100000000
 
2021
                            && (wait_time > (double) lock_wait_timeout
 
2022
                                || wait_time < 0)) {
2284
2023
 
2285
2024
                                /* Timeout exceeded or a wrap-around in system
2286
2025
                                time counter: cancel the lock request queued
2305
2044
                goto exit_func;
2306
2045
        }
2307
2046
 
2308
 
        if (some_waits) {
 
2047
        if (some_waits || srv_print_innodb_monitor
 
2048
            || srv_print_innodb_lock_monitor
 
2049
            || srv_print_innodb_tablespace_monitor
 
2050
            || srv_print_innodb_table_monitor) {
2309
2051
                goto loop;
2310
2052
        }
2311
2053
 
2312
 
        srv_lock_timeout_active = FALSE;
 
2054
        /* No one was waiting for a lock and no monitor was active:
 
2055
        suspend this thread */
 
2056
 
 
2057
        srv_lock_timeout_and_monitor_active = FALSE;
2313
2058
 
2314
2059
#if 0
2315
2060
        /* The following synchronisation is disabled, since
2319
2064
        goto loop;
2320
2065
 
2321
2066
exit_func:
2322
 
        srv_lock_timeout_active = FALSE;
 
2067
        srv_lock_timeout_and_monitor_active = FALSE;
2323
2068
 
2324
2069
        /* We count the number of threads in os_thread_exit(). A created
2325
2070
        thread should always use that to exit and not use return() to exit. */
2329
2074
        OS_THREAD_DUMMY_RETURN;
2330
2075
}
2331
2076
 
2332
 
/*********************************************************************//**
 
2077
/*************************************************************************
2333
2078
A thread which prints warnings about semaphore waits which have lasted
2334
 
too long. These can be used to track bugs which cause hangs.
2335
 
@return a dummy parameter */
 
2079
too long. These can be used to track bugs which cause hangs. */
2336
2080
UNIV_INTERN
2337
2081
os_thread_ret_t
2338
2082
srv_error_monitor_thread(
2339
2083
/*=====================*/
 
2084
                        /* out: a dummy parameter */
2340
2085
        void*   arg __attribute__((unused)))
2341
 
                        /*!< in: a dummy parameter required by
 
2086
                        /* in: a dummy parameter required by
2342
2087
                        os_thread_create */
2343
2088
{
2344
2089
        /* number of successive fatal timeouts observed */
2352
2097
        fprintf(stderr, "Error monitor thread starts, id %lu\n",
2353
2098
                os_thread_pf(os_thread_get_curr_id()));
2354
2099
#endif
2355
 
 
2356
 
#ifdef UNIV_PFS_THREAD
2357
 
        pfs_register_thread(srv_error_monitor_thread_key);
2358
 
#endif
2359
 
 
2360
2100
loop:
2361
2101
        srv_error_monitor_active = TRUE;
2362
2102
 
2386
2126
        }
2387
2127
 
2388
2128
        /* Update the statistics collected for deciding LRU
2389
 
        eviction policy. */
 
2129
        eviction policy. */
2390
2130
        buf_LRU_stat_update();
2391
2131
 
2392
 
        /* Update the statistics collected for flush rate policy. */
2393
 
        buf_flush_stat_update();
2394
 
 
2395
2132
        /* In case mutex_exit is not a memory barrier, it is
2396
2133
        theoretically possible some threads are left waiting though
2397
2134
        the semaphore is already released. Wake up those threads: */
2398
 
 
 
2135
        
2399
2136
        sync_arr_wake_threads_if_sema_free();
2400
2137
 
2401
2138
        if (sync_array_print_long_waits()) {
2437
2174
        OS_THREAD_DUMMY_RETURN;
2438
2175
}
2439
2176
 
2440
 
/**********************************************************************//**
2441
 
Check whether any background thread is active.
2442
 
@return FALSE if all are are suspended or have exited. */
2443
 
UNIV_INTERN
2444
 
ibool
2445
 
srv_is_any_background_thread_active(void)
2446
 
/*=====================================*/
2447
 
{
2448
 
        ulint   i;
2449
 
        ibool   ret = FALSE;
2450
 
 
2451
 
        mutex_enter(&kernel_mutex);
2452
 
 
2453
 
        for (i = SRV_COM; i <= SRV_MASTER; ++i) {
2454
 
                if (srv_n_threads_active[i] != 0) {
2455
 
                        ret = TRUE;
2456
 
                        break;
2457
 
                }
2458
 
        }
2459
 
 
2460
 
        mutex_exit(&kernel_mutex);
2461
 
 
2462
 
        return(ret);
2463
 
}
2464
 
 
2465
 
/*******************************************************************//**
 
2177
/***********************************************************************
2466
2178
Tells the InnoDB server that there has been activity in the database
2467
2179
and wakes up the master thread if it is suspended (not sleeping). Used
2468
2180
in the MySQL interface. Note that there is a small chance that the master
2469
 
thread stays suspended (we do not protect our operation with the
2470
 
srv_sys_t->mutex, for performance reasons). */
 
2181
thread stays suspended (we do not protect our operation with the kernel
 
2182
mutex, for performace reasons). */
2471
2183
UNIV_INTERN
2472
2184
void
2473
2185
srv_active_wake_master_thread(void)
2485
2197
        }
2486
2198
}
2487
2199
 
2488
 
/*******************************************************************//**
2489
 
Tells the purge thread that there has been activity in the database
2490
 
and wakes up the purge thread if it is suspended (not sleeping).  Note
2491
 
that there is a small chance that the purge thread stays suspended
2492
 
(we do not protect our operation with the kernel mutex, for
2493
 
performace reasons). */
2494
 
UNIV_INTERN
2495
 
void
2496
 
srv_wake_purge_thread_if_not_active(void)
2497
 
/*=====================================*/
2498
 
{
2499
 
        ut_ad(!mutex_own(&kernel_mutex));
2500
 
 
2501
 
        if (srv_n_purge_threads > 0
2502
 
            && srv_n_threads_active[SRV_WORKER] == 0) {
2503
 
 
2504
 
                mutex_enter(&kernel_mutex);
2505
 
 
2506
 
                srv_release_threads(SRV_WORKER, 1);
2507
 
 
2508
 
                mutex_exit(&kernel_mutex);
2509
 
        }
2510
 
}
2511
 
 
2512
 
/*******************************************************************//**
 
2200
/***********************************************************************
2513
2201
Wakes up the master thread if it is suspended or being suspended. */
2514
2202
UNIV_INTERN
2515
2203
void
2525
2213
        mutex_exit(&kernel_mutex);
2526
2214
}
2527
2215
 
2528
 
/*******************************************************************//**
2529
 
Wakes up the purge thread if it's not already awake. */
2530
 
UNIV_INTERN
2531
 
void
2532
 
srv_wake_purge_thread(void)
2533
 
/*=======================*/
2534
 
{
2535
 
        ut_ad(!mutex_own(&kernel_mutex));
2536
 
 
2537
 
        if (srv_n_purge_threads > 0) {
2538
 
 
2539
 
                mutex_enter(&kernel_mutex);
2540
 
 
2541
 
                srv_release_threads(SRV_WORKER, 1);
2542
 
 
2543
 
                mutex_exit(&kernel_mutex);
2544
 
        }
2545
 
}
2546
 
 
2547
 
/**********************************************************************
2548
 
The master thread is tasked to ensure that flush of log file happens
2549
 
once every second in the background. This is to ensure that not more
2550
 
than one second of trxs are lost in case of crash when
2551
 
innodb_flush_logs_at_trx_commit != 1 */
2552
 
static
2553
 
void
2554
 
srv_sync_log_buffer_in_background(void)
2555
 
/*===================================*/
2556
 
{
2557
 
        time_t  current_time = time(NULL);
2558
 
 
2559
 
        srv_main_thread_op_info = "flushing log";
2560
 
        if (difftime(current_time, srv_last_log_flush_time) >= 1) {
2561
 
                log_buffer_sync_in_background(TRUE);
2562
 
                srv_last_log_flush_time = current_time;
2563
 
                srv_log_writes_and_flush++;
2564
 
        }
2565
 
}
2566
 
 
2567
 
/********************************************************************//**
2568
 
Do a full purge, reconfigure the purge sub-system if a dynamic
2569
 
change is detected. */
2570
 
static
2571
 
void
2572
 
srv_master_do_purge(void)
2573
 
/*=====================*/
2574
 
{
2575
 
        ulint   n_pages_purged;
2576
 
 
2577
 
        ut_ad(!mutex_own(&kernel_mutex));
2578
 
 
2579
 
        ut_a(srv_n_purge_threads == 0);
2580
 
 
2581
 
        do {
2582
 
                /* Check for shutdown and change in purge config. */
2583
 
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2584
 
                        /* Nothing to purge. */
2585
 
                        n_pages_purged = 0;
2586
 
                } else {
2587
 
                        n_pages_purged = trx_purge(srv_purge_batch_size);
2588
 
                }
2589
 
 
2590
 
                srv_sync_log_buffer_in_background();
2591
 
 
2592
 
        } while (n_pages_purged > 0);
2593
 
}
2594
 
 
2595
 
/*********************************************************************//**
2596
 
The master thread controlling the server.
2597
 
@return a dummy parameter */
 
2216
/*************************************************************************
 
2217
The master thread controlling the server. */
2598
2218
UNIV_INTERN
2599
2219
os_thread_ret_t
2600
2220
srv_master_thread(
2601
2221
/*==============*/
 
2222
                        /* out: a dummy parameter */
2602
2223
        void*   arg __attribute__((unused)))
2603
 
                        /*!< in: a dummy parameter required by
 
2224
                        /* in: a dummy parameter required by
2604
2225
                        os_thread_create */
2605
2226
{
2606
 
        buf_pool_stat_t buf_stat;
2607
2227
        os_event_t      event;
 
2228
        time_t          last_flush_time;
 
2229
        time_t          current_time;
2608
2230
        ulint           old_activity_count;
2609
2231
        ulint           n_pages_purged  = 0;
2610
2232
        ulint           n_bytes_merged;
2615
2237
        ulint           n_ios_old;
2616
2238
        ulint           n_ios_very_old;
2617
2239
        ulint           n_pend_ios;
2618
 
        ulint           next_itr_time;
 
2240
        ibool           skip_sleep      = FALSE;
2619
2241
        ulint           i;
2620
2242
 
2621
2243
#ifdef UNIV_DEBUG_THREAD_CREATION
2622
2244
        fprintf(stderr, "Master thread starts, id %lu\n",
2623
2245
                os_thread_pf(os_thread_get_curr_id()));
2624
2246
#endif
2625
 
 
2626
 
#ifdef UNIV_PFS_THREAD
2627
 
        pfs_register_thread(srv_master_thread_key);
2628
 
#endif
2629
 
 
2630
2247
        srv_main_thread_process_no = os_proc_get_number();
2631
2248
        srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
2632
2249
 
2645
2262
 
2646
2263
        srv_main_thread_op_info = "reserving kernel mutex";
2647
2264
 
2648
 
        buf_get_total_stat(&buf_stat);
2649
 
        n_ios_very_old = log_sys->n_log_ios + buf_stat.n_pages_read
2650
 
                + buf_stat.n_pages_written;
 
2265
        n_ios_very_old = log_sys->n_log_ios + buf_pool->n_pages_read
 
2266
                + buf_pool->n_pages_written;
2651
2267
        mutex_enter(&kernel_mutex);
2652
2268
 
2653
2269
        /* Store the user activity counter at the start of this loop */
2663
2279
        /* ---- We run the following loop approximately once per second
2664
2280
        when there is database activity */
2665
2281
 
2666
 
        srv_last_log_flush_time = time(NULL);
2667
 
 
2668
 
        /* Sleep for 1 second on entrying the for loop below the first time. */
2669
 
        next_itr_time = ut_time_ms() + 1000;
 
2282
        skip_sleep = FALSE;
2670
2283
 
2671
2284
        for (i = 0; i < 10; i++) {
2672
 
                ulint   cur_time = ut_time_ms();
2673
 
 
2674
 
                buf_get_total_stat(&buf_stat);
2675
 
 
2676
 
                n_ios_old = log_sys->n_log_ios + buf_stat.n_pages_read
2677
 
                        + buf_stat.n_pages_written;
2678
 
 
 
2285
                n_ios_old = log_sys->n_log_ios + buf_pool->n_pages_read
 
2286
                        + buf_pool->n_pages_written;
2679
2287
                srv_main_thread_op_info = "sleeping";
2680
 
                srv_main_1_second_loops++;
2681
 
 
2682
 
                if (next_itr_time > cur_time) {
2683
 
 
2684
 
                        /* Get sleep interval in micro seconds. We use
2685
 
                        ut_min() to avoid long sleep in case of
2686
 
                        wrap around. */
2687
 
                        os_thread_sleep(ut_min(1000000,
2688
 
                                        (next_itr_time - cur_time)
2689
 
                                         * 1000));
2690
 
                        srv_main_sleeps++;
 
2288
 
 
2289
                if (!skip_sleep) {
 
2290
 
 
2291
                        os_thread_sleep(1000000);
2691
2292
                }
2692
2293
 
2693
 
                /* Each iteration should happen at 1 second interval. */
2694
 
                next_itr_time = ut_time_ms() + 1000;
 
2294
                skip_sleep = FALSE;
2695
2295
 
2696
2296
                /* ALTER TABLE in MySQL requires on Unix that the table handler
2697
2297
                can drop tables lazily after there no longer are SELECT
2708
2308
                        goto background_loop;
2709
2309
                }
2710
2310
 
2711
 
                /* Flush logs if needed */
2712
 
                srv_sync_log_buffer_in_background();
 
2311
                /* We flush the log once in a second even if no commit
 
2312
                is issued or the we have specified in my.cnf no flush
 
2313
                at transaction commit */
 
2314
 
 
2315
                srv_main_thread_op_info = "flushing log";
 
2316
                log_buffer_flush_to_disk();
2713
2317
 
2714
2318
                srv_main_thread_op_info = "making checkpoint";
2715
2319
                log_free_check();
2716
2320
 
2717
 
                /* If i/os during one second sleep were less than 5% of
2718
 
                capacity, we assume that there is free disk i/o capacity
2719
 
                available, and it makes sense to do an insert buffer merge. */
 
2321
                /* If there were less than 5 i/os during the
 
2322
                one second sleep, we assume that there is free
 
2323
                disk i/o capacity available, and it makes sense to
 
2324
                do an insert buffer merge. */
2720
2325
 
2721
 
                buf_get_total_stat(&buf_stat);
2722
2326
                n_pend_ios = buf_get_n_pending_ios()
2723
2327
                        + log_sys->n_pending_writes;
2724
 
                n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2725
 
                        + buf_stat.n_pages_written;
2726
 
                if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2727
 
                    && (n_ios - n_ios_old < SRV_RECENT_IO_ACTIVITY)) {
 
2328
                n_ios = log_sys->n_log_ios + buf_pool->n_pages_read
 
2329
                        + buf_pool->n_pages_written;
 
2330
                if (n_pend_ios < 3 && (n_ios - n_ios_old < 5)) {
2728
2331
                        srv_main_thread_op_info = "doing insert buffer merge";
2729
 
                        ibuf_contract_for_n_pages(FALSE, PCT_IO(5));
2730
 
 
2731
 
                        /* Flush logs if needed */
2732
 
                        srv_sync_log_buffer_in_background();
 
2332
                        ibuf_contract_for_n_pages(
 
2333
                                TRUE, srv_insert_buffer_batch_size / 4);
 
2334
 
 
2335
                        srv_main_thread_op_info = "flushing log";
 
2336
 
 
2337
                        log_buffer_flush_to_disk();
2733
2338
                }
2734
2339
 
2735
2340
                if (UNIV_UNLIKELY(buf_get_modified_ratio_pct()
2738
2343
                        /* Try to keep the number of modified pages in the
2739
2344
                        buffer pool under the limit wished by the user */
2740
2345
 
2741
 
                        srv_main_thread_op_info =
2742
 
                                "flushing buffer pool pages";
2743
 
                        n_pages_flushed = buf_flush_list(
2744
 
                                PCT_IO(100), IB_ULONGLONG_MAX);
2745
 
 
2746
 
                } else if (srv_adaptive_flushing) {
2747
 
 
2748
 
                        /* Try to keep the rate of flushing of dirty
2749
 
                        pages such that redo log generation does not
2750
 
                        produce bursts of IO at checkpoint time. */
2751
 
                        ulint n_flush = buf_flush_get_desired_flush_rate();
2752
 
 
2753
 
                        if (n_flush) {
2754
 
                                srv_main_thread_op_info =
2755
 
                                        "flushing buffer pool pages";
2756
 
                                n_flush = ut_min(PCT_IO(100), n_flush);
2757
 
                                n_pages_flushed =
2758
 
                                        buf_flush_list(
2759
 
                                                n_flush,
2760
 
                                                IB_ULONGLONG_MAX);
2761
 
                        }
 
2346
                        n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 100,
 
2347
                                                          IB_ULONGLONG_MAX);
 
2348
 
 
2349
                        /* If we had to do the flush, it may have taken
 
2350
                        even more than 1 second, and also, there may be more
 
2351
                        to flush. Do not sleep 1 second during the next
 
2352
                        iteration of this loop. */
 
2353
 
 
2354
                        skip_sleep = TRUE;
2762
2355
                }
2763
2356
 
2764
2357
                if (srv_activity_count == old_activity_count) {
2778
2371
        seconds */
2779
2372
        mem_validate_all_blocks();
2780
2373
#endif
2781
 
        /* If i/os during the 10 second period were less than 200% of
2782
 
        capacity, we assume that there is free disk i/o capacity
2783
 
        available, and it makes sense to flush srv_io_capacity pages.
2784
 
 
2785
 
        Note that this is done regardless of the fraction of dirty
2786
 
        pages relative to the max requested by the user. The one second
2787
 
        loop above requests writes for that case. The writes done here
2788
 
        are not required, and may be disabled. */
2789
 
 
2790
 
        buf_get_total_stat(&buf_stat);
 
2374
        /* If there were less than 200 i/os during the 10 second period,
 
2375
        we assume that there is free disk i/o capacity available, and it
 
2376
        makes sense to flush 100 pages. */
 
2377
 
2791
2378
        n_pend_ios = buf_get_n_pending_ios() + log_sys->n_pending_writes;
2792
 
        n_ios = log_sys->n_log_ios + buf_stat.n_pages_read
2793
 
                + buf_stat.n_pages_written;
2794
 
 
2795
 
        srv_main_10_second_loops++;
2796
 
        if (n_pend_ios < SRV_PEND_IO_THRESHOLD
2797
 
            && (n_ios - n_ios_very_old < SRV_PAST_IO_ACTIVITY)) {
 
2379
        n_ios = log_sys->n_log_ios + buf_pool->n_pages_read
 
2380
                + buf_pool->n_pages_written;
 
2381
        if (n_pend_ios < 3 && (n_ios - n_ios_very_old < 200)) {
2798
2382
 
2799
2383
                srv_main_thread_op_info = "flushing buffer pool pages";
2800
 
                buf_flush_list(PCT_IO(100), IB_ULONGLONG_MAX);
 
2384
                buf_flush_batch(BUF_FLUSH_LIST, 100, IB_ULONGLONG_MAX);
2801
2385
 
2802
 
                /* Flush logs if needed */
2803
 
                srv_sync_log_buffer_in_background();
 
2386
                srv_main_thread_op_info = "flushing log";
 
2387
                log_buffer_flush_to_disk();
2804
2388
        }
2805
2389
 
2806
2390
        /* We run a batch of insert buffer merge every 10 seconds,
2807
2391
        even if the server were active */
2808
2392
 
2809
2393
        srv_main_thread_op_info = "doing insert buffer merge";
2810
 
        ibuf_contract_for_n_pages(FALSE, PCT_IO(5));
2811
 
 
2812
 
        /* Flush logs if needed */
2813
 
        srv_sync_log_buffer_in_background();
2814
 
 
2815
 
        if (srv_n_purge_threads == 0) {
2816
 
                srv_main_thread_op_info = "master purging";
2817
 
 
2818
 
                srv_master_do_purge();
 
2394
        ibuf_contract_for_n_pages(TRUE, srv_insert_buffer_batch_size / 4);
 
2395
 
 
2396
        srv_main_thread_op_info = "flushing log";
 
2397
        log_buffer_flush_to_disk();
 
2398
 
 
2399
        /* We run a full purge every 10 seconds, even if the server
 
2400
        were active */
 
2401
 
 
2402
        last_flush_time = time(NULL);
 
2403
 
 
2404
        do {
2819
2405
 
2820
2406
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2821
2407
 
2822
2408
                        goto background_loop;
2823
2409
                }
2824
 
        }
 
2410
 
 
2411
                srv_main_thread_op_info = "purging";
 
2412
                n_pages_purged = trx_purge();
 
2413
 
 
2414
                current_time = time(NULL);
 
2415
 
 
2416
                if (difftime(current_time, last_flush_time) > 1) {
 
2417
                        srv_main_thread_op_info = "flushing log";
 
2418
 
 
2419
                        log_buffer_flush_to_disk();
 
2420
                        last_flush_time = current_time;
 
2421
                }
 
2422
        } while (n_pages_purged);
2825
2423
 
2826
2424
        srv_main_thread_op_info = "flushing buffer pool pages";
2827
2425
 
2833
2431
                (> 70 %), we assume we can afford reserving the disk(s) for
2834
2432
                the time it requires to flush 100 pages */
2835
2433
 
2836
 
                n_pages_flushed = buf_flush_list(
2837
 
                        PCT_IO(100), IB_ULONGLONG_MAX);
 
2434
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 100,
 
2435
                                                  IB_ULONGLONG_MAX);
2838
2436
        } else {
2839
2437
                /* Otherwise, we only flush a small number of pages so that
2840
2438
                we do not unnecessarily use much disk i/o capacity from
2841
2439
                other work */
2842
2440
 
2843
 
                n_pages_flushed = buf_flush_list(
2844
 
                          PCT_IO(10), IB_ULONGLONG_MAX);
 
2441
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 10,
 
2442
                                                  IB_ULONGLONG_MAX);
2845
2443
        }
2846
2444
 
2847
2445
        srv_main_thread_op_info = "making checkpoint";
2874
2472
 
2875
2473
        /* The server has been quiet for a while: start running background
2876
2474
        operations */
2877
 
        srv_main_background_loops++;
 
2475
 
2878
2476
        srv_main_thread_op_info = "doing background drop tables";
2879
2477
 
2880
2478
        n_tables_to_drop = row_drop_tables_for_mysql_in_background();
2888
2486
                os_thread_sleep(100000);
2889
2487
        }
2890
2488
 
2891
 
        if (srv_n_purge_threads == 0) {
2892
 
                srv_main_thread_op_info = "master purging";
2893
 
 
2894
 
                srv_master_do_purge();
2895
 
        }
 
2489
        srv_main_thread_op_info = "purging";
 
2490
 
 
2491
        /* Run a full purge */
 
2492
 
 
2493
        last_flush_time = time(NULL);
 
2494
 
 
2495
        do {
 
2496
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
 
2497
 
 
2498
                        break;
 
2499
                }
 
2500
 
 
2501
                srv_main_thread_op_info = "purging";
 
2502
                n_pages_purged = trx_purge();
 
2503
 
 
2504
                current_time = time(NULL);
 
2505
 
 
2506
                if (difftime(current_time, last_flush_time) > 1) {
 
2507
                        srv_main_thread_op_info = "flushing log";
 
2508
 
 
2509
                        log_buffer_flush_to_disk();
 
2510
                        last_flush_time = current_time;
 
2511
                }
 
2512
        } while (n_pages_purged);
2896
2513
 
2897
2514
        srv_main_thread_op_info = "reserving kernel mutex";
2898
2515
 
2908
2525
        if (srv_fast_shutdown && srv_shutdown_state > 0) {
2909
2526
                n_bytes_merged = 0;
2910
2527
        } else {
2911
 
                /* This should do an amount of IO similar to the number of
2912
 
                dirty pages that will be flushed in the call to
2913
 
                buf_flush_list below. Otherwise, the system favors
2914
 
                clean pages over cleanup throughput. */
2915
 
                n_bytes_merged = ibuf_contract_for_n_pages(FALSE,
2916
 
                                                           PCT_IO(100));
 
2528
                n_bytes_merged = ibuf_contract_for_n_pages(
 
2529
                        TRUE, srv_insert_buffer_batch_size);
2917
2530
        }
2918
2531
 
2919
2532
        srv_main_thread_op_info = "reserving kernel mutex";
2927
2540
 
2928
2541
flush_loop:
2929
2542
        srv_main_thread_op_info = "flushing buffer pool pages";
2930
 
        srv_main_flush_loops++;
 
2543
 
2931
2544
        if (srv_fast_shutdown < 2) {
2932
 
                n_pages_flushed = buf_flush_list(
2933
 
                          PCT_IO(100), IB_ULONGLONG_MAX);
 
2545
                n_pages_flushed = buf_flush_batch(BUF_FLUSH_LIST, 100,
 
2546
                                                  IB_ULONGLONG_MAX);
2934
2547
        } else {
2935
2548
                /* In the fastest shutdown we do not flush the buffer pool
2936
2549
                to data files: we set n_pages_flushed to 0 artificially. */
2948
2561
        mutex_exit(&kernel_mutex);
2949
2562
 
2950
2563
        srv_main_thread_op_info = "waiting for buffer pool flush to end";
2951
 
        buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
2952
 
 
2953
 
        /* Flush logs if needed */
2954
 
        srv_sync_log_buffer_in_background();
 
2564
        buf_flush_wait_batch_end(BUF_FLUSH_LIST);
 
2565
 
 
2566
        srv_main_thread_op_info = "flushing log";
 
2567
 
 
2568
        log_buffer_flush_to_disk();
2955
2569
 
2956
2570
        srv_main_thread_op_info = "making checkpoint";
2957
2571
 
3035
2649
                already when the event wait ends */
3036
2650
 
3037
2651
                os_thread_exit(NULL);
3038
 
 
3039
2652
        }
3040
2653
 
3041
2654
        /* When there is user activity, InnoDB will set the event and the
3043
2656
 
3044
2657
        goto loop;
3045
2658
 
3046
 
 
3047
 
#if (!defined(__SUNPRO_C) && !defined(__SUNPRO_CC))
3048
 
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3049
 
#endif
3050
 
}
3051
 
 
3052
 
/*********************************************************************//**
3053
 
Asynchronous purge thread.
3054
 
@return a dummy parameter */
3055
 
UNIV_INTERN
3056
 
os_thread_ret_t
3057
 
srv_purge_thread(
3058
 
/*=============*/
3059
 
        void*   arg __attribute__((unused)))    /*!< in: a dummy parameter
3060
 
                                                required by os_thread_create */
3061
 
{
3062
 
        srv_slot_t*     slot;
3063
 
        ulint           slot_no = ULINT_UNDEFINED;
3064
 
        ulint           n_total_purged = ULINT_UNDEFINED;
3065
 
 
3066
 
        ut_a(srv_n_purge_threads == 1);
3067
 
 
3068
 
#ifdef UNIV_DEBUG_THREAD_CREATION
3069
 
        fprintf(stderr, "InnoDB: Purge thread running, id %lu\n",
3070
 
                os_thread_pf(os_thread_get_curr_id()));
3071
 
#endif /* UNIV_DEBUG_THREAD_CREATION */
3072
 
 
3073
 
        mutex_enter(&kernel_mutex);
3074
 
 
3075
 
        slot_no = srv_table_reserve_slot(SRV_WORKER);
3076
 
 
3077
 
        slot = srv_table_get_nth_slot(slot_no);
3078
 
 
3079
 
        ++srv_n_threads_active[SRV_WORKER];
3080
 
 
3081
 
        mutex_exit(&kernel_mutex);
3082
 
 
3083
 
        while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
3084
 
 
3085
 
                ulint   n_pages_purged;
3086
 
 
3087
 
                /* If there are very few records to purge or the last
3088
 
                purge didn't purge any records then wait for activity.
3089
 
                We peek at the history len without holding any mutex
3090
 
                because in the worst case we will end up waiting for
3091
 
                the next purge event. */
3092
 
                if (trx_sys->rseg_history_len < srv_purge_batch_size
3093
 
                    || n_total_purged == 0) {
3094
 
 
3095
 
                        os_event_t      event;
3096
 
 
3097
 
                        mutex_enter(&kernel_mutex);
3098
 
 
3099
 
                        event = srv_suspend_thread();
3100
 
 
3101
 
                        mutex_exit(&kernel_mutex);
3102
 
 
3103
 
                        os_event_wait(event);
3104
 
                }
3105
 
 
3106
 
                /* Check for shutdown and whether we should do purge at all. */
3107
 
                if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND
3108
 
                    || srv_shutdown_state != 0
3109
 
                    || srv_fast_shutdown) {
3110
 
 
3111
 
                        break;
3112
 
                }
3113
 
 
3114
 
                n_total_purged = 0;
3115
 
 
3116
 
                /* Purge until there are no more records to purge and there is
3117
 
                no change in configuration or server state. */
3118
 
                do {
3119
 
                        n_pages_purged = trx_purge(srv_purge_batch_size);
3120
 
 
3121
 
                        n_total_purged += n_pages_purged;
3122
 
 
3123
 
                } while (n_pages_purged > 0 && !srv_fast_shutdown);
3124
 
 
3125
 
                srv_sync_log_buffer_in_background();
3126
 
        }
3127
 
 
3128
 
        mutex_enter(&kernel_mutex);
3129
 
 
3130
 
        ut_ad(srv_table_get_nth_slot(slot_no) == slot);
3131
 
 
3132
 
        /* Decrement the active count. */
3133
 
        srv_suspend_thread();
3134
 
 
3135
 
        slot->in_use = FALSE;
3136
 
 
3137
 
        /* Free the thread local memory. */
3138
 
        thr_local_free(os_thread_get_curr_id());
3139
 
 
3140
 
        mutex_exit(&kernel_mutex);
3141
 
 
3142
 
#ifdef UNIV_DEBUG_THREAD_CREATION
3143
 
        fprintf(stderr, "InnoDB: Purge thread exiting, id %lu\n",
3144
 
                os_thread_pf(os_thread_get_curr_id()));
3145
 
#endif /* UNIV_DEBUG_THREAD_CREATION */
3146
 
 
3147
 
        /* We count the number of threads in os_thread_exit(). A created
3148
 
        thread should always use that to exit and not use return() to exit. */
3149
 
        os_thread_exit(NULL);
3150
 
 
3151
 
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
3152
 
}
3153
 
 
3154
 
/**********************************************************************//**
3155
 
Enqueues a task to server task queue and releases a worker thread, if there
3156
 
is a suspended one. */
3157
 
UNIV_INTERN
3158
 
void
3159
 
srv_que_task_enqueue_low(
3160
 
/*=====================*/
3161
 
        que_thr_t*      thr)    /*!< in: query thread */
3162
 
{
3163
 
        ut_ad(thr);
3164
 
 
3165
 
        mutex_enter(&kernel_mutex);
3166
 
 
3167
 
        UT_LIST_ADD_LAST(queue, srv_sys->tasks, thr);
3168
 
 
3169
 
        srv_release_threads(SRV_WORKER, 1);
3170
 
 
3171
 
        mutex_exit(&kernel_mutex);
3172
 
}
 
2659
        OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
 
2660
}
 
2661
#endif /* !UNIV_HOTBACKUP */