~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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
36
 
The database server main program
37
 
 
38
 
NOTE: SQL Server 7 uses something which the documentation
39
 
calls user mode scheduled threads (UMS threads). One such
40
 
thread is usually allocated per processor. Win32
41
 
documentation does not know any UMS threads, which suggests
42
 
that the concept is internal to SQL Server 7. It may mean that
43
 
SQL Server 7 does all the scheduling of threads itself, even
44
 
in i/o waits. We should maybe modify InnoDB to use the same
45
 
technique, because thread switches within NT may be too slow.
46
 
 
47
 
SQL Server 7 also mentions fibers, which are cooperatively
48
 
scheduled threads. They can boost performance by 5 %,
49
 
according to the Delaney and Soukup's book.
50
 
 
51
 
Windows 2000 will have something called thread pooling
52
 
(see msdn website), which we could possibly use.
53
 
 
54
 
Another possibility could be to use some very fast user space
55
 
thread library. This might confuse NT though.
56
 
 
57
 
Created 10/8/1995 Heikki Tuuri
58
 
*******************************************************/
59
 
 
60
 
/* Dummy comment */
61
 
#include "srv0srv.h"
62
 
 
63
 
#include "ut0mem.h"
64
 
#include "ut0ut.h"
65
 
#include "os0proc.h"
66
 
#include "mem0mem.h"
67
 
#include "mem0pool.h"
68
 
#include "sync0sync.h"
69
 
#include "thr0loc.h"
70
 
#include "que0que.h"
71
 
#include "log0recv.h"
72
 
#include "pars0pars.h"
73
 
#include "usr0sess.h"
74
 
#include "lock0lock.h"
75
 
#include "trx0purge.h"
76
 
#include "ibuf0ibuf.h"
77
 
#include "buf0flu.h"
78
 
#include "buf0lru.h"
79
 
#include "btr0sea.h"
80
 
#include "dict0load.h"
81
 
#include "dict0boot.h"
82
 
#include "srv0start.h"
83
 
#include "row0mysql.h"
84
 
#include "ha_prototypes.h"
85
 
#include "trx0i_s.h"
86
 
#include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
87
 
 
88
 
/* This is set to TRUE if the MySQL user has set it in MySQL; currently
89
 
affects only FOREIGN KEY definition parsing */
90
 
UNIV_INTERN ibool       srv_lower_case_table_names      = FALSE;
91
 
 
92
 
/* The following counter is incremented whenever there is some user activity
93
 
in the server */
94
 
UNIV_INTERN ulint       srv_activity_count      = 0;
95
 
 
96
 
/* The following is the maximum allowed duration of a lock wait. */
97
 
UNIV_INTERN ulint       srv_fatal_semaphore_wait_threshold = 600;
98
 
 
99
 
/* How much data manipulation language (DML) statements need to be delayed,
100
 
in microseconds, in order to reduce the lagging of the purge thread. */
101
 
UNIV_INTERN ulint       srv_dml_needed_delay = 0;
102
 
 
103
 
UNIV_INTERN ibool       srv_lock_timeout_active = FALSE;
104
 
UNIV_INTERN ibool       srv_monitor_active = FALSE;
105
 
UNIV_INTERN ibool       srv_error_monitor_active = FALSE;
106
 
 
107
 
UNIV_INTERN const char* srv_main_thread_op_info = "";
108
 
 
109
 
/** Prefix used by MySQL to indicate pre-5.1 table name encoding */
110
 
UNIV_INTERN const char  srv_mysql50_table_name_prefix[9] = "#mysql50#";
111
 
 
112
 
/* Server parameters which are read from the initfile */
113
 
 
114
 
/* The following three are dir paths which are catenated before file
115
 
names, where the file name itself may also contain a path */
116
 
 
117
 
UNIV_INTERN char*       srv_data_home   = NULL;
118
 
#ifdef UNIV_LOG_ARCHIVE
119
 
UNIV_INTERN char*       srv_arch_dir    = NULL;
120
 
#endif /* UNIV_LOG_ARCHIVE */
121
 
 
122
 
/** store to its own file each table created by an user; data
123
 
dictionary tables are in the system tablespace 0 */
124
 
UNIV_INTERN my_bool     srv_file_per_table;
125
 
/** The file format to use on new *.ibd files. */
126
 
UNIV_INTERN ulint       srv_file_format = 0;
127
 
/** Whether to check file format during startup.  A value of
128
 
DICT_TF_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
129
 
set it to the highest format we support. */
130
 
UNIV_INTERN ulint       srv_max_file_format_at_startup = DICT_TF_FORMAT_MAX;
131
 
 
132
 
#if DICT_TF_FORMAT_51
133
 
# error "DICT_TF_FORMAT_51 must be 0!"
134
 
#endif
135
 
/** Place locks to records only i.e. do not use next-key locking except
136
 
on duplicate key checking and foreign key checking */
137
 
UNIV_INTERN ibool       srv_locks_unsafe_for_binlog = FALSE;
138
 
 
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
 
UNIV_INTERN ulint       srv_n_data_files = 0;
161
 
UNIV_INTERN char**      srv_data_file_names = NULL;
162
 
/* size in database pages */
163
 
UNIV_INTERN ulint*      srv_data_file_sizes = NULL;
164
 
 
165
 
/* if TRUE, then we auto-extend the last data file */
166
 
UNIV_INTERN ibool       srv_auto_extend_last_data_file  = FALSE;
167
 
/* if != 0, this tells the max size auto-extending may increase the
168
 
last data file size */
169
 
UNIV_INTERN ulint       srv_last_file_size_max  = 0;
170
 
/* If the last data file is auto-extended, we add this
171
 
many pages to it at a time */
172
 
UNIV_INTERN unsigned int srv_auto_extend_increment = 8;
173
 
UNIV_INTERN ulint*      srv_data_file_is_raw_partition = NULL;
174
 
 
175
 
/* If the following is TRUE we do not allow inserts etc. This protects
176
 
the user from forgetting the 'newraw' keyword to my.cnf */
177
 
 
178
 
UNIV_INTERN ibool       srv_created_new_raw     = FALSE;
179
 
 
180
 
UNIV_INTERN char**      srv_log_group_home_dirs = NULL;
181
 
 
182
 
UNIV_INTERN ulint       srv_n_log_groups        = ULINT_MAX;
183
 
UNIV_INTERN ulint       srv_n_log_files         = ULINT_MAX;
184
 
/* size in database pages */
185
 
UNIV_INTERN ulint       srv_log_file_size       = ULINT_MAX;
186
 
/* size in database pages */
187
 
UNIV_INTERN ulint       srv_log_buffer_size     = ULINT_MAX;
188
 
UNIV_INTERN ulong       srv_flush_log_at_trx_commit = 1;
189
 
 
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
205
 
collation */
206
 
#if defined(BUILD_DRIZZLE)
207
 
UNIV_INTERN const byte  srv_latin1_ordering[256]        /* The sort order table of the latin1
208
 
                                        character set. The following table is
209
 
                                        the MySQL order as of Feb 10th, 2002 */
210
 
= {
211
 
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
212
 
, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
213
 
, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
214
 
, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
215
 
, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27
216
 
, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F
217
 
, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
218
 
, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
219
 
, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47
220
 
, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F
221
 
, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57
222
 
, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
223
 
, 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47
224
 
, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F
225
 
, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57
226
 
, 0x58, 0x59, 0x5A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F
227
 
, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87
228
 
, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F
229
 
, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97
230
 
, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F
231
 
, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7
232
 
, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF
233
 
, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7
234
 
, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF
235
 
, 0x41, 0x41, 0x41, 0x41, 0x5C, 0x5B, 0x5C, 0x43
236
 
, 0x45, 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x49
237
 
, 0x44, 0x4E, 0x4F, 0x4F, 0x4F, 0x4F, 0x5D, 0xD7
238
 
, 0xD8, 0x55, 0x55, 0x55, 0x59, 0x59, 0xDE, 0xDF
239
 
, 0x41, 0x41, 0x41, 0x41, 0x5C, 0x5B, 0x5C, 0x43
240
 
, 0x45, 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x49
241
 
, 0x44, 0x4E, 0x4F, 0x4F, 0x4F, 0x4F, 0x5D, 0xF7
242
 
, 0xD8, 0x55, 0x55, 0x55, 0x59, 0x59, 0xDE, 0xFF
243
 
};
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;
251
 
/* 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;
255
 
/* previously requested size */
256
 
UNIV_INTERN ulint       srv_buf_pool_old_size;
257
 
/* current size in kilobytes */
258
 
UNIV_INTERN ulint       srv_buf_pool_curr_size  = 0;
259
 
/* size in bytes */
260
 
UNIV_INTERN ulint       srv_mem_pool_size       = ULINT_MAX;
261
 
UNIV_INTERN ulint       srv_lock_table_size     = ULINT_MAX;
262
 
 
263
 
/* This parameter is deprecated. Use srv_n_io_[read|write]_threads
264
 
instead. */
265
 
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
 
 
274
 
#ifdef UNIV_LOG_ARCHIVE
275
 
UNIV_INTERN ibool               srv_log_archive_on      = FALSE;
276
 
UNIV_INTERN ibool               srv_archive_recovery    = 0;
277
 
UNIV_INTERN ib_uint64_t srv_archive_recovery_limit_lsn;
278
 
#endif /* UNIV_LOG_ARCHIVE */
279
 
 
280
 
/* This parameter is used to throttle the number of insert buffers that are
281
 
merged in a batch. By increasing this parameter on a faster disk you can
282
 
possibly reduce the number of I/O operations performed to complete the
283
 
merge operation. The value of this parameter is used as is by the
284
 
background loop when the system is idle (low load), on a busy system
285
 
the parameter is scaled down by a factor of 4, this is to avoid putting
286
 
a heavier load on the I/O sub system. */
287
 
 
288
 
UNIV_INTERN ulong       srv_insert_buffer_batch_size = 20;
289
 
 
290
 
UNIV_INTERN char*       srv_file_flush_method_str = NULL;
291
 
UNIV_INTERN ulint       srv_unix_file_flush_method = SRV_UNIX_FSYNC;
292
 
UNIV_INTERN ulint       srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
293
 
 
294
 
UNIV_INTERN ulint       srv_max_n_open_files      = 300;
295
 
 
296
 
/* Number of IO operations per second the server can do */
297
 
UNIV_INTERN ulong       srv_io_capacity         = 200;
298
 
 
299
 
/* The InnoDB main thread tries to keep the ratio of modified pages
300
 
in the buffer pool to all database pages in the buffer pool smaller than
301
 
the following number. But it is not guaranteed that the value stays below
302
 
that during a time of heavy update/insert activity. */
303
 
 
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;
311
 
 
312
 
/* variable counts amount of data read in total (in bytes) */
313
 
UNIV_INTERN ulint srv_data_read = 0;
314
 
 
315
 
/* here we count the amount of data written in total (in bytes) */
316
 
UNIV_INTERN ulint srv_data_written = 0;
317
 
 
318
 
/* the number of the log write requests done */
319
 
UNIV_INTERN ulint srv_log_write_requests = 0;
320
 
 
321
 
/* the number of physical writes to the log performed */
322
 
UNIV_INTERN ulint srv_log_writes = 0;
323
 
 
324
 
/* amount of data written to the log files in bytes */
325
 
UNIV_INTERN ulint srv_os_log_written = 0;
326
 
 
327
 
/* amount of writes being done to the log files */
328
 
UNIV_INTERN ulint srv_os_log_pending_writes = 0;
329
 
 
330
 
/* we increase this counter, when there we don't have enough space in the
331
 
log buffer and have to flush it */
332
 
UNIV_INTERN ulint srv_log_waits = 0;
333
 
 
334
 
/* this variable counts the amount of times, when the doublewrite buffer
335
 
was flushed */
336
 
UNIV_INTERN ulint srv_dblwr_writes = 0;
337
 
 
338
 
/* here we store the number of pages that have been flushed to the
339
 
doublewrite buffer */
340
 
UNIV_INTERN ulint srv_dblwr_pages_written = 0;
341
 
 
342
 
/* in this variable we store the number of write requests issued */
343
 
UNIV_INTERN ulint srv_buf_pool_write_requests = 0;
344
 
 
345
 
/* here we store the number of times when we had to wait for a free page
346
 
in the buffer pool. It happens when the buffer pool is full and we need
347
 
to make a flush, in order to be able to read or create a page. */
348
 
UNIV_INTERN ulint srv_buf_pool_wait_free = 0;
349
 
 
350
 
/* variable to count the number of pages that were written from buffer
351
 
pool to the disk */
352
 
UNIV_INTERN ulint srv_buf_pool_flushed = 0;
353
 
 
354
 
/** Number of buffer pool reads that led to the
355
 
reading of a disk page */
356
 
UNIV_INTERN ulint srv_buf_pool_reads = 0;
357
 
 
358
 
/* structure to pass status variables to MySQL */
359
 
UNIV_INTERN export_struc export_vars;
360
 
 
361
 
/* If the following is != 0 we do not allow inserts etc. This protects
362
 
the user from forgetting the innodb_force_recovery keyword to my.cnf */
363
 
 
364
 
UNIV_INTERN ulint       srv_force_recovery      = 0;
365
 
/*-----------------------*/
366
 
/* We are prepared for a situation that we have this many threads waiting for
367
 
a semaphore inside InnoDB. innobase_start_or_create_for_mysql() sets the
368
 
value. */
369
 
 
370
 
