~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/handler/i_s.cc

  • Committer: Brian Aker
  • Date: 2010-02-07 01:33:54 UTC
  • Revision ID: brian@gaz-20100207013354-d2pg1n68u5c09pgo
Remove giant include header to its own file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 2007, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
5
This program is free software; you can redistribute it and/or modify it under
 
6
the terms of the GNU General Public License as published by the Free Software
 
7
Foundation; version 2 of the License.
 
8
 
 
9
This program is distributed in the hope that it will be useful, but WITHOUT
 
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
12
 
 
13
You should have received a copy of the GNU General Public License along with
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file handler/i_s.cc
 
21
InnoDB INFORMATION SCHEMA tables interface to MySQL.
 
22
 
 
23
Created July 18, 2007 Vasil Dimov
 
24
*******************************************************/
 
25
 
 
26
#include "config.h"
 
27
#include <drizzled/error.h>
 
28
#include "drizzled/charset_info.h"
 
29
#include "drizzled/internal/my_sys.h"
 
30
#include <drizzled/my_hash.h>
 
31
#include "drizzled/my_error.h"
 
32
#include <drizzled/plugin.h>
 
33
#include <drizzled/field.h>
 
34
#include <drizzled/table.h>
 
35
#include <drizzled/plugin/info_schema_table.h>
 
36
#include <drizzled/time_functions.h>
 
37
#include "drizzled/global_charset_info.h"
 
38
 
 
39
 
 
40
#include "i_s.h"
 
41
 
 
42
 
 
43
extern "C" {
 
44
#include "trx0i_s.h"
 
45
#include "trx0trx.h" /* for TRX_QUE_STATE_STR_MAX_LEN */
 
46
#include "buf0buddy.h" /* for i_s_cmpmem */
 
47
#include "buf0buf.h" /* for buf_pool and PAGE_ZIP_MIN_SIZE */
 
48
#include "ha_prototypes.h" /* for innobase_convert_name() */
 
49
#include "srv0start.h" /* for srv_was_started */
 
50
}
 
51
#include "handler0vars.h"
 
52
 
 
53
using namespace drizzled;
 
54
 
 
55
static const char plugin_author[] = "Innobase Oy";
 
56
 
 
57
#define OK(expr)                \
 
58
        if ((expr) != 0) {      \
 
59
                return(1);      \
 
60
        }
 
61
 
 
62
#define RETURN_IF_INNODB_NOT_STARTED(plugin_name)                       \
 
63
do {                                                                    \
 
64
        if (!srv_was_started) {                                         \
 
65
                push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,    \
 
66
                                    ER_CANT_FIND_SYSTEM_REC,            \
 
67
                                    "InnoDB: SELECTing from "           \
 
68
                                    "INFORMATION_SCHEMA.%s but "        \
 
69
                                    "the InnoDB storage engine "        \
 
70
                                    "is not installed", plugin_name);   \
 
71
                return(0);                                              \
 
72
        }                                                               \
 
73
} while (0)
 
74
 
 
75
#define STRUCT_FLD(name, value) value
 
76
 
 
77
plugin::InfoSchemaTable *innodb_trx_schema_table= NULL;
 
78
plugin::InfoSchemaTable *innodb_locks_schema_table= NULL;
 
79
plugin::InfoSchemaTable *innodb_lock_waits_schema_table= NULL;
 
80
plugin::InfoSchemaTable *innodb_cmp_schema_table= NULL;
 
81
plugin::InfoSchemaTable *innodb_cmp_reset_schema_table= NULL;
 
82
plugin::InfoSchemaTable *innodb_cmpmem_schema_table= NULL;
 
83
plugin::InfoSchemaTable *innodb_cmpmem_reset_schema_table= NULL;
 
84
 
 
85
static TrxISMethods trx_methods;
 
86
static CmpISMethods cmp_methods;
 
87
static CmpResetISMethods cmp_reset_methods;
 
88
static CmpmemISMethods cmpmem_methods;
 
89
static CmpmemResetISMethods cmpmem_reset_methods;
 
90
 
 
91
/*
 
92
Use the following types mapping:
 
93
 
 
94
C type  ST_FIELD_INFO::field_type
 
95
---------------------------------
 
96
long                    DRIZZLE_TYPE_LONGLONG
 
97
(field_length=MY_INT64_NUM_DECIMAL_DIGITS)
 
98
 
 
99
long unsigned           DRIZZLE_TYPE_LONGLONG
 
100
(field_length=MY_INT64_NUM_DECIMAL_DIGITS, field_flags=MY_I_S_UNSIGNED)
 
101
 
 
102
char*                   DRIZZLE_TYPE_STRING
 
103
(field_length=n)
 
104
 
 
105
float                   DRIZZLE_TYPE_FLOAT
 
106
(field_length=0 is ignored)
 
107
 
 
108
void*                   DRIZZLE_TYPE_LONGLONG
 
109
(field_length=MY_INT64_NUM_DECIMAL_DIGITS, field_flags=MY_I_S_UNSIGNED)
 
110
 
 
111
boolean (if else)       DRIZZLE_TYPE_LONG
 
112
(field_length=1)
 
113
 
 
114
time_t                  DRIZZLE_TYPE_DATETIME
 
115
(field_length=0 ignored)
 
116
---------------------------------
 
117
*/
 
118
 
 
119
/*******************************************************************//**
 
120
Auxiliary function to store time_t value in MYSQL_TYPE_DATETIME
 
121
field.
 
122
@return 0 on success */
 
123
static
 
124
int
 
125
field_store_time_t(
 
126
/*===============*/
 
127
        Field*  field,  /*!< in/out: target field for storage */
 
128
        time_t  time)   /*!< in: value to store */
 
