~percona-dev/percona-server/5.1.57-partially-ported

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# name       : innodb_purge_thread.patch
# introduced : 11 or before
# maintainer : Yasufumi
#
#!!! notice !!!
# Any small change to this file in the main branch
# should be done or reviewed by the maintainer!
diff -ruN a/storage/innodb_plugin/handler/ha_innodb.cc b/storage/innodb_plugin/handler/ha_innodb.cc
--- a/storage/innodb_plugin/handler/ha_innodb.cc	2010-08-27 16:23:31.022060427 +0900
+++ b/storage/innodb_plugin/handler/ha_innodb.cc	2010-08-27 16:25:49.004020662 +0900
@@ -11064,6 +11064,11 @@
   "Output statistics of recovery process after it.",
   NULL, NULL, FALSE);
 
+static MYSQL_SYSVAR_ULONG(use_purge_thread, srv_use_purge_thread,
+  PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
+  "Number of purge devoted threads. #### over 1 is EXPERIMENTAL ####",
+  NULL, NULL, 0, 0, UNIV_MAX_PARALLELISM, 0);
+
 static MYSQL_SYSVAR_BOOL(overwrite_relay_log_info, innobase_overwrite_relay_log_info,
   PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_READONLY,
   "During InnoDB crash recovery on slave overwrite relay-log.info "
@@ -11575,6 +11580,7 @@
 #endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
   MYSQL_SYSVAR(read_ahead_threshold),
   MYSQL_SYSVAR(io_capacity),
+  MYSQL_SYSVAR(use_purge_thread),
   NULL
 };
 
diff -ruN a/storage/innodb_plugin/handler/innodb_patch_info.h b/storage/innodb_plugin/handler/innodb_patch_info.h
--- a/storage/innodb_plugin/handler/innodb_patch_info.h	2010-08-27 16:23:31.027058629 +0900
+++ b/storage/innodb_plugin/handler/innodb_patch_info.h	2010-08-27 16:25:49.004020662 +0900
@@ -37,5 +37,6 @@
 {"innodb_split_buf_pool_mutex","More fix of buffer_pool mutex","Spliting buf_pool_mutex and optimizing based on innodb_opt_lru_count","http://www.percona.com/docs/wiki/percona-xtradb"},
 {"innodb_stats","Additional features about InnoDB statistics/optimizer","","http://www.percona.com/docs/wiki/percona-xtradb"},
 {"innodb_recovery_patches","Bugfixes and adjustments about recovery process","","http://www.percona.com/docs/wiki/percona-xtradb"},
+{"innodb_purge_thread","Enable to use purge devoted thread","","http://www.percona.com/docs/wiki/percona-xtradb"},
 {NULL, NULL, NULL, NULL}
 };
diff -ruN a/storage/innodb_plugin/include/srv0srv.h b/storage/innodb_plugin/include/srv0srv.h
--- a/storage/innodb_plugin/include/srv0srv.h	2010-08-27 16:23:31.030059162 +0900
+++ b/storage/innodb_plugin/include/srv0srv.h	2010-08-27 16:25:49.005987844 +0900
@@ -116,6 +116,8 @@
 
 extern ibool	srv_recovery_stats;
 
+extern ulint	srv_use_purge_thread;
+
 extern ibool	srv_auto_extend_last_data_file;
 extern ulint	srv_last_file_size_max;
 extern char**	srv_log_group_home_dirs;
@@ -421,6 +423,8 @@
 	SRV_RECOVERY,	/**< threads finishing a recovery */
 	SRV_INSERT,	/**< thread flushing the insert buffer to disk */
 #endif
+	SRV_PURGE,	/* thread purging undo records */
+	SRV_PURGE_WORKER,	/* thread purging undo records */
 	SRV_MASTER	/**< the master thread, (whose type number must
 			be biggest) */
 };
@@ -494,6 +498,21 @@
 /*==============*/
 	void*	arg);	/*!< in: a dummy parameter required by
 			os_thread_create */