UNIV_INTERN ulint       srv_max_n_threads       = 0;
371
 
 
372
 
/* The following controls how many threads we let inside InnoDB concurrently:
373
 
threads waiting for locks are not counted into the number because otherwise
374
 
we could get a deadlock. MySQL creates a thread for each user session, and
375
 
semaphore contention and convoy problems can occur withput this restriction.
376
 
Value 10 should be good if there are less than 4 processors + 4 disks in the
377
 
computer. Bigger computers need bigger values. Value 0 will disable the
378
 
concurrency check. */
379
 
 
380
 
UNIV_INTERN ulong       srv_thread_concurrency  = 0;
381
 
 
382
 
/* this mutex protects srv_conc data structures */
383
 
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. */
388
 
UNIV_INTERN lint        srv_conc_n_threads      = 0;
389
 
/* number of OS threads waiting in the FIFO for a permission to enter
390
 
InnoDB */
391
 
UNIV_INTERN ulint       srv_conc_n_waiting_threads = 0;
392
 
 
393
 
typedef struct srv_conc_slot_struct     srv_conc_slot_t;
394
 
struct srv_conc_slot_struct{
395
 
        os_event_t                      event;          /*!< event to wait */
396
 
        ibool                           reserved;       /*!< TRUE if slot
397
 
                                                        reserved */
398
 
        ibool                           wait_ended;     /*!< TRUE when another
399
 
                                                        thread has already set
400
 
                                                        the event and the
401
 
                                                        thread in this slot is
402
 
                                                        free to proceed; but
403
 
                                                        reserved may still be
404
 
                                                        TRUE at that point */
405
 
        UT_LIST_NODE_T(srv_conc_slot_t) srv_conc_queue; /*!< queue node */
406
 
};
407
 
 
408
 
/* queue of threads waiting to get in */
409
 
UNIV_INTERN UT_LIST_BASE_NODE_T(srv_conc_slot_t)        srv_conc_queue;
410
 
/* array of wait slots */
411
 
UNIV_INTERN srv_conc_slot_t* srv_conc_slots;
412
 
 
413
 
/* Number of times a thread is allowed to enter InnoDB within the same
414
 
SQL query after it has once got the ticket at srv_conc_enter_innodb */
415
 
#define SRV_FREE_TICKETS_TO_ENTER srv_n_free_tickets_to_enter
416
 
#define SRV_THREAD_SLEEP_DELAY srv_thread_sleep_delay
417
 
/*-----------------------*/
418
 
/* If the following is set to 1 then we do not run purge and insert buffer
419
 
merge to completion before shutdown. If it is set to 2, do not even flush the
420
 
buffer pool to data files at the shutdown: we effectively 'crash'
421
 
InnoDB (but lose no committed transactions). */
422
 
UNIV_INTERN ulint       srv_fast_shutdown       = 0;
423
 
 
424
 
/* Generate a innodb_status.<pid> file */
425
 
UNIV_INTERN ibool       srv_innodb_status       = FALSE;
426
 
 
427
 
/* When estimating number of different key values in an index, sample
428
 
this many index pages */
429
 
UNIV_INTERN ib_uint64_t srv_stats_sample_pages = 8;
430
 
 
431
 
UNIV_INTERN ibool       srv_use_doublewrite_buf = TRUE;
432
 
UNIV_INTERN ibool       srv_use_checksums = TRUE;
433
 
 
434
 
UNIV_INTERN ulong       srv_replication_delay           = 0;
435
 
 
436
 
/*-------------------------------------------*/
437
 
UNIV_INTERN ulong       srv_n_spin_wait_rounds  = 30;
438
 
UNIV_INTERN ulong       srv_n_free_tickets_to_enter = 500;
439
 
UNIV_INTERN ulong       srv_thread_sleep_delay = 10000;
440
 
UNIV_INTERN ulong       srv_spin_wait_delay     = 6;
441
 
UNIV_INTERN ibool       srv_priority_boost      = TRUE;
442
 
 
443
 
#ifdef UNIV_DEBUG
444
 
UNIV_INTERN ibool       srv_print_thread_releases       = FALSE;
445
 
UNIV_INTERN ibool       srv_print_lock_waits            = FALSE;
446
 
UNIV_INTERN ibool       srv_print_buf_io                = FALSE;
447
 
UNIV_INTERN ibool       srv_print_log_io                = FALSE;
448
 
UNIV_INTERN ibool       srv_print_latch_waits           = FALSE;
449
 
#endif /* UNIV_DEBUG */
450
 
 
451
 
UNIV_INTERN ulint               srv_n_rows_inserted             = 0;
452
 
UNIV_INTERN ulint               srv_n_rows_updated              = 0;
453
 
UNIV_INTERN ulint               srv_n_rows_deleted              = 0;
454
 
UNIV_INTERN ulint               srv_n_rows_read                 = 0;
455
 
 
456
 
static ulint    srv_n_rows_inserted_old         = 0;
457
 
static ulint    srv_n_rows_updated_old          = 0;
458
 
static ulint    srv_n_rows_deleted_old          = 0;
459
 
static ulint    srv_n_rows_read_old             = 0;
460
 
 
461
 
UNIV_INTERN ulint               srv_n_lock_wait_count           = 0;
462
 
UNIV_INTERN ulint               srv_n_lock_wait_current_count   = 0;
463
 
UNIV_INTERN ib_int64_t  srv_n_lock_wait_time            = 0;
464
 
UNIV_INTERN ulint               srv_n_lock_max_wait_time        = 0;
465
 
 
466
 
UNIV_INTERN ulint               srv_truncated_status_writes     = 0;
467
 
 
468
 
/*
469
 
  Set the following to 0 if you want InnoDB to write messages on
470
 
  stderr on startup/shutdown
471
 
*/
472
 
UNIV_INTERN ibool       srv_print_verbose_log           = TRUE;
473
 
UNIV_INTERN ibool       srv_print_innodb_monitor        = FALSE;
474
 
UNIV_INTERN ibool       srv_print_innodb_lock_monitor   = FALSE;
475
 
UNIV_INTERN ibool       srv_print_innodb_tablespace_monitor = FALSE;
476
 
UNIV_INTERN ibool       srv_print_innodb_table_monitor = FALSE;
477
 
 
478
 
/* Array of English strings describing the current state of an
479
 
i/o handler thread */
480
 
 
481
 
UNIV_INTERN const char* srv_io_thread_op_info[SRV_MAX_N_IO_THREADS];
482
 
UNIV_INTERN const char* srv_io_thread_function[SRV_MAX_N_IO_THREADS];
483
 
 
484
 
UNIV_INTERN time_t      srv_last_monitor_time;
485
 
 
486
 
UNIV_INTERN mutex_t     srv_innodb_monitor_mutex;
487
 
 
488
 
/* Mutex for locking srv_monitor_file */
489
 
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
 
/* Temporary file for innodb monitor output */
505
 
UNIV_INTERN FILE*       srv_monitor_file;
506
 
/* Mutex for locking srv_dict_tmpfile.
507
 
This mutex has a very high rank; threads reserving it should not
508
 
be holding any InnoDB latches. */
509
 
UNIV_INTERN mutex_t     srv_dict_tmpfile_mutex;
510
 
/* Temporary file for output from the data dictionary */
511
 
UNIV_INTERN FILE*       srv_dict_tmpfile;
512
 
/* Mutex for locking srv_misc_tmpfile.
513
 
This mutex has a very low rank; threads reserving it should not
514
 
acquire any further latches or sleep before releasing this one. */
515
 
UNIV_INTERN mutex_t     srv_misc_tmpfile_mutex;
516
 
/* Temporary file for miscellanous diagnostic output */
517
 
UNIV_INTERN FILE*       srv_misc_tmpfile;
518
 
 
519
 
UNIV_INTERN ulint       srv_main_thread_process_no      = 0;
520
 
UNIV_INTERN ulint       srv_main_thread_id              = 0;
521
 
 
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
 
/*
551
 
        IMPLEMENTATION OF THE SERVER MAIN PROGRAM
552
 
        =========================================
553
 
 
554
 
There is the following analogue between this database
555
 
server and an operating system kernel:
556
 
 
557
 
DB concept                      equivalent OS concept
558
 
----------                      ---------------------
559
 
transaction             --      process;
560
 
 
561
 
query thread            --      thread;
562
 
 
563
 
lock                    --      semaphore;
564
 
 
565
 
transaction set to
566
 
the rollback state      --      kill signal delivered to a process;
567
 
 
568
 
kernel                  --      kernel;
569
 
 
570
 
query thread execution:
571
 
(a) without kernel mutex
572
 
reserved                --      process executing in user mode;
573
 
(b) with kernel mutex reserved
574
 
                        --      process executing in kernel mode;
575
 
 
576
 
The server is controlled by a master thread which runs at
577
 
a priority higher than normal, that is, higher than user threads.
578
 
It sleeps most of the time, and wakes up, say, every 300 milliseconds,
579
 
to check whether there is anything happening in the server which
580
 
requires intervention of the master thread. Such situations may be,
581
 
for example, when flushing of dirty blocks is needed in the buffer
582
 
pool or old version of database rows have to be cleaned away.
583
 
 
584
 
The threads which we call user threads serve the queries of
585
 
the clients and input from the console of the server.
586
 
They run at normal priority. The server may have several
587
 
communications endpoints. A dedicated set of user threads waits
588
 
at each of these endpoints ready to receive a client request.
589
 
Each request is taken by a single user thread, which then starts
590
 
processing and, when the result is ready, sends it to the client
591
 
and returns to wait at the same endpoint the thread started from.
592
 
 
593
 
So, we do not have dedicated communication threads listening at
594
 
the endpoints and dealing the jobs to dedicated worker threads.
595
 
Our architecture saves one thread swithch per request, compared
596
 
to the solution with dedicated communication threads
597
 
which amounts to 15 microseconds on 100 MHz Pentium
598
 
running NT. If the client
599
 
is communicating over a network, this saving is negligible, but
600
 
if the client resides in the same machine, maybe in an SMP machine
601
 
on a different processor from the server thread, the saving
602
 
can be important as the threads can communicate over shared
603
 
memory with an overhead of a few microseconds.
604
 
 
605
 
We may later implement a dedicated communication thread solution
606
 
for those endpoints which communicate over a network.
607
 
 
608
 
Our solution with user threads has two problems: for each endpoint
609
 
there has to be a number of listening threads. If there are many
610
 
communication endpoints, it may be difficult to set the right number
611
 
of concurrent threads in the system, as many of the threads
612
 
may always be waiting at less busy endpoints. Another problem
613
 
is queuing of the messages, as the server internally does not
614
 
offer any queue for jobs.
615
 
 
616
 
Another group of user threads is intended for splitting the
617
 
queries and processing them in parallel. Let us call these
618
 
parallel communication threads. These threads are waiting for
619
 
parallelized tasks, suspended on event semaphores.
620
 
 
621
 
A single user thread waits for input from the console,
622
 
like a command to shut the database.
623
 
 
624
 
Utility threads are a different group of threads which takes
625
 
care of the buffer pool flushing and other, mainly background
626
 
operations, in the server.
627
 
Some of these utility threads always run at a lower than normal
628
 
priority, so that they are always in background. Some of them
629
 
may dynamically boost their priority by the pri_adjust function,
630
 
even to higher than normal priority, if their task becomes urgent.
631
 
The running of utilities is controlled by high- and low-water marks
632
 
of urgency. The urgency may be measured by the number of dirty blocks
633
 
in the buffer pool, in the case of the flush thread, for example.
634
 
When the high-water mark is exceeded, an utility starts running, until
635
 
the urgency drops under the low-water mark. Then the utility thread
636
 
suspend itself to wait for an event. The master thread is
637
 
responsible of signaling this event when the utility thread is
638
 
again needed.
639
 
 
640
 
For each individual type of utility, some threads always remain
641
 
at lower than normal priority. This is because pri_adjust is implemented
642
 
so that the threads at normal or higher priority control their
643
 
share of running time by calling sleep. Thus, if the load of the
644
 
system sudenly drops, these threads cannot necessarily utilize
645
 
the system fully. The background priority threads make up for this,
646
 
starting to run when the load drops.
647
 
 
648
 
When there is no activity in the system, also the master thread
649
 
suspends itself to wait for an event making
650
 
the server totally silent. The responsibility to signal this
651
 
event is on the user thread which again receives a message
652
 
from a client.
653
 
 
654
 
There is still one complication in our server design. If a
655
 
background utility thread obtains a resource (e.g., mutex) needed by a user
656
 
thread, and there is also some other user activity in the system,
657
 
the user thread may have to wait indefinitely long for the
658
 
resource, as the OS does not schedule a background thread if
659
 
there is some other runnable user thread. This problem is called
660
 
priority inversion in real-time programming.
661
 
 
662
 
One solution to the priority inversion problem would be to
663
 
keep record of which thread owns which resource and
664
 
in the above case boost the priority of the background thread
665
 
so that it will be scheduled and it can release the resource.
666
 
This solution is called priority inheritance in real-time programming.
667
 
A drawback of this solution is that the overhead of acquiring a mutex
668
 
increases slightly, maybe 0.2 microseconds on a 100 MHz Pentium, because
669
 
the thread has to call os_thread_get_curr_id.
670
 
This may be compared to 0.5 microsecond overhead for a mutex lock-unlock
671
 
pair. Note that the thread
672
 
cannot store the information in the resource, say mutex, itself,
673
 
because competing threads could wipe out the information if it is
674
 
stored before acquiring the mutex, and if it stored afterwards,
675
 
the information is outdated for the time of one machine instruction,
676
 
at least. (To be precise, the information could be stored to
677
 
lock_word in mutex if the machine supports atomic swap.)
678
 
 
679
 
The above solution with priority inheritance may become actual in the
680
 
future, but at the moment we plan to implement a more coarse solution,
681
 
which could be called a global priority inheritance. If a thread
682
 
has to wait for a long time, say 300 milliseconds, for a resource,
683
 
we just guess that it may be waiting for a resource owned by a background
684
 
thread, and boost the the priority of all runnable background threads
685
 
to the normal level. The background threads then themselves adjust
686
 
their fixed priority back to background after releasing all resources
687
 
they had (or, at some fixed points in their program code).
688
 
 
689
 
What is the performance of the global priority inheritance solution?
690
 
We may weigh the length of the wait time 300 milliseconds, during
691
 
which the system processes some other thread
692
 
to the cost of boosting the priority of each runnable background
693
 
thread, rescheduling it, and lowering the priority again.
694
 
On 100 MHz Pentium + NT this overhead may be of the order 100
695
 
microseconds per thread. So, if the number of runnable background
696
 
threads is not very big, say < 100, the cost is tolerable.
697
 
Utility threads probably will access resources used by
698
 
user threads not very often, so collisions of user threads
699
 
to preempted utility threads should not happen very often.
700
 
 
701
 
The thread table contains
702
 
information of the current status of each thread existing in the system,
703
 
and also the event semaphores used in suspending the master thread
704
 
and utility and parallel communication threads when they have nothing to do.
705
 
The thread table can be seen as an analogue to the process table
706
 
in a traditional Unix implementation.
707
 
 
708
 
The thread table is also used in the global priority inheritance
709
 
scheme. This brings in one additional complication: threads accessing
710
 
the thread table must have at least normal fixed priority,
711
 
because the priority inheritance solution does not work if a background
712
 
thread is preempted while possessing the mutex protecting the thread table.
713
 
So, if a thread accesses the thread table, its priority has to be
714
 
boosted at least to normal. This priority requirement can be seen similar to
715
 
the privileged mode used when processing the kernel calls in traditional
716
 
Unix.*/
717
 
 
718
 
