diff -uprN ./mysql-5.7.17/sql/mysqld.cc ./mysql-5.7.17.new/sql/mysqld.cc --- ./mysql-5.7.17/sql/mysqld.cc 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/sql/mysqld.cc 2017-09-01 11:02:17.000000000 +0800 @@ -483,6 +483,9 @@ bool host_cache_size_specified= false; bool table_definition_cache_specified= false; ulong locked_account_connection_count= 0; +/* TDSQL: reduce CPU occupancy */ +ulong idle_sleep_us; /*!< time to sleep(us) when idle */ + /** Limit of the total number of prepared statements in the server. Is necessary to protect the server against out-of-memory attacks. diff -uprN ./mysql-5.7.17/sql/mysqld.h ./mysql-5.7.17.new/sql/mysqld.h --- ./mysql-5.7.17/sql/mysqld.h 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/sql/mysqld.h 2017-09-01 11:01:20.000000000 +0800 @@ -974,4 +974,7 @@ static inline THD *_current_thd(void) #define ER(X) ER_THD(current_thd,X) +/* TDSQL: reduce CPU occupancy */ +extern ulong idle_sleep_us; /*!< time to sleep(us) when idle */ + #endif /* MYSQLD_INCLUDED */ diff -uprN ./mysql-5.7.17/sql/sys_vars.cc ./mysql-5.7.17.new/sql/sys_vars.cc --- ./mysql-5.7.17/sql/sys_vars.cc 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/sql/sys_vars.cc 2017-09-01 11:05:01.000000000 +0800 @@ -2402,6 +2402,14 @@ static Sys_var_uint Sys_pseudo_thread_id BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(check_has_super)); +/* TDSQL: reduce CPU occupancy */ +static Sys_var_ulong Sys_global_sleep_us( + "idle_sleep_us", "used in buf/buf0flu.cc", + GLOBAL_VAR(idle_sleep_us), CMD_LINE(OPT_ARG), + VALID_RANGE(0, 1000000), DEFAULT(10000), BLOCK_SIZE(1), + NO_MUTEX_GUARD, NOT_IN_BINLOG, + ON_CHECK(0), ON_UPDATE(0)); + static bool fix_max_join_size(sys_var *self, THD *thd, enum_var_type type) { SV *sv= type == OPT_GLOBAL ? &global_system_variables : &thd->variables; diff -uprN ./mysql-5.7.17/storage/innobase/buf/buf0buf.cc ./mysql-5.7.17.new/storage/innobase/buf/buf0buf.cc --- ./mysql-5.7.17/storage/innobase/buf/buf0buf.cc 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/storage/innobase/buf/buf0buf.cc 2017-09-01 11:27:04.000000000 +0800 @@ -321,6 +321,9 @@ static const ulint BUF_READ_AHEAD_PORTIO /** The buffer pools of the database */ buf_pool_t* buf_pool_ptr; +/* TDSQL: reduce CPU occupancy */ +MY_BITMAP buf_pend_reads; + /** true when resizing buffer pool is in the critical path. */ volatile bool buf_pool_resizing; @@ -1949,6 +1952,10 @@ buf_pool_init( buf_pool_ptr = (buf_pool_t*) ut_zalloc_nokey( n_instances * sizeof *buf_pool_ptr); + /* TDSQL: reduce CPU occupancy */ + bitmap_init(&buf_pend_reads, 0, n_instances, 0); + bitmap_clear_all(&buf_pend_reads); + buf_chunk_map_reg = UT_NEW_NOKEY(buf_pool_chunk_map_t()); for (i = 0; i < n_instances; i++) { @@ -1990,6 +1997,9 @@ buf_pool_free( ut_free(buf_pool_ptr); buf_pool_ptr = NULL; + + /* TDSQL: reduce CPU occupancy */ + bitmap_free(&buf_pend_reads); } /** Reallocate a control block. @@ -5116,6 +5126,9 @@ buf_page_init_for_read( void* data; buf_pool_t* buf_pool = buf_pool_get(page_id); + /* TDSQL: reduce CPU occupancy */ + ulint buffer_pool_index = buf_pool_index(buf_pool); + ut_ad(buf_pool); *err = DB_SUCCESS; @@ -5316,6 +5329,8 @@ buf_page_init_for_read( mutex_exit(&buf_pool->zip_mutex); } + /* TDSQL: reduce CPU occupancy */ + bitmap_set_bit(&buf_pend_reads, buffer_pool_index); buf_pool->n_pend_reads++; func_exit: buf_pool_mutex_exit(buf_pool); @@ -5624,6 +5639,12 @@ buf_mark_space_corrupt( ut_ad(buf_pool->n_pend_reads > 0); buf_pool->n_pend_reads--; +/* TDSQL: reduce CPU occupancy */ + if (buf_pool->n_pend_reads == 0) { + ulint buffer_pool_index = buf_pool_index(buf_pool); + bitmap_clear_bit(&buf_pend_reads, buffer_pool_index); + } + buf_pool_mutex_exit(buf_pool); return(ret); @@ -5854,6 +5875,13 @@ corrupt: ut_ad(buf_pool->n_pend_reads > 0); buf_pool->n_pend_reads--; + + /* TDSQL: reduce CPU occupancy */ + if (buf_pool->n_pend_reads == 0) { + ulint buffer_pool_index = buf_pool_index(buf_pool); + bitmap_clear_bit(&buf_pend_reads, buffer_pool_index); + } + buf_pool->stat.n_pages_read++; if (uncompressed) { @@ -6498,19 +6526,17 @@ buf_get_latched_pages_number(void) #endif /* UNIV_DEBUG */ /*********************************************************************//** -Returns the number of pending buf pool read ios. -@return number of pending read I/O operations */ +Returns whether there exists buf pool whose pending read ios is not 0. +@return whether any of buf_pend_reads.bitmap is not 0 */ ulint buf_get_n_pending_read_ios(void) /*============================*/ { - ulint pend_ios = 0; - - for (ulint i = 0; i < srv_buf_pool_instances; i++) { - pend_ios += buf_pool_from_array(i)->n_pend_reads; + for (ulint i = 0; i < bitmap_buffer_size(buf_pend_reads.n_bits) / sizeof(my_bitmap_map); i++) { + if (buf_pend_reads.bitmap[i]) + return 1; } - - return(pend_ios); + return 0; } /*********************************************************************//** diff -uprN ./mysql-5.7.17/storage/innobase/buf/buf0flu.cc ./mysql-5.7.17.new/storage/innobase/buf/buf0flu.cc --- ./mysql-5.7.17/storage/innobase/buf/buf0flu.cc 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/storage/innobase/buf/buf0flu.cc 2017-09-01 11:28:09.000000000 +0800 @@ -3357,6 +3357,9 @@ DECLARE_THREAD(buf_flush_page_cleaner_co } else { /* no activity, but woken up by event */ n_flushed = 0; + /* TDSQL: reduce CPU occupancy */ + if (idle_sleep_us != 0) + os_event_wait_time_low(buf_flush_event, idle_sleep_us, sig_count); } ut_d(buf_flush_page_cleaner_disabled_loop()); diff -uprN ./mysql-5.7.17/storage/innobase/buf/buf0rea.cc ./mysql-5.7.17.new/storage/innobase/buf/buf0rea.cc --- ./mysql-5.7.17/storage/innobase/buf/buf0rea.cc 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/storage/innobase/buf/buf0rea.cc 2017-09-01 11:41:09.000000000 +0800 @@ -86,6 +86,12 @@ buf_read_page_handle_error( ut_ad(buf_pool->n_pend_reads > 0); buf_pool->n_pend_reads--; + /* TDSQL: reduce CPU occupancy */ + if (buf_pool->n_pend_reads == 0) { + ulint buffer_pool_index = buf_pool_index(buf_pool); + bitmap_clear_bit(&buf_pend_reads, buffer_pool_index); + } + buf_pool_mutex_exit(buf_pool); } diff -uprN ./mysql-5.7.17/storage/innobase/include/buf0buf.h ./mysql-5.7.17.new/storage/innobase/include/buf0buf.h --- ./mysql-5.7.17/storage/innobase/include/buf0buf.h 2016-11-28 21:32:52.000000000 +0800 +++ ./mysql-5.7.17.new/storage/innobase/include/buf0buf.h 2017-09-01 11:08:25.000000000 +0800 @@ -41,6 +41,8 @@ Created 11/5/1995 Heikki Tuuri #include "srv0srv.h" #include +#include "my_bitmap.h" + // Forward declaration struct fil_addr_t;