Skip to content

Commit fa3edbf

Browse files
committed
Increase value of thread_cache_size to 32
Added 5 minute timeout before automaticlally removing threads from thread cache. This solves a problem with jemalloc, which is slow with a small thread cache and also makes thread_cache big enough that most users doesn't have to touch it
1 parent 260dd47 commit fa3edbf

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

mysql-test/r/mysqld--help.result

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,8 @@ The following options may be given as the first argument:
10681068
Decision to use in heuristic recover process. One of:
10691069
COMMIT, ROLLBACK
10701070
--thread-cache-size=#
1071-
How many threads we should keep in a cache for reuse
1071+
How many threads we should keep in a cache for reuse.
1072+
These are freed after 5 minutes of idle time
10721073
--thread-pool-idle-timeout=#
10731074
Timeout in seconds for an idle thread in the thread
10741075
pool.Worker thread will be shut down after timeout
@@ -1426,7 +1427,7 @@ table-cache 431
14261427
table-definition-cache 400
14271428
table-open-cache 431
14281429
tc-heuristic-recover COMMIT
1429-
thread-cache-size 0
1430+
thread-cache-size 151
14301431
thread-pool-idle-timeout 60
14311432
thread-pool-max-threads 1000
14321433
thread-pool-oversubscribe 3

mysql-test/suite/sys_vars/r/thread_cache_size_basic.result

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
SET @start_global_value = @@global.thread_cache_size;
22
SELECT @start_global_value;
33
@start_global_value
4-
0
4+
256
55
select @@global.thread_cache_size;
66
@@global.thread_cache_size
7-
0
7+
256
88
select @@session.thread_cache_size;
99
ERROR HY000: Variable 'thread_cache_size' is a GLOBAL variable
1010
show global variables like 'thread_cache_size';
1111
Variable_name Value
12-
thread_cache_size 0
12+
thread_cache_size 256
1313
show session variables like 'thread_cache_size';
1414
Variable_name Value
15-
thread_cache_size 0
15+
thread_cache_size 256
1616
select * from information_schema.global_variables where variable_name='thread_cache_size';
1717
VARIABLE_NAME VARIABLE_VALUE
18-
THREAD_CACHE_SIZE 0
18+
THREAD_CACHE_SIZE 256
1919
select * from information_schema.session_variables where variable_name='thread_cache_size';
2020
VARIABLE_NAME VARIABLE_VALUE
21-
THREAD_CACHE_SIZE 0
21+
THREAD_CACHE_SIZE 256
2222
set global thread_cache_size=1;
2323
select @@global.thread_cache_size;
2424
@@global.thread_cache_size
@@ -51,4 +51,4 @@ select @@global.thread_cache_size;
5151
SET @@global.thread_cache_size = @start_global_value;
5252
SELECT @@global.thread_cache_size;
5353
@@global.thread_cache_size
54-
0
54+
256
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--max-connections=1024

sql/mysqld.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2995,6 +2995,7 @@ void unlink_thd(THD *thd)
29952995

29962996
static bool cache_thread()
29972997
{
2998+
struct timespec abstime;
29982999
DBUG_ENTER("cache_thread");
29993000

30003001
mysql_mutex_lock(&LOCK_thread_cache);
@@ -3013,8 +3014,21 @@ static bool cache_thread()
30133014
PSI_THREAD_CALL(delete_current_thread)();
30143015
#endif
30153016

3017+
set_timespec(abstime, THREAD_CACHE_TIMEOUT);
30163018
while (!abort_loop && ! wake_thread && ! kill_cached_threads)
3017-
mysql_cond_wait(&COND_thread_cache, &LOCK_thread_cache);
3019+
{
3020+
int error= mysql_cond_timedwait(&COND_thread_cache, &LOCK_thread_cache,
3021+
&abstime);
3022+
if (error == ETIMEDOUT || error == ETIME)
3023+
{
3024+
/*
3025+
If timeout, end thread.
3026+
If a new thread is requested (wake_thread is set), we will handle
3027+
the call, even if we got a timeout (as we are already awake and free)
3028+
*/
3029+
break;
3030+
}
3031+
}
30183032
cached_thread_count--;
30193033
if (kill_cached_threads)
30203034
mysql_cond_signal(&COND_flush_thread_cache);

sql/sql_const.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@
235235
that does not respond to "initial server greeting" timely
236236
*/
237237
#define CONNECT_TIMEOUT 10
238+
/* Wait 5 minutes before removing thread from thread cache */
239+
#define THREAD_CACHE_TIMEOUT 5*60
238240

239241
/* The following can also be changed from the command line */
240242
#define DEFAULT_CONCURRENCY 10

sql/sys_vars.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,9 +3194,9 @@ static Sys_var_ulong Sys_table_cache_size(
31943194

31953195
static Sys_var_ulong Sys_thread_cache_size(
31963196
"thread_cache_size",
3197-
"How many threads we should keep in a cache for reuse",
3197+
"How many threads we should keep in a cache for reuse. These are freed after 5 minutes of idle time",
31983198
GLOBAL_VAR(thread_cache_size), CMD_LINE(REQUIRED_ARG),
3199-
VALID_RANGE(0, 16384), DEFAULT(0), BLOCK_SIZE(1));
3199+
VALID_RANGE(0, 16384), DEFAULT(256), BLOCK_SIZE(1));
32003200

32013201
#ifdef HAVE_POOL_OF_THREADS
32023202
static bool fix_tp_max_threads(sys_var *, THD *, enum_var_type)

0 commit comments

Comments
 (0)