/* Thread slot in the thread table */
719
 
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
725
 
                                        for the event of this slot */
726
 
        ib_time_t       suspend_time;   /*!< time when the thread was
727
 
                                        suspended */
728
 
        os_event_t      event;          /*!< event used in suspending the
729
 
                                        thread when it has nothing to do */
730
 
        que_thr_t*      thr;            /*!< suspended query thread (only
731
 
                                        used for MySQL threads) */
732
 
};
733
 
 
734
 
/* Table for MySQL threads where they will be suspended to wait for locks */
735
 
UNIV_INTERN srv_slot_t* srv_mysql_table = NULL;
736
 
 
737
 
UNIV_INTERN os_event_t  srv_lock_timeout_thread_event;
738
 
 
739
 
UNIV_INTERN srv_sys_t*  srv_sys = NULL;
740
 
 
741
 
/* padding to prevent other memory update hotspots from residing on
742
 
the same memory cache line */
743
 
UNIV_INTERN byte        srv_pad1[64];
744
 
/* mutex protecting the server, trx structs, query threads, and lock table */
745
 
UNIV_INTERN mutex_t*    kernel_mutex_temp;
746
 
/* padding to prevent other memory update hotspots from residing on
747
 
the same memory cache line */
748
 
UNIV_INTERN byte        srv_pad2[64];
749
 
 
750
 
#if 0
751
 
/* The following three values measure the urgency of the jobs of
752
 
buffer, version, and insert threads. They may vary from 0 - 1000.
753
 
The server mutex protects all these variables. The low-water values
754
 
tell that the server can acquiesce the utility when the value
755
 
drops below this low-water mark. */
756
 
 
757
 
static ulint    srv_meter[SRV_MASTER + 1];
758
 
static ulint    srv_meter_low_water[SRV_MASTER + 1];
759
 
static ulint    srv_meter_high_water[SRV_MASTER + 1];
760
 
static ulint    srv_meter_high_water2[SRV_MASTER + 1];
761
 
static ulint    srv_meter_foreground[SRV_MASTER + 1];
762
 
#endif
763
 
 
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
 
/* The following values give info about the activity going on in
782
 
the database. They are protected by the server mutex. The arrays
783
 
are indexed by the type of the thread. */
784
 
 
785
 
UNIV_INTERN ulint       srv_n_threads_active[SRV_MASTER + 1];
786
 
UNIV_INTERN ulint       srv_n_threads[SRV_MASTER + 1];
787
 
 
788
 
/*********************************************************************//**
789
 
Sets the info describing an i/o thread current state. */
790
 
UNIV_INTERN
791
 
void
792
 
srv_set_io_thread_op_info(
793
 
/*======================*/
794
 
        ulint           i,      /*!< in: the 'segment' of the i/o thread */
795
 
        const char*     str)    /*!< in: constant char string describing the
796
 
                                state */
797
 
{
798
 
        ut_a(i < SRV_MAX_N_IO_THREADS);
799
 
 
800
 
        srv_io_thread_op_info[i] = str;
801
 
}
802
 
 
803
 
/*********************************************************************//**
804
 
Accessor function to get pointer to n'th slot in the server thread
805
 
table.
806
 
@return pointer to the slot */
807
 
static
808
 
srv_slot_t*
809
 
srv_table_get_nth_slot(
810
 
/*===================*/
811
 
        ulint   index)          /*!< in: index of the slot */
812
 
{
813
 
        ut_a(index < OS_THREAD_MAX_N);
814
 
 
815
 
        return(srv_sys->threads + index);
816
 
}
817
 
 
818
 
/*********************************************************************//**
819
 
Gets the number of threads in the system.
820
 
@return sum of srv_n_threads[] */
821
 
UNIV_INTERN
822
 
ulint
823
 
srv_get_n_threads(void)
824
 
/*===================*/
825
 
{
826
 
        ulint   i;
827
 
        ulint   n_threads       = 0;
828
 
 
829
 
        mutex_enter(&kernel_mutex);
830
 
 
831
 
        for (i = SRV_COM; i < SRV_MASTER + 1; i++) {
832
 
 
833
 
                n_threads += srv_n_threads[i];
834
 
        }
835
 
 
836
 
        mutex_exit(&kernel_mutex);
837
 
 
838
 
        return(n_threads);
839
 
}
840
 
 
841
 
/*********************************************************************//**
842
 
Reserves a slot in the thread table for the current thread. Also creates the
843
 
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 */
846
 
static
847
 
ulint
848
 
srv_table_reserve_slot(
849
 
/*===================*/
850
 
        enum srv_thread_type    type)   /*!< in: type of the thread */
851
 
{
852
 
        srv_slot_t*     slot;
853
 
        ulint           i;
854
 
 
855
 
        ut_a(type > 0);
856
 
        ut_a(type <= SRV_MASTER);
857
 
 
858
 
        i = 0;
859
 
        slot = srv_table_get_nth_slot(i);
860
 
 
861
 
        while (slot->in_use) {
862
 
                i++;
863
 
                slot = srv_table_get_nth_slot(i);
864
 
        }
865
 
 
866
 
        ut_a(slot->in_use == FALSE);
867
 
 
868
 
        slot->in_use = TRUE;
869
 
        slot->suspended = FALSE;
870
 
        slot->type = type;
871
 
        slot->id = os_thread_get_curr_id();
872
 
        slot->handle = os_thread_get_curr();
873
 
 
874
 
        thr_local_create();
875
 
 
876
 
        thr_local_set_slot_no(os_thread_get_curr_id(), i);
877
 
 
878
 
        return(i);
879
 
}
880
 
 
881
 
/*********************************************************************//**
882
 
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 */
885
 
static
886
 
os_event_t
887
 
srv_suspend_thread(void)
888
 
/*====================*/
889
 
{
890
 
        srv_slot_t*             slot;
891
 
        os_event_t              event;
892
 
        ulint                   slot_no;
893
 
        enum srv_thread_type    type;
894
 
 
895
 
        ut_ad(mutex_own(&kernel_mutex));
896
 
 
897
 
        slot_no = thr_local_get_slot_no(os_thread_get_curr_id());
898
 
 
899
 
        if (srv_print_thread_releases) {
900
 
                fprintf(stderr,
901
 
                        "Suspending thread %lu to slot %lu\n",
902
 
                        (ulong) os_thread_get_curr_id(), (ulong) slot_no);
903
 
        }
904
 
 
905
 
        slot = srv_table_get_nth_slot(slot_no);
906
 
 
907
 
        type = slot->type;
908
 
 
909
 
        ut_ad(type >= SRV_WORKER);
910
 
        ut_ad(type <= SRV_MASTER);
911
 
 
912
 
        event = slot->event;
913
 
 
914
 
        slot->suspended = TRUE;
915
 
 
916
 
        ut_ad(srv_n_threads_active[type] > 0);
917
 
 
918
 
        srv_n_threads_active[type]--;
919
 
 
920
 
        os_event_reset(event);
921
 
 
922
 
        return(event);
923
 
}
924
 
 
925
 
/*********************************************************************//**
926
 
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 */
930
 
UNIV_INTERN
931
 
ulint
932
 
srv_release_threads(
933
 
/*================*/
934
 
        enum srv_thread_type    type,   /*!< in: thread type */
935
 
        ulint                   n)      /*!< in: number of threads to release */
936
 
{
937
 
        srv_slot_t*     slot;
938
 
        ulint           i;
939
 
        ulint           count   = 0;
940
 
 
941
 
        ut_ad(type >= SRV_WORKER);
942
 
        ut_ad(type <= SRV_MASTER);
943
 
        ut_ad(n > 0);
944
 
        ut_ad(mutex_own(&kernel_mutex));
945
 
 
946
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
947
 
 
948
 
                slot = srv_table_get_nth_slot(i);
949
 
 
950
 
                if (slot->in_use && slot->type == type && slot->suspended) {
951
 
 
952
 
                        slot->suspended = FALSE;
953
 
 
954
 
                        srv_n_threads_active[type]++;
955
 
 
956
 
                        os_event_set(slot->event);
957
 
 
958
 
                        if (srv_print_thread_releases) {
959
 
                                fprintf(stderr,
960
 
                                        "Releasing thread %lu type %lu"
961
 
                                        " from slot %lu\n",
962
 
                                        (ulong) slot->id, (ulong) type,
963
 
                                        (ulong) i);
964
 
                        }
965
 
 
966
 
                        count++;
967
 
 
968
 
                        if (count == n) {
969
 
                                break;
970
 
                        }
971
 
                }
972
 
        }
973
 
 
974
 
        return(count);
975
 
}
976
 
 
977
 
/*********************************************************************//**
978
 
Returns the calling thread type.
979
 
@return SRV_COM, ... */
980
 
UNIV_INTERN
981
 
enum srv_thread_type
982
 
srv_get_thread_type(void)
983
 
/*=====================*/
984
 
{
985
 
        ulint                   slot_no;
986
 
        srv_slot_t*             slot;
987
 
        enum srv_thread_type    type;
988
 
 
989
 
        mutex_enter(&kernel_mutex);
990
 
 
991
 
        slot_no = thr_local_get_slot_no(os_thread_get_curr_id());
992
 
 
993
 
        slot = srv_table_get_nth_slot(slot_no);
994
 
 
995
 
        type = slot->type;
996
 
 
997
 
        ut_ad(type >= SRV_WORKER);
998
 
        ut_ad(type <= SRV_MASTER);
999
 
 
1000
 
        mutex_exit(&kernel_mutex);
1001
 
 
1002
 
        return(type);
1003
 
}
1004
 
 
1005
 
/*********************************************************************//**
1006
 
Initializes the server. */
1007
 
UNIV_INTERN
1008
 
void
1009
 
srv_init(void)
1010
 
/*==========*/
1011
 
{
1012
 
        srv_conc_slot_t*        conc_slot;
1013
 
        srv_slot_t*             slot;
1014
 
        ulint                   i;
1015
 
 
1016
 
        srv_sys = mem_alloc(sizeof(srv_sys_t));
1017
 
 
1018
 
        kernel_mutex_temp = mem_alloc(sizeof(mutex_t));
1019
 
        mutex_create(kernel_mutex_key, &kernel_mutex, SYNC_KERNEL);
1020
 
 
1021
 
        mutex_create(srv_innodb_monitor_mutex_key,
1022
 
                     &srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
1023
 
 
1024
 
        srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1025
 
 
1026
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1027
 
                slot = srv_table_get_nth_slot(i);
1028
 
                slot->in_use = FALSE;
1029
 
                slot->type=0;   /* Avoid purify errors */
1030
 
                slot->event = os_event_create(NULL);
1031
 
                ut_a(slot->event);
1032
 
        }
1033
 
 
1034
 
        srv_mysql_table = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
1035
 
 
1036
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1037
 
                slot = srv_mysql_table + i;
1038
 
                slot->in_use = FALSE;
1039
 
                slot->type = 0;
1040
 
                slot->event = os_event_create(NULL);
1041
 
                ut_a(slot->event);
1042
 
        }
1043
 
 
1044
 
        srv_lock_timeout_thread_event = os_event_create(NULL);
