1
/******************************************************
2
The server main program
6
Created 10/10/1995 Heikki Tuuri
7
*******************************************************/
14
#include "sync0sync.h"
16
#include "que0types.h"
17
#include "trx0types.h"
19
extern const char* srv_main_thread_op_info;
21
/* Prefix used by MySQL to indicate pre-5.1 table name encoding */
22
extern const char srv_mysql50_table_name_prefix[9];
24
/* When this event is set the lock timeout and InnoDB monitor
25
thread starts running */
26
extern os_event_t srv_lock_timeout_thread_event;
28
/* If the last data file is auto-extended, we add this many pages to it
30
#define SRV_AUTO_EXTEND_INCREMENT \
31
(srv_auto_extend_increment * ((1024 * 1024) / UNIV_PAGE_SIZE))
33
/* This is set to TRUE if the MySQL user has set it in MySQL */
34
extern ibool srv_lower_case_table_names;
36
/* Mutex for locking srv_monitor_file */
37
extern mutex_t srv_monitor_file_mutex;
38
/* Temporary file for innodb monitor output */
39
extern FILE* srv_monitor_file;
40
/* Mutex for locking srv_dict_tmpfile.
41
This mutex has a very high rank; threads reserving it should not
42
be holding any InnoDB latches. */
43
extern mutex_t srv_dict_tmpfile_mutex;
44
/* Temporary file for output from the data dictionary */
45
extern FILE* srv_dict_tmpfile;
46
/* Mutex for locking srv_misc_tmpfile.
47
This mutex has a very low rank; threads reserving it should not
48
acquire any further latches or sleep before releasing this one. */
49
extern mutex_t srv_misc_tmpfile_mutex;
50
/* Temporary file for miscellanous diagnostic output */
51
extern FILE* srv_misc_tmpfile;
53
/* Server parameters which are read from the initfile */
55
extern char* srv_data_home;
56
#ifdef UNIV_LOG_ARCHIVE
57
extern char* srv_arch_dir;
58
#endif /* UNIV_LOG_ARCHIVE */
60
/* store to its own file each table created by an user; data
61
dictionary tables are in the system tablespace 0 */
62
extern my_bool srv_file_per_table;
63
/* The file format to use on new *.ibd files. */
64
extern ulint srv_file_format;
65
/* Whether to check file format during startup.*/
66
extern ulint srv_check_file_format_at_startup;
67
/* Place locks to records only i.e. do not use next-key locking except
68
on duplicate key checking and foreign key checking */
69
extern ibool srv_locks_unsafe_for_binlog;
71
extern ulint srv_n_data_files;
72
extern char** srv_data_file_names;
73
extern ulint* srv_data_file_sizes;
74
extern ulint* srv_data_file_is_raw_partition;
76
extern ibool srv_auto_extend_last_data_file;
77
extern ulint srv_last_file_size_max;
78
extern ulong srv_auto_extend_increment;
80
extern ibool srv_created_new_raw;
85
extern char** srv_log_group_home_dirs;
87
extern ulint srv_n_log_groups;
88
extern ulint srv_n_log_files;
89
extern ulint srv_log_file_size;
90
extern ulint srv_log_buffer_size;
91
extern ulong srv_flush_log_at_trx_commit;
93
/* The sort order table of the MySQL latin1_swedish_ci character set
95
extern const byte* srv_latin1_ordering;
96
extern ulong srv_buf_pool_size; /* requested size in bytes */
97
extern ulong srv_buf_pool_old_size; /* previously requested size */
98
extern ulong srv_buf_pool_curr_size; /* current size in bytes */
99
extern ulint srv_mem_pool_size;
100
extern ulint srv_lock_table_size;
102
extern ulint srv_n_file_io_threads;
104
#ifdef UNIV_LOG_ARCHIVE
105
extern ibool srv_log_archive_on;
106
extern ibool srv_archive_recovery;
107
extern dulint srv_archive_recovery_limit_lsn;
108
#endif /* UNIV_LOG_ARCHIVE */
110
extern ulint srv_lock_wait_timeout;
112
extern char* srv_file_flush_method_str;
113
extern ulint srv_unix_file_flush_method;
114
extern ulint srv_win_file_flush_method;
116
extern ulint srv_max_n_open_files;
118
extern ulint srv_max_dirty_pages_pct;
120
extern ulint srv_force_recovery;
121
extern ulong srv_thread_concurrency;
122
extern ulong srv_commit_concurrency;
124
extern ulint srv_max_n_threads;
126
extern lint srv_conc_n_threads;
128
extern ulint srv_fast_shutdown; /* If this is 1, do not do a
129
purge and index buffer merge.
130
If this 2, do not even flush the
131
buffer pool to data files at the
132
shutdown: we effectively 'crash'
133
InnoDB (but lose no committed
135
extern ibool srv_innodb_status;
137
extern ibool srv_stats_on_metadata;
139
extern ibool srv_use_doublewrite_buf;
140
extern ibool srv_use_checksums;
142
extern ibool srv_set_thread_priorities;
143
extern int srv_query_thread_priority;
145
extern ulong srv_max_buf_pool_modified_pct;
146
extern ulong srv_max_purge_lag;
148
extern ulint srv_replication_delay;
149
/*-------------------------------------------*/
151
extern ulint srv_n_rows_inserted;
152
extern ulint srv_n_rows_updated;
153
extern ulint srv_n_rows_deleted;
154
extern ulint srv_n_rows_read;
156
extern ibool srv_print_innodb_monitor;
157
extern ibool srv_print_innodb_lock_monitor;
158
extern ibool srv_print_innodb_tablespace_monitor;
159
extern ibool srv_print_verbose_log;
160
extern ibool srv_print_innodb_table_monitor;
162
extern ibool srv_lock_timeout_and_monitor_active;
163
extern ibool srv_error_monitor_active;
165
extern ulong srv_n_spin_wait_rounds;
166
extern ulong srv_n_free_tickets_to_enter;
167
extern ulong srv_thread_sleep_delay;
168
extern ulint srv_spin_wait_delay;
169
extern ibool srv_priority_boost;
171
extern ulint srv_mem_pool_size;
172
extern ulint srv_lock_table_size;
175
extern ibool srv_print_thread_releases;
176
extern ibool srv_print_lock_waits;
177
extern ibool srv_print_buf_io;
178
extern ibool srv_print_log_io;
179
extern ibool srv_print_latch_waits;
180
#else /* UNIV_DEBUG */
181
# define srv_print_thread_releases FALSE
182
# define srv_print_lock_waits FALSE
183
# define srv_print_buf_io FALSE
184
# define srv_print_log_io FALSE
185
# define srv_print_latch_waits FALSE
186
#endif /* UNIV_DEBUG */
188
extern ulint srv_activity_count;
189
extern ulint srv_fatal_semaphore_wait_threshold;
190
extern ulint srv_dml_needed_delay;
192
extern mutex_t* kernel_mutex_temp;/* mutex protecting the server, trx structs,
193
query threads, and lock table: we allocate
194
it from dynamic memory to get it to the
195
same DRAM page as other hotspot semaphores */
196
#define kernel_mutex (*kernel_mutex_temp)
198
#define SRV_MAX_N_IO_THREADS 100
200
/* Array of English strings describing the current state of an
201
i/o handler thread */
202
extern const char* srv_io_thread_op_info[];
203
extern const char* srv_io_thread_function[];
205
/* the number of the log write requests done */
206
extern ulint srv_log_write_requests;
208
/* the number of physical writes to the log performed */
209
extern ulint srv_log_writes;
211
/* amount of data written to the log files in bytes */
212
extern ulint srv_os_log_written;
214
/* amount of writes being done to the log files */
215
extern ulint srv_os_log_pending_writes;
217
/* we increase this counter, when there we don't have enough space in the
218
log buffer and have to flush it */
219
extern ulint srv_log_waits;
221
/* variable that counts amount of data read in total (in bytes) */
222
extern ulint srv_data_read;
224
/* here we count the amount of data written in total (in bytes) */
225
extern ulint srv_data_written;
227
/* this variable counts the amount of times, when the doublewrite buffer
229
extern ulint srv_dblwr_writes;
231
/* here we store the number of pages that have been flushed to the
232
doublewrite buffer */
233
extern ulint srv_dblwr_pages_written;
235
/* in this variable we store the number of write requests issued */
236
extern ulint srv_buf_pool_write_requests;
238
/* here we store the number of times when we had to wait for a free page
239
in the buffer pool. It happens when the buffer pool is full and we need
240
to make a flush, in order to be able to read or create a page. */
241
extern ulint srv_buf_pool_wait_free;
243
/* variable to count the number of pages that were written from the
244
buffer pool to disk */
245
extern ulint srv_buf_pool_flushed;
247
/* variable to count the number of buffer pool reads that led to the
248
reading of a disk page */
249
extern ulint srv_buf_pool_reads;
251
/* variable to count the number of sequential read-aheads were done */
252
extern ulint srv_read_ahead_seq;
254
/* variable to count the number of random read-aheads were done */
255
extern ulint srv_read_ahead_rnd;
257
/* In this structure we store status variables to be passed to MySQL */
258
typedef struct export_var_struct export_struc;
260
extern export_struc export_vars;
262
typedef struct srv_sys_struct srv_sys_t;
264
/* The server system */
265
extern srv_sys_t* srv_sys;
267
/* Alternatives for the file flush option in Unix; see the InnoDB manual
268
about what these mean */
269
#define SRV_UNIX_FSYNC 1 /* This is the default */
270
#define SRV_UNIX_O_DSYNC 2
271
#define SRV_UNIX_LITTLESYNC 3
272
#define SRV_UNIX_NOSYNC 4
273
#define SRV_UNIX_O_DIRECT 5
275
/* Alternatives for file i/o in Windows */
276
#define SRV_WIN_IO_NORMAL 1
277
#define SRV_WIN_IO_UNBUFFERED 2 /* This is the default */
279
/* Alternatives for srv_force_recovery. Non-zero values are intended
280
to help the user get a damaged database up so that he can dump intact
281
tables and rows with SELECT INTO OUTFILE. The database must not otherwise
282
be used with these options! A bigger number below means that all precautions
283
of lower numbers are included. */
285
#define SRV_FORCE_IGNORE_CORRUPT 1 /* let the server run even if it
286
detects a corrupt page */
287
#define SRV_FORCE_NO_BACKGROUND 2 /* prevent the main thread from
288
running: if a crash would occur
289
in purge, this prevents it */
290
#define SRV_FORCE_NO_TRX_UNDO 3 /* do not run trx rollback after
292
#define SRV_FORCE_NO_IBUF_MERGE 4 /* prevent also ibuf operations:
293
if they would cause a crash, better
295
#define SRV_FORCE_NO_UNDO_LOG_SCAN 5 /* do not look at undo logs when
296
starting the database: InnoDB will
297
treat even incomplete transactions
299
#define SRV_FORCE_NO_LOG_REDO 6 /* do not do the log roll-forward
300
in connection with recovery */
302
/** Types of threads existing in the system. */
303
enum srv_thread_type {
304
SRV_COM = 1, /**< threads serving communication and queries */
305
SRV_CONSOLE, /**< thread serving console */
306
SRV_WORKER, /**< threads serving parallelized queries and
307
queries released from lock wait */
309
/* Utility threads */
310
SRV_BUFFER, /**< thread flushing dirty buffer blocks */
311
SRV_RECOVERY, /**< threads finishing a recovery */
312
SRV_INSERT, /**< thread flushing the insert buffer to disk */
314
SRV_MASTER /**< the master thread, (whose type number must
318
/*************************************************************************
319
Boots Innobase server. */
324
/* out: DB_SUCCESS or error code */
325
/*************************************************************************
326
Initializes the server. */
331
/*************************************************************************
332
Frees the OS fast mutex created in srv_boot(). */
337
/*************************************************************************
338
Initializes the synchronization primitives, memory system, and the thread
342
srv_general_init(void);
343
/*==================*/
344
/*************************************************************************
345
Gets the number of threads in the system. */
348
srv_get_n_threads(void);
349
/*===================*/
350
/*************************************************************************
351
Returns the calling thread type. */
354
srv_get_thread_type(void);
355
/*=====================*/
356
/* out: SRV_COM, ... */
357
/*************************************************************************
358
Sets the info describing an i/o thread current state. */
361
srv_set_io_thread_op_info(
362
/*======================*/
363
ulint i, /* in: the 'segment' of the i/o thread */
364
const char* str); /* in: constant char string describing the
366
/*************************************************************************
367
Releases threads of the type given from suspension in the thread table.
368
NOTE! The server mutex has to be reserved by the caller! */
373
/* out: number of threads
374
released: this may be < n if
375
not enough threads were
376
suspended at the moment */
377
enum srv_thread_type type, /* in: thread type */
378
ulint n); /* in: number of threads to release */
379
/*************************************************************************
380
The master thread controlling the server. */
385
/* out: a dummy parameter */
386
void* arg); /* in: a dummy parameter required by
388
/***********************************************************************
389
Tells the Innobase server that there has been activity in the database
390
and wakes up the master thread if it is suspended (not sleeping). Used
391
in the MySQL interface. Note that there is a small chance that the master
392
thread stays suspended (we do not protect our operation with the kernel
393
mutex, for performace reasons). */
396
srv_active_wake_master_thread(void);
397
/*===============================*/
398
/***********************************************************************
399
Wakes up the master thread if it is suspended or being suspended. */
402
srv_wake_master_thread(void);
403
/*========================*/
404
/*************************************************************************
405
Puts an OS thread to wait if there are too many concurrent threads
406
(>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
409
srv_conc_enter_innodb(
410
/*==================*/
411
trx_t* trx); /* in: transaction object associated with the
413
/*************************************************************************
414
This lets a thread enter InnoDB regardless of the number of threads inside
415
InnoDB. This must be called when a thread ends a lock wait. */
418
srv_conc_force_enter_innodb(
419
/*========================*/
420
trx_t* trx); /* in: transaction object associated with the
422
/*************************************************************************
423
This must be called when a thread exits InnoDB in a lock wait or at the
424
end of an SQL statement. */
427
srv_conc_force_exit_innodb(
428
/*=======================*/
429
trx_t* trx); /* in: transaction object associated with the
431
/*************************************************************************
432
This must be called when a thread exits InnoDB. */
435
srv_conc_exit_innodb(
436
/*=================*/
437
trx_t* trx); /* in: transaction object associated with the
439
/*******************************************************************
440
Puts a MySQL OS thread to wait for a lock to be released. If an error
441
occurs during the wait trx->error_state associated with thr is
442
!= DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
443
are possible errors. DB_DEADLOCK is returned if selective deadlock
444
resolution chose this transaction as a victim. */
447
srv_suspend_mysql_thread(
448
/*=====================*/
449
que_thr_t* thr); /* in: query thread associated with the MySQL
451
/************************************************************************
452
Releases a MySQL OS thread waiting for a lock to be released, if the
453
thread is already suspended. */
456
srv_release_mysql_thread_if_suspended(
457
/*==================================*/
458
que_thr_t* thr); /* in: query thread associated with the
460
/*************************************************************************
461
A thread which wakes up threads whose lock wait may have lasted too long.
462
This also prints the info output by various InnoDB monitors. */
465
srv_lock_timeout_and_monitor_thread(
466
/*================================*/
467
/* out: a dummy parameter */
468
void* arg); /* in: a dummy parameter required by
470
/*************************************************************************
471
A thread which prints warnings about semaphore waits which have lasted
472
too long. These can be used to track bugs which cause hangs. */
475
srv_error_monitor_thread(
476
/*=====================*/
477
/* out: a dummy parameter */
478
void* arg); /* in: a dummy parameter required by
480
/**********************************************************************
481
Outputs to a file the output of the InnoDB Monitor. */
484
srv_printf_innodb_monitor(
485
/*======================*/
486
FILE* file, /* in: output stream */
487
ulint* trx_start, /* out: file position of the start of
488
the list of active transactions */
489
ulint* trx_end); /* out: file position of the end of
490
the list of active transactions */
492
/**********************************************************************
493
Function to pass InnoDB status variables to MySQL */
496
srv_export_innodb_status(void);
497
/*=====================*/
499
/* Thread slot in the thread table */
500
typedef struct srv_slot_struct srv_slot_t;
502
/* Thread table is an array of slots */
503
typedef srv_slot_t srv_table_t;
505
/* In this structure we store status variables to be passed to MySQL */
506
struct export_var_struct{
507
ulint innodb_data_pending_reads;
508
ulint innodb_data_pending_writes;
509
ulint innodb_data_pending_fsyncs;
510
ulint innodb_data_fsyncs;
511
ulint innodb_data_read;
512
ulint innodb_data_writes;
513
ulint innodb_data_written;
514
ulint innodb_data_reads;
515
ulint innodb_buffer_pool_pages_total;
516
ulint innodb_buffer_pool_pages_data;
517
ulint innodb_buffer_pool_pages_dirty;
518
ulint innodb_buffer_pool_pages_misc;
519
ulint innodb_buffer_pool_pages_free;
520
ulint innodb_buffer_pool_pages_latched;
521
ulint innodb_buffer_pool_read_requests;
522
ulint innodb_buffer_pool_reads;
523
ulint innodb_buffer_pool_wait_free;
524
ulint innodb_buffer_pool_pages_flushed;
525
ulint innodb_buffer_pool_write_requests;
526
ulint innodb_buffer_pool_read_ahead_seq;
527
ulint innodb_buffer_pool_read_ahead_rnd;
528
ulint innodb_dblwr_pages_written;
529
ulint innodb_dblwr_writes;
530
ulint innodb_log_waits;
531
ulint innodb_log_write_requests;
532
ulint innodb_log_writes;
533
ulint innodb_os_log_written;
534
ulint innodb_os_log_fsyncs;
535
ulint innodb_os_log_pending_writes;
536
ulint innodb_os_log_pending_fsyncs;
537
ulint innodb_page_size;
538
ulint innodb_pages_created;
539
ulint innodb_pages_read;
540
ulint innodb_pages_written;
541
ulint innodb_row_lock_waits;
542
ulint innodb_row_lock_current_waits;
543
ib_int64_t innodb_row_lock_time;
544
ulint innodb_row_lock_time_avg;
545
ulint innodb_row_lock_time_max;
546
ulint innodb_rows_read;
547
ulint innodb_rows_inserted;
548
ulint innodb_rows_updated;
549
ulint innodb_rows_deleted;
552
/* The server system struct */
553
struct srv_sys_struct{
554
srv_table_t* threads; /* server thread table */
555
UT_LIST_BASE_NODE_T(que_thr_t)
556
tasks; /* task queue */
557
dict_index_t* dummy_ind1; /* dummy index for old-style
558
supremum and infimum records */
559
dict_index_t* dummy_ind2; /* dummy index for new-style
560
supremum and infimum records */
563
extern ulint srv_n_threads_active[];