Skip to content

Commit 6268780

Browse files
committed
tc_active_instances: my_atomic to std::atomic
1 parent 3b3f931 commit 6268780

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

sql/mysqld.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7831,7 +7831,7 @@ SHOW_VAR status_vars[]= {
78317831
{"Subquery_cache_miss", (char*) &subquery_cache_miss, SHOW_LONG},
78327832
{"Table_locks_immediate", (char*) &locks_immediate, SHOW_LONG},
78337833
{"Table_locks_waited", (char*) &locks_waited, SHOW_LONG},
7834-
{"Table_open_cache_active_instances", (char*) &tc_active_instances, SHOW_UINT},
7834+
{"Table_open_cache_active_instances", (char*) &show_tc_active_instances, SHOW_SIMPLE_FUNC},
78357835
{"Table_open_cache_hits", (char*) offsetof(STATUS_VAR, table_open_cache_hits), SHOW_LONGLONG_STATUS},
78367836
{"Table_open_cache_misses", (char*) offsetof(STATUS_VAR, table_open_cache_misses), SHOW_LONGLONG_STATUS},
78377837
{"Table_open_cache_overflows", (char*) offsetof(STATUS_VAR, table_open_cache_overflows), SHOW_LONGLONG_STATUS},

sql/table_cache.cc

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
ulong tdc_size; /**< Table definition cache threshold for LRU eviction. */
5757
ulong tc_size; /**< Table cache threshold for LRU eviction. */
5858
uint32 tc_instances;
59-
uint32 tc_active_instances= 1;
59+
static std::atomic<uint32_t> tc_active_instances(1);
6060
static std::atomic<bool> tc_contention_warning_reported;
6161

6262
/** Data collections. */
@@ -163,7 +163,7 @@ struct Table_cache_instance
163163
overhead on TABLE object release. All other table cache mutex acquistions
164164
are considered out of hot path and are not instrumented either.
165165
*/
166-
void lock_and_check_contention(uint32 n_instances, uint32 instance)
166+
void lock_and_check_contention(uint32_t n_instances, uint32_t instance)
167167
{
168168
if (mysql_mutex_trylock(&LOCK_table_cache))
169169
{
@@ -172,11 +172,10 @@ struct Table_cache_instance
172172
{
173173
if (n_instances < tc_instances)
174174
{
175-
if (my_atomic_cas32_weak_explicit((int32*) &tc_active_instances,
176-
(int32*) &n_instances,
177-
(int32) n_instances + 1,
178-
MY_MEMORY_ORDER_RELAXED,
179-
MY_MEMORY_ORDER_RELAXED))
175+
if (tc_active_instances.
176+
compare_exchange_weak(n_instances, n_instances + 1,
177+
std::memory_order_relaxed,
178+
std::memory_order_relaxed))
180179
{
181180
sql_print_information("Detected table cache mutex contention at instance %d: "
182181
"%d%% waits. Additional table cache instance "
@@ -354,8 +353,8 @@ void tc_purge(bool mark_flushed)
354353

355354
void tc_add_table(THD *thd, TABLE *table)
356355
{
357-
uint32 i= thd->thread_id % my_atomic_load32_explicit((int32*) &tc_active_instances,
358-
MY_MEMORY_ORDER_RELAXED);
356+
uint32_t i=
357+
thd->thread_id % tc_active_instances.load(std::memory_order_relaxed);
359358
TABLE *LRU_table= 0;
360359
TDC_element *element= table->s->tdc;
361360

@@ -408,10 +407,8 @@ void tc_add_table(THD *thd, TABLE *table)
408407

409408
TABLE *tc_acquire_table(THD *thd, TDC_element *element)
410409
{
411-
uint32 n_instances=
412-
my_atomic_load32_explicit((int32*) &tc_active_instances,
413-
MY_MEMORY_ORDER_RELAXED);
414-
uint32 i= thd->thread_id % n_instances;
410+
uint32_t n_instances= tc_active_instances.load(std::memory_order_relaxed);
411+
uint32_t i= thd->thread_id % n_instances;
415412
TABLE *table;
416413

417414
tc[i].lock_and_check_contention(n_instances, i);
@@ -1342,3 +1339,14 @@ int tdc_iterate(THD *thd, my_hash_walk_action action, void *argument,
13421339
}
13431340
return res;
13441341
}
1342+
1343+
1344+
int show_tc_active_instances(THD *thd, SHOW_VAR *var, char *buff,
1345+
enum enum_var_type scope)
1346+
{
1347+
var->type= SHOW_UINT;
1348+
var->value= buff;
1349+
*(reinterpret_cast<uint32_t*>(buff))=
1350+
tc_active_instances.load(std::memory_order_relaxed);
1351+
return 0;
1352+
}

sql/table_cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ enum enum_tdc_remove_table_type
7171
extern ulong tdc_size;
7272
extern ulong tc_size;
7373
extern uint32 tc_instances;
74-
extern uint32 tc_active_instances;
7574

7675
extern bool tdc_init(void);
7776
extern void tdc_start_shutdown(void);
@@ -98,6 +97,8 @@ extern int tdc_iterate(THD *thd, my_hash_walk_action action, void *argument,
9897
bool no_dups= false);
9998

10099
extern uint tc_records(void);
100+
int show_tc_active_instances(THD *thd, SHOW_VAR *var, char *buff,
101+
enum enum_var_type scope);
101102
extern void tc_purge(bool mark_flushed= false);
102103
extern void tc_add_table(THD *thd, TABLE *table);
103104
extern void tc_release_table(TABLE *table);

0 commit comments

Comments
 (0)