1045
 
 
1046
 
        for (i = 0; i < SRV_MASTER + 1; i++) {
1047
 
                srv_n_threads_active[i] = 0;
1048
 
                srv_n_threads[i] = 0;
1049
 
#if 0
1050
 
                srv_meter[i] = 30;
1051
 
                srv_meter_low_water[i] = 50;
1052
 
                srv_meter_high_water[i] = 100;
1053
 
                srv_meter_high_water2[i] = 200;
1054
 
                srv_meter_foreground[i] = 250;
1055
 
#endif
1056
 
        }
1057
 
 
1058
 
        UT_LIST_INIT(srv_sys->tasks);
1059
 
 
1060
 
        /* Create dummy indexes for infimum and supremum records */
1061
 
 
1062
 
        dict_ind_init();
1063
 
 
1064
 
        /* Init the server concurrency restriction data structures */
1065
 
 
1066
 
        os_fast_mutex_init(&srv_conc_mutex);
1067
 
 
1068
 
        UT_LIST_INIT(srv_conc_queue);
1069
 
 
1070
 
        srv_conc_slots = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_conc_slot_t));
1071
 
 
1072
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1073
 
                conc_slot = srv_conc_slots + i;
1074
 
                conc_slot->reserved = FALSE;
1075
 
                conc_slot->event = os_event_create(NULL);
1076
 
                ut_a(conc_slot->event);
1077
 
        }
1078
 
 
1079
 
        /* Initialize some INFORMATION SCHEMA internal structures */
1080
 
        trx_i_s_cache_init(trx_i_s_cache);
1081
 
}
1082
 
 
1083
 
/*********************************************************************//**
1084
 
Frees the data structures created in srv_init(). */
1085
 
UNIV_INTERN
1086
 
void
1087
 
srv_free(void)
1088
 
/*==========*/
1089
 
{
1090
 
        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
 
}
1105
 
 
1106
 
/*********************************************************************//**
1107
 
Initializes the synchronization primitives, memory system, and the thread
1108
 
local storage. */
1109
 
UNIV_INTERN
1110
 
void
1111
 
srv_general_init(void)
1112
 
/*==================*/
1113
 
{
1114
 
        ut_mem_init();
1115
 
        /* Reset the system variables in the recovery module. */
1116
 
        recv_sys_var_init();
1117
 
        os_sync_init();
1118
 
        sync_init();
1119
 
        mem_init(srv_mem_pool_size);
1120
 
        thr_local_init();
1121
 
}
1122
 
 
1123
 
/*======================= InnoDB Server FIFO queue =======================*/
1124
 
 
1125
 
/* Maximum allowable purge history length.  <=0 means 'infinite'. */
1126
 
UNIV_INTERN ulong       srv_max_purge_lag               = 0;
1127
 
 
1128
 
/*********************************************************************//**
1129
 
Puts an OS thread to wait if there are too many concurrent threads
1130
 
(>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
1131
 
UNIV_INTERN
1132
 
void
1133
 
srv_conc_enter_innodb(
1134
 
/*==================*/
1135
 
        trx_t*  trx)    /*!< in: transaction object associated with the
1136
 
                        thread */
1137
 
{
1138
 
        ibool                   has_slept = FALSE;
1139
 
        srv_conc_slot_t*        slot      = NULL;
1140
 
        ulint                   i;
1141
 
 
1142
 
        if (trx->mysql_thd != NULL
1143
 
            && thd_is_replication_slave_thread(trx->mysql_thd)) {
1144
 
 
1145
 
                UT_WAIT_FOR(srv_conc_n_threads
1146
 
                            < (lint)srv_thread_concurrency,
1147
 
                            srv_replication_delay * 1000);
1148
 
 
1149
 
                return;
1150
 
        }
1151
 
 
1152
 
        /* If trx has 'free tickets' to enter the engine left, then use one
1153
 
        such ticket */
1154
 
 
1155
 
        if (trx->n_tickets_to_enter_innodb > 0) {
1156
 
                trx->n_tickets_to_enter_innodb--;
1157
 
 
1158
 
                return;
1159
 
        }
1160
 
 
1161
 
        os_fast_mutex_lock(&srv_conc_mutex);
1162
 
retry:
1163
 
        if (trx->declared_to_be_inside_innodb) {
1164
 
                ut_print_timestamp(stderr);
1165
 
                fputs("  InnoDB: Error: trying to declare trx"
1166
 
                      " to enter InnoDB, but\n"
1167
 
                      "InnoDB: it already is declared.\n", stderr);
1168
 
                trx_print(stderr, trx, 0);
1169
 
                putc('\n', stderr);
1170
 
                os_fast_mutex_unlock(&srv_conc_mutex);
1171
 
 
1172
 
                return;
1173
 
        }
1174
 
 
1175
 
        ut_ad(srv_conc_n_threads >= 0);
1176
 
 
1177
 
        if (srv_conc_n_threads < (lint)srv_thread_concurrency) {
1178
 
 
1179
 
                srv_conc_n_threads++;
1180
 
                trx->declared_to_be_inside_innodb = TRUE;
1181
 
                trx->n_tickets_to_enter_innodb = SRV_FREE_TICKETS_TO_ENTER;
1182
 
 
1183
 
                os_fast_mutex_unlock(&srv_conc_mutex);
1184
 
 
1185
 
                return;
1186
 
        }
1187
 
 
1188
 
        /* If the transaction is not holding resources, let it sleep
1189
 
        for SRV_THREAD_SLEEP_DELAY microseconds, and try again then */
1190
 
 
1191
 
        if (!has_slept && !trx->has_search_latch
1192
 
            && NULL == UT_LIST_GET_FIRST(trx->trx_locks)) {
1193
 
 
1194
 
                has_slept = TRUE; /* We let it sleep only once to avoid
1195
 
                                  starvation */
1196
 
 
1197
 
                srv_conc_n_waiting_threads++;
1198
 
 
1199
 
                os_fast_mutex_unlock(&srv_conc_mutex);
1200
 
 
1201
 
                trx->op_info = "sleeping before joining InnoDB queue";
1202
 
 
1203
 
                /* Peter Zaitsev suggested that we take the sleep away
1204
 
                altogether. But the sleep may be good in pathological
1205
 
                situations of lots of thread switches. Simply put some
1206
 
                threads aside for a while to reduce the number of thread
1207
 
                switches. */
1208
 
                if (SRV_THREAD_SLEEP_DELAY > 0) {
1209
 
                        os_thread_sleep(SRV_THREAD_SLEEP_DELAY);
1210
 
                }
1211
 
 
1212
 
                trx->op_info = "";
1213
 
 
1214
 
                os_fast_mutex_lock(&srv_conc_mutex);
1215
 
 
1216
 
                srv_conc_n_waiting_threads--;
1217
 
 
1218
 
                goto retry;
1219
 
        }
1220
 
 
1221
 
        /* Too many threads inside: put the current thread to a queue */
1222
 
 
1223
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1224
 
                slot = srv_conc_slots + i;
1225
 
 
1226
 
                if (!slot->reserved) {
1227
 
 
1228
 
                        break;
1229
 
                }
1230
 
        }
1231
 
 
1232
 
        if (i == OS_THREAD_MAX_N) {
1233
 
                /* Could not find a free wait slot, we must let the
1234
 
                thread enter */
1235
 
 
1236
 
                srv_conc_n_threads++;
1237
 
                trx->declared_to_be_inside_innodb = TRUE;
1238
 
                trx->n_tickets_to_enter_innodb = 0;
1239
 
 
1240
 
                os_fast_mutex_unlock(&srv_conc_mutex);
1241
 
 
1242
 
                return;
1243
 
        }
1244
 
 
1245
 
        /* Release possible search system latch this thread has */
1246
 
        if (trx->has_search_latch) {
1247
 
                trx_search_latch_release_if_reserved(trx);
1248
 
        }
1249
 
 
1250
 
        /* Add to the queue */
1251
 
        slot->reserved = TRUE;
1252
 
        slot->wait_ended = FALSE;
1253
 
 
1254
 
        UT_LIST_ADD_LAST(srv_conc_queue, srv_conc_queue, slot);
1255
 
 
1256
 
        os_event_reset(slot->event);
1257
 
 
1258
 
        srv_conc_n_waiting_threads++;
1259
 
 
1260
 
        os_fast_mutex_unlock(&srv_conc_mutex);
1261
 
 
1262
 
        /* Go to wait for the event; when a thread leaves InnoDB it will
1263
 
        release this thread */
1264
 
 
1265
 
        trx->op_info = "waiting in InnoDB queue";
1266
 
 
1267
 
        os_event_wait(slot->event);
1268
 
 
1269
 
        trx->op_info = "";
1270
 
 
1271
 
        os_fast_mutex_lock(&srv_conc_mutex);
1272
 
 
1273
 
        srv_conc_n_waiting_threads--;
1274
 
 
1275
 
        /* NOTE that the thread which released this thread already
1276
 
        incremented the thread counter on behalf of this thread */
1277
 
 
1278
 
        slot->reserved = FALSE;
1279
 
 
1280
 
        UT_LIST_REMOVE(srv_conc_queue, srv_conc_queue, slot);
1281
 
 
1282
 
        trx->declared_to_be_inside_innodb = TRUE;
1283
 
        trx->n_tickets_to_enter_innodb = SRV_FREE_TICKETS_TO_ENTER;
1284
 
 
1285
 
        os_fast_mutex_unlock(&srv_conc_mutex);
1286
 
}
1287
 
 
1288
 
/*********************************************************************//**
1289
 
This lets a thread enter InnoDB regardless of the number of threads inside
1290
 
InnoDB. This must be called when a thread ends a lock wait. */
1291
 
UNIV_INTERN
1292
 
void
1293
 
srv_conc_force_enter_innodb(
1294
 
/*========================*/
1295
 
        trx_t*  trx)    /*!< in: transaction object associated with the
1296
 
                        thread */
1297
 
{
1298
 
        if (UNIV_LIKELY(!srv_thread_concurrency)) {
1299
 
 
1300
 
                return;
1301
 
        }
1302
 
 
1303
 
        ut_ad(srv_conc_n_threads >= 0);
1304
 
 
1305
 
        os_fast_mutex_lock(&srv_conc_mutex);
1306
 
 
1307
 
        srv_conc_n_threads++;
1308
 
        trx->declared_to_be_inside_innodb = TRUE;
1309
 
        trx->n_tickets_to_enter_innodb = 1;
1310
 
 
1311
 
        os_fast_mutex_unlock(&srv_conc_mutex);
1312
 
}
1313
 
 
1314
 
/*********************************************************************//**
1315
 
This must be called when a thread exits InnoDB in a lock wait or at the
1316
 
end of an SQL statement. */
1317
 
UNIV_INTERN
1318
 
void
1319
 
srv_conc_force_exit_innodb(
1320
 
/*=======================*/
1321
 
        trx_t*  trx)    /*!< in: transaction object associated with the
1322
 
                        thread */
1323
 
{
1324
 
        srv_conc_slot_t*        slot    = NULL;
1325
 
 
1326
 
        if (trx->mysql_thd != NULL
1327
 
            && thd_is_replication_slave_thread(trx->mysql_thd)) {
1328
 
 
1329
 
                return;
1330
 
        }
1331
 
 
1332
 
        if (trx->declared_to_be_inside_innodb == FALSE) {
1333
 
 
1334
 
                return;
1335
 
        }
1336
 
 
1337
 
        os_fast_mutex_lock(&srv_conc_mutex);
1338
 
 
1339
 
        ut_ad(srv_conc_n_threads > 0);
1340
 
        srv_conc_n_threads--;
1341
 
        trx->declared_to_be_inside_innodb = FALSE;
1342
 
        trx->n_tickets_to_enter_innodb = 0;
1343
 
 
1344
 
        if (srv_conc_n_threads < (lint)srv_thread_concurrency) {
1345
 
                /* Look for a slot where a thread is waiting and no other
1346
 
                thread has yet released the thread */
1347
 
 
1348
 
                slot = UT_LIST_GET_FIRST(srv_conc_queue);
1349
 
 
1350
 
                while (slot && slot->wait_ended == TRUE) {
1351
 
                        slot = UT_LIST_GET_NEXT(srv_conc_queue, slot);
1352
 
                }
1353
 
 
1354
 
                if (slot != NULL) {
1355
 
                        slot->wait_ended = TRUE;
1356
 
 
1357
 
                        /* We increment the count on behalf of the released
1358
 
                        thread */
1359
 
 
1360
 
                        srv_conc_n_threads++;
1361
 
                }
1362
 
        }
1363
 
 
1364
 
        os_fast_mutex_unlock(&srv_conc_mutex);
1365
 
 
1366
 
        if (slot != NULL) {
1367
 
                os_event_set(slot->event);
1368
 
        }
1369
 
}
1370
 
 
1371
 
/*********************************************************************//**
1372
 
This must be called when a thread exits InnoDB. */
1373
 
UNIV_INTERN
1374
 
void
1375
 
srv_conc_exit_innodb(
1376
 
/*=================*/
1377
 
        trx_t*  trx)    /*!< in: transaction object associated with the
1378
 
                        thread */
1379
 
{
1380
 
        if (trx->n_tickets_to_enter_innodb > 0) {
1381
 
                /* We will pretend the thread is still inside InnoDB though it
1382
 
                now leaves the InnoDB engine. In this way we save
1383
 
                a lot of semaphore operations. srv_conc_force_exit_innodb is
1384
 
                used to declare the thread definitely outside InnoDB. It
1385
 
                should be called when there is a lock wait or an SQL statement
1386
 
                ends. */
1387
 
 
1388
 
                return;
1389
 
        }
1390
 
 
1391
 
        srv_conc_force_exit_innodb(trx);
1392
 
}
1393
 
 
1394
 
