Skip to content

Commit

Permalink
Add CPU usage and delay columns to P_S.processlist and P_S.threads
Browse files Browse the repository at this point in the history
Summary: This change adds PSI APIs to record quantum start/end and delay in the `PFS_thread` object. The CPU scheduler calls these APIs at the appropriate points (see D52988152). Note that the APIs do not take any write locks like statement or session, so this is just the best effort; if the results don't make sense the query should be rerun. The `performance_schema.processlist` and `performance_schema.threads` tables now include two new columns to show the current `CPU_USAGE` and `DELAY_TOTAL` for all running queries. If a connection is idle, the columns show `NULL` values.

Differential Revision: D52988122

fbshipit-source-id: de9df4f

-------------------------------------------------------------------------------------------------

Bump P_S version for processlist and threads tables changes

Summary:
The previous change D52988122 missed the version bump. As a result, the data dictionary on prod instances doesn't get updated and new columns do not show up.

Differential Revision: D56020631

fbshipit-source-id: 81e7b50

-------------------------------------------------------------------------------------------------

Add pfs checks to quantum tracking APIs

Summary:
Turns out that through the following config the creation of PFS thread for `THD` object could be disabled, so `nullptr` would be passed to the APIs.

```
[mysqld.1]
loose-performance-schema-max-thread_instances=318
loose-performance-schema-max-thread_classes=12
```

Differential Revision: D53060806

fbshipit-source-id: a47e8f6
  • Loading branch information
george-reynya authored and Herman Lee committed Sep 10, 2024
1 parent 8b42978 commit b4f911b
Show file tree
Hide file tree
Showing 31 changed files with 612 additions and 101 deletions.
34 changes: 34 additions & 0 deletions include/mysql/components/services/bits/psi_thread_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,40 @@ typedef int (*get_thread_held_locks_v1_t)(PSI_thread *thread,
const char **held_lock_names,
int max_count);

/**
Record start of delay before first quantum.
@param thread the thread instrumentation
@param delay_start Delay start timestamp in ns.
*/
typedef void (*thread_start_delay_v1_t)(PSI_thread *thread,
int64_t delay_start);

/**
Record start of quantum and end of delay between quantums.
@param thread the thread instrumentation
@param quantum_start Delay start timestamp in ns.
*/
typedef void (*thread_start_quantum_v1_t)(PSI_thread *thread,
int64_t quantum_start);

/**
Record end of quantum and start of delay between quantums.
@param thread the thread instrumentation
@param quantum_end Delay start timestamp in ns.
*/
typedef void (*thread_end_quantum_v1_t)(PSI_thread *thread,
int64_t quantum_end);

/**
Reset CPU stats at the end of query.
@param thread the thread instrumentation
*/
typedef void (*thread_reset_cpu_stats_v1_t)(PSI_thread *thread);

typedef struct PSI_thread_info_v5 PSI_thread_info;

typedef void (*thread_detect_telemetry_v7_t)(PSI_thread *thread);
Expand Down
12 changes: 12 additions & 0 deletions include/mysql/components/services/psi_thread_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ END_SERVICE_DEFINITION(psi_thread_v5)
Status: deprecated, use version 7 instead.
Changes compared to version 5:
- added set_thread_secondary_engine
- added thread_start_delay, thread_start_quantum, thread_end_quantum,
thread_reset_cpu_stats
*/

BEGIN_SERVICE_DEFINITION(psi_thread_v6)
Expand Down Expand Up @@ -302,6 +304,16 @@ notify_session_connect_v1_t notify_session_connect;
notify_session_disconnect_v1_t notify_session_disconnect;
/** @sa notify_session_change_user_v1_t. */
notify_session_change_user_v1_t notify_session_change_user;
/** @sa get_thread_held_locks_v1_t. */
get_thread_held_locks_v1_t get_thread_held_locks;
/** @sa thread_start_delay_v1_t. */
thread_start_delay_v1_t thread_start_delay;
/** @sa thread_start_quantum_v1_t. */
thread_start_quantum_v1_t thread_start_quantum;
/** @sa thread_end_quantum_v1_t. */
thread_end_quantum_v1_t thread_end_quantum;
/** @sa thread_reset_cpu_stats_v1_t. */
thread_reset_cpu_stats_v1_t thread_reset_cpu_stats;
END_SERVICE_DEFINITION(psi_thread_v6)