129
{
 
130
        DRIZZLE_TIME    my_time;
 
131
        struct tm       tm_time;
 
132
 
 
133
#if 0
 
134
        /* use this if you are sure that `variables' and `time_zone'
 
135
        are always initialized */
 
136
        session->variables.time_zone->gmt_sec_to_TIME(
 
137
                &my_time, (time_t) time);
 
138
#else
 
139
        localtime_r(&time, &tm_time);
 
140
        localtime_to_TIME(&my_time, &tm_time);
 
141
        my_time.time_type = DRIZZLE_TIMESTAMP_DATETIME;
 
142
#endif
 
143
 
 
144
        return(field->store_time(&my_time, DRIZZLE_TIMESTAMP_DATETIME));
 
145
}
 
146
 
 
147
/*******************************************************************//**
 
148
Auxiliary function to store char* value in MYSQL_TYPE_STRING field.
 
149
@return 0 on success */
 
150
static
 
151
int
 
152
field_store_string(
 
153
/*===============*/
 
154
        Field*          field,  /*!< in/out: target field for storage */
 
155
        const char*     str)    /*!< in: NUL-terminated utf-8 string,
 
156
                                or NULL */
 
157
{
 
158
        int     ret;
 
159
 
 
160
        if (str != NULL) {
 
161
 
 
162
                ret = field->store(str, strlen(str),
 
163
                                   system_charset_info);
 
164
                field->set_notnull();
 
165
        } else {
 
166
 
 
167
                ret = 0; /* success */
 
168
                field->set_null();
 
169
        }
 
170
 
 
171
        return(ret);
 
172
}
 
173
 
 
174
/*******************************************************************//**
 
175
Auxiliary function to store ulint value in DRIZZLE_TYPE_LONGLONG field.
 
176
If the value is ULINT_UNDEFINED then the field it set to NULL.
 
177
@return 0 on success */
 
178
static
 
179
int
 
180
field_store_ulint(
 
181
/*==============*/
 
182
        Field*  field,  /*!< in/out: target field for storage */
 
183
        ulint   n)      /*!< in: value to store */
 
184
{
 
185
        int     ret;
 
186
 
 
187
        if (n != ULINT_UNDEFINED) {
 
188
 
 
189
                ret = field->store(n);
 
190
                field->set_notnull();
 
191
        } else {
 
192
 
 
193
                ret = 0; /* success */
 
194
                field->set_null();
 
195
        }
 
196
 
 
197
        return(ret);
 
198
}
 
199
 
 
200
/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_trx */
 
201
static plugin::ColumnInfo       innodb_trx_fields_info[] =
 
202
{
 
203
#define IDX_TRX_ID              0
 
204
        plugin::ColumnInfo("trx_id",
 
205
                  TRX_ID_MAX_LEN + 1,
 
206
                  DRIZZLE_TYPE_VARCHAR,
 
207
                  0,
 
208
                  0,
 
209
                  ""),
 
210
 
 
211
#define IDX_TRX_STATE           1
 
212
        plugin::ColumnInfo("trx_state",
 
213
                  TRX_QUE_STATE_STR_MAX_LEN + 1,
 
214
                  DRIZZLE_TYPE_VARCHAR,
 
215
                  0,
 
216
                  0,
 
217
                  ""),
 
218
 
 
219
#define IDX_TRX_STARTED         2
 
220
        plugin::ColumnInfo("trx_started",
 
221
                  0,
 
222
                  DRIZZLE_TYPE_DATETIME,
 
223
                  0,
 
224
                  0,
 
225
                  ""),
 
226
 
 
227
#define IDX_TRX_REQUESTED_LOCK_ID       3
 
228
        plugin::ColumnInfo("trx_requested_lock_id",
 
229
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
 
230
                  DRIZZLE_TYPE_VARCHAR,
 
231
                  0,
 
232
                  MY_I_S_MAYBE_NULL,
 
233
                  ""),
 
234
 
 
235
#define IDX_TRX_WAIT_STARTED    4
 
236
        plugin::ColumnInfo("trx_wait_started",
 
237
                  0,
 
238
                  DRIZZLE_TYPE_DATETIME,
 
239
                  0,
 
240
                  MY_I_S_MAYBE_NULL,
 
241
                  ""),
 
242
 
 
243
#define IDX_TRX_WEIGHT          5
 
244
        plugin::ColumnInfo("trx_weight",
 
245
                  MY_INT64_NUM_DECIMAL_DIGITS,
 
246
                  DRIZZLE_TYPE_LONGLONG,
 
247
                  0,
 
248
                  MY_I_S_UNSIGNED,
 
249
                  ""),
 
250
 
 
251
#define IDX_TRX_DRIZZLE_THREAD_ID       6
 
252
        plugin::ColumnInfo("trx_mysql_thread_id",
 
253
                  MY_INT64_NUM_DECIMAL_DIGITS,
 
254
                  DRIZZLE_TYPE_LONGLONG,
 
255
                  0,
 
256
                  MY_I_S_UNSIGNED,
 
257
                  ""),
 
258
 
 
259
#define IDX_TRX_QUERY           7
 
260
        plugin::ColumnInfo("trx_query",
 
261
                  TRX_I_S_TRX_QUERY_MAX_LEN,
 
262
                  DRIZZLE_TYPE_VARCHAR,
 
263
                  0,
 
264
                  MY_I_S_MAYBE_NULL,
 
265
                  ""),
 
266
 
 
267
        plugin::ColumnInfo()
 
268
};
 
269
 
 
270
/*******************************************************************//**
 
271
Read data from cache buffer and fill the INFORMATION_SCHEMA.innodb_trx
 
272
table with it.
 
273
@return 0 on success */
 
274
static
 
275
int
 
276
fill_innodb_trx_from_cache(
 
277
/*=======================*/
 
278
        trx_i_s_cache_t*        cache,  /*!< in: cache to read from */
 
279
        Table*                  table,  /*!< in/out: fill this table */
 
280
        plugin::InfoSchemaTable *schema_table)
 