/*========================================================================*/
1395
 
 
1396
 
/*********************************************************************//**
1397
 
Normalizes init parameter values to use units we use inside InnoDB.
1398
 
@return DB_SUCCESS or error code */
1399
 
static
1400
 
ulint
1401
 
srv_normalize_init_values(void)
1402
 
/*===========================*/
1403
 
{
1404
 
        ulint   n;
1405
 
        ulint   i;
1406
 
 
1407
 
        n = srv_n_data_files;
1408
 
 
1409
 
        for (i = 0; i < n; i++) {
1410
 
                srv_data_file_sizes[i] = srv_data_file_sizes[i]
1411
 
                        * ((1024 * 1024) / UNIV_PAGE_SIZE);
1412
 
        }
1413
 
 
1414
 
        srv_last_file_size_max = srv_last_file_size_max
1415
 
                * ((1024 * 1024) / UNIV_PAGE_SIZE);
1416
 
 
1417
 
        srv_log_file_size = srv_log_file_size / UNIV_PAGE_SIZE;
1418
 
 
1419
 
        srv_log_buffer_size = srv_log_buffer_size / UNIV_PAGE_SIZE;
1420
 
 
1421
 
        srv_lock_table_size = 5 * (srv_buf_pool_size / UNIV_PAGE_SIZE);
1422
 
 
1423
 
        return(DB_SUCCESS);
1424
 
}
1425
 
 
1426
 
/*********************************************************************//**
1427
 
Boots the InnoDB server.
1428
 
@return DB_SUCCESS or error code */
1429
 
UNIV_INTERN
1430
 
ulint
1431
 
srv_boot(void)
1432
 
/*==========*/
1433
 
{
1434
 
        ulint   err;
1435
 
 
1436
 
        /* Transform the init parameter values given by MySQL to
1437
 
        use units we use inside InnoDB: */
1438
 
 
1439
 
        err = srv_normalize_init_values();
1440
 
 
1441
 
        if (err != DB_SUCCESS) {
1442
 
                return(err);
1443
 
        }
1444
 
 
1445
 
        /* Initialize synchronization primitives, memory management, and thread
1446
 
        local storage */
1447
 
 
1448
 
        srv_general_init();
1449
 
 
1450
 
        /* Initialize this module */
1451
 
 
1452
 
        srv_init();
1453
 
 
1454
 
        return(DB_SUCCESS);
1455
 
}
1456
 
 
1457
 
/*********************************************************************//**
1458
 
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 */
1461
 
static
1462
 
srv_slot_t*
1463
 
srv_table_reserve_slot_for_mysql(void)
1464
 
/*==================================*/
1465
 
{
1466
 
        srv_slot_t*     slot;
1467
 
        ulint           i;
1468
 
 
1469
 
        ut_ad(mutex_own(&kernel_mutex));
1470
 
 
1471
 
        i = 0;
1472
 
        slot = srv_mysql_table + i;
1473
 
 
1474
 
        while (slot->in_use) {
1475
 
                i++;
1476
 
 
1477
 
                if (i >= OS_THREAD_MAX_N) {
1478
 
 
1479
 
                        ut_print_timestamp(stderr);
1480
 
 
1481
 
                        fprintf(stderr,
1482
 
                                "  InnoDB: There appear to be %lu MySQL"
1483
 
                                " threads currently waiting\n"
1484
 
                                "InnoDB: inside InnoDB, which is the"
1485
 
                                " upper limit. Cannot continue operation.\n"
1486
 
                                "InnoDB: We intentionally generate"
1487
 
                                " a seg fault to print a stack trace\n"
1488
 
                                "InnoDB: on Linux. But first we print"
1489
 
                                " a list of waiting threads.\n", (ulong) i);
1490
 
 
1491
 
                        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1492
 
 
1493
 
                                slot = srv_mysql_table + i;
1494
 
 
1495
 
                                fprintf(stderr,
1496
 
                                        "Slot %lu: thread id %lu, type %lu,"
1497
 
                                        " in use %lu, susp %lu, time %lu\n",
1498
 
                                        (ulong) i,
1499
 
                                        (ulong) os_thread_pf(slot->id),
1500
 
                                        (ulong) slot->type,
1501
 
                                        (ulong) slot->in_use,
1502
 
                                        (ulong) slot->suspended,
1503
 
                                        (ulong) difftime(ut_time(),
1504
 
                                                         slot->suspend_time));
1505
 
                        }
1506
 
 
1507
 
                        ut_error;
1508
 
                }
1509
 
 
1510
 
                slot = srv_mysql_table + i;
1511
 
        }
1512
 
 
1513
 
        ut_a(slot->in_use == FALSE);
1514
 
 
1515
 
        slot->in_use = TRUE;
1516
 
        slot->id = os_thread_get_curr_id();
1517
 
        slot->handle = os_thread_get_curr();
1518
 
 
1519
 
        return(slot);
1520
 
}
1521
 
 
1522
 
/***************************************************************//**
1523
 
Puts a MySQL OS thread to wait for a lock to be released. If an error
1524
 
occurs during the wait trx->error_state associated with thr is
1525
 
!= DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
1526
 
are possible errors. DB_DEADLOCK is returned if selective deadlock
1527
 
resolution chose this transaction as a victim. */
1528
 
UNIV_INTERN
1529
 
void
1530
 
srv_suspend_mysql_thread(
1531
 
/*=====================*/
1532
 
        que_thr_t*      thr)    /*!< in: query thread associated with the MySQL
1533
 
                                OS thread */
1534
 
{
1535
 
        srv_slot_t*     slot;
1536
 
        os_event_t      event;
1537
 
        double          wait_time;
1538
 
        trx_t*          trx;
1539
 
        ulint           had_dict_lock;
1540
 
        ibool           was_declared_inside_innodb      = FALSE;
1541
 
        ib_int64_t      start_time                      = 0;
1542
 
        ib_int64_t      finish_time;
1543
 
        ulint           diff_time;
1544
 
        ulint           sec;
1545
 
        ulint           ms;
1546
 
        ulong           lock_wait_timeout;
1547
 
 
1548
 
        ut_ad(!mutex_own(&kernel_mutex));
1549
 
 
1550
 
        trx = thr_get_trx(thr);
1551
 
 
1552
 
        os_event_set(srv_lock_timeout_thread_event);
1553
 
 
1554
 
        mutex_enter(&kernel_mutex);
1555
 
 
1556
 
        trx->error_state = DB_SUCCESS;
1557
 
 
1558
 
        if (thr->state == QUE_THR_RUNNING) {
1559
 
 
1560
 
                ut_ad(thr->is_active == TRUE);
1561
 
 
1562
 
                /* The lock has already been released or this transaction
1563
 
                was chosen as a deadlock victim: no need to suspend */
1564
 
 
1565
 
                if (trx->was_chosen_as_deadlock_victim) {
1566
 
 
1567
 
                        trx->error_state = DB_DEADLOCK;
1568
 
                        trx->was_chosen_as_deadlock_victim = FALSE;
1569
 
                }
1570
 
 
1571
 
                mutex_exit(&kernel_mutex);
1572
 
 
1573
 
                return;
1574
 
        }
1575
 
 
1576
 
        ut_ad(thr->is_active == FALSE);
1577
 
 
1578
 
        slot = srv_table_reserve_slot_for_mysql();
1579
 
 
1580
 
        event = slot->event;
1581
 
 
1582
 
        slot->thr = thr;
1583
 
 
1584
 
        os_event_reset(event);
1585
 
 
1586
 
        slot->suspend_time = ut_time();
1587
 
 
1588
 
        if (thr->lock_state == QUE_THR_LOCK_ROW) {
1589
 
                srv_n_lock_wait_count++;
1590
 
                srv_n_lock_wait_current_count++;
1591
 
 
1592
 
                if (ut_usectime(&sec, &ms) == -1) {
1593
 
                        start_time = -1;
1594
 
                } else {
1595
 
                        start_time = (ib_int64_t) sec * 1000000 + ms;
1596
 
                }
1597
 
        }
1598
 
        /* Wake the lock timeout monitor thread, if it is suspended */
1599
 
 
1600
 
        os_event_set(srv_lock_timeout_thread_event);
1601
 
 
1602
 
        mutex_exit(&kernel_mutex);
1603
 
 
1604
 
        if (trx->declared_to_be_inside_innodb) {
1605
 
 
1606
 
                was_declared_inside_innodb = TRUE;
1607
 
 
1608
 
                /* We must declare this OS thread to exit InnoDB, since a
1609
 
                possible other thread holding a lock which this thread waits
1610
 
                for must be allowed to enter, sooner or later */
1611
 
 
1612
 
                srv_conc_force_exit_innodb(trx);
1613
 
        }
1614
 
 
1615
 
        had_dict_lock = trx->dict_operation_lock_mode;
1616
 
 
1617
 
        switch (had_dict_lock) {
1618
 
        case RW_S_LATCH:
1619
 
                /* Release foreign key check latch */
1620
 
                row_mysql_unfreeze_data_dictionary(trx);
1621
 
                break;
1622
 
        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
 
                /* Release fast index creation latch */
1636
 
                row_mysql_unlock_data_dictionary(trx);
1637
 
                break;
1638
 
        }
1639
 
 
1640
 
        ut_a(trx->dict_operation_lock_mode == 0);
1641
 
 
1642
 
        /* Suspend this thread and wait for the event. */
1643
 
 
1644
 
        os_event_wait(event);
1645
 
 
1646
 
        /* After resuming, reacquire the data dictionary latch if
1647
 
        necessary. */
1648
 
 
1649
 
        switch (had_dict_lock) {
1650
 
        case RW_S_LATCH:
1651
 
                row_mysql_freeze_data_dictionary(trx);
1652
 
                break;
1653
 
        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
 
                row_mysql_lock_data_dictionary(trx);
1658
 
                break;
1659
 
        }
1660
 
 
1661
 
        if (was_declared_inside_innodb) {
1662
 
 
1663
 
                /* Return back inside InnoDB */
1664
 
 
1665
 
                srv_conc_force_enter_innodb(trx);
1666
 
        }
1667
 
 
1668
 
        mutex_enter(&kernel_mutex);
1669
 
 
1670
 
        /* Release the slot for others to use */
1671
 
 
1672
 
        slot->in_use = FALSE;
1673
 
 
1674
 
        wait_time = ut_difftime(ut_time(), slot->suspend_time);
1675
 
 
1676
 
        if (thr->lock_state == QUE_THR_LOCK_ROW) {
1677
 
                if (ut_usectime(&sec, &ms) == -1) {
1678
 
                        finish_time = -1;
1679
 
                } else {
1680
 
                        finish_time = (ib_int64_t) sec * 1000000 + ms;
1681
 
                }
1682
 
 
1683
 
                diff_time = (ulint) (finish_time - start_time);
1684
 
 
1685
 
                srv_n_lock_wait_current_count--;
1686
 
                srv_n_lock_wait_time = srv_n_lock_wait_time + diff_time;
1687
 
                if (diff_time > srv_n_lock_max_wait_time &&
1688
 
                    /* only update the variable if we successfully
1689
 
                    retrieved the start and finish times. See Bug#36819. */
1690
 
                    start_time != -1 && finish_time != -1) {
1691
 
                        srv_n_lock_max_wait_time = diff_time;
1692
 
                }
1693
 
 
1694
 
                /* Record the lock wait time for this thread */
1695
 
                thd_set_lock_wait_time(trx->mysql_thd, diff_time);
1696
 
        }
1697
 
 
1698
 
        if (trx->was_chosen_as_deadlock_victim) {
1699
 
 
1700
 
                trx->error_state = DB_DEADLOCK;
1701
 
                trx->was_chosen_as_deadlock_victim = FALSE;
1702
 
        }
1703
 
 
1704
 
        mutex_exit(&kernel_mutex);
1705
 
 
1706
 
        /* InnoDB system transactions (such as the purge, and
1707
 
        incomplete transactions that are being rolled back after crash
1708
 
        recovery) will use the global value of
1709
 
        innodb_lock_wait_timeout, because trx->mysql_thd == NULL. */
1710
 
        lock_wait_timeout = thd_lock_wait_timeout(trx->mysql_thd);
1711
 
 
1712
 
        if (lock_wait_timeout < 100000000
1713
 
            && wait_time > (double) lock_wait_timeout) {
1714
 
 
1715
 
                trx->error_state = DB_LOCK_WAIT_TIMEOUT;
1716
 
        }
1717
 
 
1718
 
        if (trx_is_interrupted(trx)) {
1719
 
 
1720
 
                trx->error_state = DB_INTERRUPTED;
1721
 
        }
1722
 
}
1723
 
 
1724
 
/********************************************************************//**
1725
 
Releases a MySQL OS thread waiting for a lock to be released, if the
1726
 
thread is already suspended. */
1727
 
UNIV_INTERN
1728
 
void
1729
 
srv_release_mysql_thread_if_suspended(
1730
 
/*==================================*/
1731
 
        que_thr_t*      thr)    /*!< in: query thread associated with the
1732
 
                                MySQL OS thread  */
1733
 
{
1734
 
        srv_slot_t*     slot;
1735
 
        ulint           i;
1736
 
 
1737
 
        ut_ad(mutex_own(&kernel_mutex));
1738
 
 
1739
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
1740
 
 
1741
 
                slot = srv_mysql_table + i;
1742
 
 
1743
 
                if (slot->in_use && slot->thr == thr) {
1744
 
                        /* Found */
1745
 
 
1746
 
                        os_event_set(slot->event);
1747
 
 
1748
 
                        return;
1749
 
                }
1750
 
        }