+/*************************************************************************
+The undo purge thread. */
+UNIV_INTERN
+os_thread_ret_t
+srv_purge_thread(
+/*=============*/
+	void*	arg);	/* in: a dummy parameter required by
+			os_thread_create */
+/*************************************************************************
+The undo purge thread. */
+UNIV_INTERN
+os_thread_ret_t
+srv_purge_worker_thread(
+/*====================*/
+	void*	arg);
 /*******************************************************************//**
 Tells the Innobase server that there has been activity in the database
 and wakes up the master thread if it is suspended (not sleeping). Used
diff -ruN a/storage/innodb_plugin/include/trx0purge.h b/storage/innodb_plugin/include/trx0purge.h
--- a/storage/innodb_plugin/include/trx0purge.h	2010-08-04 02:24:19.000000000 +0900
+++ b/storage/innodb_plugin/include/trx0purge.h	2010-08-27 16:25:49.006988119 +0900
@@ -114,6 +114,25 @@
 ulint
 trx_purge(void);
 /*===========*/
+/**********************************************************************
+This function runs a purge worker batch */
+UNIV_INTERN
+void
+trx_purge_worker(
+/*=============*/
+	ulint	worker_id);
+/**********************************************************************
+This function waits the event for worker batch */
+UNIV_INTERN
+void
+trx_purge_worker_wait(void);
+/*========================*/
+/**********************************************************************
+This function wakes the waiting worker batch */
+UNIV_INTERN
+void
+trx_purge_worker_wake(void);
+/*========================*/
 /******************************************************************//**
 Prints information of the purge system to stderr. */
 UNIV_INTERN
@@ -131,6 +150,11 @@
 					of the trx system and it never ends */
 	que_t*		query;		/*!< The query graph which will do the
 					parallelized purge operation */
+	ulint		n_worker;
+	os_event_t	worker_event;
+	sess_t**	sess_arr;
+	trx_t**		trx_arr;
+	que_t**		query_arr;
 	rw_lock_t	latch;		/*!< The latch protecting the purge view.
 					A purge operation must acquire an
 					x-latch here for the instant at which
diff -ruN a/storage/innodb_plugin/log/log0log.c b/storage/innodb_plugin/log/log0log.c
--- a/storage/innodb_plugin/log/log0log.c	2010-08-27 15:54:18.086987682 +0900
+++ b/storage/innodb_plugin/log/log0log.c	2010-08-27 16:25:49.010020843 +0900
@@ -3150,6 +3150,16 @@
 		goto loop;
 	}
 
+	/* Check that the purge threads ended */
+	if (srv_use_purge_thread
+	    && (srv_n_threads_active[SRV_PURGE] != 0
+		|| srv_n_threads_active[SRV_PURGE_WORKER] != 0)) {
+
+		mutex_exit(&kernel_mutex);
+
+		goto loop;
+	}
+
 	mutex_exit(&kernel_mutex);
 
 	mutex_enter(&(log_sys->mutex));
diff -ruN a/storage/innodb_plugin/srv/srv0srv.c b/storage/innodb_plugin/srv/srv0srv.c
--- a/storage/innodb_plugin/srv/srv0srv.c	2010-08-27 16:23:31.042058997 +0900
+++ b/storage/innodb_plugin/srv/srv0srv.c	2010-08-27 16:25:49.014058600 +0900
@@ -146,6 +146,8 @@
 
 UNIV_INTERN ibool	srv_recovery_stats = FALSE;
 
+UNIV_INTERN ulint	srv_use_purge_thread = 0;
+
 /* if TRUE, then we auto-extend the last data file */
 UNIV_INTERN ibool	srv_auto_extend_last_data_file	= FALSE;
 /* if != 0, this tells the max size auto-extending may increase the
@@ -2639,10 +2641,10 @@
 	srv_main_thread_process_no = os_proc_get_number();
 	srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
 
-	srv_table_reserve_slot(SRV_MASTER);
 
 	mutex_enter(&kernel_mutex);
 
+	srv_table_reserve_slot(SRV_MASTER);
 	srv_n_threads_active[SRV_MASTER]++;
 
 	mutex_exit(&kernel_mutex);
@@ -3100,6 +3102,7 @@
 	/* Flush logs if needed */
 	srv_sync_log_buffer_in_background();
 
