~tsarev/percona-server/5.5_log_queries_in_memory

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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
--- /dev/null
+++ b/plugin/slowlog_in_memory/CMakeLists.txt
@@ -0,0 +1,17 @@
+# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+
+MYSQL_ADD_PLUGIN(slowlog_in_memory slowlog_in_memory.cc
+  MODULE_ONLY MODULE_OUTPUT_NAME "slowlog_in_memory")
--- /dev/null
+++ b/plugin/slowlog_in_memory/slowlog_in_memory.cc
@@ -0,0 +1,295 @@
+/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
+
+  This program is free software; you can redistribute it and/or
+  modify it under the terms of the GNU General Public License
+  as published by the Free Software Foundation; version 2 of
+  the License.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "ut_slow_log_list.h"
+
+#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__) || __GNUC__ == 2 && __GNUC_MINOR__ < 8)
+#define __attribute__(A)
+#endif
+typedef struct st_mysql_sys_var SYS_VAR;
+uint max_memory_for_slow_log;
+
+/* lock management */
+
+slow_log_list *global_slow_log_list= NULL;
+
+/*
+  Initialize the plugin at server start or plugin installation.
+
+  SYNOPSIS
+    slow_log_in_memory_plugin_init()
+
+  DESCRIPTION
+    Does nothing.
+
+  RETURN VALUE
+    0                    success
+    1                    failure (cannot happen)
+*/
+
+static int slow_log_in_memory_plugin_init(void *arg __attribute__((unused)))
+{
+#ifdef HAVE_PSI_INTERFACE
+  init_semisync_psi_keys();
+#endif
+
+  mysql_mutex_init(key_ss_mutex_LOCK_slow_event_id_,
+                   &LOCK_slow_event_id_, MY_MUTEX_INIT_FAST);
+  global_slow_log_list= new slow_log_list(max_memory_for_slow_log);
+
+  return 0; 
+}
+
+
+/*
+  Terminate the plugin at server shutdown or plugin deinstallation.
+
+  SYNOPSIS
+    slow_log_in_memory_plugin_deinit()
+    Does nothing.
+
+  RETURN VALUE
+    0                    success
+    1                    failure (cannot happen)
+*/
+
+static int slow_log_in_memory_plugin_deinit(void *arg __attribute__((unused)))
+{
+  if (global_slow_log_list)
+  {
+    delete global_slow_log_list;
+    global_slow_log_list= NULL;
+  }
+
+  mysql_mutex_destroy(&LOCK_slow_event_id_);
+  return 0;
+}
+
+/*
+  Foo
+
+  SYNOPSIS
+    slow_log_in_memory_notify()
+      thd                connection context
+
+  DESCRIPTION
+*/
+
+static void slow_log_in_memory_notify(MYSQL_THD thd __attribute__((unused)),
+                              unsigned int event_class,
+                              const void *event)
+{
+  if (max_memory_for_slow_log == 0) return;
+
+  if (event_class == MYSQL_AUDIT_GENERAL_CLASS)
+  {
+    const struct mysql_event_general *event_general=
+      (const struct mysql_event_general *) event;
+    if (event_general->event_subclass != MYSQL_AUDIT_GENERAL_STATUS)
+      return;
+
+    ulonglong utime_of_query= thd->current_utime() - thd->utime_after_lock;
+
+#ifndef DBUG_OFF
+    if (thd->variables.query_exec_time != 0)
+      utime_of_query= thd->lex->sql_command != SQLCOM_SET_OPTION ? thd->variables.query_exec_time : 0;
+#endif
+
+    if (utime_of_query <= thd->variables.long_query_time)
+      return;
+
+    slow_log_t *tmp= new slow_log_t(event_general->general_time, event_general->general_thread_id, thd->db, thd->innodb_trx_id, event_general->general_error_code, (double)utime_of_query/1000000, event_general->general_query);
+    global_slow_log_list->push_back(tmp);
+  }
+}
+
+
+/*
+  Plugin type-specific descriptor
+*/
+
+static struct st_mysql_audit slow_log_in_memory_descriptor=
+{
+  MYSQL_AUDIT_INTERFACE_VERSION,                    /* interface version    */
+  NULL,                                             /* release_thd function */
+  slow_log_in_memory_notify,                                /* notify function      */
+  { (unsigned long) MYSQL_AUDIT_GENERAL_CLASSMASK } /* class mask           */
+};
+
+static void change_max_memory_for_slow_log(MYSQL_THD thd,
+					SYS_VAR *var,
+					void *ptr,
+					const void *val)
+{
+  uint new_len= *(uint *)val;
+  *(uint *)ptr= new_len;
+  
+  global_slow_log_list->change_max_memory(new_len);
+  return;
+}
+
+static MYSQL_SYSVAR_UINT(max_memory, max_memory_for_slow_log,
+  PLUGIN_VAR_OPCMDARG,
+  "The max memory that can be allocated for slow log.",
+  NULL,				// check
+  &change_max_memory_for_slow_log, // update
+  1024*1024, 0, 1024*1024*1024, 1);
+
+static SYS_VAR* slow_log_in_memory_system_vars[]= {
+  MYSQL_SYSVAR(max_memory),
+  NULL,
+};
+
+static uint slow_log_current_memory(MYSQL_THD thd, SHOW_VAR *var, char *buff)
+{
+  var->type= SHOW_INT;
+  var->value= (char*)&global_slow_log_list->current_memory;
+  return 0;
+}
+
+/* plugin status variables */
+static SHOW_VAR slow_log_in_memory_status_vars[]= {
+  {"slow_log_current_memory",
+   (char*) &slow_log_current_memory,
+   SHOW_FUNC},
+  {NULL, NULL, SHOW_LONG},
+};
+
+
+/*
+   I_S_SLOW_LOG 
+*/
+static struct st_mysql_information_schema i_s_slow_log=
+{
+  MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
+};
+
+static ST_FIELD_INFO i_s_slow_log_fields[] =
+{
+  {"EVENT_ID", 11, MYSQL_TYPE_LONGLONG, 0, 0, 0, SKIP_OPEN_TABLE},
+  {"TIMESTAMP", 11, MYSQL_TYPE_LONGLONG, 0, 0, 0, SKIP_OPEN_TABLE},
+  {"THREAD_ID", 11, MYSQL_TYPE_LONG, 0, 0, 0, SKIP_OPEN_TABLE},
+  {"DB", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 1, 0, SKIP_OPEN_TABLE},
+  {"INNODB_TRX_ID", 11, MYSQL_TYPE_LONGLONG, 0, 1, 0, SKIP_OPEN_TABLE},
+  {"ERRNO", 11, MYSQL_TYPE_LONG, 0, 0, 0, SKIP_OPEN_TABLE},
+  {"MS_QUERY_TIME", 11, MYSQL_TYPE_FLOAT, 0, 0, 0, SKIP_OPEN_TABLE},
+
+  {"QUERY", 255, MYSQL_TYPE_STRING, 0, 1, 0, SKIP_OPEN_TABLE},
+  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE}
+};
+
+
+int i_s_fill_table_slow_log(THD* thd, TABLE_LIST* tables, COND* cond)
+{
+  if (!global_slow_log_list) return 0;
+ 
+  TABLE *table= tables->table;
+
+  mysql_mutex_lock(&global_slow_log_list->LOCK_slow_log_);
+
+  slow_log_t *item= global_slow_log_list->first;
+  while (item)
+  {
+    restore_record(table, s->default_values);
+
+    table->field[0]->store(item->event_id, TRUE);
+    table->field[1]->store(item->timestamp, TRUE);
+    table->field[2]->store(item->thread_id, TRUE);
+
+    if (item->schema)
+    {
+      table->field[3]->store(item->schema, item->schema_len-1, system_charset_info);
+      table->field[3]->set_notnull();
+    }
+
+    if (item->innodb_trx_id)
+    {
+      table->field[4]->store(item->innodb_trx_id, TRUE);
+      table->field[4]->set_notnull();
+    }
+
+    table->field[5]->store(item->cmd_errno, TRUE);
+    table->field[6]->store(item->time_of_query);
+
+    if (item->general_query)
+    {
+      table->field[7]->store(item->general_query, item->general_query_len-1, system_charset_info);
+      table->field[7]->set_notnull();
+    }
+
+    schema_table_store_record(thd, table);
+    item= item->next;
+  }
+
+  mysql_mutex_unlock(&global_slow_log_list->LOCK_slow_log_);
+  return 0;
+}
+
+
+static int i_s_slow_log_plugin_init(void *arg __attribute__((unused)))
+{
+  ST_SCHEMA_TABLE* schema;
+
+  schema = (ST_SCHEMA_TABLE*) arg;
+
+  schema->fields_info = i_s_slow_log_fields;
+  schema->fill_table = i_s_fill_table_slow_log;
+  return 0;
+}
+
+
+static int i_s_slow_log_plugin_deinit(void *arg __attribute__((unused)))
+{
+  return(0);
+}
+
+/*
+  Plugin library descriptor
+*/
+
+mysql_declare_plugin(slow_log_in_memory)
+{
+  MYSQL_AUDIT_PLUGIN,         /* type                            */
+  &slow_log_in_memory_descriptor,     /* descriptor                      */
+  "SLOW_LOG_IN_MEMORY_AUDIT",               /* name                            */
+  "Percona",              /* author                          */
+  "Error code histogram Audit",        /* description                     */
+  PLUGIN_LICENSE_GPL,
+  slow_log_in_memory_plugin_init,     /* init function (when loaded)     */
+  slow_log_in_memory_plugin_deinit,   /* deinit function (when unloaded) */
+  0x0001,                     /* version                         */
+  slow_log_in_memory_status_vars,              /* status variables                */
+  slow_log_in_memory_system_vars,                       /* system variables                */
+  NULL,
+  0,
+},
+{
+  MYSQL_INFORMATION_SCHEMA_PLUGIN,         /* type                            */
+  &i_s_slow_log,     /* descriptor                      */
+  "SLOW_LOG",               /* name                            */
+  "Percona",              /* author                          */
+  "latest slow log in memory",        /* description                     */
+  PLUGIN_LICENSE_GPL,
+  i_s_slow_log_plugin_init,     /* init function (when loaded)     */
+  i_s_slow_log_plugin_deinit,   /* deinit function (when unloaded) */
+  0x0001,                     /* version                         */
+  NULL,              /* status variables                */
+  NULL,                       /* system variables                */
+  NULL,
+  0,
+}
+mysql_declare_plugin_end;
+
--- /dev/null
+++ b/plugin/slowlog_in_memory/ut_slow_log_list.h
@@ -0,0 +1,230 @@
+#ifndef UT_SLOW_LOG_LIST_H
+#define UT_SLOW_LOG_LIST_H
+
+#define MYSQL_SERVER
+#include <mysql/plugin_audit.h>
+#include "unireg.h"
+#include "sql_show.h"
+#include "sql_class.h"
+
+mysql_mutex_t LOCK_slow_event_id_;
+
+#ifdef HAVE_PSI_INTERFACE
+PSI_mutex_key key_ss_mutex_LOCK_slow_log_;
+PSI_mutex_key key_ss_mutex_LOCK_slow_event_id_;
+
+static PSI_mutex_info all_semisync_mutexes[]=
+{
+  {&key_ss_mutex_LOCK_slow_log_, "LOCK_slow_log_", 0},
+  {&key_ss_mutex_LOCK_slow_event_id_, "LOCK_slow_event_id_", 0}
+};
+
+static void init_semisync_psi_keys(void)
+{
+  const char* category= "semisync";
+  int count;
+
+  if (PSI_server == NULL)
+    return;
+
+  count= array_elements(all_semisync_mutexes);
+  PSI_server->register_mutex(category, all_semisync_mutexes, count);
+
+}
+#endif /* HAVE_PSI_INTERFACE */
+
+/* unique event id for each slow query */
+ulonglong global_event_id= 0;
+
+const uint MAX_QUERY_LEN= 255;
+
+class slow_log_t
+{
+public:
+  ulonglong event_id;
+  ulonglong timestamp;
+  ulong thread_id;
+  char *schema;
+  ulonglong innodb_trx_id;
+  int cmd_errno;
+  double time_of_query;
+  char *general_query;
+  
+  uint schema_len;
+  uint general_query_len;
+
+  slow_log_t *next;
+
+  slow_log_t(ulonglong timestamp, ulong thread_id, const char *schema,
+    ulonglong innodb_trx_id, int cmd_errno, double time_of_query, const char *general_query):next(NULL)
+  {
+    this->timestamp= timestamp;
+    this->thread_id= thread_id;
+
+    if (schema)
+    {
+      this->schema_len= strlen(schema) + 1;
+      this->schema= strndup(schema, this->schema_len);
+    }
+    else
+    {
+      this->schema= NULL;
+      this->schema_len= 0;
+    }
+
+    this->innodb_trx_id= innodb_trx_id;
+    this->cmd_errno= cmd_errno;
+    this->time_of_query= time_of_query;
+
+    if (general_query)
+    {
+      this->general_query_len= strlen(general_query) + 1;
+
+      if (this->general_query_len > MAX_QUERY_LEN)
+      {
+        this->general_query_len= MAX_QUERY_LEN;
+      }
+
+      this->general_query= strndup(general_query, this->general_query_len);
+      this->general_query[this->general_query_len-1]= '\0';
+    }
+    else
+    {
+      this->general_query= NULL;
+      this->general_query_len= 0;
+    }
+
+    this->real_size= sizeof(slow_log_t) + this->schema_len + this->general_query_len;
+    
+    mysql_mutex_lock(&LOCK_slow_event_id_);
+    this->event_id= ++global_event_id;
+    mysql_mutex_unlock(&LOCK_slow_event_id_);
+  }
+
+  ~slow_log_t()
+  {
+    if (schema)
+    {
+       free(schema);
+       schema= NULL;
+    }
+
+    if (general_query)
+    {
+       free(general_query);
+       general_query= NULL;
+    }
+  }
+
+  uint size_of_instance()
+  {
+    return real_size;
+  }
+
+  uint real_size;
+};
+
+class slow_log_list
+{
+public:
+  slow_log_t *first, *last;
+  mysql_mutex_t LOCK_slow_log_;
+
+  uint current_memory;
+
+  void change_max_memory(uint new_len)
+  {
+    mysql_mutex_lock(&LOCK_slow_log_);
+
+    max_memory= new_len;
+
+    /* if the size is larger than current size, just change the value*/
+    if (max_memory >= current_memory)
+    {
+      mysql_mutex_unlock(&LOCK_slow_log_);
+      return;
+    }
+
+    while (current_memory > max_memory)
+    {
+      pop_front();
+    }
+
+    mysql_mutex_unlock(&LOCK_slow_log_);
+  }
+
+  slow_log_list(uint max_memory)
+  {
+    first= NULL;
+    last= NULL;
+    current_memory= 0;
+    this->max_memory= max_memory;
+
+    mysql_mutex_init(key_ss_mutex_LOCK_slow_log_,
+                &LOCK_slow_log_, MY_MUTEX_INIT_FAST);
+  }
+
+  ~slow_log_list()
+  {
+    mysql_mutex_lock(&LOCK_slow_log_);
+    while (first)
+    {
+      pop_front();
+    }
+    mysql_mutex_unlock(&LOCK_slow_log_);
+
+    mysql_mutex_destroy(&LOCK_slow_log_);
+  }
+
+  void push_back(slow_log_t * item)
+  {
+    mysql_mutex_lock(&LOCK_slow_log_);
+
+    uint size= item->size_of_instance();
+
+    if (size > max_memory)
+    {
+      sql_print_warning("the insert item size %u > %u discarded", size, max_memory);
+      mysql_mutex_unlock(&LOCK_slow_log_);
+      return;
+    }
+
+    while (current_memory + size > max_memory)
+    {
+      pop_front();
+    }
+
+    if (!first)
+    {
+       first= item;
+       last= item;
+    }
+    else
+    {
+      last->next= item;
+      last= item;
+    }
+
+    current_memory+= size;
+    mysql_mutex_unlock(&LOCK_slow_log_);
+  }
+
+  /*functions call pop_front should have hold LOCK_slow_log_*/
+  void pop_front()
+  {
+    if (!first) return;
+
+    slow_log_t *item= first;
+    first= first->next;
+
+    if (!first) last= NULL;
+
+    current_memory-= item->size_of_instance();
+    delete item;
+  }
+
+private:
+  uint max_memory;
+};
+#endif
+
--- /dev/null
+++ b/mysql-test/include/have_slog_log_in_memory_audit_plugin.inc
@@ -0,0 +1,21 @@
+#
+# Check if server has support for loading plugins
+#
+if (`SELECT @@have_dynamic_loading != 'YES'`) {
+  --skip Requires dynamic loading
+}
+
+#
+# Check if the variable SLOW_LOG_IN_MEMORY_AUDIT is set
+#
+if (!$SLOW_LOG_IN_MEMORY_AUDIT)
+{
+  skip Need slow_log_in_memory plugins;
+}
+
+#
+# Check if --plugin-dir was setup for slow_log_in_memory
+#
+if (`SELECT CONCAT('--plugin-dir=', @@plugin_dir) != '$SLOW_LOG_IN_MEMORY_AUDIT_OPT'`) {
+  --skip SLOW_LOG plugin requires that --plugin-dir is set to the slow_log_in_memory plugin dir (either the .opt file does not contain \$SLOW_LOG_IN_MEMORY_AUDIT_OPT or another plugin is in use)
+}
--- a/mysql-test/include/plugin.defs
+++ b/mysql-test/include/plugin.defs
@@ -40,3 +40,5 @@
 ha_federated       storage/federated  FEDERATED_PLUGIN
 mypluglib          plugin/fulltext    SIMPLE_PARSER
 libdaemon_example  plugin/daemon_example DAEMONEXAMPLE
