1
/*****************************************************************************
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
4
Copyright (c) 2008, Google Inc.
6
Portions of this file contain modifications contributed and copyrighted by
7
Google, Inc. Those modifications are gratefully acknowledged and are described
8
briefly in the InnoDB documentation. The contributions by Google are
9
incorporated with their permission, and subject to the conditions contained in
10
the file COPYING.Google.
12
This program is free software; you can redistribute it and/or modify it under
13
the terms of the GNU General Public License as published by the Free Software
14
Foundation; version 2 of the License.
16
This program is distributed in the hope that it will be useful, but WITHOUT
17
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License along with
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22
Place, Suite 330, Boston, MA 02111-1307 USA
24
*****************************************************************************/
26
/******************************************************
27
The server main program
29
Created 10/10/1995 Heikki Tuuri
30
*******************************************************/
36
#include "sync0sync.h"
38
#include "que0types.h"
39
#include "trx0types.h"
41
extern const char* srv_main_thread_op_info;
43
/* Prefix used by MySQL to indicate pre-5.1 table name encoding */
44
extern const char srv_mysql50_table_name_prefix[9];
46
/* When this event is set the lock timeout and InnoDB monitor
47
thread starts running */
48
extern os_event_t srv_lock_timeout_thread_event;
50
/* If the last data file is auto-extended, we add this many pages to it
52
#define SRV_AUTO_EXTEND_INCREMENT \
53
(srv_auto_extend_increment * ((1024 * 1024) / UNIV_PAGE_SIZE))
55
/* This is set to TRUE if the MySQL user has set it in MySQL */
56
extern ibool srv_lower_case_table_names;
58
/* Mutex for locking srv_monitor_file */
59
extern mutex_t srv_monitor_file_mutex;
60
/* Temporary file for innodb monitor output */
61
extern FILE* srv_monitor_file;
62
/* Mutex for locking srv_dict_tmpfile.
63
This mutex has a very high rank; threads reserving it should not
64
be holding any InnoDB latches. */
65
extern mutex_t srv_dict_tmpfile_mutex;
66
/* Temporary file for output from the data dictionary */
67
extern FILE* srv_dict_tmpfile;
68
/* Mutex for locking srv_misc_tmpfile.
69
This mutex has a very low rank; threads reserving it should not
70
acquire any further latches or sleep before releasing this one. */
71
extern mutex_t srv_misc_tmpfile_mutex;
72
/* Temporary file for miscellanous diagnostic output */
73
extern FILE* srv_misc_tmpfile;
75
/* Server parameters which are read from the initfile */
77
extern char* srv_data_home;
78
#ifdef UNIV_LOG_ARCHIVE
79
extern char* srv_arch_dir;
80
#endif /* UNIV_LOG_ARCHIVE */
82
/* store to its own file each table created by an user; data
83
dictionary tables are in the system tablespace 0 */
84
extern my_bool srv_file_per_table;
85
/* The file format to use on new *.ibd files. */
86
extern ulint srv_file_format;
87
/* Whether to check file format during startup.*/
88
extern ulint srv_check_file_format_at_startup;
89
/* Place locks to records only i.e. do not use next-key locking except
90
on duplicate key checking and foreign key checking */
91
extern ibool srv_locks_unsafe_for_binlog;
93
extern ulint srv_n_data_files;
94
extern char** srv_data_file_names;
95
extern ulint* srv_data_file_sizes;
96
extern ulint* srv_data_file_is_raw_partition;
98
extern ibool srv_auto_extend_last_data_file;
99
extern ulint srv_last_file_size_max;
100
extern ulong srv_auto_extend_increment;
102
extern ibool srv_created_new_raw;
104
#define SRV_NEW_RAW 1
105
#define SRV_OLD_RAW 2
107
extern char** srv_log_group_home_dirs;
109
extern ulint srv_n_log_groups;
110
extern ulint srv_n_log_files;
111
extern ulint srv_log_file_size;
112
extern ulint srv_log_buffer_size;
113
extern ulong srv_flush_log_at_trx_commit;
115
/* The sort order table of the MySQL latin1_swedish_ci character set
117
extern const byte srv_latin1_ordering[256];
118
extern bool srv_use_sys_malloc;
119
extern ulint srv_buf_pool_size; /* requested size in bytes */
120
extern ulint srv_buf_pool_old_size; /* previously requested size */
121
extern ulint srv_buf_pool_curr_size; /* current size in bytes */
122
extern ulint srv_mem_pool_size;
123
extern ulint srv_lock_table_size;
125
extern ulint srv_n_file_io_threads;
127
#ifdef UNIV_LOG_ARCHIVE
128
extern ibool srv_log_archive_on;
129
extern ibool srv_archive_recovery;
130
extern dulint srv_archive_recovery_limit_lsn;
131
#endif /* UNIV_LOG_ARCHIVE */
133
extern char* srv_file_flush_method_str;
134
extern ulint srv_unix_file_flush_method;
135
extern ulint srv_win_file_flush_method;
137
extern ulint srv_max_n_open_files;
139
extern ulint srv_max_dirty_pages_pct;
141
extern ulint srv_force_recovery;
142
extern ulong srv_thread_concurrency;
143
extern ulong srv_commit_concurrency;
145
extern ulint srv_max_n_threads;
147
extern lint srv_conc_n_threads;
149
extern ulint srv_fast_shutdown; /* If this is 1, do not do a
150
purge and index buffer merge.
151
If this 2, do not even flush the
152
buffer pool to data files at the
153
shutdown: we effectively 'crash'
154
InnoDB (but lose no committed
156
extern ibool srv_innodb_status;
158
extern ib_uint64_t srv_stats_sample_pages;
160
extern ibool srv_use_doublewrite_buf;
161
extern ibool srv_use_checksums;
163
extern ibool srv_set_thread_priorities;
164
extern int srv_query_thread_priority;
166
extern ulong srv_max_buf_pool_modified_pct;
167
extern ulong srv_max_purge_lag;
169
extern ulong srv_replication_delay;
170
/*-------------------------------------------*/
172
extern ulint srv_n_rows_inserted;
173
extern ulint srv_n_rows_updated;
174
extern ulint srv_n_rows_deleted;
175
extern ulint srv_n_rows_read;
177
extern ibool srv_print_innodb_monitor;
178
extern ibool srv_print_innodb_lock_monitor;
179
extern ibool srv_print_innodb_tablespace_monitor;
180
extern ibool srv_print_verbose_log;
181
extern ibool srv_print_innodb_table_monitor;
183
extern ibool srv_lock_timeout_and_monitor_active;
184
extern ibool srv_error_monitor_active;
186
extern ulong srv_n_spin_wait_rounds;
187
extern ulong srv_n_free_tickets_to_enter;
188
extern ulong srv_thread_sleep_delay;
189
extern ulint srv_spin_wait_delay;
190
extern ibool srv_priority_boost;
194
extern ibool srv_print_thread_releases;
195
extern ibool srv_print_lock_waits;
196
extern ibool srv_print_buf_io;
197
extern ibool srv_print_log_io;
198
extern ibool srv_print_latch_waits;
199
#else /* UNIV_DEBUG */
200
# define srv_print_thread_releases FALSE
201
# define srv_print_lock_waits FALSE
202
# define srv_print_buf_io FALSE
203
# define srv_print_log_io FALSE
204
# define srv_print_latch_waits FALSE
205
#endif /* UNIV_DEBUG */
207
extern ulint srv_activity_count;
208
extern ulint srv_fatal_semaphore_wait_threshold;
209
extern ulint srv_dml_needed_delay;
211
extern mutex_t* kernel_mutex_temp;/* mutex protecting the server, trx structs,
212
query threads, and lock table: we allocate
213
it from dynamic memory to get it to the
214
same DRAM page as other hotspot semaphores */
215
#define kernel_mutex (*kernel_mutex_temp)
217
#define SRV_MAX_N_IO_THREADS 100
219
/* Array of English strings describing the current state of an
220
i/o handler thread */
221
extern const char* srv_io_thread_op_info[];
222
extern const char* srv_io_thread_function[];
224
/* the number of the log write requests done */
225
extern ulint srv_log_write_requests;
227
/* the number of physical writes to the log performed */
228
extern ulint srv_log_writes;
230
/* amount of data written to the log files in bytes */
231
extern ulint srv_os_log_written;
233
/* amount of writes being done to the log files */
234
extern ulint srv_os_log_pending_writes;
236
/* we increase this counter, when there we don't have enough space in the
237
log buffer and have to flush it */
238
extern ulint srv_log_waits;
240
/* variable that counts amount of data read in total (in bytes) */
241
extern ulint srv_data_read;
243
/* here we count the amount of data written in total (in bytes) */
244
extern ulint srv_data_written;
246
/* this variable counts the amount of times, when the doublewrite buffer
248
extern ulint srv_dblwr_writes;
250
/* here we store the number of pages that have been flushed to the
251
doublewrite buffer */
252
extern ulint srv_dblwr_pages_written;
254
/* in this variable we store the number of write requests issued */
255
extern ulint srv_buf_pool_write_requests;
257
/* here we store the number of times when we had to wait for a free page
258
in the buffer pool. It happens when the buffer pool is full and we need
259
to make a flush, in order to be able to read or create a page. */
260
extern ulint srv_buf_pool_wait_free;
262
/* variable to count the number of pages that were written from the
263
buffer pool to disk */
264
extern ulint srv_buf_pool_flushed;
266
/* variable to count the number of buffer pool reads that led to the
267
reading of a disk page */
268
extern ulint srv_buf_pool_reads;
270
/* variable to count the number of sequential read-aheads were done */
271
extern ulint srv_read_ahead_seq;
273
/* variable to count the number of random read-aheads were done */
274
extern ulint srv_read_ahead_rnd;
276
/* In this structure we store status variables to be passed to MySQL */
277
typedef struct export_var_struct export_struc;
279
extern export_struc export_vars;
281
typedef struct srv_sys_struct srv_sys_t;
283
/* The server system */
284
extern srv_sys_t* srv_sys;
286
/* Alternatives for the file flush option in Unix; see the InnoDB manual
287
about what these mean */
288
#define SRV_UNIX_FSYNC 1 /* This is the default */
289
#define SRV_UNIX_O_DSYNC 2
290
#define SRV_UNIX_LITTLESYNC 3
291
#define SRV_UNIX_NOSYNC 4
292
#define SRV_UNIX_O_DIRECT 5
294
/* Alternatives for file i/o in Windows */
295
#define SRV_WIN_IO_NORMAL 1
296
#define SRV_WIN_IO_UNBUFFERED 2 /* This is the default */
298
/* Alternatives for srv_force_recovery. Non-zero values are intended
299
to help the user get a damaged database up so that he can dump intact
300
tables and rows with SELECT INTO OUTFILE. The database must not otherwise
301
be used with these options! A bigger number below means that all precautions
302
of lower numbers are included. */
304
#define SRV_FORCE_IGNORE_CORRUPT 1 /* let the server run even if it
305
detects a corrupt page */
306
#define SRV_FORCE_NO_BACKGROUND 2 /* prevent the main thread from
307
running: if a crash would occur
308
in purge, this prevents it */
309
#define SRV_FORCE_NO_TRX_UNDO 3 /* do not run trx rollback after
311
#define SRV_FORCE_NO_IBUF_MERGE 4 /* prevent also ibuf operations:
312
if they would cause a crash, better
314
#define SRV_FORCE_NO_UNDO_LOG_SCAN 5 /* do not look at undo logs when
315
starting the database: InnoDB will
316
treat even incomplete transactions
318
#define SRV_FORCE_NO_LOG_REDO 6 /* do not do the log roll-forward
319
in connection with recovery */
321
/** Types of threads existing in the system. */
322
enum srv_thread_type {
323
SRV_COM = 1, /**< threads serving communication and queries */
324
SRV_CONSOLE, /**< thread serving console */
325
SRV_WORKER, /**< threads serving parallelized queries and
326
queries released from lock wait */
328
/* Utility threads */
329
SRV_BUFFER, /**< thread flushing dirty buffer blocks */
330
SRV_RECOVERY, /**< threads finishing a recovery */
331
SRV_INSERT, /**< thread flushing the insert buffer to disk */
333
SRV_MASTER /**< the master thread, (whose type number must
337
/*************************************************************************
338
Boots Innobase server. */
343
/* out: DB_SUCCESS or error code */
344
/*************************************************************************
345
Initializes the server. */
350
/*************************************************************************
351
Frees the OS fast mutex created in srv_boot(). */
356
/*************************************************************************
357
Initializes the synchronization primitives, memory system, and the thread
361
srv_general_init(void);
362
/*==================*/
363
/*************************************************************************
364
Gets the number of threads in the system. */
367
srv_get_n_threads(void);
368
/*===================*/
369
/*************************************************************************
370
Returns the calling thread type. */
373
srv_get_thread_type(void);
374
/*=====================*/
375
/* out: SRV_COM, ... */
376
/*************************************************************************
377
Sets the info describing an i/o thread current state. */
380
srv_set_io_thread_op_info(
381
/*======================*/
382
ulint i, /* in: the 'segment' of the i/o thread */
383
const char* str); /* in: constant char string describing the
385
/*************************************************************************
386
Releases threads of the type given from suspension in the thread table.
387
NOTE! The server mutex has to be reserved by the caller! */
392
/* out: number of threads
393
released: this may be < n if
394
not enough threads were
395
suspended at the moment */
396
enum srv_thread_type type, /* in: thread type */
397
ulint n); /* in: number of threads to release */
398
/*************************************************************************
399
The master thread controlling the server. */
404
/* out: a dummy parameter */
405
void* arg); /* in: a dummy parameter required by
407
/***********************************************************************
408
Tells the Innobase server that there has been activity in the database
409
and wakes up the master thread if it is suspended (not sleeping). Used
410
in the MySQL interface. Note that there is a small chance that the master
411
thread stays suspended (we do not protect our operation with the kernel
412
mutex, for performace reasons). */
415
srv_active_wake_master_thread(void);
416
/*===============================*/
417
/***********************************************************************
418
Wakes up the master thread if it is suspended or being suspended. */
421
srv_wake_master_thread(void);
422
/*========================*/
423
/*************************************************************************
424
Puts an OS thread to wait if there are too many concurrent threads
425
(>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
428
srv_conc_enter_innodb(
429
/*==================*/
430
trx_t* trx); /* in: transaction object associated with the
432
/*************************************************************************
433
This lets a thread enter InnoDB regardless of the number of threads inside
434
InnoDB. This must be called when a thread ends a lock wait. */
437
srv_conc_force_enter_innodb(
438
/*========================*/
439
trx_t* trx); /* in: transaction object associated with the
441
/*************************************************************************
442
This must be called when a thread exits InnoDB in a lock wait or at the
443
end of an SQL statement. */
446
srv_conc_force_exit_innodb(
447
/*=======================*/
448
trx_t* trx); /* in: transaction object associated with the
450
/*************************************************************************
451
This must be called when a thread exits InnoDB. */
454
srv_conc_exit_innodb(
455
/*=================*/
456
trx_t* trx); /* in: transaction object associated with the
458
/*******************************************************************
459
Puts a MySQL OS thread to wait for a lock to be released. If an error
460
occurs during the wait trx->error_state associated with thr is
461
!= DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
462
are possible errors. DB_DEADLOCK is returned if selective deadlock
463
resolution chose this transaction as a victim. */
466
srv_suspend_mysql_thread(
467
/*=====================*/
468
que_thr_t* thr); /* in: query thread associated with the MySQL
470
/************************************************************************
471
Releases a MySQL OS thread waiting for a lock to be released, if the
472
thread is already suspended. */
475
srv_release_mysql_thread_if_suspended(
476
/*==================================*/
477
que_thr_t* thr); /* in: query thread associated with the
479
/*************************************************************************
480
A thread which wakes up threads whose lock wait may have lasted too long.
481
This also prints the info output by various InnoDB monitors. */
484
srv_lock_timeout_and_monitor_thread(
485
/*================================*/
486
/* out: a dummy parameter */
487
void* arg); /* in: a dummy parameter required by
489
/*************************************************************************
490
A thread which prints warnings about semaphore waits which have lasted
491
too long. These can be used to track bugs which cause hangs. */
494
srv_error_monitor_thread(
495
/*=====================*/
496
/* out: a dummy parameter */
497
void* arg); /* in: a dummy parameter required by
499
/**********************************************************************
500
Outputs to a file the output of the InnoDB Monitor. */
503
srv_printf_innodb_monitor(
504
/*======================*/
505
FILE* file, /* in: output stream */
506
ulint* trx_start, /* out: file position of the start of
507
the list of active transactions */
508
ulint* trx_end); /* out: file position of the end of
509
the list of active transactions */
511
/**********************************************************************
512
Function to pass InnoDB status variables to MySQL */
515
srv_export_innodb_status(void);
516
/*=====================*/
518
/* Thread slot in the thread table */
519
typedef struct srv_slot_struct srv_slot_t;
521
/* Thread table is an array of slots */
522
typedef srv_slot_t srv_table_t;
524
/* In this structure we store status variables to be passed to MySQL */
525
struct export_var_struct{
526
ulint innodb_data_pending_reads;
527
ulint innodb_data_pending_writes;
528
ulint innodb_data_pending_fsyncs;
529
ulint innodb_data_fsyncs;
530
ulint innodb_data_read;
531
ulint innodb_data_writes;
532
ulint innodb_data_written;
533
ulint innodb_data_reads;
534
ulint innodb_buffer_pool_pages_total;
535
ulint innodb_buffer_pool_pages_data;
536
ulint innodb_buffer_pool_pages_dirty;
537
ulint innodb_buffer_pool_pages_misc;
538
ulint innodb_buffer_pool_pages_free;
540
ulint innodb_buffer_pool_pages_latched;
541
#endif /* UNIV_DEBUG */
542
ulint innodb_buffer_pool_read_requests;
543
ulint innodb_buffer_pool_reads;
544
ulint innodb_buffer_pool_wait_free;
545
ulint innodb_buffer_pool_pages_flushed;
546
ulint innodb_buffer_pool_write_requests;
547
ulint innodb_buffer_pool_read_ahead_seq;
548
ulint innodb_buffer_pool_read_ahead_rnd;
549
ulint innodb_dblwr_pages_written;
550
ulint innodb_dblwr_writes;
551
ibool innodb_have_atomic_builtins;
552
ulint innodb_log_waits;
553
ulint innodb_log_write_requests;
554
ulint innodb_log_writes;
555
ulint innodb_os_log_written;
556
ulint innodb_os_log_fsyncs;
557
ulint innodb_os_log_pending_writes;
558
ulint innodb_os_log_pending_fsyncs;
559
ulint innodb_page_size;
560
ulint innodb_pages_created;
561
ulint innodb_pages_read;
562
ulint innodb_pages_written;
563
ulint innodb_row_lock_waits;
564
ulint innodb_row_lock_current_waits;
565
ib_int64_t innodb_row_lock_time;
566
ulint innodb_row_lock_time_avg;
567
ulint innodb_row_lock_time_max;
568
ulint innodb_rows_read;
569
ulint innodb_rows_inserted;
570
ulint innodb_rows_updated;
571
ulint innodb_rows_deleted;
574
/* The server system struct */
575
struct srv_sys_struct{
576
srv_table_t* threads; /* server thread table */
577
UT_LIST_BASE_NODE_T(que_thr_t)
578
tasks; /* task queue */
579
dict_index_t* dummy_ind1; /* dummy index for old-style
580
supremum and infimum records */
581
dict_index_t* dummy_ind2; /* dummy index for new-style
582
supremum and infimum records */
585
extern ulint srv_n_threads_active[];