+	if (!srv_use_purge_thread) {
 	/* We run a full purge every 10 seconds, even if the server
 	were active */
 	do {
@@ -3116,6 +3119,7 @@
 		srv_sync_log_buffer_in_background();
 
 	} while (n_pages_purged);
+	}
 
 	srv_main_thread_op_info = "flushing buffer pool pages";
 
@@ -3184,6 +3188,7 @@
 		os_thread_sleep(100000);
 	}
 
+	if (!srv_use_purge_thread) {
 	srv_main_thread_op_info = "purging";
 
 	/* Run a full purge */
@@ -3200,6 +3205,7 @@
 		srv_sync_log_buffer_in_background();
 
 	} while (n_pages_purged);
+	}
 
 	srv_main_thread_op_info = "reserving kernel mutex";
 
@@ -3352,3 +3358,143 @@
 
 	OS_THREAD_DUMMY_RETURN;	/* Not reached, avoid compiler warning */
 }
+
+/*************************************************************************
+A thread which is devoted to purge, for take over the master thread's
+purging */
+UNIV_INTERN
+os_thread_ret_t
+srv_purge_thread(
+/*=============*/
+	void*	arg __attribute__((unused)))
+			/* in: a dummy parameter required by os_thread_create */
+{
+	ulint	n_pages_purged;
+	ulint	n_pages_purged_sum = 1; /* dummy */
+	ulint	history_len;
+	ulint	sleep_ms= 10000; /* initial: 10 sec. */
+	ibool	can_be_last = FALSE;
+
+#ifdef UNIV_DEBUG_THREAD_CREATION
+	fprintf(stderr, "Purge thread starts, id %lu\n",
+		os_thread_pf(os_thread_get_curr_id()));
+#endif
+
+	mutex_enter(&kernel_mutex);
+	srv_table_reserve_slot(SRV_PURGE);
+	srv_n_threads_active[SRV_PURGE]++;
+	mutex_exit(&kernel_mutex);
+
+loop:
+	if (srv_shutdown_state > 0) {
+		if (srv_fast_shutdown) {
+			/* someone other should wait the end of the workers */
+			goto exit_func;
+		}
+
+		mutex_enter(&kernel_mutex);
+		if (srv_n_threads_active[SRV_PURGE_WORKER]) {
+			can_be_last = FALSE;
+		} else {
+			can_be_last = TRUE;
+		}
+		mutex_exit(&kernel_mutex);
+
+		sleep_ms = 10;
+	}
+
+	os_thread_sleep( sleep_ms * 1000 );
+
+	history_len = trx_sys->rseg_history_len;
+	if (history_len > 1000)
+		sleep_ms /= 10;
+	if (sleep_ms < 10)
+		sleep_ms = 10;
+
+	n_pages_purged_sum = 0;
+
+	do {
+		if (srv_fast_shutdown && srv_shutdown_state > 0) {
+			goto exit_func;
+		}
+		n_pages_purged = trx_purge();
+		n_pages_purged_sum += n_pages_purged;
+	} while (n_pages_purged);
+
+	if (srv_shutdown_state > 0 && can_be_last) {
+		/* the last trx_purge() is executed without workers */
+		goto exit_func;
+	}
+
+	if (n_pages_purged_sum) {
+		srv_active_wake_master_thread();
+	}
+
+	if (n_pages_purged_sum == 0)
+		sleep_ms *= 10;
+	if (sleep_ms > 10000)
+		sleep_ms = 10000;
+
+	goto loop;
+
+exit_func:
+	trx_purge_worker_wake(); /* It may not make sense. for safety only */
+
+	/* wake master thread to flush the pages */
+	srv_wake_master_thread();
+
+	mutex_enter(&kernel_mutex);
+	srv_n_threads_active[SRV_PURGE]--;
+	mutex_exit(&kernel_mutex);
+	os_thread_exit(NULL);
+
+	OS_THREAD_DUMMY_RETURN;
+}
+
+/*************************************************************************
+A thread which is devoted to purge, for take over the master thread's
+purging */
+UNIV_INTERN
+os_thread_ret_t
+srv_purge_worker_thread(
+/*====================*/
+	void*	arg)
+{
+	ulint	worker_id; /* index for array */
+
+	worker_id = *((ulint*)arg);
+
+#ifdef UNIV_DEBUG_THREAD_CREATION
+	fprintf(stderr, "Purge worker thread starts, id %lu\n",
+		os_thread_pf(os_thread_get_curr_id()));
+#endif
+	mutex_enter(&kernel_mutex);
+	srv_table_reserve_slot(SRV_PURGE_WORKER);
+	srv_n_threads_active[SRV_PURGE_WORKER]++;
+	mutex_exit(&kernel_mutex);
+
+loop:
+	/* purge worker threads only works when srv_shutdown_state==0 */
+	/* for safety and exactness. */
+	if (srv_shutdown_state > 0) {
+		goto exit_func;
+	}
+
+	trx_purge_worker_wait();
+
+	if (srv_shutdown_state > 0) {
+		goto exit_func;
+	}
+
+	trx_purge_worker(worker_id);
+
+	goto loop;
+
+exit_func:
+	mutex_enter(&kernel_mutex);
+	srv_n_threads_active[SRV_PURGE_WORKER]--;
+	mutex_exit(&kernel_mutex);
+	os_thread_exit(NULL);
+
+	OS_THREAD_DUMMY_RETURN;
+}
diff -ruN a/storage/innodb_plugin/srv/srv0start.c b/storage/innodb_plugin/srv/srv0start.c
--- a/storage/innodb_plugin/srv/srv0start.c	2010-08-27 15:59:21.760987922 +0900
+++ b/storage/innodb_plugin/srv/srv0start.c	2010-08-27 16:25:49.017058492 +0900
@@ -126,9 +126,9 @@
 static ulint		ios;
 
 /** io_handler_thread parameters for thread identification */
