Lock table cache mutex and check contention. Instance is considered contested if more than 20% of mutex acquisitions can't be served immediately. Up to 100 000 probes may be performed to avoid instance activation on short sporadic peaks. 100 000 is estimated maximum number of queries one instance can serve in one second. These numbers work well on a 2 socket / 20 core / 40 th
| 173 | are considered out of hot path and are not instrumented either. |
| 174 | */ |
| 175 | void lock_and_check_contention(uint32_t n_instances, uint32_t instance) |
| 176 | { |
| 177 | if (mysql_mutex_trylock(&LOCK_table_cache)) |
| 178 | { |
| 179 | mysql_mutex_lock(&LOCK_table_cache); |
| 180 | if (++mutex_waits == 20000) |
| 181 | { |
| 182 | if (n_instances < tc_instances) |
| 183 | { |
| 184 | if (tc_active_instances. |
| 185 | compare_exchange_weak(n_instances, n_instances + 1, |
| 186 | std::memory_order_relaxed, |
| 187 | std::memory_order_relaxed)) |
| 188 | { |
| 189 | sql_print_information("Detected table cache mutex contention at instance %d: " |
| 190 | "%d%% waits. Additional table cache instance " |
| 191 | "activated. Number of instances after " |
| 192 | "activation: %d.", |
| 193 | instance + 1, |
| 194 | mutex_waits * 100 / (mutex_nowaits + mutex_waits), |
| 195 | n_instances + 1); |
| 196 | } |
| 197 | } |
| 198 | else if (!tc_contention_warning_reported.exchange(true, |
| 199 | std::memory_order_relaxed)) |
| 200 | { |
| 201 | sql_print_warning("Detected table cache mutex contention at instance %d: " |
| 202 | "%d%% waits. Additional table cache instance " |
| 203 | "cannot be activated: consider raising " |
| 204 | "table_open_cache_instances. Number of active " |
| 205 | "instances: %d.", |
| 206 | instance + 1, |
| 207 | mutex_waits * 100 / (mutex_nowaits + mutex_waits), |
| 208 | n_instances); |
| 209 | } |
| 210 | mutex_waits= 0; |
| 211 | mutex_nowaits= 0; |
| 212 | } |
| 213 | } |
| 214 | else if (++mutex_nowaits == 80000) |
| 215 | { |
| 216 | mutex_waits= 0; |
| 217 | mutex_nowaits= 0; |
| 218 | } |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 |
no test coverage detected