281
{
 
282
        Field** fields;
 
283
        ulint   rows_num;
 
284
        char    lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
 
285
        ulint   i;
 
286
 
 
287
        fields = table->field;
 
288
 
 
289
        rows_num = trx_i_s_cache_get_rows_used(cache,
 
290
                                               I_S_INNODB_TRX);
 
291
 
 
292
        for (i = 0; i < rows_num; i++) {
 
293
 
 
294
                i_s_trx_row_t*  row;
 
295
                char            trx_id[TRX_ID_MAX_LEN + 1];
 
296
 
 
297
                row = (i_s_trx_row_t*)
 
298
                        trx_i_s_cache_get_nth_row(
 
299
                                cache, I_S_INNODB_TRX, i);
 
300
 
 
301
                /* trx_id */
 
302
                ut_snprintf(trx_id, sizeof(trx_id), TRX_ID_FMT, row->trx_id);
 
303
                OK(field_store_string(fields[IDX_TRX_ID], trx_id));
 
304
 
 
305
                /* trx_state */
 
306
                OK(field_store_string(fields[IDX_TRX_STATE],
 
307
                                      row->trx_state));
 
308
 
 
309
                /* trx_started */
 
310
                OK(field_store_time_t(fields[IDX_TRX_STARTED],
 
311
                                      (time_t) row->trx_started));
 
312
 
 
313
                /* trx_requested_lock_id */
 
314
                /* trx_wait_started */
 
315
                if (row->trx_wait_started != 0) {
 
316
 
 
317
                        OK(field_store_string(
 
318
                                   fields[IDX_TRX_REQUESTED_LOCK_ID],
 
319
                                   trx_i_s_create_lock_id(
 
320
                                           row->requested_lock_row,
 
321
                                           lock_id, sizeof(lock_id))));
 
322
                        /* field_store_string() sets it no notnull */
 
323
 
 
324
                        OK(field_store_time_t(
 
325
                                   fields[IDX_TRX_WAIT_STARTED],
 
326
                                   (time_t) row->trx_wait_started));
 
327
                        fields[IDX_TRX_WAIT_STARTED]->set_notnull();
 
328
                } else {
 
329
 
 
330
                        fields[IDX_TRX_REQUESTED_LOCK_ID]->set_null();
 
331
                        fields[IDX_TRX_WAIT_STARTED]->set_null();
 
332
                }
 
333
 
 
334
                /* trx_weight */
 
335
                OK(fields[IDX_TRX_WEIGHT]->store((int64_t) row->trx_weight,
 
336
                                                 true));
 
337
 
 
338
                /* trx_mysql_thread_id */
 
339
                OK(fields[IDX_TRX_DRIZZLE_THREAD_ID]->store(
 
340
                           row->trx_mysql_thread_id));
 
341
 
 
342
                /* trx_query */
 
343
                OK(field_store_string(fields[IDX_TRX_QUERY],
 
344
                                      row->trx_query));
 
345
 
 
346
                schema_table->addRow(table->record[0],
 
347
                                     table->s->reclength);
 
348
        }
 
349
 
 
350
        return(0);
 
351
}
 
352
 
 
353
/*******************************************************************//**
 
354
Bind the dynamic table INFORMATION_SCHEMA.innodb_trx
 
355
@return 0 on success */
 
356
int
 
357
innodb_trx_init()
 
358
/*============*/
 
359
{
 
360
        if ((innodb_trx_schema_table= new plugin::InfoSchemaTable("INNODB_TRX")) == NULL)
 
361
                return(1);
 
362
 
 
363
        innodb_trx_schema_table->setColumnInfo(innodb_trx_fields_info);
 
364
        innodb_trx_schema_table->setInfoSchemaMethods(&trx_methods);
 
365
 
 
366
        return(0);
 
367
}
 
368
 
 
369
 
 
370
/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_locks */
 
371
static plugin::ColumnInfo innodb_locks_fields_info[] =
 
372
{
 
373
#define IDX_LOCK_ID             0
 
374
        plugin::ColumnInfo("lock_id",
 
375
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
 
376
                  DRIZZLE_TYPE_VARCHAR,
 
377
                  0,
 
378
                  0,
 
379
                  ""),
 
380
 
 
381
#define IDX_LOCK_TRX_ID         1
 
382
        plugin::ColumnInfo("lock_trx_id",
 
383
                  TRX_ID_MAX_LEN + 1,
 
384
                  DRIZZLE_TYPE_VARCHAR,
 
385
                  0,
 
386
                  0,
 
387
                  ""),
 
388
 
 
389
#define IDX_LOCK_MODE           2
 
390
        plugin::ColumnInfo("lock_mode",
 
391
         /* S[,GAP] X[,GAP] IS[,GAP] IX[,GAP] AUTO_INC UNKNOWN */
 
392
                  32,
 
393
                  DRIZZLE_TYPE_VARCHAR,
 
394
                  0,
 
395
                  0,
 
396
                  ""),
 
397
 
 
398
#define IDX_LOCK_TYPE           3
 
399
        plugin::ColumnInfo("lock_type",
 
400
                  32, /* RECORD|TABLE|UNKNOWN */
 
401
                  DRIZZLE_TYPE_VARCHAR,
 
402
                  0,
 
403
                  0,
 
404
                  ""),
 
405
 
 
406
#define IDX_LOCK_TABLE          4
 
407
        plugin::ColumnInfo("lock_table",
 
408
                  1024,
 
409
                  DRIZZLE_TYPE_VARCHAR,
 
410
                  0,
 
411
                  0,
 
412
                  ""),
 
413
 
 
414
#define IDX_LOCK_INDEX          5
 
415
        plugin::ColumnInfo("lock_index",
 
416
                  1024,
 
417
                  DRIZZLE_TYPE_VARCHAR,
 
418
                  0,
 
419
                  MY_I_S_MAYBE_NULL,
 
420
                  ""),
 
421
 
 
422
#define IDX_LOCK_SPACE          6
 
423
        plugin::ColumnInfo("lock_space",
 
424
                  MY_INT64_NUM_DECIMAL_DIGITS,
 
425
                  DRIZZLE_TYPE_LONGLONG,
 
426
                  0,
 
427
                  MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL,
 
428
                  ""),
 
429
 
 
430
#define IDX_LOCK_PAGE           7
 
431
        plugin::ColumnInfo("lock_page",
 
432
                  MY_INT64_NUM_DECIMAL_DIGITS,
 
433
                  DRIZZLE_TYPE_LONGLONG,
 
434
                  0,
 
435
                  MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL,
 
436
                  ""),
 
437
 
 
438
#define IDX_LOCK_REC            8
 
439
        plugin::ColumnInfo("lock_rec",
 
440
                  MY_INT64_NUM_DECIMAL_DIGITS,
 
441
                  DRIZZLE_TYPE_LONGLONG,
 
442
                  0,
 
443
                  MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL,
 
444
                  ""),
 
445
 
 
446
#define IDX_LOCK_DATA           9
 
447
        plugin::ColumnInfo("lock_data",
 
448
                  TRX_I_S_LOCK_DATA_MAX_LEN,
 
449
                  DRIZZLE_TYPE_VARCHAR,
 
450
                  0,
 
451
                  MY_I_S_MAYBE_NULL,
 
452
                  ""),
 
453
 
 
454
        plugin::ColumnInfo()
 
455
};
 
