~tsarev/percona-server/5.5_percona_server_variables_fix_bug_785566

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
# name       : innodb_opt_lru_count.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/innobase/buf/buf0buddy.c b/storage/innobase/buf/buf0buddy.c
--- a/storage/innobase/buf/buf0buddy.c	2010-11-03 07:01:13.000000000 +0900
+++ b/storage/innobase/buf/buf0buddy.c	2010-12-03 15:20:49.593024343 +0900
@@ -137,7 +137,7 @@
 			      ut_ad(buf_page_get_state(ut_list_node_313)
 				    == BUF_BLOCK_ZIP_FREE)));
 #endif /* !UNIV_DEBUG_VALGRIND */
-	bpage = UT_LIST_GET_FIRST(buf_pool->zip_free[i]);
+	bpage = UT_LIST_GET_LAST(buf_pool->zip_free[i]);
 
 	if (bpage) {
 		UNIV_MEM_VALID(bpage, BUF_BUDDY_LOW << i);
diff -ruN a/storage/innobase/buf/buf0buf.c b/storage/innobase/buf/buf0buf.c
--- a/storage/innobase/buf/buf0buf.c	2010-12-03 15:18:48.866986963 +0900
+++ b/storage/innobase/buf/buf0buf.c	2010-12-03 15:20:49.595987311 +0900
@@ -881,9 +881,9 @@
 	block->page.in_zip_hash = FALSE;
 	block->page.in_flush_list = FALSE;
 	block->page.in_free_list = FALSE;
-	block->page.in_LRU_list = FALSE;
 	block->in_unzip_LRU_list = FALSE;
 #endif /* UNIV_DEBUG */
+	block->page.in_LRU_list = FALSE;
 #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
 	block->n_pointers = 0;
 #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
@@ -1494,7 +1494,7 @@
 
 	memcpy(dpage, bpage, sizeof *dpage);
 
-	ut_d(bpage->in_LRU_list = FALSE);
+	bpage->in_LRU_list = FALSE;
 	ut_d(bpage->in_page_hash = FALSE);
 
 	/* relocate buf_pool->LRU */
@@ -3747,8 +3747,8 @@
 		bpage->in_zip_hash = FALSE;
 		bpage->in_flush_list = FALSE;
 		bpage->in_free_list = FALSE;
-		bpage->in_LRU_list = FALSE;
 #endif /* UNIV_DEBUG */
+		bpage->in_LRU_list = FALSE;
 
 		ut_d(bpage->in_page_hash = TRUE);
 
@@ -3911,7 +3911,7 @@
 	ibuf_merge_or_delete_for_page(NULL, space, offset, zip_size, TRUE);
 
 	/* Flush pages from the end of the LRU list if necessary */
-	buf_flush_free_margin(buf_pool);
+	buf_flush_free_margin(buf_pool, FALSE);
 
 	frame = block->frame;
 
diff -ruN a/storage/innobase/buf/buf0flu.c b/storage/innobase/buf/buf0flu.c
--- a/storage/innobase/buf/buf0flu.c	2010-12-03 15:18:48.868953442 +0900
+++ b/storage/innobase/buf/buf0flu.c	2010-12-03 15:20:49.599986956 +0900
@@ -431,19 +431,21 @@
 				buf_page_in_file(bpage) and in the LRU list */
 {
 #ifdef UNIV_DEBUG
-	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
-	ut_ad(buf_pool_mutex_own(buf_pool));
+	//buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
+	//ut_ad(buf_pool_mutex_own(buf_pool));
 #endif
-	ut_ad(mutex_own(buf_page_get_mutex(bpage)));
-	ut_ad(bpage->in_LRU_list);
+	//ut_ad(mutex_own(buf_page_get_mutex(bpage)));
+	//ut_ad(bpage->in_LRU_list);
 
-	if (UNIV_LIKELY(buf_page_in_file(bpage))) {
+	if (UNIV_LIKELY(bpage->in_LRU_list && buf_page_in_file(bpage))) {
 
 		return(bpage->oldest_modification == 0
 		       && buf_page_get_io_fix(bpage) == BUF_IO_NONE
 		       && bpage->buf_fix_count == 0);
 	}
 
+	/* permited not to own LRU_mutex..  */
+/*
 	ut_print_timestamp(stderr);
 	fprintf(stderr,
 		"  InnoDB: Error: buffer block state %lu"
@@ -451,6 +453,7 @@
 		(ulong) buf_page_get_state(bpage));
 	ut_print_buf(stderr, bpage, sizeof(buf_page_t));
 	putc('\n', stderr);
+*/
 
 	return(FALSE);
 }
@@ -1983,8 +1986,14 @@
 	buf_page_t*	bpage;
 	ulint		n_replaceable;
 	ulint		distance	= 0;
+	ibool		have_LRU_mutex = FALSE;
 
-	buf_pool_mutex_enter(buf_pool);
+	if(UT_LIST_GET_LEN(buf_pool->unzip_LRU))
+		have_LRU_mutex = TRUE;
+retry:
+	//buf_pool_mutex_enter(buf_pool);
+	if (have_LRU_mutex)
+		buf_pool_mutex_enter(buf_pool);
 
 	n_replaceable = UT_LIST_GET_LEN(buf_pool->free);
 
@@ -1995,7 +2004,13 @@
 		   + BUF_FLUSH_EXTRA_MARGIN(buf_pool))
 	       && (distance < BUF_LRU_FREE_SEARCH_LEN(buf_pool))) {
 
-		mutex_t* block_mutex = buf_page_get_mutex(bpage);
+		mutex_t* block_mutex;
+		if (!bpage->in_LRU_list) {
+			/* reatart. but it is very optimistic */
+			bpage = UT_LIST_GET_LAST(buf_pool->LRU);
+			continue;
+		}
+		block_mutex = buf_page_get_mutex(bpage);
 
 		mutex_enter(block_mutex);
 
@@ -2010,11 +2025,18 @@
 		bpage = UT_LIST_GET_PREV(LRU, bpage);
 	}
 
