1
0
mirror of https://github.com/MariaDB/server.git synced 2026-10-23 18:49:29 +03:00
Files
mariadb/sql/ha_handler_stats.h
Mohammad Tafzeel Shams 91c4460e25 MDEV-38305: Expose adaptive hash index statistics in ANALYZE FORMAT=JSON
Expose InnoDB's Adaptive Hash Index (AHI) statistics through ANALYZE
FORMAT=JSON output and global status variables to provide query-level
and system-level visibility into AHI usage and effectiveness.
This allows DBAs and developers to monitor how well
the adaptive hash index is serving their workloads on a per-query basis.

The r_ahi_stats object (nested inside r_engine_stats) now reports four
key metrics: ahi_searches (successful AHI lookups), ahi_searches_btree
(AHI misses requiring B-tree fallback), ahi_rows_added (rows inserted
into AHI), and ahi_pages_added (pages indexed by AHI). These same
metrics are exposed globally via innodb_status_variables.

Transaction-local tracking eliminates contention by accumulating
statistics in trx_t fields (n_sea, n_non_sea, n_ahi_rows_added,
n_ahi_pages_added) and aggregating them into global atomic counters
during trx_t::commit_cleanup() and trx_t::free(),
following the pattern established for buf_pool.stat.n_page_gets.

AHI counters are now organized as data members of the btr_sea structure
where they logically belong, using anonymous unions to enable both
atomic writes and direct non-atomic reads.

- btr_ahi_inc_searches(): Increment trx->n_sea when AHI lookup succeeds.
- btr_ahi_inc_searches_btree(): Increment trx->n_non_sea when AHI lookup
  fails and falls back to B-tree search.
- btr_ahi_inc_rows_added(): Increment trx->n_ahi_rows_added when rows
  are added to the adaptive hash index structure.
- btr_ahi_inc_pages_added(): Increment trx->n_ahi_pages_added when new
  pages are indexed by AHI.
- btr_cur_t::search_leaf(): Call btr_ahi_inc_searches() on successful
  AHI hit and btr_ahi_inc_searches_btree() on AHI miss.
- btr_sea: Add four counter members with anonymous unions to allow atomic
  writes and lockless reads: hit_count/hit_count_nonatomic (successful AHI
  lookups), miss_count/miss_count_nonatomic (B-tree searches after AHI miss),
  rows_added/rows_added_nonatomic (rows added to AHI), and
  pages_added/pages_added_nonatomic (pages added to AHI). Also includes
  hit_count_old and miss_count_old variant for computing deltas in monitor
  output.
- btr0cur.h/btr0cur.cc: Remove global declarations and definitions of
  btr_cur_n_sea, btr_cur_n_non_sea, btr_cur_n_sea_old, btr_cur_n_non_sea_old,
  btr_ahi_n_rows_added, and btr_ahi_n_pages_added as they are now data
  members of btr_sea.
- export_var_t: Remove innodb_ahi_hit, innodb_ahi_miss, innodb_ahi_rows_added,
  and innodb_ahi_pages_added members. MySQL status variables now point
  directly to btr_search counters.
- innodb_status_variables[]: Change AHI status variable pointers from
  &export_vars.innodb_ahi_* to &btr_search.*_nonatomic, enabling zero-copy
  reads: adaptive_hash_hash_searches points to &btr_search.hit_count_nonatomic,
  adaptive_hash_non_hash_searches to &btr_search.miss_count_nonatomic,
  adaptive_hash_rows_added to &btr_search.rows_added_nonatomic, and
  adaptive_hash_pages_added to &btr_search.pages_added_nonatomic.
- MONITOR_OVLD_ADAPTIVE_HASH_ROW_ADDED, MONITOR_OVLD_ADAPTIVE_HASH_PAGE_ADDED:
  Convert monitor counters to "overload" type that read from atomic counters
  btr_search.rows_added and btr_search.pages_added respectively instead of
  maintaining separate statistics.
- trx_t::commit_cleanup() and trx_t::free(): Update transaction cleanup to
  increment btr_search.hit_count, btr_search.miss_count, btr_search.rows_added,
  and btr_search.pages_added instead of the old global counters.
- trace_engine_stats(): Output r_ahi_stats object with all four AHI
  counters in JSON format when any AHI activity is detected during query
  execution.
- ha_handler_stats: Added ahi_searches, ahi_searches_btree, ahi_rows_added,
  and ahi_pages_added fields to track per-query AHI statistics.
- Add mtr parameter to btr_search_move_or_delete_hash_entries(),
  btr_cur_t::search_info_update(), btr_search_update_hash_on_insert(),
  btr_search_update_hash_ref(), btr_search_info_update_hash(), and
  btr_search_build_page_hash_index() to allow updating AHI statistics.
- ahi_stats.test: Comprehensive verification of AHI statistics reporting
  across different scenarios: insufficient accesses (no AHI build),
  threshold triggering (AHI construction), heavy warmup (full AHI
  utilization), and disabled AHI (verify zero statistics).
- check_ahi_status.inc: Reusable include file for executing queries with
  configurable warmup repetitions and extracting AHI statistics from
  ANALYZE FORMAT=JSON output using JSON path expressions.
2026-06-14 23:40:25 +05:30

97 lines
2.8 KiB
C++

#ifndef HA_HANDLER_STATS_INCLUDED
#define HA_HANDLER_STATS_INCLUDED
/*
Copyright (c) 2023, MariaDB Foundation
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-1335 USA
*/
/* Definitions for parameters to do with handler-routines */
class ha_handler_stats
{
public:
ulonglong pages_accessed; /* Pages accessed from page cache */
ulonglong pages_updated; /* Pages changed in page cache */
ulonglong pages_read_count; /* Pages read from disk */
/* Time spent reading pages, in timer_tracker_frequency() units */
ulonglong pages_read_time;
/*
Number of pages that we've requested to prefetch while running the query.
Note that we don't know:
- how much time was spent reading these pages (and how to count the time
if reading was done in parallel)
- whether the pages were read by "us" or somebody else...
*/
ulonglong pages_prefetched;
ulonglong undo_records_read;
ulonglong ahi_searches; /* Successful adaptive hash lookups */
ulonglong ahi_searches_btree; /* B-tree searches (AHI miss) */
ulonglong ahi_rows_added; /* Rows added to adaptive hash index */
ulonglong ahi_pages_added; /* Pages added to adaptive hash index */
/* Time spent in engine, in timer_tracker_frequency() units */
ulonglong engine_time;
/*
Index Condition Pushdown: number of times condition was checked for index
tuple
*/
ulonglong icp_attempts;
/*
Index Condition Pushdown: number of times condition check evaluated to TRUE
*/
ulonglong icp_match;
uint active; /* <> 0 if status has to be updated */
ha_handler_stats()
{
active= 0;
}
#define first_stat pages_accessed
#define last_stat icp_match
inline void reset()
{
bzero((void*) this, sizeof(*this));
}
inline void add(ha_handler_stats *stats)
{
ulonglong *to= &first_stat;
ulonglong *from= &stats->first_stat;
do
{
(*to)+= *from++;
} while (to++ != &last_stat);
}
inline bool has_stats()
{
if (!active)
return 0;
ulonglong *to= &first_stat;
do
{
if (*to)
return 1;
} while (to++ != &last_stat);
return 0;
}
};
#endif /* HA_HANDLER_STATS_INCLUDED */