456
 
 
457
/*******************************************************************//**
 
458
Read data from cache buffer and fill the INFORMATION_SCHEMA.innodb_locks
 
459
table with it.
 
460
@return 0 on success */
 
461
static
 
462
int
 
463
fill_innodb_locks_from_cache(
 
464
/*=========================*/
 
465
        trx_i_s_cache_t*        cache,  /*!< in: cache to read from */
 
466
        Session*                session,/*!< in: MySQL client connection */
 
467
        Table*                  table,  /*!< in/out: fill this table */
 
468
        plugin::InfoSchemaTable *schema_table)
 
469
{
 
470
        Field** fields;
 
471
        ulint   rows_num;
 
472
        char    lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
 
473
        ulint   i;
 
474
 
 
475
        fields = table->field;
 
476
 
 
477
        rows_num = trx_i_s_cache_get_rows_used(cache,
 
478
                                               I_S_INNODB_LOCKS);
 
479
 
 
480
        for (i = 0; i < rows_num; i++) {
 
481
 
 
482
                i_s_locks_row_t*        row;
 
483
 
 
484
                /* note that the decoded database or table name is
 
485
                never expected to be longer than NAME_LEN;
 
486
                NAME_LEN for database name
 
487
                2 for surrounding quotes around database name
 
488
                NAME_LEN for table name
 
489
                2 for surrounding quotes around table name
 
490
                1 for the separating dot (.)
 
491
                9 for the #mysql50# prefix */
 
492
                char                    buf[2 * NAME_LEN + 14];
 
493
                const char*             bufend;
 
494
 
 
495
                char                    lock_trx_id[TRX_ID_MAX_LEN + 1];
 
496
 
 
497
                row = (i_s_locks_row_t*)
 
498
                        trx_i_s_cache_get_nth_row(
 
499
                                cache, I_S_INNODB_LOCKS, i);
 
500
 
 
501
                /* lock_id */
 
502
                trx_i_s_create_lock_id(row, lock_id, sizeof(lock_id));
 
503
                OK(field_store_string(fields[IDX_LOCK_ID],
 
504
                                      lock_id));
 
505
 
 
506
                /* lock_trx_id */
 
507
                ut_snprintf(lock_trx_id, sizeof(lock_trx_id),
 
508
                            TRX_ID_FMT, row->lock_trx_id);
 
509
                OK(field_store_string(fields[IDX_LOCK_TRX_ID], lock_trx_id));
 
510
 
 
511
                /* lock_mode */
 
512
                OK(field_store_string(fields[IDX_LOCK_MODE],
 
513
                                      row->lock_mode));
 
514
 
 
515
                /* lock_type */
 
516
                OK(field_store_string(fields[IDX_LOCK_TYPE],
 
517
                                      row->lock_type));
 
518
 
 
519
                /* lock_table */
 
520
                bufend = innobase_convert_name(buf, sizeof(buf),
 
521
                                               row->lock_table,
 
522
                                               strlen(row->lock_table),
 
523
                                               session, TRUE);
 
524
                OK(fields[IDX_LOCK_TABLE]->store(buf, bufend - buf,
 
525
                                                 system_charset_info));
 
526
 
 
527
                /* lock_index */
 
528
                if (row->lock_index != NULL) {
 
529
 
 
530
                        bufend = innobase_convert_name(buf, sizeof(buf),
 
531
                                                       row->lock_index,
 
532
                                                       strlen(row->lock_index),
 
533
                                                       session, FALSE);
 
534
                        OK(fields[IDX_LOCK_INDEX]->store(buf, bufend - buf,
 
535
                                                         system_charset_info));
 
536
                        fields[IDX_LOCK_INDEX]->set_notnull();
 
537
                } else {
 
538
 
 
539
                        fields[IDX_LOCK_INDEX]->set_null();
 
540
                }
 
541
 
 
542
                /* lock_space */
 
543
                OK(field_store_ulint(fields[IDX_LOCK_SPACE],
 
544
                                     row->lock_space));
 
545
 
 
546
                /* lock_page */
 
547
                OK(field_store_ulint(fields[IDX_LOCK_PAGE],
 
548
                                     row->lock_page));
 
549
 
 
550
                /* lock_rec */
 
551
                OK(field_store_ulint(fields[IDX_LOCK_REC],
 
552
                                     row->lock_rec));
 
553
 
 
554
                /* lock_data */
 
555
                OK(field_store_string(fields[IDX_LOCK_DATA],
 
556
                                      row->lock_data));
 
557
 
 
558
                schema_table->addRow(table->record[0],
 
559
                                     table->s->reclength);
 
560
        }
 
561
 
 
562
        return(0);
 
563
}
 
564
 
 
565
/*******************************************************************//**
 
566
Bind the dynamic table INFORMATION_SCHEMA.innodb_locks
 
567
@return 0 on success */
 
568
int
 
569
innodb_locks_init()
 
570
/*==============*/
 