-	buf_pool_mutex_exit(buf_pool);
+	//buf_pool_mutex_exit(buf_pool);
+	if (have_LRU_mutex)
+		buf_pool_mutex_exit(buf_pool);
 
 	if (n_replaceable >= BUF_FLUSH_FREE_BLOCK_MARGIN(buf_pool)) {
 
 		return(0);
+	} else if (!have_LRU_mutex) {
+		/* confirm it again with LRU_mutex for exactness */
+		have_LRU_mutex = TRUE;
+		distance = 0;
+		goto retry;
 	}
 
 	return(BUF_FLUSH_FREE_BLOCK_MARGIN(buf_pool)
@@ -2032,7 +2054,8 @@
 void
 buf_flush_free_margin(
 /*==================*/
-	buf_pool_t*	buf_pool)		/*!< in: Buffer pool instance */
+	buf_pool_t*	buf_pool,		/*!< in: Buffer pool instance */
+	ibool		wait)
 {
 	ulint	n_to_flush;
 
@@ -2043,7 +2066,7 @@
 
 		n_flushed = buf_flush_LRU(buf_pool, n_to_flush);
 
-		if (n_flushed == ULINT_UNDEFINED) {
+		if (wait && n_flushed == ULINT_UNDEFINED) {
 			/* There was an LRU type flush batch already running;
 			let us wait for it to end */
 
@@ -2056,8 +2079,9 @@
 Flushes pages from the end of all the LRU lists. */
 UNIV_INTERN
 void
-buf_flush_free_margins(void)
+buf_flush_free_margins(
 /*========================*/
+	ibool	wait)
 {
 	ulint	i;
 
@@ -2066,7 +2090,7 @@
 
 		buf_pool = buf_pool_from_array(i);
 
-		buf_flush_free_margin(buf_pool);
+		buf_flush_free_margin(buf_pool, wait);
 	}
 }
 
diff -ruN a/storage/innobase/buf/buf0lru.c b/storage/innobase/buf/buf0lru.c
--- a/storage/innobase/buf/buf0lru.c	2010-11-03 07:01:13.000000000 +0900
+++ b/storage/innobase/buf/buf0lru.c	2010-12-03 15:20:49.602952786 +0900
@@ -996,7 +996,7 @@
 
 	/* No free block was found: try to flush the LRU list */
 
-	buf_flush_free_margin(buf_pool);
+	buf_flush_free_margin(buf_pool, TRUE);
 	++srv_buf_pool_wait_free;
 
 	os_aio_simulated_wake_handler_threads();
@@ -1193,7 +1193,7 @@
 
 	/* Remove the block from the LRU list */
 	UT_LIST_REMOVE(LRU, buf_pool->LRU, bpage);
-	ut_d(bpage->in_LRU_list = FALSE);
+	bpage->in_LRU_list = FALSE;
 
 	buf_unzip_LRU_remove_block_if_needed(bpage);
 
@@ -1272,7 +1272,7 @@
 
 	ut_ad(!bpage->in_LRU_list);
 	UT_LIST_ADD_LAST(LRU, buf_pool->LRU, bpage);
-	ut_d(bpage->in_LRU_list = TRUE);
+	bpage->in_LRU_list = TRUE;
 
 	if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
 
@@ -1342,7 +1342,7 @@
 		buf_pool->LRU_old_len++;
 	}
 
-	ut_d(bpage->in_LRU_list = TRUE);
+	bpage->in_LRU_list = TRUE;
 
 	if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
 
@@ -1593,7 +1593,7 @@
 				buf_page_set_old(b, buf_page_is_old(b));
 #endif /* UNIV_LRU_DEBUG */
 			} else {
-				ut_d(b->in_LRU_list = FALSE);
+				b->in_LRU_list = FALSE;
 				buf_LRU_add_block_low(b, buf_page_is_old(b));
 			}
 