1751
 
 
1752
 
        /* not found */
1753
 
}
1754
 
 
1755
 
/******************************************************************//**
1756
 
Refreshes the values used to calculate per-second averages. */
1757
 
static
1758
 
void
1759
 
srv_refresh_innodb_monitor_stats(void)
1760
 
/*==================================*/
1761
 
{
1762
 
        mutex_enter(&srv_innodb_monitor_mutex);
1763
 
 
1764
 
        srv_last_monitor_time = time(NULL);
1765
 
 
1766
 
        os_aio_refresh_stats();
1767
 
 
1768
 
        btr_cur_n_sea_old = btr_cur_n_sea;
1769
 
        btr_cur_n_non_sea_old = btr_cur_n_non_sea;
1770
 
 
1771
 
        log_refresh_stats();
1772
 
 
1773
 
        buf_refresh_io_stats_all();
1774
 
 
1775
 
        srv_n_rows_inserted_old = srv_n_rows_inserted;
1776
 
        srv_n_rows_updated_old = srv_n_rows_updated;
1777
 
        srv_n_rows_deleted_old = srv_n_rows_deleted;
1778
 
        srv_n_rows_read_old = srv_n_rows_read;
1779
 
 
1780
 
        mutex_exit(&srv_innodb_monitor_mutex);
1781
 
}
1782
 
 
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 */
1787
 
UNIV_INTERN
1788
 
ibool
1789
 
srv_printf_innodb_monitor(
1790
 
/*======================*/
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
1794
 
                                the list of active transactions */
1795
 
        ulint*  trx_end)        /*!< out: file position of the end of
1796
 
                                the list of active transactions */
1797
 
{
1798
 
        double  time_elapsed;
1799
 
        time_t  current_time;
1800
 
        ulint   n_reserved;
1801
 
        ibool   ret;
1802
 
 
1803
 
        mutex_enter(&srv_innodb_monitor_mutex);
1804
 
 
1805
 
        current_time = time(NULL);
1806
 
 
1807
 
        /* We add 0.001 seconds to time_elapsed to prevent division
1808
 
        by zero if two users happen to call SHOW INNODB STATUS at the same
1809
 
        time */
1810
 
 
1811
 
        time_elapsed = difftime(current_time, srv_last_monitor_time)
1812
 
                + 0.001;
1813
 
 
1814
 
        srv_last_monitor_time = time(NULL);
1815
 
 
1816
 
        fputs("\n=====================================\n", file);
1817
 
 
1818
 
        ut_print_timestamp(file);
1819
 
        fprintf(file,
1820
 
                " INNODB MONITOR OUTPUT\n"
1821
 
                "=====================================\n"
1822
 
                "Per second averages calculated from the last %lu seconds\n",
1823
 
                (ulong)time_elapsed);
1824
 
 
1825
 
        fputs("-----------------\n"
1826
 
              "BACKGROUND THREAD\n"
1827
 
              "-----------------\n", file);
1828
 
        srv_print_master_thread_info(file);
1829
 
 
1830
 
        fputs("----------\n"
1831
 
              "SEMAPHORES\n"
1832
 
              "----------\n", file);
1833
 
        sync_print(file);
1834
 
 
1835
 
        /* Conceptually, srv_innodb_monitor_mutex has a very high latching
1836
 
        order level in sync0sync.h, while dict_foreign_err_mutex has a very
1837
 
        low level 135. Therefore we can reserve the latter mutex here without
1838
 
        a danger of a deadlock of threads. */
1839
 
 
1840
 
        mutex_enter(&dict_foreign_err_mutex);
1841
 
 
1842
 
        if (ftell(dict_foreign_err_file) != 0L) {
1843
 
                fputs("------------------------\n"
1844
 
                      "LATEST FOREIGN KEY ERROR\n"
1845
 
                      "------------------------\n", file);
1846
 
                ut_copy_file(file, dict_foreign_err_file);
1847
 
        }
1848
 
 
1849
 
        mutex_exit(&dict_foreign_err_mutex);
1850
 
 
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
 
 
1876
 
        fputs("--------\n"
1877
 
              "FILE I/O\n"
1878
 
              "--------\n", file);
1879
 
        os_aio_print(file);
1880
 
 
1881
 
        fputs("-------------------------------------\n"
1882
 
              "INSERT BUFFER AND ADAPTIVE HASH INDEX\n"
1883
 
              "-------------------------------------\n", file);
1884
 
        ibuf_print(file);
1885
 
 
1886
 
        ha_print_info(file, btr_search_sys->hash_index);
1887
 
 
1888
 
        fprintf(file,
1889
 
                "%.2f hash searches/s, %.2f non-hash searches/s\n",
1890
 
                (btr_cur_n_sea - btr_cur_n_sea_old)
1891
 
                / time_elapsed,
1892
 
                (btr_cur_n_non_sea - btr_cur_n_non_sea_old)
1893
 
                / time_elapsed);
1894
 
        btr_cur_n_sea_old = btr_cur_n_sea;
1895
 
        btr_cur_n_non_sea_old = btr_cur_n_non_sea;
1896
 
 
1897
 
        fputs("---\n"
1898
 
              "LOG\n"
1899
 
              "---\n", file);
1900
 
        log_print(file);
1901
 
 
1902
 
        fputs("----------------------\n"
1903
 
              "BUFFER POOL AND MEMORY\n"
1904
 
              "----------------------\n", file);
1905
 
        fprintf(file,
1906
 
                "Total memory allocated " ULINTPF
1907
 
                "; in additional pool allocated " ULINTPF "\n",
1908
 
                ut_total_allocated_memory,
1909
 
                mem_pool_get_reserved(mem_comm_pool));
1910
 
        fprintf(file, "Dictionary memory allocated " ULINTPF "\n",
1911
 
                dict_sys->size);
1912
 
 
1913
 
        buf_print_io(file);
1914
 
 
1915
 
        fputs("--------------\n"
1916
 
              "ROW OPERATIONS\n"
1917
 
              "--------------\n", file);
1918
 
        fprintf(file, "%ld queries inside InnoDB, %lu queries in queue\n",
1919
 
                (long) srv_conc_n_threads,
1920
 
                (ulong) srv_conc_n_waiting_threads);
1921
 
 
1922
 
        fprintf(file, "%lu read views open inside InnoDB\n",
1923
 
                UT_LIST_GET_LEN(trx_sys->view_list));
1924
 
 
1925
 
        n_reserved = fil_space_get_n_reserved_extents(0);
1926
 
        if (n_reserved > 0) {
1927
 
                fprintf(file,
1928
 
                        "%lu tablespace extents now reserved for"
1929
 
                        " B-tree split operations\n",
1930
 
                        (ulong) n_reserved);
1931
 
        }
1932
 
 
1933
 
#ifdef UNIV_LINUX
1934
 
        fprintf(file, "Main thread process no. %lu, id %lu, state: %s\n",
1935
 
                (ulong) srv_main_thread_process_no,
1936
 
                (ulong) srv_main_thread_id,
1937
 
                srv_main_thread_op_info);
1938
 
#else
1939
 
        fprintf(file, "Main thread id %lu, state: %s\n",
1940
 
                (ulong) srv_main_thread_id,
1941
 
                srv_main_thread_op_info);
1942
 
#endif
1943
 
        fprintf(file,
1944
 
                "Number of rows inserted " ULINTPF
1945
 
                ", updated " ULINTPF ", deleted " ULINTPF
1946
 
                ", read " ULINTPF "\n",
1947
 
                srv_n_rows_inserted,
1948
 
                srv_n_rows_updated,
1949
 
                srv_n_rows_deleted,
1950
 
                srv_n_rows_read);
1951
 
        fprintf(file,
1952
 
                "%.2f inserts/s, %.2f updates/s,"
1953
 
                " %.2f deletes/s, %.2f reads/s\n",
1954
 
                (srv_n_rows_inserted - srv_n_rows_inserted_old)
1955
 
                / time_elapsed,
1956
 
                (srv_n_rows_updated - srv_n_rows_updated_old)
1957
 
                / time_elapsed,
1958
 
                (srv_n_rows_deleted - srv_n_rows_deleted_old)
1959
 
                / time_elapsed,
1960
 
                (srv_n_rows_read - srv_n_rows_read_old)
1961
 
                / time_elapsed);
1962
 
 
1963
 
        srv_n_rows_inserted_old = srv_n_rows_inserted;
1964
 
        srv_n_rows_updated_old = srv_n_rows_updated;
1965
 
        srv_n_rows_deleted_old = srv_n_rows_deleted;
1966
 
        srv_n_rows_read_old = srv_n_rows_read;
1967
 
 
1968
 
        fputs("----------------------------\n"
1969
 
              "END OF INNODB MONITOR OUTPUT\n"
1970
 
              "============================\n", file);
1971
 
        mutex_exit(&srv_innodb_monitor_mutex);
1972
 
        fflush(file);
1973
 
 
1974
 
        return(ret);
1975
 
}
1976
 
 
1977
 
/******************************************************************//**
1978
 
Function to pass InnoDB status variables to MySQL */
1979
 
UNIV_INTERN
1980
 
void
1981
 
srv_export_innodb_status(void)
1982
 
/*==========================*/
1983
 
{
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
 
        mutex_enter(&srv_innodb_monitor_mutex);
1993
 
 
1994
 
        export_vars.innodb_data_pending_reads
1995
 
                = os_n_pending_reads;
1996
 
        export_vars.innodb_data_pending_writes
1997
 
                = os_n_pending_writes;
1998
 
        export_vars.innodb_data_pending_fsyncs
1999
 
                = fil_n_pending_log_flushes
2000
 
                + fil_n_pending_tablespace_flushes;
2001
 
        export_vars.innodb_data_fsyncs = os_n_fsyncs;
2002
 
        export_vars.innodb_data_read = srv_data_read;
2003
 
        export_vars.innodb_data_reads = os_n_file_reads;
2004
 
        export_vars.innodb_data_writes = os_n_file_writes;
2005
 
        export_vars.innodb_data_written = srv_data_written;
2006
 
        export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
2007
 
        export_vars.innodb_buffer_pool_write_requests
2008
 
                = srv_buf_pool_write_requests;
2009
 
        export_vars.innodb_buffer_pool_wait_free = srv_buf_pool_wait_free;
2010
 
        export_vars.innodb_buffer_pool_pages_flushed = srv_buf_pool_flushed;
2011
 
        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;
2019
 
#ifdef UNIV_DEBUG
2020
 
        export_vars.innodb_buffer_pool_pages_latched
2021
 
                = buf_get_latched_pages_number();
2022
 
#endif /* UNIV_DEBUG */
2023
 
        export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
2024
 
 
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
2032
 
        export_vars.innodb_page_size = UNIV_PAGE_SIZE;
2033
 
        export_vars.innodb_log_waits = srv_log_waits;
2034
 
        export_vars.innodb_os_log_written = srv_os_log_written;
2035
 
        export_vars.innodb_os_log_fsyncs = fil_n_log_flushes;
2036
 
        export_vars.innodb_os_log_pending_fsyncs = fil_n_pending_log_flushes;
2037
 
        export_vars.innodb_os_log_pending_writes = srv_os_log_pending_writes;
2038
 
        export_vars.innodb_log_write_requests = srv_log_write_requests;
2039
 
        export_vars.innodb_log_writes = srv_log_writes;
2040
 
        export_vars.innodb_dblwr_pages_written = srv_dblwr_pages_written;
2041
 
        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;
2045
 
        export_vars.innodb_row_lock_waits = srv_n_lock_wait_count;
2046
 
        export_vars.innodb_row_lock_current_waits
2047
 
                = srv_n_lock_wait_current_count;
2048
 
        export_vars.innodb_row_lock_time = srv_n_lock_wait_time / 1000;
2049
 
        if (srv_n_lock_wait_count > 0) {
2050
 
                export_vars.innodb_row_lock_time_avg = (ulint)
2051
 
                        (srv_n_lock_wait_time / 1000 / srv_n_lock_wait_count);
2052
 
        } else {
2053
 
                export_vars.innodb_row_lock_time_avg = 0;
2054
 
        }
2055
 
        export_vars.innodb_row_lock_time_max
2056
 
                = srv_n_lock_max_wait_time / 1000;
2057
 
        export_vars.innodb_rows_read = srv_n_rows_read;
2058
 
        export_vars.innodb_rows_inserted = srv_n_rows_inserted;
2059
 
        export_vars.innodb_rows_updated = srv_n_rows_updated;
2060
 
        export_vars.innodb_rows_deleted = srv_n_rows_deleted;
2061
 
        export_vars.innodb_truncated_status_writes = srv_truncated_status_writes;
2062
 
 
2063
 
        mutex_exit(&srv_innodb_monitor_mutex);
2064
 
}
2065
 
 
2066
 
/*********************************************************************//**
2067
 
A thread which prints the info output by various InnoDB monitors.
2068
 
@return a dummy parameter */
2069
 
UNIV_INTERN
2070
 
os_thread_ret_t
2071
 
srv_monitor_thread(
2072
 
/*===============*/
2073
 
        void*   arg __attribute__((unused)))
2074
 
                        /*!< in: a dummy parameter required by
2075
 
                        os_thread_create */
2076
 