571
{
 
572
 
 
573
        if ((innodb_locks_schema_table= new plugin::InfoSchemaTable("INNODB_LOCKS")) == NULL)
 
574
                return(1);
 
575
 
 
576
        innodb_locks_schema_table->setColumnInfo(innodb_locks_fields_info);
 
577
        innodb_locks_schema_table->setInfoSchemaMethods(&trx_methods);
 
578
        return(0);
 
579
}
 
580
 
 
581
 
 
582
/* Fields of the dynamic table INFORMATION_SCHEMA.innodb_lock_waits */
 
583
static plugin::ColumnInfo innodb_lock_waits_fields_info[] =
 
584
{
 
585
#define IDX_REQUESTING_TRX_ID   0
 
586
        plugin::ColumnInfo("requesting_trx_id",
 
587
                  TRX_ID_MAX_LEN + 1,
 
588
                  DRIZZLE_TYPE_VARCHAR,
 
589
                  0,
 
590
                  0,
 
591
                  ""),
 
592
 
 
593
#define IDX_REQUESTED_LOCK_ID   1
 
594
        plugin::ColumnInfo("requested_lock_id",
 
595
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
 
596
                  DRIZZLE_TYPE_VARCHAR,
 
597
                  0,
 
598
                  0,
 
599
                  ""),
 
600
 
 
601
#define IDX_BLOCKING_TRX_ID     2
 
602
        plugin::ColumnInfo("blocking_trx_id",
 
603
                  TRX_ID_MAX_LEN + 1,
 
604
                  DRIZZLE_TYPE_VARCHAR,
 
605
                  0,
 
606
                  0,
 
607
                  ""),
 
608
 
 
609
#define IDX_BLOCKING_LOCK_ID    3
 
610
        plugin::ColumnInfo("blocking_lock_id",
 
611
                  TRX_I_S_LOCK_ID_MAX_LEN + 1,
 
612
                  DRIZZLE_TYPE_VARCHAR,
 
613
                  0,
 
614
                  0,
 
615
                  ""),
 
616
 
 
617
        plugin::ColumnInfo()
 
618
};
 
619
 
 
620
/*******************************************************************//**
 
621
Read data from cache buffer and fill the
 
622
INFORMATION_SCHEMA.innodb_lock_waits table with it.
 
623
@return 0 on success */
 
624
static
 
625
int
 
626
fill_innodb_lock_waits_from_cache(
 
627
/*==============================*/
 
628
        trx_i_s_cache_t*        cache,  /*!< in: cache to read from */
 
629
        Table*                  table,  /*!< in/out: fill this table */
 
630
        plugin::InfoSchemaTable *schema_table)
 
631
{
 
632
        Field** fields;
 
633
        ulint   rows_num;
 
634
        char    requested_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
 
635
        char    blocking_lock_id[TRX_I_S_LOCK_ID_MAX_LEN + 1];
 
636
        ulint   i;
 
637
 
 
638
        fields = table->field;
 
639
 
 
640
        rows_num = trx_i_s_cache_get_rows_used(cache,
 
641
                                               I_S_INNODB_LOCK_WAITS);
 
642
 
 
643
        for (i = 0; i < rows_num; i++) {
 
644
 
 
645
                i_s_lock_waits_row_t*   row;
 
646
 
 
647
                char    requesting_trx_id[TRX_ID_MAX_LEN + 1];
 
648
                char    blocking_trx_id[TRX_ID_MAX_LEN + 1];
 
649
 
 
650
                row = (i_s_lock_waits_row_t*)
 
651
                        trx_i_s_cache_get_nth_row(
 
652
                                cache, I_S_INNODB_LOCK_WAITS, i);
 
653
 
 
654
                /* requesting_trx_id */
 
655
                ut_snprintf(requesting_trx_id, sizeof(requesting_trx_id),
 
656
                            TRX_ID_FMT, row->requested_lock_row->lock_trx_id);
 
657
                OK(field_store_string(fields[IDX_REQUESTING_TRX_ID],
 
658
                                      requesting_trx_id));
 
659
 
 
660
                /* requested_lock_id */
 
661
                OK(field_store_string(
 
662
                           fields[IDX_REQUESTED_LOCK_ID],
 
663
                           trx_i_s_create_lock_id(
 
664
                                   row->requested_lock_row,
 
665
                                   requested_lock_id,
 
666
                                   sizeof(requested_lock_id))));
 
667
 
 
668
                /* blocking_trx_id */
 
669
                ut_snprintf(blocking_trx_id, sizeof(blocking_trx_id),
 
670
                            TRX_ID_FMT, row->blocking_lock_row->lock_trx_id);
 
671
                OK(field_store_string(fields[IDX_BLOCKING_TRX_ID],
 
672
                                      blocking_trx_id));
 
673
 
 
674
                /* blocking_lock_id */
 
675
                OK(field_store_string(
 
676
                           fields[IDX_BLOCKING_LOCK_ID],
 
677
                           trx_i_s_create_lock_id(
 
678
                                   row->blocking_lock_row,
 
679
                                   blocking_lock_id,
 
680
                                   sizeof(blocking_lock_id))));
 
681
 
 
682
                schema_table->addRow(table->record[0],
 
683
                                     table->s->reclength);
 
684
        }
 
685
 
 
686
        return(0);
 
687
}
 
688
 
 
689
/*******************************************************************//**
 
690
Bind the dynamic table INFORMATION_SCHEMA.innodb_lock_waits
 
691
@return 0 on success */
 
692
int
 
693
innodb_lock_waits_init()
 
694
/*===================*/
 
695
{
 
696
 
 
697
        if ((innodb_lock_waits_schema_table= new plugin::InfoSchemaTable("INNODB_LOCK_WAITS")) == NULL)
 
698
                return(1);
 
699
 
 
700
        innodb_lock_waits_schema_table->setColumnInfo(innodb_lock_waits_fields_info);
 
701
        innodb_lock_waits_schema_table->setInfoSchemaMethods(&trx_methods);
 
702
 
 
703
 
 
704
        return(0);
 
705
}
 
