1 # name : sql_no_fcache.patch
6 # Any small change to this file in the main branch
7 # should be done or reviewed by the maintainer!
8 --- a/client/mysqldump.c
9 +++ b/client/mysqldump.c
11 static uint opt_protocol= 0;
12 static char *opt_plugin_dir= 0, *opt_default_auth= 0;
14 +static my_bool server_supports_sql_no_fcache= FALSE;
17 Dynamic_string wrapper functions. In this file use these
18 wrappers, they will terminate the process if there is
19 @@ -1496,6 +1498,17 @@
20 /* Don't switch charsets for 4.1 and earlier. (bug#34192). */
21 server_supports_switching_charsets= FALSE;
24 + /* Check to see if we support SQL_NO_FCACHE on this server. */
25 + if (mysql_query(mysql, "SELECT SQL_NO_FCACHE NOW()") == 0)
27 + MYSQL_RES *res = mysql_store_result(mysql);
30 + mysql_free_result(res);
32 + server_supports_sql_no_fcache= TRUE;
35 As we're going to set SQL_MODE, it would be lost on reconnect, so we
37 @@ -3177,7 +3190,12 @@
39 /* now build the query string */
41 - dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * INTO OUTFILE '");
42 + dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ ");
43 + if (server_supports_sql_no_fcache)
45 + dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
47 + dynstr_append_checked(&query_string, "* INTO OUTFILE '");
48 dynstr_append_checked(&query_string, filename);
49 dynstr_append_checked(&query_string, "'");
51 @@ -3227,7 +3245,12 @@
52 check_io(md_result_file);
55 - dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * FROM ");
56 + dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ ");
57 + if (server_supports_sql_no_fcache)
59 + dynstr_append_checked(&query_string, "/*!50084 SQL_NO_FCACHE */ ");
61 + dynstr_append_checked(&query_string, "* FROM ");
62 dynstr_append_checked(&query_string, result_table);
66 +++ b/include/flashcache_ioctl.h
68 +/****************************************************************************
69 + * flashcache_ioctl.h
70 + * FlashCache: Device mapper target for block-level disk caching
72 + * Copyright 2010 Facebook, Inc.
73 + * Author: Mohan Srinivasan (mohan@facebook.com)
75 + * Based on DM-Cache:
76 + * Copyright (C) International Business Machines Corp., 2006
77 + * Author: Ming Zhao (mingzhao@ufl.edu)
79 + * This program is free software; you can redistribute it and/or modify
80 + * it under the terms of the GNU General Public License as published by
81 + * the Free Software Foundation; under version 2 of the License.
83 + * This program is distributed in the hope that it will be useful,
84 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
85 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86 + * GNU General Public License for more details.
88 + * You should have received a copy of the GNU General Public License
89 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
90 + ****************************************************************************/
92 +#ifndef FLASHCACHE_IOCTL_H
93 +#define FLASHCACHE_IOCTL_H
95 +#include <linux/types.h>
97 +#define FLASHCACHE_IOCTL 0xfe
100 + FLASHCACHEADDNCPID_CMD=200,
101 + FLASHCACHEDELNCPID_CMD,
102 + FLASHCACHEDELNCALL_CMD,
103 + FLASHCACHEADDWHITELIST_CMD,
104 + FLASHCACHEDELWHITELIST_CMD,
105 + FLASHCACHEDELWHITELISTALL_CMD,
108 +#define FLASHCACHEADDNCPID _IOW(FLASHCACHE_IOCTL, FLASHCACHEADDNCPID_CMD, pid_t)
109 +#define FLASHCACHEDELNCPID _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELNCPID_CMD, pid_t)
110 +#define FLASHCACHEDELNCALL _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELNCALL_CMD, pid_t)
112 +#define FLASHCACHEADDBLACKLIST FLASHCACHEADDNCPID
113 +#define FLASHCACHEDELBLACKLIST FLASHCACHEDELNCPID
114 +#define FLASHCACHEDELALLBLACKLIST FLASHCACHEDELNCALL
116 +#define FLASHCACHEADDWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEADDWHITELIST_CMD, pid_t)
117 +#define FLASHCACHEDELWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELIST_CMD, pid_t)
118 +#define FLASHCACHEDELALLWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELISTALL_CMD, pid_t)
122 +++ b/patch_info/sql_no_fcache.info
124 +File=sql_no_fcache.patch
125 +Name=Support for flashcache including the SQL_NO_FCACHE option that prevents blocks from being cached during a query.
133 { "SQL_CACHE", SYM(SQL_CACHE_SYM)},
134 { "SQL_CALC_FOUND_ROWS", SYM(SQL_CALC_FOUND_ROWS)},
135 { "SQL_NO_CACHE", SYM(SQL_NO_CACHE_SYM)},
136 + { "SQL_NO_FCACHE", SYM(SQL_NO_FCACHE_SYM)},
137 { "SQL_SMALL_RESULT", SYM(SQL_SMALL_RESULT)},
138 { "SQL_THREAD", SYM(SQL_THREAD)},
139 { "SQL_TSI_SECOND", SYM(SECOND_SYM)},
143 extern char language[FN_REFLEN];
144 extern "C" MYSQL_PLUGIN_IMPORT ulong server_id;
145 extern ulong concurrency;
147 +extern int cachedev_fd;
148 extern time_t server_start_time, flush_status_time;
149 extern char *opt_mysql_tmpdir, mysql_charsets_dir[];
150 extern int mysql_unpacked_real_data_home_len;
154 #ifdef HAVE_SYS_PRCTL_H
155 #include <sys/prctl.h>
157 +#if defined(__linux__)
159 +#include <sys/statfs.h>
160 +#include "flashcache_ioctl.h"
163 #include <thr_alarm.h>
164 #include <ft_global.h>
166 ulong binlog_cache_use= 0, binlog_cache_disk_use= 0;
167 ulong binlog_stmt_cache_use= 0, binlog_stmt_cache_disk_use= 0;
168 ulong max_connections, max_connect_errors;
172 +my_bool cachedev_enabled= FALSE;
175 Maximum length of parameter value which can be set through
176 mysql_send_long_data() call.
177 @@ -4102,6 +4112,97 @@
178 #define decrement_handler_count()
179 #endif /* defined(_WIN32) || defined(HAVE_SMEM) */
181 +#if defined(__linux__)
183 + * Auto detect if we support flash cache on the host system.
184 + * This needs to be called before we setuid away from root
185 + * to avoid permission problems on opening the device node.
187 +static void init_cachedev(void)
189 + struct statfs stfs_data_home_dir;
190 + struct statfs stfs;
191 + struct mntent *ent;
192 + pid_t pid = getpid();
194 + const char *error_message= NULL;
196 + // disabled by default
198 + cachedev_enabled= FALSE;
200 + if (!mysql_data_home)
202 + error_message= "mysql_data_home not set";
206 + if (statfs(mysql_data_home, &stfs_data_home_dir) < 0)
208 + error_message= "statfs failed";
212 + mounts = setmntent("/etc/mtab", "r");
213 + if (mounts == NULL)
215 + error_message= "setmntent failed";
219 + while ((ent = getmntent(mounts)) != NULL)
221 + if (statfs(ent->mnt_dir, &stfs) < 0)
223 + if (memcmp(&stfs.f_fsid, &stfs_data_home_dir.f_fsid, sizeof(fsid_t)) == 0)
230 + error_message= "getmntent loop failed";
234 + cachedev_fd = open(ent->mnt_fsname, O_RDONLY);
235 + if (cachedev_fd < 0)
237 + error_message= "open flash device failed";
241 + /* cleanup previous whitelistings */
242 + if (ioctl(cachedev_fd, FLASHCACHEDELALLWHITELIST, &pid) < 0)
244 + close(cachedev_fd);
246 + error_message= "ioctl failed";
248 + ioctl(cachedev_fd, FLASHCACHEADDWHITELIST, &pid);
252 + sql_print_information("Flashcache bypass: %s",
253 + (cachedev_fd > 0) ? "enabled" : "disabled");
255 + sql_print_information("Flashcache setup error is : %s\n", error_message);
257 + cachedev_enabled= TRUE;
261 +static void cleanup_cachedev(void)
263 + pid_t pid = getpid();
265 + if (cachedev_enabled) {
266 + ioctl(cachedev_fd, FLASHCACHEDELWHITELIST, &pid);
267 + close(cachedev_fd);
273 #ifndef EMBEDDED_LIBRARY
275 @@ -4363,6 +4464,10 @@
279 +#if defined(__linux__)
284 We have enough space for fiddling with the argv, continue
286 @@ -4574,6 +4679,10 @@
290 +#if defined(__linux__)
291 + cleanup_cachedev();
297 @@ -6421,6 +6530,7 @@
298 {"Delayed_errors", (char*) &delayed_insert_errors, SHOW_LONG},
299 {"Delayed_insert_threads", (char*) &delayed_insert_threads, SHOW_LONG_NOFLUSH},
300 {"Delayed_writes", (char*) &delayed_insert_writes, SHOW_LONG},
301 + {"Flashcache_enabled", (char*) &cachedev_enabled, SHOW_BOOL },
302 {"Flush_commands", (char*) &refresh_version, SHOW_LONG_NOFLUSH},
303 {"Handler_commit", (char*) offsetof(STATUS_VAR, ha_commit_count), SHOW_LONG_STATUS},
304 {"Handler_delete", (char*) offsetof(STATUS_VAR, ha_delete_count), SHOW_LONG_STATUS},
309 lex->subqueries= FALSE;
310 lex->context_analysis_only= 0;
311 + lex->disable_flashcache= FALSE;
312 lex->derived_tables= 0;
313 lex->safe_to_cache_query= 1;
314 lex->leaf_tables_insert= 0;
317 @@ -2346,6 +2346,7 @@
319 enum enum_yes_no_unknown tx_chain, tx_release;
320 bool safe_to_cache_query;
321 + bool disable_flashcache;
322 bool subqueries, ignore;
323 st_parsing_options parsing_options;
324 Alter_info alter_info;
325 --- a/sql/sql_select.cc
326 +++ b/sql/sql_select.cc
329 #define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
331 +#include <sys/syscall.h>
332 +#include <sys/ioctl.h>
333 +#if defined(__linux__)
334 +#include "flashcache_ioctl.h"
337 const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
338 "MAYBE_REF","ALL","range","index","fulltext",
339 "ref_or_null","unique_subquery","index_subquery",
341 ulong setup_tables_done_option)
345 register SELECT_LEX *select_lex = &lex->select_lex;
346 DBUG_ENTER("handle_select");
347 +#if defined(__linux__)
348 + if(lex->disable_flashcache && cachedev_fd > 0)
350 + pid = syscall(SYS_gettid);
351 + ioctl(cachedev_fd, FLASHCACHEADDNCPID, &pid);
355 MYSQL_SELECT_START(thd->query());
357 if (select_lex->master_unit()->is_union() ||
360 result->abort_result_set();
362 +#if defined(__linux__)
363 + if (lex->disable_flashcache && cachedev_fd > 0)
365 + ioctl(cachedev_fd, FLASHCACHEDELNCPID, &pid);
368 MYSQL_SELECT_DONE((int) res, (ulong) thd->limit_found_rows);
371 --- a/sql/sql_yacc.yy
372 +++ b/sql/sql_yacc.yy
373 @@ -1284,6 +1284,7 @@
375 %token SQL_CALC_FOUND_ROWS
376 %token SQL_NO_CACHE_SYM
377 +%token SQL_NO_FCACHE_SYM
378 %token SQL_SMALL_RESULT
379 %token SQL_SYM /* SQL-2003-R */
381 @@ -7362,6 +7363,10 @@
382 Lex->select_lex.sql_cache= SELECT_LEX::SQL_NO_CACHE;
385 + | SQL_NO_FCACHE_SYM
387 + Lex->disable_flashcache= TRUE;
393 +++ b/mysql-test/r/percona_sql_no_fcache.result
395 +drop table if exists t1;
396 +create table t (a int not null);
397 +insert into t values (1),(2),(3);
398 +SELECT SQL_NO_FCACHE SLEEP(0);
401 +SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM t;
408 +++ b/mysql-test/t/percona_sql_no_fcache.test
411 +drop table if exists t1;
414 +create table t (a int not null);
415 +insert into t values (1),(2),(3);
417 +SELECT SQL_NO_FCACHE SLEEP(0);
418 +SELECT /*!40001 SQL_NO_CACHE */ /*!50084 SQL_NO_FCACHE */ * FROM t;