~tsarev/percona-server/5.1_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
# name       : innodb_fast_shutdown
# introduced : 13
# maintainer : Alexey
#
# Shutting down XtraDB takes uninterruptible sleep()s up to 10
# seconds, even when there is no actual work to do during shutdown.
#
# This patch removes most such delays during shutdown, as found using
# PMP. This makes standard test run very close in speed to with
# --loose-innodb-fast-shutdown=2, and greatly speeds up running the test
# suite.
#
# The patch also implements os_event_wait_time() for POSIX systems.
diff -ruN /dev/null b/COPYING.innodb_fast_shutdown
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ b/COPYING.innodb_fast_shutdown	2010-11-16 21:37:51.000000000 +0300
@@ -0,0 +1,10 @@
+Copyright (c) 2010, Kristian Nielsen
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+    * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff -ruN a/storage/innodb_plugin/include/os0sync.h b/storage/innodb_plugin/include/os0sync.h
--- a/storage/innodb_plugin/include/os0sync.h	2010-11-16 21:33:00.000000000 +0300
+++ b/storage/innodb_plugin/include/os0sync.h	2010-11-16 21:34:06.000000000 +0300
@@ -189,14 +189,14 @@
 
 /**********************************************************//**
 Waits for an event object until it is in the signaled state or
-a timeout is exceeded. In Unix the timeout is always infinite.
+a timeout is exceeded.
 @return	0 if success, OS_SYNC_TIME_EXCEEDED if timeout was exceeded */
 UNIV_INTERN
 ulint
 os_event_wait_time(
 /*===============*/
 	os_event_t	event,	/*!< in: event to wait */
-	ulint		time);	/*!< in: timeout in microseconds, or
+	ulint		wtime);	/*!< in: timeout in microseconds, or
 				OS_SYNC_INFINITE_TIME */
 #ifdef __WIN__
 /**********************************************************//**
diff -ruN a/storage/innodb_plugin/include/srv0srv.h b/storage/innodb_plugin/include/srv0srv.h
--- a/storage/innodb_plugin/include/srv0srv.h	2010-11-16 21:33:00.000000000 +0300
+++ b/storage/innodb_plugin/include/srv0srv.h	2010-11-16 21:34:06.000000000 +0300
@@ -57,6 +57,9 @@
 thread starts running */
 extern os_event_t	srv_lock_timeout_thread_event;
 
+/* This event is set at shutdown to wakeup threads from sleep */
+extern os_event_t	srv_shutdown_event;
+
 /* If the last data file is auto-extended, we add this many pages to it
 at a time */
 #define SRV_AUTO_EXTEND_INCREMENT	\
diff -ruN a/storage/innodb_plugin/log/log0log.c b/storage/innodb_plugin/log/log0log.c
--- a/storage/innodb_plugin/log/log0log.c	2010-11-16 21:33:00.000000000 +0300
+++ b/storage/innodb_plugin/log/log0log.c	2010-11-16 21:34:06.000000000 +0300
@@ -3103,6 +3103,7 @@
 	algorithm only works if the server is idle at shutdown */
 
 	srv_shutdown_state = SRV_SHUTDOWN_CLEANUP;
+	os_event_set(srv_shutdown_event);
 loop:
 	os_thread_sleep(100000);
 
diff -ruN a/storage/innodb_plugin/os/os0sync.c b/storage/innodb_plugin/os/os0sync.c
--- a/storage/innodb_plugin/os/os0sync.c	2010-11-16 21:33:00.000000000 +0300
+++ b/storage/innodb_plugin/os/os0sync.c	2010-11-16 21:34:06.000000000 +0300
@@ -31,6 +31,9 @@
 
 #ifdef __WIN__
 #include <windows.h>
+#else
+#include <sys/time.h>
+#include <time.h>
 #endif
 
 #include "ut0mem.h"