{
2077
 
        double          time_elapsed;
2078
 
        time_t          current_time;
2079
 
        time_t          last_table_monitor_time;
2080
 
        time_t          last_tablespace_monitor_time;
2081
 
        time_t          last_monitor_time;
2082
 
        ulint           mutex_skipped;
2083
 
        ibool           last_srv_print_monitor;
2084
 
 
2085
 
#ifdef UNIV_DEBUG_THREAD_CREATION
2086
 
        fprintf(stderr, "Lock timeout thread starts, id %lu\n",
2087
 
                os_thread_pf(os_thread_get_curr_id()));
2088
 
#endif
2089
 
 
2090
 
#ifdef UNIV_PFS_THREAD
2091
 
        pfs_register_thread(srv_monitor_thread_key);
2092
 
#endif
2093
 
 
2094
 
        UT_NOT_USED(arg);
2095
 
        srv_last_monitor_time = time(NULL);
2096
 
        last_table_monitor_time = time(NULL);
2097
 
        last_tablespace_monitor_time = time(NULL);
2098
 
        last_monitor_time = time(NULL);
2099
 
        mutex_skipped = 0;
2100
 
        last_srv_print_monitor = srv_print_innodb_monitor;
2101
 
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);
2108
 
 
2109
 
        current_time = time(NULL);
2110
 
 
2111
 
        time_elapsed = difftime(current_time, last_monitor_time);
2112
 
 
2113
 
        if (time_elapsed > 15) {
2114
 
                last_monitor_time = time(NULL);
2115
 
 
2116
 
                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;
2137
 
                }
2138
 
 
2139
 
 
2140
 
                if (srv_innodb_status) {
2141
 
                        mutex_enter(&srv_monitor_file_mutex);
2142
 
                        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
 
 
2151
 
                        os_file_set_eof(srv_monitor_file);
2152
 
                        mutex_exit(&srv_monitor_file_mutex);
2153
 
                }
2154
 
 
2155
 
                if (srv_print_innodb_tablespace_monitor
2156
 
                    && difftime(current_time,
2157
 
                                last_tablespace_monitor_time) > 60) {
2158
 
                        last_tablespace_monitor_time = time(NULL);
2159
 
 
2160
 
                        fputs("========================"
2161
 
                              "========================\n",
2162
 
                              stderr);
2163
 
 
2164
 
                        ut_print_timestamp(stderr);
2165
 
 
2166
 
                        fputs(" INNODB TABLESPACE MONITOR OUTPUT\n"
2167
 
                              "========================"
2168
 
                              "========================\n",
2169
 
                              stderr);
2170
 
 
2171
 
                        fsp_print(0);
2172
 
                        fputs("Validating tablespace\n", stderr);
2173
 
                        fsp_validate(0);
2174
 
                        fputs("Validation ok\n"
2175
 
                              "---------------------------------------\n"
2176
 
                              "END OF INNODB TABLESPACE MONITOR OUTPUT\n"
2177
 
                              "=======================================\n",
2178
 
                              stderr);
2179
 
                }
2180
 
 
2181
 
                if (srv_print_innodb_table_monitor
2182
 
                    && difftime(current_time, last_table_monitor_time) > 60) {
2183
 
 
2184
 
                        last_table_monitor_time = time(NULL);
2185
 
 
2186
 
                        fputs("===========================================\n",
2187
 
                              stderr);
2188
 
 
2189
 
                        ut_print_timestamp(stderr);
2190
 
 
2191
 
                        fputs(" INNODB TABLE MONITOR OUTPUT\n"
2192
 
                              "===========================================\n",
2193
 
                              stderr);
2194
 
                        dict_print();
2195
 
 
2196
 
                        fputs("-----------------------------------\n"
2197
 
                              "END OF INNODB TABLE MONITOR OUTPUT\n"
2198
 
                              "==================================\n",
2199
 
                              stderr);
2200
 
                }
2201
 
        }
2202
 
 
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
 
        mutex_enter(&kernel_mutex);
2258
 
 
2259
 
        some_waits = FALSE;
2260
 
 
2261
 
        /* Check of all slots if a thread is waiting there, and if it
2262
 
        has exceeded the time limit */
2263
 
 
2264
 
        for (i = 0; i < OS_THREAD_MAX_N; i++) {
2265
 
 
2266
 
                slot = srv_mysql_table + i;
2267
 
 
2268
 
                if (slot->in_use) {
2269
 
                        trx_t*  trx;
2270
 
                        ulong   lock_wait_timeout;
2271
 
 
2272
 
                        some_waits = TRUE;
2273
 
 
2274
 
                        wait_time = ut_difftime(ut_time(), slot->suspend_time);
2275
 
 
2276
 
                        trx = thr_get_trx(slot->thr);
2277
 
                        lock_wait_timeout = thd_lock_wait_timeout(
2278
 
                                trx->mysql_thd);
2279
 
 
2280
 
                        if (trx_is_interrupted(trx)
2281
 
                            || (lock_wait_timeout < 100000000
2282
 
                                && (wait_time > (double) lock_wait_timeout
2283
 
                                    || wait_time < 0))) {
2284
 
 
2285
 
                                /* Timeout exceeded or a wrap-around in system
2286
 
                                time counter: cancel the lock request queued
2287
 
                                by the transaction and release possible
2288
 
                                other transactions waiting behind; it is
2289
 
                                possible that the lock has already been
2290
 
                                granted: in that case do nothing */
2291
 
 
2292
 
                                if (trx->wait_lock) {
2293
 
                                        lock_cancel_waiting_and_release(
2294
 
                                                trx->wait_lock);
2295
 
                                }
2296
 
                        }
2297
 
                }
2298
 
        }
2299
 
 
2300
 
        os_event_reset(srv_lock_timeout_thread_event);
2301
 
 
2302
 
        mutex_exit(&kernel_mutex);
2303
 
 
2304
 
        if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
2305
 
                goto exit_func;
2306
 
        }
2307
 
 
2308
 
        if (some_waits) {
2309
 
                goto loop;
2310
 
        }
2311
 
 
2312
 
        srv_lock_timeout_active = FALSE;
2313
 
 
2314
 
#if 0
2315
 
        /* The following synchronisation is disabled, since
2316
 
        the InnoDB monitor output is to be updated every 15 seconds. */
2317
 
        os_event_wait(srv_lock_timeout_thread_event);
2318
 
#endif
2319
 
        goto loop;
2320
 
 
2321
 
exit_func:
2322
 
        srv_lock_timeout_active = FALSE;
2323
 
 
2324
 
        /* We count the number of threads in os_thread_exit(). A created
2325
 
        thread should always use that to exit and not use return() to exit. */
2326
 
 
2327
 
        os_thread_exit(NULL);
2328
 
 
2329
 
        OS_THREAD_DUMMY_RETURN;
2330
 
}
2331
 
 
2332
 
/*********************************************************************//**
2333
 
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 */
2336
 
UNIV_INTERN
2337
 
os_thread_ret_t
2338
 
srv_error_monitor_thread(
2339
 
/*=====================*/
2340
 
        void*   arg __attribute__((unused)))
2341
 
                        /*!< in: a dummy parameter required by
2342
 
                        os_thread_create */
2343
 
{
2344
 
        /* number of successive fatal timeouts observed */
2345
 
        ulint           fatal_cnt       = 0;
2346
 
        ib_uint64_t     old_lsn;
2347
 
        ib_uint64_t     new_lsn;
2348
 
 
2349
 
        old_lsn = srv_start_lsn;
2350
 
 
2351
 
#ifdef UNIV_DEBUG_THREAD_CREATION
2352
 
        fprintf(stderr, "Error monitor thread starts, id %lu\n",
2353
 
                os_thread_pf(os_thread_get_curr_id()));
2354
 
#endif
2355
 
 
2356
 
#ifdef UNIV_PFS_THREAD
2357
 
        pfs_register_thread(srv_error_monitor_thread_key);
2358
 
#endif
2359
 
 
2360
 
loop:
2361
 
        srv_error_monitor_active = TRUE;
2362
 
 
2363
 
        /* Try to track a strange bug reported by Harald Fuchs and others,
2364
 
        where the lsn seems to decrease at times */
2365
 
 
2366
 
        new_lsn = log_get_lsn();
2367
 
 
2368
 
        if (new_lsn < old_lsn) {
2369
 
                ut_print_timestamp(stderr);
2370
 
                fprintf(stderr,
2371
 
                        "  InnoDB: Error: old log sequence number %"PRIu64""
2372
 
                        " was greater\n"
2373
 
                        "InnoDB: than the new log sequence number %"PRIu64"!\n"
2374
 
                        "InnoDB: Please submit a bug report"
2375
 
                        " to http://bugs.mysql.com\n",
2376
 
                        old_lsn, new_lsn);
2377
 
        }
2378
 
 
2379
 
        old_lsn = new_lsn;
2380
 
 
2381
 
        if (difftime(time(NULL), srv_last_monitor_time) > 60) {
2382
 
                /* We referesh InnoDB Monitor values so that averages are
2383
 
                printed from at most 60 last seconds */
2384
 
 
2385
 
                srv_refresh_innodb_monitor_stats();
2386
 
        }
2387
 
 
2388
 
        /* Update the statistics collected for deciding LRU
2389
 
        eviction policy. */
2390
 
        buf_LRU_stat_update();
2391
 
 
2392
 
        /* Update the statistics collected for flush rate policy. */
2393
 
        buf_flush_stat_update();
2394
 
 
2395
 
        /* In case mutex_exit is not a memory barrier, it is
2396
 
        theoretically possible some threads are left waiting though
2397
 
        the semaphore is already released. Wake up those threads: */
2398
 
 
2399
 
        sync_arr_wake_threads_if_sema_free();
2400
 
 
2401
 
        if (sync_array_print_long_waits()) {
2402
 
                fatal_cnt++;
2403
 
                if (fatal_cnt > 10) {
2404
 
 
2405
 
                        fprintf(stderr,
2406
 
                                "InnoDB: Error: semaphore wait has lasted"
2407
 
                                " > %lu seconds\n"
2408
 
                                "InnoDB: We intentionally crash the server,"
2409
 
                                " because it appears to be hung.\n",
2410
 
                                (ulong) srv_fatal_semaphore_wait_threshold);
2411
 
 
2412
 
                        ut_error;
2413
 
                }
2414
 
        } else {
2415
 
                fatal_cnt = 0;
2416
 
        }
2417
 
 
2418
 
        /* Flush stderr so that a database user gets the output
2419
 
        to possible MySQL error file */
2420
 
 
2421
 
        fflush(stderr);
2422
 
 
2423
 
        os_thread_sleep(1000000);
2424
 
 
2425
 
        if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
2426
 
 
2427
 
                goto loop;
2428
 
        }
2429
 
 
2430
 
        srv_error_monitor_active = FALSE;
2431
 
 
2432
 
        /* We count the number of threads in os_thread_exit(). A created
2433
 
        thread should always use that to exit and not use return() to exit. */
2434
 
 
2435
 
        os_thread_exit(NULL);
2436
 
 
2437
 
        OS_THREAD_DUMMY_RETURN;
2438
 
}
2439
 
 
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
 
/*******************************************************************//**
2466
 
Tells the InnoDB server that there has been activity in the database
2467
 
and wakes up the master thread if it is suspended (not sleeping). Used
2468
 
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). */
2471
 
UNIV_INTERN
2472
 
void
2473
 
srv_active_wake_master_thread(void)
2474
 
/*===============================*/
2475
 
{
2476
 
        srv_activity_count++;
2477
 
 
2478
 
        if (srv_n_threads_active[SRV_MASTER] == 0) {
2479
 
 
2480
 
                mutex_enter(&kernel_mutex);
2481
 
 
2482
 
                srv_release_threads(SRV_MASTER, 1);
2483
 
 
2484
 
                mutex_exit(&kernel_mutex);
2485
 
        }
2486
 
}
2487
 
 
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
 
/*******************************************************************//**
2513
 
Wakes up the master thread if it is suspended or being suspended. */
2514
 
UNIV_INTERN
2515
 
void
2516
 
srv_wake_master_thread(void)
2517
 
/*========================*/
2518
 
{
2519
 
        srv_activity_count++;
2520
 
 
2521
 
        mutex_enter(&kernel_mutex);
2522
 
 
2523
 
        srv_release_threads(SRV_MASTER, 1);
2524
 
 
2525
 
        mutex_exit(&kernel_mutex);
2526
 
}
2527
 
 
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 */
2598
 
UNIV_INTERN
2599
 
os_thread_ret_t
2600
 
srv_master_thread(
2601
 
/*==============*/
2602
 
        void*   arg __attribute__((unused)))
2603
 
                        /*!< in: a dummy parameter required by
2604
 
                        os_thread_create */
2605
 
