| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/clone0monitor.h |
| 29 |
Performance Schema stage instrumentation to monitor clone progress. |
| 30 |
|
| 31 |
****************************************************************************/ |
| 32 |
|
| 33 |
#ifndef CLONE_MONITOR_H |
| 34 |
#define CLONE_MONITOR_H |
| 35 |
|
| 36 |
/* mysql_stage_inc_work_completed */ |
| 37 |
#include "mysql/psi/mysql_stage.h" |
| 38 |
#include "univ.i" |
| 39 |
#include "ut0dbg.h" |
| 40 |
|
| 41 |
/** Function to alert caller for long wait. |
| 42 |
@return error code */ |
| 43 |
using Clone_Alert_Func = std::function<int()>; |
| 44 |
|
| 45 |
/** Class used to report CLONE progress via Performance Schema. */ |
| 46 |
class Clone_Monitor { |
| 47 |
public: |
| 48 |
/** Constructor */ |
| 49 |
Clone_Monitor() |
| 50 |
: m_estimate(), |
| 51 |
m_work_done(), |
| 52 |
m_progress(), |
| 53 |
m_estimate_bytes_left(), |
| 54 |
m_work_bytes_left(), |
| 55 |
m_cur_phase(NOT_STARTED) { |
| 56 |
m_pfs_data_chunk_size = ut_2_exp(PFS_DATA_CHUNK_SIZE_POW2); |
| 57 |
} |
| 58 |
|
| 59 |
/** Destructor. */ |
| 60 |
~Clone_Monitor() { |
| 61 |
if (m_progress == nullptr) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
mysql_end_stage(); |
| 66 |
} |
| 67 |
|
| 68 |
/** Initialize all monitoring data. |
| 69 |
@param[in] key PFS key to register stage event |
| 70 |
@param[in] enable if true, enable PFS trackig. */ |
| 71 |
void init_state(PSI_stage_key key, bool enable) { |
| 72 |
change_phase(); |
| 73 |
m_progress = nullptr; |
| 74 |
m_estimate = 0; |
| 75 |
m_work_done = 0; |
| 76 |
m_estimate_bytes_left = 0; |
| 77 |
m_work_bytes_left = 0; |
| 78 |
|
| 79 |
if (enable && key != PSI_NOT_INSTRUMENTED) { |
| 80 |
m_progress = mysql_set_stage(key); |
| 81 |
} |
| 82 |
|
| 83 |
if (m_progress == nullptr) { |
| 84 |
m_cur_phase = NOT_STARTED; |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
m_cur_phase = ESTIMATE_WORK; |
| 89 |
} |
| 90 |
|
| 91 |
/** @return true if in estimation phase */ |
| 92 |
bool is_estimation_phase() const { return (m_cur_phase == ESTIMATE_WORK); } |
| 93 |
|
| 94 |
/** @return estimated work in bytes */ |
| 95 |
uint64_t get_estimate() { |
| 96 |
uint64_t ret_estimate = 0; |
| 97 |
if (m_estimate > 0) { |
| 98 |
ret_estimate = m_estimate << PFS_DATA_CHUNK_SIZE_POW2; |
| 99 |
} |
| 100 |
ret_estimate += m_estimate_bytes_left; |
| 101 |
return (ret_estimate); |
| 102 |
} |
| 103 |
|
| 104 |
/** Update the work estimated for the clone operation. |
| 105 |
@param[in] size size in bytes that needs to transferred |
| 106 |
across. */ |
| 107 |
void add_estimate(uint64_t size) { |
| 108 |
m_estimate += convert_bytes_to_work(size, true); |
| 109 |
|
| 110 |
if (m_cur_phase == NOT_STARTED) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
ut_ad(m_cur_phase == ESTIMATE_WORK); |
| 115 |
ut_ad(m_progress != nullptr); |
| 116 |
|
| 117 |
mysql_stage_set_work_estimated(m_progress, m_estimate); |
| 118 |
} |
| 119 |
|
| 120 |
/** Update the progress of the clone operation. |
| 121 |
param[in] size size in bytes that is being transferred |
| 122 |
across. */ |
| 123 |
void update_work(uint size) { |
| 124 |
if (m_cur_phase == NOT_STARTED) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
ut_ad(m_progress != nullptr); |
| 129 |
ut_ad(m_cur_phase == COMPLETE_WORK); |
| 130 |
|
| 131 |
m_work_done += convert_bytes_to_work(size, false); |
| 132 |
mysql_stage_set_work_completed(m_progress, m_work_done); |
| 133 |
} |
| 134 |
|
| 135 |
/** Change from one phase to the other. */ |
| 136 |
void change_phase() { |
| 137 |
switch (m_cur_phase) { |
| 138 |
case NOT_STARTED: |
| 139 |
return; |
| 140 |
|
| 141 |
case ESTIMATE_WORK: |
| 142 |
if (m_estimate_bytes_left != 0) { |
| 143 |
mysql_stage_set_work_estimated(m_progress, m_estimate + 1); |
| 144 |
} |
| 145 |
|
| 146 |
m_cur_phase = COMPLETE_WORK; |
| 147 |
break; |
| 148 |
|
| 149 |
case COMPLETE_WORK: |
| 150 |
if (m_work_bytes_left != 0) { |
| 151 |
uint64_t rounded_estimate = m_estimate; |
| 152 |
if (m_estimate_bytes_left != 0) { |
| 153 |
++rounded_estimate; |
| 154 |
} |
| 155 |
if (m_work_done < rounded_estimate) { |
| 156 |
m_work_done++; |
| 157 |
} |
| 158 |
mysql_stage_set_work_completed(m_progress, m_work_done); |
| 159 |
} |
| 160 |
|
| 161 |
m_cur_phase = NOT_STARTED; |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private: |
| 167 |
/** Translate bytes to work unit. |
| 168 |
@param[in] size size in bytes that needs to be converted to the |
| 169 |
@param[in] is_estimate if called during estimation |
| 170 |
corresponding work unit. |
| 171 |
@return the number of PFS chunks that the size constitutes. */ |
| 172 |
uint64_t convert_bytes_to_work(uint64_t size, bool is_estimate) { |
| 173 |
auto &bytes_left = is_estimate ? m_estimate_bytes_left : m_work_bytes_left; |
| 174 |
size += bytes_left; |
| 175 |
|
| 176 |
auto aligned_size = ut_uint64_align_down(size, m_pfs_data_chunk_size); |
| 177 |
bytes_left = size - aligned_size; |
| 178 |
|
| 179 |
return (aligned_size >> PFS_DATA_CHUNK_SIZE_POW2); |
| 180 |
} |
| 181 |
|
| 182 |
/* Number of PFS chunks which needs to be transferred across. */ |
| 183 |
uint64_t m_estimate; |
| 184 |
|
| 185 |
/* Number of PFS chunks already transferred. */ |
| 186 |
uint64_t m_work_done; |
| 187 |
|
| 188 |
/* Performance schema accounting object. */ |
| 189 |
PSI_stage_progress *m_progress; |
| 190 |
|
| 191 |
/* Size in bytes which couldn't fit the chunk during estimation. */ |
| 192 |
uint64_t m_estimate_bytes_left; |
| 193 |
|
| 194 |
/* Size in bytes which couldn't fit the chunk during transfer. */ |
| 195 |
uint64_t m_work_bytes_left; |
| 196 |
|
| 197 |
/* Current phase. */ |
| 198 |
enum { NOT_STARTED = 0, ESTIMATE_WORK, COMPLETE_WORK } m_cur_phase; |
| 199 |
|
| 200 |
/* PFS Chunk size in power of 2 in unit of bytes. */ |
| 201 |
static const int PFS_DATA_CHUNK_SIZE_POW2 = 20; |
| 202 |
|
| 203 |
/* PFS chunk size. */ |
| 204 |
uint m_pfs_data_chunk_size; |
| 205 |
}; |
| 206 |
|
| 207 |
#endif /* CLONE_MONITOR_H */ |