diff -ruN a/storage/innobase/buf/buf0rea.c b/storage/innobase/buf/buf0rea.c
--- a/storage/innobase/buf/buf0rea.c	2010-12-03 15:18:48.870953384 +0900
+++ b/storage/innobase/buf/buf0rea.c	2010-12-03 15:20:49.604956032 +0900
@@ -200,7 +200,7 @@
 	}
 
 	/* Flush pages from the end of the LRU list if necessary */
-	buf_flush_free_margin(buf_pool);
+	buf_flush_free_margin(buf_pool, TRUE);
 
 	/* Increment number of I/O operations used for LRU policy. */
 	buf_LRU_stat_inc_io();
@@ -474,7 +474,7 @@
 	os_aio_simulated_wake_handler_threads();
 
 	/* Flush pages from the end of the LRU list if necessary */
-	buf_flush_free_margin(buf_pool);
+	buf_flush_free_margin(buf_pool, TRUE);
 
 #ifdef UNIV_DEBUG
 	if (buf_debug_prints && (count > 0)) {
@@ -562,7 +562,7 @@
 	os_aio_simulated_wake_handler_threads();
 
 	/* Flush pages from the end of all the LRU lists if necessary */
-	buf_flush_free_margins();
+	buf_flush_free_margins(FALSE);
 
 #ifdef UNIV_DEBUG
 	if (buf_debug_prints) {
@@ -656,7 +656,7 @@
 	os_aio_simulated_wake_handler_threads();
 
 	/* Flush pages from the end of all the LRU lists if necessary */
-	buf_flush_free_margins();
+	buf_flush_free_margins(FALSE);
 
 #ifdef UNIV_DEBUG
 	if (buf_debug_prints) {
diff -ruN a/storage/innobase/include/buf0buf.h b/storage/innobase/include/buf0buf.h
--- a/storage/innobase/include/buf0buf.h	2010-11-03 07:01:13.000000000 +0900
+++ b/storage/innobase/include/buf0buf.h	2010-12-03 15:20:49.608986590 +0900
@@ -1387,11 +1387,11 @@
 
 	UT_LIST_NODE_T(buf_page_t) LRU;
 					/*!< node of the LRU list */
-#ifdef UNIV_DEBUG
+//#ifdef UNIV_DEBUG
 	ibool		in_LRU_list;	/*!< TRUE if the page is in
 					the LRU list; used in
 					debugging */
-#endif /* UNIV_DEBUG */
+//#endif /* UNIV_DEBUG */
 	unsigned	old:1;		/*!< TRUE if the block is in the old
 					blocks in buf_pool->LRU_old */
 	unsigned	freed_page_clock:31;/*!< the value of
diff -ruN a/storage/innobase/include/buf0flu.h b/storage/innobase/include/buf0flu.h
--- a/storage/innobase/include/buf0flu.h	2010-11-03 07:01:13.000000000 +0900
+++ b/storage/innobase/include/buf0flu.h	2010-12-03 15:20:49.609953185 +0900
@@ -65,13 +65,15 @@
 void
 buf_flush_free_margin(
 /*==================*/
-	 buf_pool_t*	buf_pool);
+	 buf_pool_t*	buf_pool,
+	ibool		wait);
 /*********************************************************************//**
 Flushes pages from the end of all the LRU lists. */
 UNIV_INTERN
 void
-buf_flush_free_margins(void);
+buf_flush_free_margins(
 /*=========================*/
+	ibool		wait);
 #endif /* !UNIV_HOTBACKUP */
 /********************************************************************//**
 Initializes a page for writing to the tablespace. */