{
2606
 
        buf_pool_stat_t buf_stat;
2607
 
        os_event_t      event;
2608
 
        ulint           old_activity_count;
2609
 
        ulint           n_pages_purged  = 0;
2610
 
        ulint           n_bytes_merged;
2611
 
        ulint           n_pages_flushed;
2612
 
        ulint           n_bytes_archived;
2613
 
        ulint           n_tables_to_drop;
2614
 
        ulint           n_ios;
2615
 
        ulint           n_ios_old;
2616
 
        ulint           n_ios_very_old;
2617
 
        ulint           n_pend_ios;
2618
 
        ulint           next_itr_time;
2619
 
        ulint           i;
2620
 
 
2621
 
#ifdef UNIV_DEBUG_THREAD_CREATION
2622
 
        fprintf(stderr, "Master thread starts, id %lu\n",
2623
 
                os_thread_pf(os_thread_get_curr_id()));
2624
 
#endif
2625
 
 
2626
 
#ifdef UNIV_PFS_THREAD
2627
 
        pfs_register_thread(srv_master_thread_key);
2628
 
#endif
2629
 
 
2630
 
        srv_main_thread_process_no = os_proc_get_number();
2631
 
        srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
2632
 
 
2633
 
        srv_table_reserve_slot(SRV_MASTER);
2634
 
 
2635
 
        mutex_enter(&kernel_mutex);
2636
 
 
2637
 
        srv_n_threads_active[SRV_MASTER]++;
2638
 
 
2639
 
        mutex_exit(&kernel_mutex);
2640
 
 
2641
 
loop:
2642
 
        /*****************************************************************/
2643
 
        /* ---- When there is database activity by users, we cycle in this
2644
 
        loop */
2645
 
 
2646
 
        srv_main_thread_op_info = "reserving kernel mutex";
2647
 
 
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;
2651
 
        mutex_enter(&kernel_mutex);
2652
 
 
2653
 
        /* Store the user activity counter at the start of this loop */
2654
 
        old_activity_count = srv_activity_count;
2655
 
 
2656
 
        mutex_exit(&kernel_mutex);
2657
 
 
2658
 
        if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {
2659
 
 
2660
 
                goto suspend_thread;
2661
 
        }
2662
 
 
2663
 
        /* ---- We run the following loop approximately once per second
2664
 
        when there is database activity */
2665
 
 
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;
2670
 
 
2671
 
        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
 
 
2679
 
                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++;
2691
 
                }
2692
 
 
2693
 
                /* Each iteration should happen at 1 second interval. */
2694
 
                next_itr_time = ut_time_ms() + 1000;
2695
 
 
2696
 
                /* ALTER TABLE in MySQL requires on Unix that the table handler
2697
 
                can drop tables lazily after there no longer are SELECT
2698
 
                queries to them. */
2699
 
 
2700
 
                srv_main_thread_op_info = "doing background drop tables";
2701
 
 
2702
 
                row_drop_tables_for_mysql_in_background();
2703
 
 
2704
 
                srv_main_thread_op_info = "";
2705
 
 
2706
 
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2707
 
 
2708
 
                        goto background_loop;
2709
 
                }
2710
 
 
2711
 
                /* Flush logs if needed */
2712
 
                srv_sync_log_buffer_in_background();
2713
 
 
2714
 
                srv_main_thread_op_info = "making checkpoint";
2715
 
                log_free_check();
2716
 
 
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. */
2720
 
 
2721
 
                buf_get_total_stat(&buf_stat);
2722
 
                n_pend_ios = buf_get_n_pending_ios()
2723
 
                        + 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)) {
2728
 
                        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();
2733
 
                }
2734
 
 
2735
 
                if (UNIV_UNLIKELY(buf_get_modified_ratio_pct()
2736
 
                                  > srv_max_buf_pool_modified_pct)) {
2737
 
 
2738
 
                        /* Try to keep the number of modified pages in the
2739
 
                        buffer pool under the limit wished by the user */
2740
 
 
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
 
                        }
2762
 
                }
2763
 
 
2764
 
                if (srv_activity_count == old_activity_count) {
2765
 
 
2766
 
                        /* There is no user activity at the moment, go to
2767
 
                        the background loop */
2768
 
 
2769
 
                        goto background_loop;
2770
 
                }
2771
 
        }
2772
 
 
2773
 
        /* ---- We perform the following code approximately once per
2774
 
        10 seconds when there is database activity */
2775
 
 
2776
 
#ifdef MEM_PERIODIC_CHECK
2777
 
        /* Check magic numbers of every allocated mem block once in 10
2778
 
        seconds */
2779
 
        mem_validate_all_blocks();
2780
 
#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);
2791
 
        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)) {
2798
 
 
2799
 
                srv_main_thread_op_info = "flushing buffer pool pages";
2800
 
                buf_flush_list(PCT_IO(100), IB_ULONGLONG_MAX);
2801
 
 
2802
 
                /* Flush logs if needed */
2803
 
                srv_sync_log_buffer_in_background();
2804
 
        }
2805
 
 
2806
 
        /* We run a batch of insert buffer merge every 10 seconds,
2807
 
        even if the server were active */
2808
 
 
2809
 
        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();
2819
 
 
2820
 
                if (srv_fast_shutdown && srv_shutdown_state > 0) {
2821
 
 
2822
 
                        goto background_loop;
2823
 
                }
2824
 
        }
2825
 
 
2826
 
        srv_main_thread_op_info = "flushing buffer pool pages";
2827
 
 
2828
 
        /* Flush a few oldest pages to make a new checkpoint younger */
2829
 
 
2830
 
        if (buf_get_modified_ratio_pct() > 70) {
2831
 
 
2832
 
                /* If there are lots of modified pages in the buffer pool
2833
 
                (> 70 %), we assume we can afford reserving the disk(s) for
2834
 
                the time it requires to flush 100 pages */
2835
 
 
2836
 
                n_pages_flushed = buf_flush_list(
2837
 
                        PCT_IO(100), IB_ULONGLONG_MAX);
2838
 
        } else {
2839
 
                /* Otherwise, we only flush a small number of pages so that
2840
 
                we do not unnecessarily use much disk i/o capacity from
2841
 
                other work */
2842
 
 
2843
 
                n_pages_flushed = buf_flush_list(
2844
 
                          PCT_IO(10), IB_ULONGLONG_MAX);
2845
 
        }
2846
 
 
2847
 
        srv_main_thread_op_info = "making checkpoint";
2848
 
 
2849
 
        /* Make a new checkpoint about once in 10 seconds */
2850
 
 
2851
 
        log_checkpoint(TRUE, FALSE);
2852
 
 
2853
 
        srv_main_thread_op_info = "reserving kernel mutex";
2854
 
 
2855
 
        mutex_enter(&kernel_mutex);
2856
 
 
2857
 
        /* ---- When there is database activity, we jump from here back to
2858
 
        the start of loop */
2859
 
 
2860
 
        if (srv_activity_count != old_activity_count) {
2861
 
                mutex_exit(&kernel_mutex);
2862
 
                goto loop;
2863
 
        }
2864
 
 
2865
 
        mutex_exit(&kernel_mutex);
2866
 
 
2867
 
        /* If the database is quiet, we enter the background loop */
2868
 
 
2869
 
        /*****************************************************************/
2870
 
background_loop:
2871
 
        /* ---- In this loop we run background operations when the server
2872
 
        is quiet from user activity. Also in the case of a shutdown, we
2873
 
        loop here, flushing the buffer pool to the data files. */
2874
 
 
2875
 
        /* The server has been quiet for a while: start running background
2876
 
        operations */
2877
 
        srv_main_background_loops++;
2878
 
        srv_main_thread_op_info = "doing background drop tables";
2879
 
 
2880
 
        n_tables_to_drop = row_drop_tables_for_mysql_in_background();
2881
 
 
2882
 
        if (n_tables_to_drop > 0) {
2883
 
                /* Do not monopolize the CPU even if there are tables waiting
2884
 
                in the background drop queue. (It is essentially a bug if
2885
 
                MySQL tries to drop a table while there are still open handles
2886
 
                to it and we had to put it to the background drop queue.) */
2887
 
 
2888
 
                os_thread_sleep(100000);
2889
 
        }
2890
 
 
2891
 
        if (srv_n_purge_threads == 0) {
2892
 
                srv_main_thread_op_info = "master purging";
2893
 
 
2894
 
                srv_master_do_purge();
2895
 
        }
2896
 
 
2897
 
        srv_main_thread_op_info = "reserving kernel mutex";
2898
 
 
2899
 
        mutex_enter(&kernel_mutex);
2900
 
        if (srv_activity_count != old_activity_count) {
2901
 
                mutex_exit(&kernel_mutex);
2902
 
                goto loop;
2903
 
        }
2904
 
        mutex_exit(&kernel_mutex);
2905
 
 
2906
 
        srv_main_thread_op_info = "doing insert buffer merge";
2907
 
 
2908
 
        if (srv_fast_shutdown && srv_shutdown_state > 0) {
2909
 
                n_bytes_merged = 0;
2910
 
        } 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));
2917
 
        }
2918
 
 
2919
 
        srv_main_thread_op_info = "reserving kernel mutex";
2920
 
 
2921
 
        mutex_enter(&kernel_mutex);
2922
 
        if (srv_activity_count != old_activity_count) {
2923
 
                mutex_exit(&kernel_mutex);
2924
 
                goto loop;
2925
 
        }
2926
 
        mutex_exit(&kernel_mutex);
2927
 
 
2928
 
flush_loop:
2929
 
        srv_main_thread_op_info = "flushing buffer pool pages";
2930
 
        srv_main_flush_loops++;
2931
 
        if (srv_fast_shutdown < 2) {
2932
 
                n_pages_flushed = buf_flush_list(
2933
 
                          PCT_IO(100), IB_ULONGLONG_MAX);
2934
 
        } else {
2935
 
                /* In the fastest shutdown we do not flush the buffer pool
2936
 
                to data files: we set n_pages_flushed to 0 artificially. */
2937
 
 
2938
 
                n_pages_flushed = 0;
2939
 
        }
2940
 
 
2941
 
        srv_main_thread_op_info = "reserving kernel mutex";
2942
 
 
2943
 
        mutex_enter(&kernel_mutex);
2944
 
        if (srv_activity_count != old_activity_count) {
2945
 
                mutex_exit(&kernel_mutex);
2946
 
                goto loop;
2947
 
        }
2948
 
        mutex_exit(&kernel_mutex);
2949
 
 
2950
 
        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();
2955
 
 
2956
 
        srv_main_thread_op_info = "making checkpoint";
2957
 
 
2958
 
        log_checkpoint(TRUE, FALSE);
2959
 
 
2960
 
        if (buf_get_modified_ratio_pct() > srv_max_buf_pool_modified_pct) {
2961
 
 
2962
 
                /* Try to keep the number of modified pages in the
2963
 
                buffer pool under the limit wished by the user */
2964
 
 
2965
 
                goto flush_loop;
2966
 
        }
2967
 
 
2968
 
        srv_main_thread_op_info = "reserving kernel mutex";
2969
 
 
2970
 
        mutex_enter(&kernel_mutex);
2971
 
        if (srv_activity_count != old_activity_count) {
2972
 
                mutex_exit(&kernel_mutex);
2973
 
                goto loop;
2974
 
        }
2975
 
        mutex_exit(&kernel_mutex);
2976
 
        /*
2977
 
        srv_main_thread_op_info = "archiving log (if log archive is on)";
2978
 
 
2979
 
        log_archive_do(FALSE, &n_bytes_archived);
2980
 
        */
2981
 
        n_bytes_archived = 0;
2982
 
 
2983
 
        /* Keep looping in the background loop if still work to do */
2984
 
 
2985
 
        if (srv_fast_shutdown && srv_shutdown_state > 0) {
2986
 
                if (n_tables_to_drop + n_pages_flushed
2987
 
                    + n_bytes_archived != 0) {
2988
 
 
2989
 
                        /* If we are doing a fast shutdown (= the default)
2990
 
                        we do not do purge or insert buffer merge. But we
2991
 
                        flush the buffer pool completely to disk.
2992
 
                        In a 'very fast' shutdown we do not flush the buffer
2993
 
                        pool to data files: we have set n_pages_flushed to
2994
 
                        0 artificially. */
2995
 
 
2996
 
                        goto background_loop;
2997
 
                }
2998
 
        } else if (n_tables_to_drop
2999
 
                   + n_pages_purged + n_bytes_merged + n_pages_flushed
3000
 
                   + n_bytes_archived != 0) {
3001
 
                /* In a 'slow' shutdown we run purge and the insert buffer
3002
 
                merge to completion */
3003
 
 
3004
 
                goto background_loop;
3005
 
        }
3006
 
 
3007
 
        /* There is no work for background operations either: suspend
3008
 
        master thread to wait for more server activity */
3009
 
 
3010
 
suspend_thread:
3011
 
        srv_main_thread_op_info = "suspending";
3012
 
 
3013
 
        mutex_enter(&kernel_mutex);
3014
 
 
3015
 
        if (row_get_background_drop_list_len_low() > 0) {
3016
 
                mutex_exit(&kernel_mutex);
3017
 
 
3018
 
                goto loop;
3019
 
        }
3020
 
 
3021
 
        event = srv_suspend_thread();
3022
 
 
3023
 
        mutex_exit(&kernel_mutex);
3024
 
 
3025
 
        /* DO NOT CHANGE THIS STRING. innobase_start_or_create_for_mysql()
3026
 
        waits for database activity to die down when converting < 4.1.x
3027
 
        databases, and relies on this string being exactly as it is. InnoDB
3028
 
        manual also mentions this string in several places. */
3029
 
        srv_main_thread_op_info = "waiting for server activity";
3030
 
 
3031
 
        os_event_wait(event);
3032
 
 
3033
 
        if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
3034
 
                /* This is only extra safety, the thread should exit
3035
 
                already when the event wait ends */
3036
 
 
3037
 
                os_thread_exit(NULL);
3038
 
 
3039
 
        }
3040
 
 
3041
 
        /* When there is user activity, InnoDB will set the event and the
3042
 
        main thread goes back to loop. */
3043
 
 
3044
 
        goto loop;
3045
 
 
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
 
}