mariadb/storage/innobase/include/mariadb_stats.h
Monty 99bd226059 MDEV-31558 Add InnoDB engine information to the slow query log
The new statistics is enabled by adding the "engine", "innodb" or "full"
option to --log-slow-verbosity

Example output:

 # Pages_accessed: 184  Pages_read: 95  Pages_updated: 0  Old_rows_read: 1
 # Pages_read_time: 17.0204  Engine_time: 248.1297

Page_read_time is time doing physical reads inside a storage engine.
(Writes cannot be tracked as these are usually done in the background).
Engine_time is the time spent inside the storage engine for the full
duration of the read/write/update calls. It uses the same code as
'analyze statement' for calculating the time spent.

The engine statistics is done with a generic interface that should be
easy for any engine to use. It can also easily be extended to provide
even more statistics.

Currently only InnoDB has counters for Pages_% and Undo_% status.
Engine_time works for all engines.

Implementation details:

class ha_handler_stats holds all engine stats.  This class is included
in handler and THD classes.
While a query is running, all statistics is updated in the handler. In
close_thread_tables() the statistics is added to the THD.

handler::handler_stats is a pointer to where statistics should be
collected. This is set to point to handler::active_handler_stats if
stats are requested. If not, it is set to 0.
handler_stats has also an element, 'active' that is 1 if stats are
requested. This is to allow engines to avoid doing any 'if's while
updating the statistics.

Cloned or partition tables have the pointer set to the base table if
status are requested.

There is a small performance impact when using --log-slow-verbosity=engine:
- All engine calls in 'select' will be timed.
- IO calls for InnoDB reads will be timed.
- Incrementation of counters are done on local variables and accesses
  are inline, so these should have very little impact.
- Statistics has to be reset for each statement for the THD and each
  used handler. This is only 40 bytes, which should be neglectable.
- For partition tables we have to loop over all partitions to update
  the handler_status as part of table_init(). Can be optimized in the
  future to only do this is log-slow-verbosity changes. For this to work
  we have to update handler_status for all opened partitions and
  also for all partitions opened in the future.

Other things:
- Added options 'engine' and 'full' to log-slow-verbosity.
- Some of the new files in the test suite comes from Percona server, which
  has similar status information.
- buf_page_optimistic_get(): Do not increment any counter, since we are
  only validating a pointer, not performing any buf_pool.page_hash lookup.
- Added THD argument to save_explain_data_intern().
- Switched arguments for save_explain_.*_data() to have
  always THD first (generates better code as other functions also have THD
  first).
2023-07-07 12:53:18 +03:00

119 lines
2.9 KiB
C++

/*****************************************************************************
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 Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
#ifndef mariadb_stats_h
#define mariadb_stats_h
/* Include file to handle mariadbd handler specific stats */
#include "ha_handler_stats.h"
#include "my_rdtsc.h"
/* Not active threads are ponting to this structure */
extern thread_local ha_handler_stats mariadb_dummy_stats;
/* Points to either THD->handler_stats or mariad_dummy_stats */
extern thread_local ha_handler_stats *mariadb_stats;
/*
Returns 1 if MariaDB wants engine status
*/
inline bool mariadb_stats_active()
{
return mariadb_stats->active != 0;
}
inline bool mariadb_stats_active(ha_handler_stats *stats)
{
return stats->active != 0;
}
/* The following functions increment different engine status */
inline void mariadb_increment_pages_accessed()
{
mariadb_stats->pages_accessed++;
}
inline void mariadb_increment_pages_updated(ulonglong count)
{
mariadb_stats->pages_updated+= count;
}
inline void mariadb_increment_pages_read()
{
mariadb_stats->pages_read_count++;
}
inline void mariadb_increment_undo_records_read()
{
mariadb_stats->undo_records_read++;
}
/*
The following has to be identical code as measure() in sql_analyze_stmt.h
One should only call this if mariadb_stats_active() is true.
*/
inline ulonglong mariadb_measure()
{
#if (MY_TIMER_ROUTINE_CYCLES)
return my_timer_cycles();
#else
return my_timer_microseconds();
#endif
}
/*
Call this only of start_time != 0
See buf0rea.cc for an example of how to use it efficiently
*/
inline void mariadb_increment_pages_read_time(ulonglong start_time)
{
ha_handler_stats *stats= mariadb_stats;
ulonglong end_time= mariadb_measure();
/* Check that we only call this if active, see example! */
DBUG_ASSERT(start_time);
DBUG_ASSERT(mariadb_stats_active(stats));
stats->pages_read_time+= (end_time - start_time);
}
/*
Helper class to set mariadb_stats temporarly for one call in handler.cc
*/
class mariadb_set_stats
{
public:
uint flag;
mariadb_set_stats(ha_handler_stats *stats)
{
mariadb_stats= stats ? stats : &mariadb_dummy_stats;
}
~mariadb_set_stats()
{
mariadb_stats= &mariadb_dummy_stats;
}
};
#endif /* mariadb_stats_h */