File: table_stats.cc

package info (click to toggle)
mariadb 1%3A11.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 772,520 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (97 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (2)
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
namespace Show {

static ST_FIELD_INFO table_stats_fields[]=
{
  Column("TABLE_SCHEMA",      Varchar(NAME_LEN), NOT_NULL, "Table_schema"),
  Column("TABLE_NAME",        Varchar(NAME_LEN), NOT_NULL, "Table_name"),
  Column("ROWS_READ",             SLonglong(),   NOT_NULL, "Rows_read"),
  Column("ROWS_CHANGED",          SLonglong(),   NOT_NULL, "Rows_changed"),
  Column("ROWS_CHANGED_X_INDEXES",SLonglong(),   NOT_NULL, "Rows_changed_x_#indexes"),
  Column("ROWS_INSERTED",         SLonglong(),   NOT_NULL, "Rows_inserted"),
  Column("ROWS_UPDATED",          SLonglong(),   NOT_NULL, "Rows_updated"),
  Column("ROWS_DELETED",          SLonglong(),   NOT_NULL, "Rows_deleted"),
  Column("KEY_READ_HITS",         SLonglong(),   NOT_NULL, "Key_read_hits"),
  Column("KEY_READ_MISSES",       SLonglong(),   NOT_NULL, "Key_read_misses"),
  Column("PAGES_ACCESSED",        SLonglong(),   NOT_NULL, "Pages_accessed"),
  Column("PAGES_READ_FROM_DISK",  SLonglong(),   NOT_NULL, "Pages_read_from_disk"),
  CEnd()
};

} // namespace Show

static int table_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
{
  TABLE *table= tables->table;

  mysql_mutex_lock(&LOCK_global_table_stats);
  for (uint i= 0; i < global_table_stats.records; i++)
  {
    char *end_of_schema;
    TABLE_STATS *table_stats=
      (TABLE_STATS*)my_hash_element(&global_table_stats, i);
    TABLE_LIST tmp_table;
    size_t schema_length, table_name_length;
    struct rows_stats *rows_stats= &table_stats->rows_stats;
    ulonglong rows_changed;

    end_of_schema= strend(table_stats->table);
    schema_length= (size_t) (end_of_schema - table_stats->table);
    table_name_length= strlen(table_stats->table + schema_length + 1);

    bzero((char*) &tmp_table,sizeof(tmp_table));
    tmp_table.db.str= table_stats->table;
    tmp_table.db.length= schema_length;
    tmp_table.table_name.str= end_of_schema+1;
    tmp_table.table_name.length= table_name_length;
    tmp_table.grant.privilege= NO_ACL;
    if (check_access(thd, SELECT_ACL, tmp_table.db.str,
                     &tmp_table.grant.privilege, NULL, 0, 1) ||
        check_grant(thd, SELECT_ACL, &tmp_table, 1, 1, 1))
      continue;

    rows_changed= (rows_stats->inserted + rows_stats->updated +
                   rows_stats->deleted);
    table->field[0]->store(table_stats->table, schema_length,
                           system_charset_info);
    table->field[1]->store(table_stats->table + schema_length+1,
                           table_name_length, system_charset_info);
    table->field[2]->store((longlong) rows_stats->read, TRUE);
    table->field[3]->store((longlong) rows_changed, TRUE);
    table->field[4]->store((longlong)table_stats->rows_changed_x_indexes,
                           TRUE);
    table->field[5]->store((longlong) rows_stats->inserted, TRUE);
    table->field[6]->store((longlong) rows_stats->updated, TRUE);
    table->field[7]->store((longlong) rows_stats->deleted, TRUE);
    table->field[8]->store((longlong) rows_stats->key_read_hit, TRUE);
    table->field[9]->store((longlong) rows_stats->key_read_miss, TRUE);
    table->field[10]->store((longlong) rows_stats->pages_accessed, TRUE);
    table->field[11]->store((longlong) rows_stats->pages_read_count, TRUE);

    if (schema_table_store_record(thd, table))
    {
      mysql_mutex_unlock(&LOCK_global_table_stats);
      return 1;
    }
  }
  mysql_mutex_unlock(&LOCK_global_table_stats);
  return 0;
}

static int table_stats_reset()
{
  mysql_mutex_lock(&LOCK_global_table_stats);
  free_global_table_stats();
  init_global_table_stats();
  mysql_mutex_unlock(&LOCK_global_table_stats);
  return 0;
}

static int table_stats_init(void *p)
{
  ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
  schema->fields_info= Show::table_stats_fields;
  schema->fill_table= table_stats_fill;
  schema->reset_table= table_stats_reset;
  return 0;
}