MySQL 26.10.0
Source Code Documentation
health_monitor_process_stats_mapping.h
Go to the documentation of this file.
1/* Copyright (c) 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#pragma once
25
27
28#include <cstdint>
29#include <ctime>
30#include <utility>
31#include <vector>
32
34
35namespace hm {
36
38
39/**
40 Update process thread warning suppression after conversion.
41
42 @param[in] result Conversion result
43 @param[in,out] error_state Existing warning suppression state
44 @return true when the caller must log this result
45*/
47 Process_threads_conversion_result result, bool &error_state);
48
49/**
50 Convert process threads snapshot into multiple Health Monitor process records.
51 - Process_identity, Thread_inventory and Thread_cpu_times are required.
52 - Runtime, extended CPU, scheduler and I/O capabilities are optional.
53
54 A failed optional capability still produces the required rows. For example,
55 if I/O collection fails, the thread rows still contain identity, state and
56 CPU time, however, READ_BYTES and WRITE_BYTES remain NULL, and the
57 corresponding metrics are not delivered.
58
59 @param[in] snapshot Process threads snapshot to convert
60 @param[in] timestamp Timestamp for every converted record
61 @param[in,out] output Destination records, unchanged on failure
62 @return complete success, 'degraded' success or conversion failure
63*/
66 time_t timestamp, std::vector<System_Process_Stats> &output);
67
69 time_t m_timestamp{0};
70 uint64_t m_pid{0};
71 std::string m_process_name;
72 uint64_t m_tid{0};
73 std::string m_thread_name;
74 char m_state{'\0'};
75 uint64_t m_utime_ms{0};
76 uint64_t m_stime_ms{0};
77 uint64_t m_cutime_ms{0};
78 uint64_t m_cstime_ms{0};
79 uint32_t m_num_threads{0};
80 uint64_t m_vsize_bytes{0};
81 uint64_t m_rss_bytes{0};
82 uint64_t m_rsslim_bytes{0};
83 uint32_t m_processor{0};
85 uint64_t m_guest_time_ms{0};
86 uint64_t m_cguest_time_ms{0};
87 uint64_t m_read_bytes{0};
88 uint64_t m_write_bytes{0};
89 bool m_cpu_available{false};
93 bool m_io_available{false};
94};
95
96/**
97 Convert a process thread record into a Performance Schema table row.
98
99 @param[in] record process thread record to convert
100 @return Converted table row
101*/
102System_process_stats_row make_system_process_stats_row(
103 const System_Process_Stats &record);
104
105/**
106 Determine whether a process thread metric column is NULL.
107
108 @param[in] row Table row
109 @param[in] index Performance Schema column index
110 @return true when the column is NULL
111*/
112bool system_process_stats_column_is_null(const System_process_stats_row &row,
113 unsigned int index);
114
116 user_time,
124 processor,
130};
131
132/**
133 Determine whether a process thread metric is available.
134
135 @param[in] record process thread record
136 @param[in] metric Metric to inspect
137 @return true when the selected metric is available
138*/
141 switch (metric) {
144 return record.m_cpu_available;
149 return record.m_extended_cpu_available;
154 return record.m_runtime_available;
157 return record.m_scheduler_available;
160 return record.m_io_available;
161 }
162 return false;
163}
164
165/**
166 Select the newest record that contains a process thread metric.
167
168 Records are read by descending index. The search is limited to the newest
169 timestamp so an unavailable current sample cannot fall back to stale data.
170
171 @param[in] count Number of records available to read
172 @param[in] metric Metric that must be available
173 @param[in] reader Function that copies the record at an index
174 @param[out] output Selected record, unchanged when no record is selected
175 @return true when a record was selected
176*/
177template <typename Reader>
179 const Process_thread_metric metric,
180 Reader &&reader,
181 System_Process_Stats &output) {
182 bool have_timestamp = false;
183 time_t timestamp = 0;
184 for (int index = count - 1; index >= 0; --index) {
185 System_Process_Stats candidate;
186 if (!reader(index, candidate)) return false;
187 if (!have_timestamp) {
188 timestamp = candidate.m_timestamp;
189 have_timestamp = true;
190 } else if (timestamp != candidate.m_timestamp) {
191 break;
192 }
193 if (process_thread_metric_available(candidate, metric)) {
194 output = std::move(candidate);
195 return true;
196 }
197 }
198 return false;
199}
200
201/**
202 Deliver one process thread metric.
203
204 The delivery function receives the metric value. It is not called when the
205 selected metric is unavailable. A value of zero remains an available
206 measurement.
207
208 @param[in] record process thread record
209 @param[in] metric Metric to deliver
210 @param[in] delivery Delivery function
211 @return true when the delivery function was called
212*/
213template <typename Delivery>
215 const Process_thread_metric metric,
216 Delivery &&delivery) {
217 if (!process_thread_metric_available(record, metric)) return false;
218
219 uint64_t value{0};
220 switch (metric) {
222 value = record.m_utime_ms;
223 break;
225 value = record.m_stime_ms;
226 break;
228 value = record.m_cutime_ms;
229 break;
231 value = record.m_cstime_ms;
232 break;
234 value = record.m_num_threads;
235 break;
237 value = record.m_vsize_bytes;
238 break;
240 value = record.m_rss_bytes;
241 break;
243 value = record.m_rsslim_bytes;
244 break;
246 value = record.m_processor;
247 break;
249 value = record.m_delayacct_blkio_ms;
250 break;
252 value = record.m_guest_time_ms;
253 break;
255 value = record.m_cguest_time_ms;
256 break;
258 value = record.m_read_bytes;
259 break;
261 value = record.m_write_bytes;
262 break;
263 }
264
265 std::forward<Delivery>(delivery)(value);
266 return true;
267}
268
269} // namespace hm
static int count
Definition: myisam_ftdump.cc:45
static int record
Definition: mysqltest.cc:195
constexpr value_type timestamp
Definition: classic_protocol_constants.h:278
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
Definition: component_command.h:29
bool select_latest_process_thread_metric(const int count, const Process_thread_metric metric, Reader &&reader, System_Process_Stats &output)
Select the newest record that contains a process thread metric.
Definition: health_monitor_process_stats_mapping.h:178
Process_threads_conversion_result convert_process_threads_snapshot(const mysql::system_info::Process_threads_snapshot &snapshot, const time_t timestamp, std::vector< System_Process_Stats > &output)
Convert process threads snapshot into multiple Health Monitor process records.
Definition: health_monitor_process_stats_mapping.cc:201
bool process_thread_metric_available(const System_Process_Stats &record, const Process_thread_metric metric)
Determine whether a process thread metric is available.
Definition: health_monitor_process_stats_mapping.h:139
Process_threads_conversion_result
Definition: health_monitor_process_stats_mapping.h:37
bool update_process_threads_error_state(const Process_threads_conversion_result result, bool &error_state)
Update process thread warning suppression after conversion.
Definition: health_monitor_process_stats_mapping.cc:190
Process_thread_metric
Definition: health_monitor_process_stats_mapping.h:115
bool deliver_process_thread_metric(const System_Process_Stats &record, const Process_thread_metric metric, Delivery &&delivery)
Deliver one process thread metric.
Definition: health_monitor_process_stats_mapping.h:214
bool system_process_stats_column_is_null(const System_process_stats_row &row, const unsigned int index)
Determine whether a process thread metric column is NULL.
Definition: health_monitor_process_stats_mapping.cc:297
System_process_stats_row make_system_process_stats_row(const System_Process_Stats &record)
Convert a process thread record into a Performance Schema table row.
Definition: health_monitor_process_stats_mapping.cc:266
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: health_monitor_system_bits.h:93
time_t m_timestamp
Definition: health_monitor_system_bits.h:96
Definition: health_monitor_process_stats_mapping.h:68
uint32_t m_processor
Definition: health_monitor_process_stats_mapping.h:83
char m_state
Definition: health_monitor_process_stats_mapping.h:74
std::string m_process_name
Definition: health_monitor_process_stats_mapping.h:71
time_t m_timestamp
Definition: health_monitor_process_stats_mapping.h:69
uint64_t m_read_bytes
Definition: health_monitor_process_stats_mapping.h:87
uint64_t m_cutime_ms
Definition: health_monitor_process_stats_mapping.h:77
uint64_t m_tid
Definition: health_monitor_process_stats_mapping.h:72
bool m_extended_cpu_available
Definition: health_monitor_process_stats_mapping.h:91
uint64_t m_rss_bytes
Definition: health_monitor_process_stats_mapping.h:81
bool m_scheduler_available
Definition: health_monitor_process_stats_mapping.h:92
bool m_io_available
Definition: health_monitor_process_stats_mapping.h:93
bool m_runtime_available
Definition: health_monitor_process_stats_mapping.h:90
uint64_t m_rsslim_bytes
Definition: health_monitor_process_stats_mapping.h:82
bool m_cpu_available
Definition: health_monitor_process_stats_mapping.h:89
std::string m_thread_name
Definition: health_monitor_process_stats_mapping.h:73
uint64_t m_delayacct_blkio_ms
Definition: health_monitor_process_stats_mapping.h:84
uint64_t m_pid
Definition: health_monitor_process_stats_mapping.h:70
uint64_t m_guest_time_ms
Definition: health_monitor_process_stats_mapping.h:85
uint64_t m_utime_ms
Definition: health_monitor_process_stats_mapping.h:75
uint64_t m_stime_ms
Definition: health_monitor_process_stats_mapping.h:76
uint32_t m_num_threads
Definition: health_monitor_process_stats_mapping.h:79
uint64_t m_vsize_bytes
Definition: health_monitor_process_stats_mapping.h:80
uint64_t m_cguest_time_ms
Definition: health_monitor_process_stats_mapping.h:86
uint64_t m_cstime_ms
Definition: health_monitor_process_stats_mapping.h:78
uint64_t m_write_bytes
Definition: health_monitor_process_stats_mapping.h:88
Process thread capabilities collected at one collection point.
Definition: system_info.h:450
Definition: result.h:30