/*
Expand Down
15 changes: 15 additions & 0 deletions include/mysql/psi/psi_abi_thread_v1.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@
typedef int (*get_thread_held_locks_v1_t)(PSI_thread *thread,
const char **held_lock_names,
int max_count);
typedef void (*thread_start_delay_v1_t)(PSI_thread *thread,
int64_t delay_start);
typedef void (*thread_start_quantum_v1_t)(PSI_thread *thread,
int64_t quantum_start);
typedef void (*thread_end_quantum_v1_t)(PSI_thread *thread,
int64_t quantum_end);
typedef void (*thread_reset_cpu_stats_v1_t)(PSI_thread *thread);
typedef struct PSI_thread_info_v5 PSI_thread_info;
typedef void (*thread_detect_telemetry_v7_t)(PSI_thread *thread);
typedef void (*thread_abort_telemetry_v7_t)(PSI_thread *thread);
Expand Down Expand Up @@ -303,6 +310,10 @@
notify_session_change_user_v1_t notify_session_change_user;
set_mem_cnt_THD_v1_t set_mem_cnt_THD;
get_thread_held_locks_v1_t get_thread_held_locks;
thread_start_delay_v1_t thread_start_delay;
thread_start_quantum_v1_t thread_start_quantum;
thread_end_quantum_v1_t thread_end_quantum;
thread_reset_cpu_stats_v1_t thread_reset_cpu_stats;
};
struct PSI_thread_service_v7 {
register_thread_v5_t register_thread;
Expand Down Expand Up @@ -348,6 +359,10 @@
thread_detect_telemetry_v7_t detect_telemetry;
thread_abort_telemetry_v7_t abort_telemetry;
get_thread_held_locks_v1_t get_thread_held_locks;
thread_start_delay_v1_t thread_start_delay;
thread_start_quantum_v1_t thread_start_quantum;
thread_end_quantum_v1_t thread_end_quantum;
thread_reset_cpu_stats_v1_t thread_reset_cpu_stats;
};
typedef struct PSI_thread_service_v7 PSI_thread_service_t;
extern PSI_thread_service_t *psi_thread_service;
19 changes: 19 additions & 0 deletions include/mysql/psi/psi_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ struct PSI_thread_service_v5 {
@since PSI_THREAD_VERSION_6
Changes from version 5:
- added set_thread_secondary_engine
- added thread_start_delay, thread_start_quantum, thread_end_quantum,
thread_reset_cpu_stats
*/
struct PSI_thread_service_v6 {
/** @sa register_thread_v5_t. */
Expand Down Expand Up @@ -388,6 +390,14 @@ struct PSI_thread_service_v6 {
set_mem_cnt_THD_v1_t set_mem_cnt_THD;
/** @sa get_thread_held_locks_v1_t. */
get_thread_held_locks_v1_t get_thread_held_locks;
/** @sa thread_start_delay_v1_t. */
thread_start_delay_v1_t thread_start_delay;
/** @sa thread_start_quantum_v1_t. */
thread_start_quantum_v1_t thread_start_quantum;
/** @sa thread_end_quantum_v1_t. */
thread_end_quantum_v1_t thread_end_quantum;
/** @sa thread_reset_cpu_stats_v1_t. */
thread_reset_cpu_stats_v1_t thread_reset_cpu_stats;
};

/**
Expand Down Expand Up @@ -480,8 +490,17 @@ struct PSI_thread_service_v7 {

thread_detect_telemetry_v7_t detect_telemetry;
thread_abort_telemetry_v7_t abort_telemetry;

/** @sa get_thread_held_locks_v1_t. */
get_thread_held_locks_v1_t get_thread_held_locks;
/** @sa thread_start_delay_v1_t. */
thread_start_delay_v1_t thread_start_delay;
/** @sa thread_start_quantum_v1_t. */
thread_start_quantum_v1_t thread_start_quantum;
/** @sa thread_end_quantum_v1_t. */
thread_end_quantum_v1_t thread_end_quantum;
/** @sa thread_reset_cpu_stats_v1_t. */
thread_reset_cpu_stats_v1_t thread_reset_cpu_stats;
};

typedef struct PSI_thread_service_v7 PSI_thread_service_t;
Expand Down
8 changes: 8 additions & 0 deletions include/pfs_thread_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ void pfs_abort_telemetry_vc(PSI_thread *thread);
int pfs_get_thread_held_locks_vc(PSI_thread *thread,
const char **held_lock_names, int max_count);

void pfs_thread_start_delay_vc(PSI_thread *thread, int64_t delay_start);

void pfs_thread_start_quantum_vc(PSI_thread *thread, int64_t quantum_start);

void pfs_thread_end_quantum_vc(PSI_thread *thread, int64_t quantum_end);

void pfs_thread_reset_cpu_stats_vc(PSI_thread *thread);

#endif /* WITH_LOCK_ORDER */
#endif /* MYSQL_DYNAMIC_PLUGIN */
#endif /* MYSQL_SERVER || PFS_DIRECT_CALL */
Expand Down
2 changes: 2 additions & 0 deletions mysql-test/r/dd_upgrade_test.result
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ threads CREATE TABLE `threads` (
`MAX_TOTAL_MEMORY` bigint unsigned NOT NULL,
`TELEMETRY_ACTIVE` enum('YES','NO') NOT NULL,
`THREAD_PRIORITY` int DEFAULT NULL,
`CPU_USAGE` time(6) DEFAULT NULL,
`DELAY_TOTAL` time(6) DEFAULT NULL,
PRIMARY KEY (`THREAD_ID`),
KEY `PROCESSLIST_ID` (`PROCESSLIST_ID`),
KEY `THREAD_OS_ID` (`THREAD_OS_ID`),
Expand Down
3 changes: 1 addition & 2 deletions mysql-test/suite/perfschema/include/processlist_load.inc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SHOW FULL PROCESSLIST;
--echo
--echo # Performance Schema processlist table
--echo
--replace_column 1 <Id> 3 <Host> 6 <Time> 7 <State>
--replace_column 1 <Id> 3 <Host> 6 <Time> 7 <State> 10 <Cpu_usage> 11 <Delay_total>
--replace_regex /Daemon/<Command>/ /Connect/<Command>/ /Sleep/<Command>/
select * from performance_schema.processlist order by user, id;

Expand All @@ -36,4 +36,3 @@ select * from performance_schema.processlist order by user, id;
--replace_column 1 <Id> 3 <Host> 6 <Time> 7 <State>
--replace_regex /Daemon/<Command>/ /Connect/<Command>/ /Sleep/<Command>/
select * from information_schema.processlist order by user, id;

2 changes: 1 addition & 1 deletion mysql-test/suite/perfschema/r/dd_version_check.result
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Checking the data dictionary properties ..."
SUBSTRING_INDEX(SUBSTRING(properties, LOCATE('PS_VERSION', properties), 30), ';', 1)
PS_VERSION=80036024
PS_VERSION=80036025
"Checking the performance schema database structure ..."
Loading

0 comments on commit b4f911b

Please sign in to comment.