MySQL 26.10.0
Source Code Documentation
health_monitor_memory.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 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
26#include <cstdint>
27#include <ctime>
28#include <memory>
29#include <string>
30#include <utility>
31
34
35namespace hm {
36
39
40 time_t m_timestamp{0};
41 std::string m_name;
42 uint64_t m_pid{0};
43 uint64_t m_vmrss_bytes{0};
44 uint64_t m_vmdata_bytes{0};
45 uint64_t m_vmswap_bytes{0};
46 uint64_t m_page_faults{0};
50};
51
53 System_Memory_Info() = default;
54
55 time_t m_timestamp{0};
56 uint64_t m_total_bytes{0};
57 uint64_t m_free_bytes{0};
58 uint64_t m_avail_bytes{0};
59 uint64_t m_cached_bytes{0};
60 uint64_t m_swap_total_bytes{0};
61 uint64_t m_swap_free_bytes{0};
62 bool m_cache_available{false};
64};
65
66/**
67 Memory Info
68 Record structure for health_process_memory and health_system_memory.
69*/
71 Memory_Info() = default;
75};
76
77/**
78 Timestamp and publish the records for one memory sample.
79
80 A 'sample' is one-time memory observation. 'Publication' means making its
81 record(s) visible through writers provided by the caller.
82
83 The caller provides writers to store the records in their respective buffers.
84
85 @param[in,out] memory_info Memory sample to publish
86 @param[in] timestamp Timestamp applied to published records
87 @param[in] write_process Writer for a process memory record
88 @param[in] write_system Writer for a system memory record
89*/
90template <typename Process_writer, typename System_writer>
92 Process_writer &&write_process,
93 System_writer &&write_system) {
94 memory_info.sys.m_timestamp = timestamp;
95 if (memory_info.m_process_memory_available) {
96 memory_info.proc.m_timestamp = timestamp;
97 std::forward<Process_writer>(write_process)(memory_info.proc);
98 }
99 std::forward<System_writer>(write_system)(memory_info.sys);
100}
101
102/**
103 Memory Collector
104
105 The Memory Collector monitors server memory usage, publishes memory
106 statistics, and emits warnings when available memory falls below the
107 configured thresholds. Unlike the Disk Collector, the Memory Collector
108 does not take corrective action when memory is low.
109*/
111 public:
113
114 protected:
115 // Sample collection
116 void pre_process() override;
117 void process() override;
118 void post_process() override;
119
120 public:
121 // Settings
122 void set_buffer_size() final;
123 bool settings_changed() override;
124 void set_params() final;
125 bool running() const override;
126
127 // Memory info
128 std::shared_ptr<Stat_Buffer<Process_Memory_Info>> proc_buffer() {
129 return m_proc_buffer;
130 }
131 std::shared_ptr<Stat_Buffer<System_Memory_Info>> sys_buffer() {
132 return m_sys_buffer;
133 }
134 int get_memory_info(Memory_Info &memory_info);
136 bool &unavailable);
137 int get_system_memory_info(System_Memory_Info &sys_memory_info);
138 void update_per_sample_totals(const Memory_Info &memory_info);
139 void set_memory_profile(const Memory_Info &memory_info);
140
141 // Collector state
142 static void set_state_params();
143 void set_state(ms::Memory_State &new_state);
149 void check_memory_levels() const;
150
157
158 // Logging
159 void log_resource_status() override;
160 std::string format_settings() override;
161 std::string format_status_info() override;
162 std::string tag_collector_state() override;
163
164 void log_warning_level(int);
167
168 // System variables
169 uint64_t m_warn_level_1_mb{0};
170 uint64_t m_warn_level_2_mb{0};
171 uint64_t m_warn_level_3_mb{0};
172 std::atomic<bool> m_settings_changed{false};
173
174 // Current memory levels in MiB
175 uint64_t m_total_mb{0}; // total memory im mb
176 uint64_t m_avail_mb{0}; // previous available memory
177 uint64_t m_last_avail_mb{0}; // last availale memory
178 float m_used_pct{0}; // used memory, pct of total
179 int m_delta_pct{10}; // pct change to trigger status message
180 uint64_t m_process_mb{0}; // memory used by mysqld
183 bool m_refresh_memory_profile{false}; // true if settings change
184
185 bool m_sysinfo_error{false};
186 Memory_Info m_info; // memory stats, raw
187
188 // Constants
189 const std::string k_warning_level{"Warning Level"};
190
191 private:
192 std::shared_ptr<Stat_Buffer<Process_Memory_Info>> m_proc_buffer;
193 std::shared_ptr<Stat_Buffer<System_Memory_Info>> m_sys_buffer;
194
201 &m_memory_key, "memory_collector", PSI_FLAG_ONLY_GLOBAL_STAT,
202 PSI_VOLATILITY_PROVISIONING, "Health Monitor system memory data buffer"};
203};
204
205} // namespace hm
Definition: health_monitor_collector.h:41
static constexpr uint64_t no_warn_level
Definition: health_monitor_collector.h:121
Memory Collector.
Definition: health_monitor_memory.h:110
uint64_t m_last_warn_level_mb
Definition: health_monitor_memory.h:182
uint64_t m_process_mb
Definition: health_monitor_memory.h:180
uint64_t m_avail_mb
Definition: health_monitor_memory.h:176
void set_warning_state()
Definition: health_monitor_memory.h:147
uint64_t m_max_warn_level_mb
Definition: health_monitor_memory.h:181
std::string format_status_info() override
Memory info formatted string.
Definition: health_monitor_memory.cc:277
const std::string k_warning_level
Definition: health_monitor_memory.h:189
ms::Stop_State m_stop_state
Definition: health_monitor_memory.h:153
std::shared_ptr< Stat_Buffer< System_Memory_Info > > sys_buffer()
Definition: health_monitor_memory.h:131
std::shared_ptr< Stat_Buffer< System_Memory_Info > > m_sys_buffer
Definition: health_monitor_memory.h:193
void set_state(ms::Memory_State &new_state)
Set the next state based on the available memory.
Definition: health_monitor_memory.cc:381
Memory_Info m_info
Definition: health_monitor_memory.h:186
void post_process() override
Final tasks before exiting the thread.
Definition: health_monitor_memory.cc:101
std::shared_ptr< Stat_Buffer< Process_Memory_Info > > m_proc_buffer
Definition: health_monitor_memory.h:192
uint64_t m_warn_level_3_mb
Definition: health_monitor_memory.h:171
int get_process_memory_info(Process_Memory_Info &process_info, bool &unavailable)
Get the physical memory used by the current process.
Definition: health_monitor_memory.cc:185
uint64_t m_warn_level_1_mb
Definition: health_monitor_memory.h:169
void set_params() final
Set collection parameters from the system variables.
Definition: health_monitor_memory.cc:138
bool m_refresh_memory_profile
Definition: health_monitor_memory.h:183
void check_memory_levels() const
Check the available memory against the memory thresholds.
Definition: health_monitor_memory.cc:394
PSI_rwlock_info m_rwlock_info
Definition: health_monitor_memory.h:196
uint64_t m_warn_level_2_mb
Definition: health_monitor_memory.h:170
void pre_process() override
Tasks before starting the collection loop.
Definition: health_monitor_memory.cc:62
static void set_state_params()
Set threshold params for all memory states.
Definition: health_monitor_memory.cc:376
bool settings_changed() override
Indicate if the Memory Collector settings have changed.
Definition: health_monitor_memory.cc:123
int get_memory_info(Memory_Info &memory_info)
Get the current system and process memory info.
Definition: health_monitor_memory.cc:163
void set_buffer_size() final
Resize the stat buffer if necessary.
Definition: health_monitor_memory.cc:109
std::atomic< bool > m_settings_changed
Definition: health_monitor_memory.h:172
ms::Error_State m_error_state
Definition: health_monitor_memory.h:156
uint64_t m_last_avail_mb
Definition: health_monitor_memory.h:177
ms::Start_State m_start_state
Definition: health_monitor_memory.h:152
PSI_memory_key m_memory_key
Definition: health_monitor_memory.h:199
ms::Warning_State m_warning_state
Definition: health_monitor_memory.h:155
PSI_memory_info m_memory_info
Definition: health_monitor_memory.h:200
int get_system_memory_info(System_Memory_Info &sys_memory_info)
Get memory stats for the system.
Definition: health_monitor_memory.cc:201
void log_warning_error_condition()
Error condition warning.
Definition: health_monitor_memory.cc:333
void log_warning_level()
Definition: health_monitor_memory.h:165
Memory_Collector()
Definition: health_monitor_memory.cc:41
std::string tag_collector_state() override
Collector state tag.
Definition: health_monitor_memory.cc:295
uint64_t m_total_mb
Definition: health_monitor_memory.h:175
PSI_rwlock_key m_rwlock_key
Definition: health_monitor_memory.h:195
int m_delta_pct
Definition: health_monitor_memory.h:179
std::string format_settings() override
Memory Collector settings formatted string.
Definition: health_monitor_memory.cc:262
std::shared_ptr< Stat_Buffer< Process_Memory_Info > > proc_buffer()
Definition: health_monitor_memory.h:128
void update_per_sample_totals(const Memory_Info &memory_info)
Update totals used for status and warning messages, convert to MiB.
Definition: health_monitor_memory.cc:211
void process() override
Collect the current memory stats and write to the stat buffer.
Definition: health_monitor_memory.cc:70
bool m_sysinfo_error
Definition: health_monitor_memory.h:185
void set_error_state()
Definition: health_monitor_memory.h:148
void set_normal_state()
Definition: health_monitor_memory.h:146
bool running() const override
Return health_monitor.memory_running.
Definition: health_monitor_memory.cc:154
void log_resource_status() override
Memory status, depending on state.
Definition: health_monitor_memory.cc:341
ms::Normal_State m_normal_state
Definition: health_monitor_memory.h:154
void set_stop_state()
Definition: health_monitor_memory.h:145
float m_used_pct
Definition: health_monitor_memory.h:178
ms::Memory_State * m_current_state
Definition: health_monitor_memory.h:151
void set_start_state()
Definition: health_monitor_memory.h:144
void set_memory_profile(const Memory_Info &memory_info)
Update the memory warning thresholds, convert to MiB.
Definition: health_monitor_memory.cc:246
Error State.
Definition: health_monitor_memory_state.h:126
Definition: health_monitor_memory_state.h:35
Normal State.
Definition: health_monitor_memory_state.h:101
Start State.
Definition: health_monitor_memory_state.h:73
Stop State.
Definition: health_monitor_memory_state.h:87
Warning State.
Definition: health_monitor_memory_state.h:112
#define PSI_DOCUMENT_ME
Definition: component_common.h:29
#define PSI_FLAG_SINGLETON
Singleton flag.
Definition: component_common.h:35
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
unsigned int PSI_rwlock_key
Instrumented rwlock key.
Definition: psi_rwlock_bits.h:44
#define PSI_VOLATILITY_PROVISIONING
Definition: psi_bits.h:155
#define PSI_FLAG_ONLY_GLOBAL_STAT
Global stat only flag.
Definition: psi_bits.h:112
constexpr value_type timestamp
Definition: classic_protocol_constants.h:278
Definition: component_command.h:29
void publish_memory_records(Memory_Info &memory_info, time_t timestamp, Process_writer &&write_process, System_writer &&write_system)
Timestamp and publish the records for one memory sample.
Definition: health_monitor_memory.h:91
Memory instrument information.
Definition: psi_memory_bits.h:58
Rwlock information.
Definition: psi_rwlock_bits.h:162
Memory Info Record structure for health_process_memory and health_system_memory.
Definition: health_monitor_memory.h:70
bool m_process_memory_available
Definition: health_monitor_memory.h:74
System_Memory_Info sys
Definition: health_monitor_memory.h:73
Memory_Info()=default
Process_Memory_Info proc
Definition: health_monitor_memory.h:72
Definition: health_monitor_memory.h:37
bool m_page_faults_available
Definition: health_monitor_memory.h:49
uint64_t m_page_faults
Definition: health_monitor_memory.h:46
uint64_t m_vmdata_bytes
Definition: health_monitor_memory.h:44
time_t m_timestamp
Definition: health_monitor_memory.h:40
uint64_t m_vmswap_bytes
Definition: health_monitor_memory.h:45
bool m_residency_available
Definition: health_monitor_memory.h:47
uint64_t m_vmrss_bytes
Definition: health_monitor_memory.h:43
bool m_details_available
Definition: health_monitor_memory.h:48
std::string m_name
Definition: health_monitor_memory.h:41
uint64_t m_pid
Definition: health_monitor_memory.h:42
Definition: health_monitor_memory.h:52
uint64_t m_swap_total_bytes
Definition: health_monitor_memory.h:60
bool m_swap_capacity_available
Definition: health_monitor_memory.h:63
time_t m_timestamp
Definition: health_monitor_memory.h:55
uint64_t m_avail_bytes
Definition: health_monitor_memory.h:58
uint64_t m_total_bytes
Definition: health_monitor_memory.h:56
uint64_t m_swap_free_bytes
Definition: health_monitor_memory.h:61
bool m_cache_available
Definition: health_monitor_memory.h:62
uint64_t m_cached_bytes
Definition: health_monitor_memory.h:59
System_Memory_Info()=default
uint64_t m_free_bytes
Definition: health_monitor_memory.h:57