706
 
 
707
 
 
708
/*******************************************************************//**
 
709
Common function to fill any of the dynamic tables:
 
710
INFORMATION_SCHEMA.innodb_trx
 
711
INFORMATION_SCHEMA.innodb_locks
 
712
INFORMATION_SCHEMA.innodb_lock_waits
 
713
@return 0 on success */
 
714
int
 
715
TrxISMethods::fillTable(
 
716
/*======================*/
 
717
        Session*        session,/*!< in: thread */
 
718
        Table*  table,  /*!< in/out: tables to fill */
 
719
        plugin::InfoSchemaTable *schema_table)
 
720
{
 
721
        const char*             table_name;
 
722
        int                     ret;
 
723
        trx_i_s_cache_t*        cache;
 
724
 
 
725
        /* minimize the number of places where global variables are
 
726
        referenced */
 
727
        cache = trx_i_s_cache;
 
728
 
 
729
        /* which table we have to fill? */
 
730
        table_name = schema_table->getName().c_str();
 
731
        /* or table_name = tables->schema_table->table_name; */
 
732
 
 
733
        RETURN_IF_INNODB_NOT_STARTED(table_name);
 
734
 
 
735
        /* update the cache */
 
736
        trx_i_s_cache_start_write(cache);
 
737
        trx_i_s_possibly_fetch_data_into_cache(cache);
 
738
        trx_i_s_cache_end_write(cache);
 
739
 
 
740
        if (trx_i_s_cache_is_truncated(cache)) {
 
741
 
 
742
                /* XXX show warning to user if possible */
 
743
                fprintf(stderr, "Warning: data in %s truncated due to "
 
744
                        "memory limit of %d bytes\n", table_name,
 
745
                        TRX_I_S_MEM_LIMIT);
 
746
        }
 
747
 
 
748
        ret = 0;
 
749
 
 
750
        trx_i_s_cache_start_read(cache);
 
751
 
 
752
        if (innobase_strcasecmp(table_name, "innodb_trx") == 0) {
 
753
 
 
754
                if (fill_innodb_trx_from_cache(
 
755
                        cache, table, schema_table) != 0) {
 
756
 
 
757
                        ret = 1;
 
758
                }
 
759
 
 
760
        } else if (innobase_strcasecmp(table_name, "innodb_locks") == 0) {
 
761
 
 
762
                if (fill_innodb_locks_from_cache(
 
763
                        cache, session, table, schema_table) != 0) {
 
764
 
 
765
                        ret = 1;
 
766
                }
 
767
 
 
768
        } else if (innobase_strcasecmp(table_name, "innodb_lock_waits") == 0) {
 
769
 
 
770
                if (fill_innodb_lock_waits_from_cache(
 
771
                        cache, table, schema_table) != 0) {
 
772
 
 
773
                        ret = 1;
 
774
                }
 
775
 
 
776
        } else {
 
777
 
 
778
                /* huh! what happened!? */
 
779
                fprintf(stderr,
 
780
                        "InnoDB: trx_i_s_common_fill_table() was "
 
781
                        "called to fill unknown table: %s.\n"
 
782
                        "This function only knows how to fill "
 
783
                        "innodb_trx, innodb_locks and "
 
784
                        "innodb_lock_waits tables.\n", table_name);
 
785
 
 
786
                ret = 1;
 
787
        }
 
788
 
 
789
        trx_i_s_cache_end_read(cache);
 
790
 
 
791
#if 0
 
792
        return(ret);
 
793
#else
 
794
        /* if this function returns something else than 0 then a
 
795
        deadlock occurs between the mysqld server and mysql client,
 
796
        see http://bugs.mysql.com/29900 ; when that bug is resolved
 
797
        we can enable the return(ret) above */
 
798
        return(0);
 
799
#endif
 
800
}
 
801
 
 
802
/* Fields of the dynamic table information_schema.innodb_cmp. */
 
803
static plugin::ColumnInfo       i_s_cmp_fields_info[] =
 
804
{
 
805
        plugin::ColumnInfo("page_size",
 
806
                  5,
 
807
                  DRIZZLE_TYPE_LONG,
 
808
                  0,
 
809
                  0,
 
810
                  "Compressed Page Size"),
 
811
 
 
812
        plugin::ColumnInfo("compress_ops",
 
813
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
814
                  DRIZZLE_TYPE_LONG,
 
815
                  0,
 
816
                  0,
 
817
                  "Total Number of Compressions"),
 
818
 
 
819
        plugin::ColumnInfo("compress_ops_ok",
 
820
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
821
                  DRIZZLE_TYPE_LONG,
 
822
                  0,
 
823
                  0,
 
824
                  "Total Number of Successful Compressions"),
 
825
 
 
826
        plugin::ColumnInfo("compress_time",
 
827
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
828
                  DRIZZLE_TYPE_LONG,
 
829
                  0,
 
830
                  0,
 
831
                  "Total Duration of Compressions in Seconds"),
 
832
 
 
833
        plugin::ColumnInfo("uncompress_ops",
 
834
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
835
                  DRIZZLE_TYPE_LONG,
 
836
                  0,
 
837
                  0,
 
838
                  "Total Number of Decompressions"),
 
839
 
 
840
        plugin::ColumnInfo("uncompress_time",
 
841
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
842
                  DRIZZLE_TYPE_LONG,
 
843
                  0,
 
844
                  0,
 
845
                  "Total Duration of Decompressions in Seconds"),
 
846
 
 
847
        plugin::ColumnInfo()
 
848
};
 
849
 
 
850
 
 
851
/*******************************************************************//**
 
852
Fill the dynamic table information_schema.innodb_cmp or
 
853
innodb_cmp_reset.
 
854
@return 0 on success, 1 on failure */
 
855
static
 
856
int
 
857
i_s_cmp_fill_low(
 
858
/*=============*/
 
859
        Session*        session,/*!< in: thread */
 
860
        Table*  table,  /*!< in/out: tables to fill */
 
861
        plugin::InfoSchemaTable *schema_table,
 
862
        ibool           reset)  /*!< in: TRUE=reset cumulated counts */
 
