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[256];
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 char* srv_file_flush_method_str;
111
extern ulint srv_unix_file_flush_method;
112
extern ulint srv_win_file_flush_method;
114
extern ulint srv_max_n_open_files;
116
extern ulint srv_max_dirty_pages_pct;
118
extern ulint srv_force_recovery;
119
extern ulong srv_thread_concurrency;
120
extern ulong srv_commit_concurrency;
122
extern ulint srv_max_n_threads;
124
extern lint srv_conc_n_threads;
126
extern ulint srv_fast_shutdown; /* If this is 1, do not do a
127
purge and index buffer merge.
128
If this 2, do not even flush the
129
buffer pool to data files at the
130
shutdown: we effectively 'crash'
131
InnoDB (but lose no committed
133
extern ibool srv_innodb_status;
135
extern uint64_t srv_stats_sample_pages;
137
extern ibool srv_use_doublewrite_buf;
138
extern ibool srv_use_checksums;
140
extern ibool srv_set_thread_priorities;
141
extern int srv_query_thread_priority;
143
extern ulong srv_max_buf_pool_modified_pct;
144
extern ulong srv_max_purge_lag;
146
extern ulong srv_replication_delay;
147
/*-------------------------------------------*/
149
extern ulint srv_n_rows_inserted;
150
extern ulint srv_n_rows_updated;
151
extern ulint srv_n_rows_deleted;
152
extern ulint srv_n_rows_read;
154
extern ibool srv_print_innodb_monitor;
155
extern ibool srv_print_innodb_lock_monitor;
156
extern ibool srv_print_innodb_tablespace_monitor;
157
extern ibool srv_print_verbose_log;
158
extern ibool srv_print_innodb_table_monitor;
160
extern ibool srv_lock_timeout_and_monitor_active;
161
extern ibool srv_error_monitor_active;
163
extern ulong srv_n_spin_wait_rounds;
164
extern ulong srv_n_free_tickets_to_enter;
165
extern ulong srv_thread_sleep_delay;
166
extern ulint srv_spin_wait_delay;
167
extern ibool srv_priority_boost;
171
extern ibool srv_print_thread_releases;
172
extern ibool srv_print_lock_waits;
173
extern ibool srv_print_buf_io;
174
extern ibool srv_print_log_io;
175
extern ibool srv_print_latch_waits;
176
#else /* UNIV_DEBUG */
177
# define srv_print_thread_releases FALSE
178
# define srv_print_lock_waits FALSE
179
# define srv_print_buf_io FALSE
180
# define srv_print_log_io FALSE
181
# define srv_print_latch_waits FALSE
182
#endif /* UNIV_DEBUG */
184
extern ulint srv_activity_count;
185
extern ulint srv_fatal_semaphore_wait_threshold;
186
extern ulint srv_dml_needed_delay;
188
extern mutex_t* kernel_mutex_temp;/* mutex protecting the server, trx structs,
189
query threads, and lock table: we allocate
190
it from dynamic memory to get it to the
191
same DRAM page as other hotspot semaphores */
192
#define kernel_mutex (*kernel_mutex_temp)
194
#define SRV_MAX_N_IO_THREADS 100
196
/* Array of English strings describing the current state of an
197
i/o handler thread */
198
extern const char* srv_io_thread_op_info[];
199
extern const char* srv_io_thread_function[];
201
/* the number of the log write requests done */
202
extern ulint srv_log_write_requests;
204
/* the number of physical writes to the log performed */
205
extern ulint srv_log_writes;
207
/* amount of data written to the log files in bytes */
208
extern ulint srv_os_log_written;
210
/* amount of writes being done to the log files */
211
extern ulint srv_os_log_pending_writes;
213
/* we increase this counter, when there we don't have enough space in the
214
log buffer and have to flush it */
215
extern ulint srv_log_waits;
217
/* variable that counts amount of data read in total (in bytes) */
218
extern ulint srv_data_read;
220
/* here we count the amount of data written in total (in bytes) */
221
extern ulint srv_data_written;
223
/* this variable counts the amount of times, when the doublewrite buffer
225
extern ulint srv_dblwr_writes;
227
/* here we store the number of pages that have been flushed to the
228
doublewrite buffer */
229
extern ulint srv_dblwr_pages_written;
231
/* in this variable we store the number of write requests issued */
232
extern ulint srv_buf_pool_write_requests;
234
/* here we store the number of times when we had to wait for a free page
235
in the buffer pool. It happens when the buffer pool is full and we need
236
to make a flush, in order to be able to read or create a page. */
237
extern ulint srv_buf_pool_wait_free;
239
/* variable to count the number of pages that were written from the
240
buffer pool to disk */
241
extern ulint srv_buf_pool_flushed;
243
/* variable to count the number of buffer pool reads that led to the
244
reading of a disk page */
245
extern ulint srv_buf_pool_reads;
247
/* variable to count the number of sequential read-aheads were done */
248
extern ulint srv_read_ahead_seq;
250
/* variable to count the number of random read-aheads were done */
251
extern ulint srv_read_ahead_rnd;
253
/* In this structure we store status variables to be passed to MySQL */
254
typedef struct export_var_struct export_struc;
256
extern export_struc export_vars;
258
typedef struct srv_sys_struct srv_sys_t;
260
/* The server system */
261
extern srv_sys_t* srv_sys;
263
/* Alternatives for the file flush option in Unix; see the InnoDB manual
264
about what these mean */
265
#define SRV_UNIX_FSYNC 1 /* This is the default */
266
#define SRV_UNIX_O_DSYNC 2
267
#define SRV_UNIX_LITTLESYNC 3
268
#define SRV_UNIX_NOSYNC 4
269
#define SRV_UNIX_O_DIRECT 5
271
/* Alternatives for file i/o in Windows */
272
#define SRV_WIN_IO_NORMAL 1
273
#define SRV_WIN_IO_UNBUFFERED 2 /* This is the default */
275
/* Alternatives for srv_force_recovery. Non-zero values are intended
276
to help the user get a damaged database up so that he can dump intact
277
tables and rows with SELECT INTO OUTFILE. The database must not otherwise
278
be used with these options! A bigger number below means that all precautions
279
of lower numbers are included. */
281
#define SRV_FORCE_IGNORE_CORRUPT 1 /* let the server run even if it
282
detects a corrupt page */
283
#define SRV_FORCE_NO_BACKGROUND 2 /* prevent the main thread from
284
running: if a crash would occur
285
in purge, this prevents it */
286
#define SRV_FORCE_NO_TRX_UNDO 3 /* do not run trx rollback after
288
#define SRV_FORCE_NO_IBUF_MERGE 4 /* prevent also ibuf operations:
289
if they would cause a crash, better
291
#define SRV_FORCE_NO_UNDO_LOG_SCAN 5 /* do not look at undo logs when
292
starting the database: InnoDB will
293
treat even incomplete transactions
295
#define SRV_FORCE_NO_LOG_REDO 6 /* do not do the log roll-forward
296
in connection with recovery */
298
/** Types of threads existing in the system. */
299
enum srv_thread_type {
300
SRV_COM = 1, /**< threads serving communication and queries */
301
SRV_CONSOLE, /**< thread serving console */
302
SRV_WORKER, /**< threads serving parallelized queries and
303
queries released from lock wait */
305
/* Utility threads */
306
SRV_BUFFER, /**< thread flushing dirty buffer blocks */
307
SRV_RECOVERY, /**< threads finishing a recovery */
308
SRV_INSERT, /**< thread flushing the insert buffer to disk */
310
SRV_MASTER /**< the master thread, (whose type number must
314
/*************************************************************************
315
Boots Innobase server. */
320
/* out: DB_SUCCESS or error code */
321
/*************************************************************************
322
Initializes the server. */
327
/*************************************************************************
328
Frees the OS fast mutex created in srv_boot(). */
333
/*************************************************************************
334
Initializes the synchronization primitives, memory system, and the thread
338
srv_general_init(void);
339
/*==================*/
340
/*************************************************************************
341
Gets the number of threads in the system. */
344
srv_get_n_threads(void);
345
/*===================*/
346
/*************************************************************************
347
Returns the calling thread type. */
350
srv_get_thread_type(void);
351
/*=====================*/
352
/* out: SRV_COM, ... */
353
/*************************************************************************
354
Sets the info describing an i/o thread current state. */
357
srv_set_io_thread_op_info(
358
/*======================*/
359
ulint i, /* in: the 'segment' of the i/o thread */
360
const char* str); /* in: constant char string describing the
362
/*************************************************************************
363
Releases threads of the type given from suspension in the thread table.
364
NOTE! The server mutex has to be reserved by the caller! */
369
/* out: number of threads
370
released: this may be < n if
371
not enough threads were
372
suspended at the moment */
373
enum srv_thread_type type, /* in: thread type */
374
ulint n); /* in: number of threads to release */
375
/*************************************************************************
376
The master thread controlling the server. */
381
/* out: a dummy parameter */
382
void* arg); /* in: a dummy parameter required by
384
/***********************************************************************
385
Tells the Innobase server that there has been activity in the database
386
and wakes up the master thread if it is suspended (not sleeping). Used
387
in the MySQL interface. Note that there is a small chance that the master
388
thread stays suspended (we do not protect our operation with the kernel
389
mutex, for performace reasons). */
392
srv_active_wake_master_thread(void);
393
/*===============================*/
394
/***********************************************************************
395
Wakes up the master thread if it is suspended or being suspended. */
398
srv_wake_master_thread(void);
399
/*========================*/
400
/*************************************************************************
401
Puts an OS thread to wait if there are too many concurrent threads
402
(>= srv_thread_concurrency) inside InnoDB. The threads wait in a FIFO queue. */
405
srv_conc_enter_innodb(
406
/*==================*/
407
trx_t* trx); /* in: transaction object associated with the
409
/*************************************************************************
410
This lets a thread enter InnoDB regardless of the number of threads inside
411
InnoDB. This must be called when a thread ends a lock wait. */
414
srv_conc_force_enter_innodb(
415
/*========================*/
416
trx_t* trx); /* in: transaction object associated with the
418
/*************************************************************************
419
This must be called when a thread exits InnoDB in a lock wait or at the
420
end of an SQL statement. */
423
srv_conc_force_exit_innodb(
424
/*=======================*/
425
trx_t* trx); /* in: transaction object associated with the
427
/*************************************************************************
428
This must be called when a thread exits InnoDB. */
431
srv_conc_exit_innodb(
432
/*=================*/
433
trx_t* trx); /* in: transaction object associated with the
435
/*******************************************************************
436
Puts a MySQL OS thread to wait for a lock to be released. If an error
437
occurs during the wait trx->error_state associated with thr is
438
!= DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
439
are possible errors. DB_DEADLOCK is returned if selective deadlock
440
resolution chose this transaction as a victim. */
443
srv_suspend_mysql_thread(
444
/*=====================*/
445
que_thr_t* thr); /* in: query thread associated with the MySQL
447
/************************************************************************
448
Releases a MySQL OS thread waiting for a lock to be released, if the
449
thread is already suspended. */
452
srv_release_mysql_thread_if_suspended(
453
/*==================================*/
454
que_thr_t* thr); /* in: query thread associated with the
456
/*************************************************************************
457
A thread which wakes up threads whose lock wait may have lasted too long.
458
This also prints the info output by various InnoDB monitors. */
461
srv_lock_timeout_and_monitor_thread(
462
/*================================*/
463
/* out: a dummy parameter */
464
void* arg); /* in: a dummy parameter required by
466
/*************************************************************************
467
A thread which prints warnings about semaphore waits which have lasted
468
too long. These can be used to track bugs which cause hangs. */
471
srv_error_monitor_thread(
472
/*=====================*/
473
/* out: a dummy parameter */
474
void* arg); /* in: a dummy parameter required by
476
/**********************************************************************
477
Outputs to a file the output of the InnoDB Monitor. */
480
srv_printf_innodb_monitor(
481
/*======================*/
482
FILE* file, /* in: output stream */
483
ulint* trx_start, /* out: file position of the start of
484
the list of active transactions */
485
ulint* trx_end); /* out: file position of the end of
486
the list of active transactions */
488
/**********************************************************************
489
Function to pass InnoDB status variables to MySQL */
492
srv_export_innodb_status(void);
493
/*=====================*/
495
/* Thread slot in the thread table */
496
typedef struct srv_slot_struct srv_slot_t;
498
/* Thread table is an array of slots */
499
typedef srv_slot_t srv_table_t;
501
/* In this structure we store status variables to be passed to MySQL */
502
struct export_var_struct{
503
ulint innodb_data_pending_reads;
504
ulint innodb_data_pending_writes;
505
ulint innodb_data_pending_fsyncs;
506
ulint innodb_data_fsyncs;
507
ulint innodb_data_read;
508
ulint innodb_data_writes;
509
ulint innodb_data_written;
510
ulint innodb_data_reads;
511
ulint innodb_buffer_pool_pages_total;
512
ulint innodb_buffer_pool_pages_data;
513
ulint innodb_buffer_pool_pages_dirty;
514
ulint innodb_buffer_pool_pages_misc;
515
ulint innodb_buffer_pool_pages_free;
517
ulint innodb_buffer_pool_pages_latched;
518
#endif /* UNIV_DEBUG */
519
ulint innodb_buffer_pool_read_requests;
520
ulint innodb_buffer_pool_reads;
521
ulint innodb_buffer_pool_wait_free;
522
ulint innodb_buffer_pool_pages_flushed;
523
ulint innodb_buffer_pool_write_requests;
524
ulint innodb_buffer_pool_read_ahead_seq;
525
ulint innodb_buffer_pool_read_ahead_rnd;
526
ulint innodb_dblwr_pages_written;
527
ulint innodb_dblwr_writes;
528
ulint innodb_log_waits;
529
ulint innodb_log_write_requests;
530
ulint innodb_log_writes;
531
ulint innodb_os_log_written;
532
ulint innodb_os_log_fsyncs;
533
ulint innodb_os_log_pending_writes;
534
ulint innodb_os_log_pending_fsyncs;
535
ulint innodb_page_size;
536
ulint innodb_pages_created;
537
ulint innodb_pages_read;
538
ulint innodb_pages_written;
539
ulint innodb_row_lock_waits;
540
ulint innodb_row_lock_current_waits;
541
ib_int64_t innodb_row_lock_time;
542
ulint innodb_row_lock_time_avg;
543
ulint innodb_row_lock_time_max;
544
ulint innodb_rows_read;
545
ulint innodb_rows_inserted;
546
ulint innodb_rows_updated;
547
ulint innodb_rows_deleted;
550
/* The server system struct */
551
struct srv_sys_struct{
552
srv_table_t* threads; /* server thread table */
553
UT_LIST_BASE_NODE_T(que_thr_t)
554
tasks; /* task queue */
555
dict_index_t* dummy_ind1; /* dummy index for old-style
556
supremum and infimum records */
557
dict_index_t* dummy_ind2; /* dummy index for new-style
558
supremum and infimum records */
561
extern ulint srv_n_threads_active[];