-static ulint		n[SRV_MAX_N_IO_THREADS + 6];
+static ulint		n[SRV_MAX_N_IO_THREADS + 6 + UNIV_MAX_PARALLELISM];
 /** io_handler_thread identifiers */
-static os_thread_id_t	thread_ids[SRV_MAX_N_IO_THREADS + 6];
+static os_thread_id_t	thread_ids[SRV_MAX_N_IO_THREADS + 6 + UNIV_MAX_PARALLELISM];
 
 /** We use this mutex to test the return value of pthread_mutex_trylock
    on successful locking. HP-UX does NOT return 0, though Linux et al do. */
@@ -1725,6 +1725,20 @@
 
 	os_thread_create(&srv_master_thread, NULL, thread_ids
 			 + (1 + SRV_MAX_N_IO_THREADS));
+
+	if (srv_use_purge_thread) {
+		ulint i;
+
+		os_thread_create(&srv_purge_thread, NULL, thread_ids
+				 + (5 + SRV_MAX_N_IO_THREADS));
+
+		for (i = 0; i < srv_use_purge_thread - 1; i++) {
+			n[6 + i + SRV_MAX_N_IO_THREADS] = i; /* using as index for arrays in purge_sys */
+			os_thread_create(&srv_purge_worker_thread,
+					 n + (6 + i + SRV_MAX_N_IO_THREADS),
+					 thread_ids + (6 + i + SRV_MAX_N_IO_THREADS));
+		}
+	}
 #ifdef UNIV_DEBUG
 	/* buf_debug_prints = TRUE; */
 #endif /* UNIV_DEBUG */
diff -ruN a/storage/innodb_plugin/trx/trx0purge.c b/storage/innodb_plugin/trx/trx0purge.c
--- a/storage/innodb_plugin/trx/trx0purge.c	2010-08-04 02:24:20.000000000 +0900
+++ b/storage/innodb_plugin/trx/trx0purge.c	2010-08-27 16:25:49.020058576 +0900
@@ -184,8 +184,9 @@
 @return	own: the query graph */
 static
 que_t*