863
{
 
864
        int     status  = 0;
 
865
 
 
866
 
 
867
        RETURN_IF_INNODB_NOT_STARTED(schema_table->getName().c_str());
 
868
 
 
869
        for (uint i = 0; i < PAGE_ZIP_NUM_SSIZE - 1; i++) {
 
870
                page_zip_stat_t*        zip_stat = &page_zip_stat[i];
 
871
 
 
872
                table->field[0]->store(PAGE_ZIP_MIN_SIZE << i);
 
873
 
 
874
                /* The cumulated counts are not protected by any
 
875
                mutex.  Thus, some operation in page0zip.c could
 
876
                increment a counter between the time we read it and
 
877
                clear it.  We could introduce mutex protection, but it
 
878
                could cause a measureable performance hit in
 
879
                page0zip.c. */
 
880
                table->field[1]->store(zip_stat->compressed);
 
881
                table->field[2]->store(zip_stat->compressed_ok);
 
882
                table->field[3]->store(
 
883
                        (ulong) (zip_stat->compressed_usec / 1000000));
 
884
                table->field[4]->store(zip_stat->decompressed);
 
885
                table->field[5]->store(
 
886
                        (ulong) (zip_stat->decompressed_usec / 1000000));
 
887
 
 
888
                if (reset) {
 
889
                        memset(zip_stat, 0, sizeof *zip_stat);
 
890
                }
 
891
 
 
892
                schema_table->addRow(table->record[0],
 
893
                                     table->s->reclength);
 
894
        }
 
895
 
 
896
        return(status);
 
897
}
 
898
 
 
899
/*******************************************************************//**
 
900
Fill the dynamic table information_schema.innodb_cmp.
 
901
@return 0 on success, 1 on failure */
 
902
int
 
903
CmpISMethods::fillTable(
 
904
/*=========*/
 
905
        Session*        session,/*!< in: thread */
 
906
        Table*  table,  /*!< in/out: tables to fill */
 
907
        plugin::InfoSchemaTable *schema_table)
 
908
{
 
909
        return(i_s_cmp_fill_low(session, table, schema_table, FALSE));
 
910
}
 
911
 
 
912
/*******************************************************************//**
 
913
Fill the dynamic table information_schema.innodb_cmp_reset.
 
914
@return 0 on success, 1 on failure */
 
915
int
 
916
CmpResetISMethods::fillTable(
 
917
/*===============*/
 
918
        Session*        session,/*!< in: thread */
 
919
        Table*  table,  /*!< in/out: tables to fill */
 
920
        plugin::InfoSchemaTable *schema_table)
 
921
{
 
922
        return(i_s_cmp_fill_low(session, table, schema_table, TRUE));
 
923
}
 
924
 
 
925
/*******************************************************************//**
 
926
Bind the dynamic table information_schema.innodb_cmp.
 
927
@return 0 on success */
 
928
int
 
929
i_s_cmp_init()
 
930
/*=========*/
 
931
{
 
932
 
 
933
        if ((innodb_cmp_schema_table= new plugin::InfoSchemaTable("INNODB_CMP")) == NULL)
 
934
                return(1);
 
935
 
 
936
        innodb_cmp_schema_table->setColumnInfo(i_s_cmp_fields_info);
 
937
        innodb_cmp_schema_table->setInfoSchemaMethods(&cmp_methods);
 
938
 
 
939
        return(0);
 
940
}
 
941
 
 
942
/*******************************************************************//**
 
943
Bind the dynamic table information_schema.innodb_cmp_reset.
 
944
@return 0 on success */
 
945
int
 
946
i_s_cmp_reset_init()
 
947
/*===============*/
 
948
{
 
949
 
 
950
        if ((innodb_cmp_reset_schema_table= new plugin::InfoSchemaTable("INNODB_CMP_RESET")) == NULL)
 
951
                return(1);
 
952
 
 
953
        innodb_cmp_reset_schema_table->setColumnInfo(i_s_cmp_fields_info);
 
954
        innodb_cmp_reset_schema_table->setInfoSchemaMethods(&cmp_reset_methods);
 
955
 
 
956
        return(0);
 
957
}
 
958
 
 
959
 
 
960
 
 
961
/* Fields of the dynamic table information_schema.innodb_cmpmem. */
 
962
static plugin::ColumnInfo       i_s_cmpmem_fields_info[] =
 
963
{
 
964
        plugin::ColumnInfo("page_size",
 
965
                  5,
 
966
                  DRIZZLE_TYPE_LONG,
 
967
                  0,
 
968
                  0,
 
969
                  "Buddy Block Size"),
 
970
 
 
971
        plugin::ColumnInfo("pages_used",
 
972
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
973
                  DRIZZLE_TYPE_LONG,
 
974
                  0,
 
975
                  0,
 
976
                  "Currently in Use"),
 
977
 
 
978
        plugin::ColumnInfo("pages_free",
 
979
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
980
                  DRIZZLE_TYPE_LONG,
 
981
                  0,
 
982
                  0,
 
983
                  "Currently Available"),
 
984
 
 
985
        plugin::ColumnInfo("relocation_ops",
 
986
                  MY_INT64_NUM_DECIMAL_DIGITS,
 
987
                  DRIZZLE_TYPE_LONGLONG,
 
988
                  0,
 
989
                  0,
 
990
                  "Total Number of Relocations"),
 
991
 
 
992
        plugin::ColumnInfo("relocation_time",
 
993
                  MY_INT32_NUM_DECIMAL_DIGITS,
 
994
                  DRIZZLE_TYPE_LONG,
 
995
                  0,
 
996
                  0,
 
997
                  "Total Duration of Relocations, in Seconds"),
 
998
 
 
999
        plugin::ColumnInfo()
 
1000
};
 
1001
 
 
1002
/*******************************************************************//**
 
1003
Fill the dynamic table information_schema.innodb_cmpmem or
 
1004
innodb_cmpmem_reset.
 
1005
@return 0 on success, 1 on failure */
 
1006
static
 
1007
int
 