@@ -407,14 +410,14 @@
 
 /**********************************************************//**
 Waits for an event object until it is in the signaled state or
-a timeout is exceeded. In Unix the timeout is always infinite.
+a timeout is exceeded.
 @return	0 if success, OS_SYNC_TIME_EXCEEDED if timeout was exceeded */
 UNIV_INTERN
 ulint
 os_event_wait_time(
 /*===============*/
 	os_event_t	event,	/*!< in: event to wait */
-	ulint		time)	/*!< in: timeout in microseconds, or
+	ulint		wtime)	/*!< in: timeout in microseconds, or
 				OS_SYNC_INFINITE_TIME */
 {
 #ifdef __WIN__
@@ -422,8 +425,8 @@
 
 	ut_a(event);
 
-	if (time != OS_SYNC_INFINITE_TIME) {
-		err = WaitForSingleObject(event->handle, (DWORD) time / 1000);
+	if (wtime != OS_SYNC_INFINITE_TIME) {
+		err = WaitForSingleObject(event->handle, (DWORD) wtime / 1000);
 	} else {
 		err = WaitForSingleObject(event->handle, INFINITE);
 	}
@@ -439,13 +442,47 @@
 		return(1000000); /* dummy value to eliminate compiler warn. */
 	}
 #else
-	UT_NOT_USED(time);
+	int		err;
+	int		ret = 0;
+	ulint		tmp;
+	ib_int64_t	old_count;
+	struct timeval	tv_start;
+	struct timespec	timeout;
+
+	if (wtime == OS_SYNC_INFINITE_TIME) {
+		os_event_wait(event);
+		return 0;
+	}
+
+	/* Compute the absolute point in time at which to time out. */
+	gettimeofday(&tv_start, NULL);
+	tmp = tv_start.tv_usec + wtime;
+	timeout.tv_sec = tv_start.tv_sec + (tmp / 1000000);
+	timeout.tv_nsec = (tmp % 1000000) * 1000;
+
+	os_fast_mutex_lock(&(event->os_mutex));
+	old_count = event->signal_count;
 
-	/* In Posix this is just an ordinary, infinite wait */
+	for (;;) {
+		if (event->is_set == TRUE || event->signal_count != old_count)
+			break;
+
+		err = pthread_cond_timedwait(&(event->cond_var),
+					     &(event->os_mutex), &timeout);
+		if (err == ETIMEDOUT) {
+			ret = OS_SYNC_TIME_EXCEEDED;
+			break;
+		}
+	}
 
-	os_event_wait(event);
+	os_fast_mutex_unlock(&(event->os_mutex));
+
+	if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
+
+		os_thread_exit(NULL);
+	}
 
-	return(0);
+	return ret;
 #endif
 }
 
diff -ruN a/storage/innodb_plugin/srv/srv0srv.c b/storage/innodb_plugin/srv/srv0srv.c
--- a/storage/innodb_plugin/srv/srv0srv.c	2010-11-16 21:33:00.000000000 +0300
+++ b/storage/innodb_plugin/srv/srv0srv.c	2010-11-16 21:34:06.000000000 +0300
@@ -708,6 +708,8 @@
 
 UNIV_INTERN os_event_t	srv_lock_timeout_thread_event;
 
+UNIV_INTERN os_event_t	srv_shutdown_event;
+
 UNIV_INTERN srv_sys_t*	srv_sys	= NULL;
 
 /* padding to prevent other memory update hotspots from residing on
@@ -1013,6 +1015,7 @@
 	}
 
 	srv_lock_timeout_thread_event = os_event_create(NULL);
+	srv_shutdown_event = os_event_create(NULL);
 
 	for (i = 0; i < SRV_MASTER + 1; i++) {
 		srv_n_threads_active[i] = 0;
@@ -2240,7 +2243,7 @@
 	/* Wake up every 5 seconds to see if we need to print
 	monitor information. */
 
-	os_thread_sleep(5000000);
+	os_event_wait_time(srv_shutdown_event, 5000000);
 
 	current_time = time(NULL);
 
@@ -2382,7 +2385,7 @@
 	/* When someone is waiting for a lock, we wake up every second
 	and check if a timeout has passed for a lock wait */
 
-	os_thread_sleep(1000000);
+	os_event_wait_time(srv_shutdown_event, 1000000);
 
 	srv_lock_timeout_active = TRUE;
 
@@ -2556,7 +2559,7 @@
 
 	fflush(stderr);
 
-	os_thread_sleep(1000000);
+	os_event_wait_time(srv_shutdown_event, 1000000);
 
 	if (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP) {
 
@@ -2600,7 +2603,7 @@
 	last_dump_time = time(NULL);
 
 loop:
-	os_thread_sleep(5000000);
+	os_event_wait_time(srv_shutdown_event, 5000000);
 
 	if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
 		goto exit_func;
@@ -2783,7 +2786,7 @@
 		if (!skip_sleep) {
 		if (next_itr_time > cur_time) {
 
-			os_thread_sleep(ut_min(1000000, (next_itr_time - cur_time) * 1000));
+			os_event_wait_time(srv_shutdown_event, ut_min(1000000, (next_itr_time - cur_time) * 1000));
 			srv_main_sleeps++;
 
 			/*
@@ -3490,9 +3493,10 @@
 		mutex_exit(&kernel_mutex);
 
 		sleep_ms = 10;
+		os_event_reset(srv_shutdown_event);
 	}
 
-	os_thread_sleep( sleep_ms * 1000 );
+	os_event_wait_time(srv_shutdown_event, sleep_ms * 1000);
 
 	history_len = trx_sys->rseg_history_len;
 	if (history_len > 1000)