+slowlog_in_memory  plugin/slowlog_in_memory SLOW_LOG
+slowlog_in_memory  plugin/slowlog_in_memory SLOW_LOG_IN_MEMORY_AUDIT 
--- /dev/null
+++ b/mysql-test/r/slow_log_in_memory.result
@@ -0,0 +1,69 @@
+#
+# feature: slow log show in information_schema.slow_log 
+#
+install plugin SLOW_LOG_IN_MEMORY_AUDIT  soname 'slowlog_in_memory.so';
+install plugin SLOW_LOG  soname 'slowlog_in_memory.so';
+show variables like 'slow_log_in_memory_audit_max_memory';
+Variable_name	Value
+slow_log_in_memory_audit_max_memory	1048576
+SET long_query_time=2;
+SET query_exec_time=2.123456;
+select 1;
+1
+1
+SET query_exec_time=0;
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+1
+1
+SET query_exec_time=0;
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+1
+1
+SET query_exec_time=0;
+show global status like 'slow_log_current_memory';
+Variable_name	Value
+slow_log_current_memory	XX
+select * from information_schema.slow_log;
+EVENT_ID	TIMESTAMP	THREAD_ID	DB	INNODB_TRX_ID	ERRNO	MS_QUERY_TIME	QUERY
+1	XX	XX	test	XX	0	2.123456	select 1
+2	XX	XX	test	XX	0	2.1	select 1
+3	XX	XX	test	XX	0	2.1	select 1
+# decrease max_memory and wash eldest
+set global slow_log_in_memory_audit_max_memory=240;
+use information_schema;
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+1
+1
+SET query_exec_time=0;
+show global status like 'slow_log_current_memory';
+Variable_name	Value
+slow_log_current_memory	XX
+select * from information_schema.slow_log;
+EVENT_ID	TIMESTAMP	THREAD_ID	DB	INNODB_TRX_ID	ERRNO	MS_QUERY_TIME	QUERY
+3	XX	XX	test	XX	0	2.1	select 1
+4	XX	XX	information_schema	XX	0	2.1	select 1
+# set to zero to disable audit
+set global slow_log_in_memory_audit_max_memory=0;
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+1
+1
+SET query_exec_time=0;
+show global status like 'slow_log_current_memory';
+Variable_name	Value
+slow_log_current_memory	0
+select * from information_schema.slow_log;
+EVENT_ID	TIMESTAMP	THREAD_ID	DB	INNODB_TRX_ID	ERRNO	MS_QUERY_TIME	QUERY
+uninstall plugin SLOW_LOG;
+uninstall plugin SLOW_LOG_IN_MEMORY_AUDIT;
+Warnings:
+Warning	1620	Plugin is busy and will be uninstalled on shutdown
+SET SESSION query_exec_time=default;
+SET long_query_time=default;
--- /dev/null
+++ b/mysql-test/suite/sys_vars/r/slow_log_in_memory_audit.result
@@ -0,0 +1,31 @@
+install plugin SLOW_LOG_IN_MEMORY_AUDIT  soname 'slowlog_in_memory.so';
+install plugin SLOW_LOG  soname 'slowlog_in_memory.so';
+select @@global.slow_log_in_memory_audit_max_memory;
+@@global.slow_log_in_memory_audit_max_memory
+1048576
+show global variables like 'slow_log_in_memory_audit_max_memory';
+Variable_name	Value
+slow_log_in_memory_audit_max_memory	1048576
+select * from information_schema.global_variables where variable_name='slow_log_in_memory_audit_max_memory';
+VARIABLE_NAME	VARIABLE_VALUE
+SLOW_LOG_IN_MEMORY_AUDIT_MAX_MEMORY	1048576
+set global slow_log_in_memory_audit_max_memory=2097152;
+select @@global.slow_log_in_memory_audit_max_memory;
+@@global.slow_log_in_memory_audit_max_memory
+2097152
+show global variables like 'slow_log_in_memory_audit_max_memory';
+Variable_name	Value
+slow_log_in_memory_audit_max_memory	2097152
+set global slow_log_in_memory_audit_max_memory=1.1;
+ERROR 42000: Incorrect argument type to variable 'slow_log_in_memory_audit_max_memory'
+set global slow_log_in_memory_audit_max_memory=1e1;
+ERROR 42000: Incorrect argument type to variable 'slow_log_in_memory_audit_max_memory'
+set global slow_log_in_memory_audit_max_memory="some text";
+ERROR 42000: Incorrect argument type to variable 'slow_log_in_memory_audit_max_memory'
+select @@global.slow_log_in_memory_audit_max_memory;
+@@global.slow_log_in_memory_audit_max_memory
+2097152
+UNINSTALL PLUGIN SLOW_LOG;
+UNINSTALL PLUGIN SLOW_LOG_IN_MEMORY_AUDIT;
+Warnings:
+Warning	1620	Plugin is busy and will be uninstalled on shutdown
--- /dev/null
+++ b/mysql-test/suite/sys_vars/t/slow_log_in_memory_audit-master.opt
@@ -0,0 +1 @@
+$SLOW_LOG_IN_MEMORY_AUDIT_OPT
--- /dev/null
+++ b/mysql-test/suite/sys_vars/t/slow_log_in_memory_audit.test
@@ -0,0 +1,40 @@
+
+#
+# exists as a global only
+#
+source include/not_embedded.inc;
+source include/have_slog_log_in_memory_audit_plugin.inc;
+# The following is to prevent a mis-match on windows that has the name of of the lib ending with 'dll' 
+--replace_regex /\.dll/.so/
+install plugin SLOW_LOG_IN_MEMORY_AUDIT  soname 'slowlog_in_memory.so';
+install plugin SLOW_LOG  soname 'slowlog_in_memory.so';
+select @@global.slow_log_in_memory_audit_max_memory;
+
+show global variables like 'slow_log_in_memory_audit_max_memory';
+select * from information_schema.global_variables where variable_name='slow_log_in_memory_audit_max_memory';
+
+#
+# show that it's writable
+#
+set global slow_log_in_memory_audit_max_memory=2097152;
+select @@global.slow_log_in_memory_audit_max_memory;
+show global variables like 'slow_log_in_memory_audit_max_memory';
+
+#
+# incorrect types
+#
+--error ER_WRONG_TYPE_FOR_VAR
+set global slow_log_in_memory_audit_max_memory=1.1;
+--error ER_WRONG_TYPE_FOR_VAR
+set global slow_log_in_memory_audit_max_memory=1e1;
+--error ER_WRONG_TYPE_FOR_VAR
+set global slow_log_in_memory_audit_max_memory="some text";
+
+
+#
+# Cleanup
+#
+select @@global.slow_log_in_memory_audit_max_memory;
+UNINSTALL PLUGIN SLOW_LOG;
+UNINSTALL PLUGIN SLOW_LOG_IN_MEMORY_AUDIT;
+
--- /dev/null
+++ b/mysql-test/t/slow_log_in_memory-master.opt
@@ -0,0 +1 @@
+$SLOW_LOG_IN_MEMORY_AUDIT_OPT
--- /dev/null
+++ b/mysql-test/t/slow_log_in_memory.test
@@ -0,0 +1,66 @@
+--source include/have_debug.inc
+--source include/have_slog_log_in_memory_audit_plugin.inc
+
+--echo #
+--echo # feature: slow log show in information_schema.slow_log 
+--echo #
+
+install plugin SLOW_LOG_IN_MEMORY_AUDIT  soname 'slowlog_in_memory.so';
+install plugin SLOW_LOG  soname 'slowlog_in_memory.so';
+show variables like 'slow_log_in_memory_audit_max_memory';
+
+SET long_query_time=2;
+SET query_exec_time=2.123456;
+select 1;
+SET query_exec_time=0;
+
+connect (con1,localhost,root,,);
+
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+SET query_exec_time=0;
+
+connect (con2,localhost,root,,);
+
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+SET query_exec_time=0;
+--replace_column 2 XX
+show global status like 'slow_log_current_memory';
+--replace_column 2 XX 3 XX 5 XX
+select * from information_schema.slow_log;
+
+--echo # decrease max_memory and wash eldest
+set global slow_log_in_memory_audit_max_memory=360;
+connection con1;
+use information_schema;
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+SET query_exec_time=0;
+SET log_slow_filter='filesort';
+SET query_exec_time=2.1;
+select 1;
+SET log_slow_filter=default;
+SET query_exec_time=0;
+--replace_column 2 XX
+show global status like 'slow_log_current_memory';
+--replace_column 2 XX 3 XX 5 XX
+select * from information_schema.slow_log;
+
+--echo # set to zero to disable audit
+connection con1;
+set global slow_log_in_memory_audit_max_memory=0;
+SET long_query_time=2;
+SET query_exec_time=2.1;
+select 1;
+SET query_exec_time=0;
+show global status like 'slow_log_current_memory';
+select * from information_schema.slow_log;
+
+uninstall plugin SLOW_LOG;
+uninstall plugin SLOW_LOG_IN_MEMORY_AUDIT;
+SET SESSION query_exec_time=default;
+SET long_query_time=default;