1008
i_s_cmpmem_fill_low(
 
1009
/*================*/
 
1010
        Session*        session,/*!< in: thread */
 
1011
        Table*  table,  /*!< in/out: tables to fill */
 
1012
        plugin::InfoSchemaTable *schema_table,
 
1013
        ibool           reset)  /*!< in: TRUE=reset cumulated counts */
 
1014
{
 
1015
        int     status  = 0;
 
1016
 
 
1017
        RETURN_IF_INNODB_NOT_STARTED(schema_table->getName().c_str());
 
1018
 
 
1019
        buf_pool_mutex_enter();
 
1020
 
 
1021
        for (uint x = 0; x <= BUF_BUDDY_SIZES; x++) {
 
1022
                buf_buddy_stat_t*       buddy_stat = &buf_buddy_stat[x];
 
1023
 
 
1024
                table->field[0]->store(BUF_BUDDY_LOW << x);
 
1025
                table->field[1]->store(buddy_stat->used);
 
1026
                table->field[2]->store(UNIV_LIKELY(x < BUF_BUDDY_SIZES)
 
1027
                                       ? UT_LIST_GET_LEN(buf_pool->zip_free[x])
 
1028
                                       : 0);
 
1029
                table->field[3]->store((int64_t) buddy_stat->relocated, true);
 
1030
                table->field[4]->store(
 
1031
                        (ulong) (buddy_stat->relocated_usec / 1000000));
 
1032
 
 
1033
                if (reset) {
 
1034
                        /* This is protected by buf_pool_mutex. */
 
1035
                        buddy_stat->relocated = 0;
 
1036
                        buddy_stat->relocated_usec = 0;
 
1037
                }
 
1038
 
 
1039
                schema_table->addRow(table->record[0],
 
1040
                                     table->s->reclength);
 
1041
        }
 
1042
 
 
1043
        buf_pool_mutex_exit();
 
1044
        return(status);
 
1045
}
 
1046
 
 
1047
/*******************************************************************//**
 
1048
Fill the dynamic table information_schema.innodb_cmpmem.
 
1049
@return 0 on success, 1 on failure */
 
1050
int
 
1051
CmpmemISMethods::fillTable(
 
1052
/*============*/
 
1053
        Session*        session,/*!< in: thread */
 
1054
        Table*  table, /*!< in/out: tables to fill */
 
1055
        plugin::InfoSchemaTable *schema_table)  
 
1056
{
 
1057
        return(i_s_cmpmem_fill_low(session, table, schema_table, FALSE));
 
1058
}
 
1059
 
 
1060
/*******************************************************************//**
 
1061
Fill the dynamic table information_schema.innodb_cmpmem_reset.
 
1062
@return 0 on success, 1 on failure */
 
1063
int
 
1064
CmpmemResetISMethods::fillTable(
 
1065
/*==================*/
 
1066
        Session*        session,/*!< in: thread */
 
1067
        Table*  table,  /*!< in/out: tables to fill */
 
1068
        plugin::InfoSchemaTable *schema_table)
 
1069
{
 
1070
        return(i_s_cmpmem_fill_low(session, table, schema_table, TRUE));
 
1071
}
 
1072
 
 
1073
/*******************************************************************//**
 
1074
Bind the dynamic table information_schema.innodb_cmpmem.
 
1075
@return 0 on success */
 
1076
int
 
1077
i_s_cmpmem_init()
 
1078
/*============*/
 
1079
{
 
1080
 
 
1081
        if ((innodb_cmpmem_schema_table= new plugin::InfoSchemaTable("INNODB_CMPMEM")) == NULL)
 
1082
                return(1);
 
1083
 
 
1084
        innodb_cmpmem_schema_table->setColumnInfo(i_s_cmpmem_fields_info);
 
1085
        innodb_cmpmem_schema_table->setInfoSchemaMethods(&cmpmem_methods);
 
1086
 
 
1087
        return(0);
 
1088
}
 
1089
 
 
1090
/*******************************************************************//**
 
1091
Bind the dynamic table information_schema.innodb_cmpmem_reset.
 
1092
@return 0 on success */
 
1093
int
 
1094
i_s_cmpmem_reset_init()
 
1095
/*==================*/
 
1096
{
 
1097
        if ((innodb_cmpmem_reset_schema_table= new plugin::InfoSchemaTable("INNODB_CMPMEM_RESET")) == NULL)
 
1098
                return(1);
 
1099
 
 
1100
        innodb_cmpmem_reset_schema_table->setColumnInfo(i_s_cmpmem_fields_info);
 
1101
        innodb_cmpmem_reset_schema_table->setInfoSchemaMethods(&cmpmem_reset_methods);
 
1102
        return(0);
 
1103
}
 
1104
 
 
1105
 
 
1106
/*******************************************************************//**
 
1107
Unbind a dynamic INFORMATION_SCHEMA table.
 
1108
@return 0 on success */
 
1109
int
 
1110
i_s_common_deinit(
 
1111
/*==============*/
 
1112
        plugin::Registry &registry)     /*!< in/out: table schema object */
 
1113
{
 
1114
        registry.remove(innodb_trx_schema_table);
 
1115
        registry.remove(innodb_locks_schema_table);
 
1116
        registry.remove(innodb_lock_waits_schema_table);
 
1117
        registry.remove(innodb_cmp_schema_table);
 
1118
        registry.remove(innodb_cmp_reset_schema_table);
 
1119
        registry.remove(innodb_cmpmem_schema_table);
 
1120
        registry.remove(innodb_cmpmem_reset_schema_table);
 
1121
 
 
1122
        delete innodb_trx_schema_table;
 
1123
        delete innodb_locks_schema_table;
 
1124
        delete innodb_lock_waits_schema_table;
 
1125
        delete innodb_cmp_schema_table;
 
1126
        delete innodb_cmp_reset_schema_table;
 
1127
        delete innodb_cmpmem_schema_table;
 
1128
        delete innodb_cmpmem_reset_schema_table;
 
1129
 
 
1130
        return(0);
 
1131
}