-trx_purge_graph_build(void)
+trx_purge_graph_build(
 /*=======================*/
+	trx_t*	trx)
 {
 	mem_heap_t*	heap;
 	que_fork_t*	fork;
@@ -194,7 +195,7 @@
 
 	heap = mem_heap_create(512);
 	fork = que_fork_create(NULL, NULL, QUE_FORK_PURGE, heap);
-	fork->trx = purge_sys->trx;
+	fork->trx = trx;
 
 	thr = que_thr_create(fork, heap);
 
@@ -243,10 +244,35 @@
 
 	ut_a(trx_start_low(purge_sys->trx, ULINT_UNDEFINED));
 
-	purge_sys->query = trx_purge_graph_build();
+	purge_sys->query = trx_purge_graph_build(purge_sys->trx);
 
 	purge_sys->view = read_view_oldest_copy_or_open_new(ut_dulint_zero,
 							    purge_sys->heap);
+
+	purge_sys->n_worker = 0;
+	if (srv_use_purge_thread > 1) {
+		/* Use worker threads */
+		ulint i;
+
+		purge_sys->n_worker = srv_use_purge_thread - 1;
+
+		purge_sys->sess_arr = mem_alloc(sizeof(sess_t*) * purge_sys->n_worker);
+		purge_sys->trx_arr = mem_alloc(sizeof(trx_t*) * purge_sys->n_worker);
+		purge_sys->query_arr = mem_alloc(sizeof(que_t*) * purge_sys->n_worker);
+
+		purge_sys->worker_event = os_event_create(NULL);
+		os_event_reset(purge_sys->worker_event);
+
+		for (i = 0; i < purge_sys->n_worker; i++) {
+			purge_sys->sess_arr[i] = sess_open();
+
+			purge_sys->trx_arr[i] = purge_sys->sess_arr[i]->trx;
+			purge_sys->trx_arr[i]->is_purge = 1;
+			ut_a(trx_start_low(purge_sys->trx_arr[i], ULINT_UNDEFINED));
+
+			purge_sys->query_arr[i] = trx_purge_graph_build(purge_sys->trx_arr[i]);
+		}
+	}
 }
 
 /************************************************************************
@@ -1144,7 +1170,7 @@
 
 	/* Handle at most 20 undo log pages in one purge batch */
 
-	purge_sys->handle_limit = purge_sys->n_pages_handled + 20;
+	purge_sys->handle_limit = purge_sys->n_pages_handled + 20 * (srv_use_purge_thread + 1);
 
 	old_pages_handled = purge_sys->n_pages_handled;
 
@@ -1163,6 +1189,9 @@
 
 	mutex_exit(&kernel_mutex);
 
+	if (purge_sys->n_worker)
+		os_event_set(purge_sys->worker_event);
+
 	/*	srv_que_task_enqueue(thr2); */
 
 	if (srv_print_thread_releases) {
@@ -1172,6 +1201,9 @@
 
 	que_run_threads(thr);
 
+	if (purge_sys->n_worker)
+		os_event_reset(purge_sys->worker_event);
+
 	if (srv_print_thread_releases) {
 
 		fprintf(stderr,
@@ -1182,6 +1214,52 @@
 	return(purge_sys->n_pages_handled - old_pages_handled);
 }
 
+/**********************************************************************
+This function runs a purge worker batch */
+UNIV_INTERN
+void
+trx_purge_worker(
+/*=============*/
+	ulint	worker_id)
+{
+	que_thr_t*	thr;
+
+	mutex_enter(&kernel_mutex);
+
+	thr = que_fork_start_command(purge_sys->query_arr[worker_id]);
+
+	ut_ad(thr);
+
+	mutex_exit(&kernel_mutex);
+
+	que_run_threads(thr);
+
+	if (purge_sys->state == TRX_STOP_PURGE) { /* optimistic */
+		os_event_reset(purge_sys->worker_event);
+	}
+}
+
+/**********************************************************************
+This function waits the event for worker batch */
+UNIV_INTERN
+void
+trx_purge_worker_wait(void)
+/*=======================*/
+{
+	os_event_wait(purge_sys->worker_event);
+}
+
+/**********************************************************************
+This function wakes the waiting worker batch */
+UNIV_INTERN
+void
+trx_purge_worker_wake(void)
+/*=======================*/
+{
+	if (purge_sys->n_worker)
+		os_event_set(purge_sys->worker_event);
+}
+
 /******************************************************************//**
 Prints information of the purge system to stderr. */
 UNIV_INTERN