Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify and make counter atomic #567

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions storage/innobase/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9697,11 +9697,9 @@ ha_innobase::index_read(
error = 0;
table->status = 0;
if (m_prebuilt->table->is_system_db) {
srv_stats.n_system_rows_read.add(
thd_get_thread_id(m_prebuilt->trx->mysql_thd), 1);
srv_stats.n_system_rows_read.inc();
} else {
srv_stats.n_rows_read.add(
thd_get_thread_id(m_prebuilt->trx->mysql_thd), 1);
srv_stats.n_rows_read.inc();
}
break;

Expand Down Expand Up @@ -10020,11 +10018,9 @@ ha_innobase::general_fetch(
error = 0;
table->status = 0;
if (m_prebuilt->table->is_system_db) {
srv_stats.n_system_rows_read.add(
thd_get_thread_id(trx->mysql_thd), 1);
srv_stats.n_system_rows_read.inc();
} else {
srv_stats.n_rows_read.add(
thd_get_thread_id(trx->mysql_thd), 1);
srv_stats.n_rows_read.inc();
}
break;
case DB_RECORD_NOT_FOUND:
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/include/srv0srv.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Created 10/10/1995 Heikki Tuuri
/** Global counters used inside InnoDB. */
struct srv_stats_t
{
typedef ib_counter_t<ulint, 64> ulint_ctr_64_t;
typedef ib_counter_t ulint_ctr_64_t;
typedef simple_counter<lsn_t> lsn_ctr_1_t;
typedef simple_counter<ulint> ulint_ctr_1_t;
typedef simple_counter<int64_t> int64_ctr_1_t;
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/include/sync0rw.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Created 9/11/1995 Heikki Tuuri

/** Counters for RW locks. */
struct rw_lock_stats_t {
typedef ib_counter_t<int64_t, IB_N_SLOTS> int64_counter_t;
typedef ib_counter_t int64_counter_t;

/** number of spin waits on rw-latches,
resulted during shared (read) locks */
Expand Down
57 changes: 7 additions & 50 deletions storage/innobase/include/ut0counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,69 +86,26 @@ struct counter_indexer_t : public generic_indexer_t<Type, N> {

#define default_indexer_t counter_indexer_t

/** Class for using fuzzy counters. The counter is not protected by any
mutex and the results are not guaranteed to be 100% accurate but close
enough. Creates an array of counters and separates each element by the
CACHE_LINE_SIZE bytes */
template <
typename Type,
int N = IB_N_SLOTS,
template<typename, int> class Indexer = default_indexer_t>
/** Atomic counter */
struct MY_ALIGNED(CACHE_LINE_SIZE) ib_counter_t
{
#ifdef UNIV_DEBUG
~ib_counter_t()
{
size_t n = (CACHE_LINE_SIZE / sizeof(Type));

/* Check that we aren't writing outside our defined bounds. */
for (size_t i = 0; i < UT_ARR_SIZE(m_counter); i += n) {
for (size_t j = 1; j < n - 1; ++j) {
ut_ad(m_counter[i + j] == 0);
}
}
}
#endif /* UNIV_DEBUG */
ib_counter_t() : m_counter(0) {}

/** Increment the counter by 1. */
void inc() UNIV_NOTHROW { add(1); }

/** Increment the counter by 1.
@param[in] index a reasonably thread-unique identifier */
void inc(size_t index) UNIV_NOTHROW { add(index, 1); }

/** Add to the counter.
@param[in] n amount to be added */
void add(Type n) UNIV_NOTHROW { add(m_policy.get_rnd_offset(), n); }

/** Add to the counter.
@param[in] index a reasonably thread-unique identifier
@param[in] n amount to be added */
void add(size_t index, Type n) UNIV_NOTHROW {
size_t i = m_policy.offset(index);

ut_ad(i < UT_ARR_SIZE(m_counter));

m_counter[i] += n;
}
void add(uint64_t n) UNIV_NOTHROW { my_atomic_add64(&m_counter, n); }

/* @return total value - not 100% accurate, since it is not atomic. */
operator Type() const UNIV_NOTHROW {
Type total = 0;

for (size_t i = 0; i < N; ++i) {
total += m_counter[m_policy.offset(i)];
}

return(total);
operator uint64_t() const UNIV_NOTHROW {
return(my_atomic_load64_explicit(&m_counter,
MY_MEMORY_ORDER_RELAXED));
}

private:
/** Indexer into the array */
Indexer<Type, N>m_policy;

/** Slot 0 is unused. */
Type m_counter[(N + 1) * (CACHE_LINE_SIZE / sizeof(Type))];
uint64_t m_counter;
};

#endif /* ut0counter_h */
16 changes: 8 additions & 8 deletions storage/innobase/row/row0mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,9 @@ row_insert_for_mysql(
que_thr_stop_for_mysql_no_error(thr, trx);

if (table->is_system_db) {
srv_stats.n_system_rows_inserted.inc(size_t(trx->id));
srv_stats.n_system_rows_inserted.inc();
} else {
srv_stats.n_rows_inserted.inc(size_t(trx->id));
srv_stats.n_rows_inserted.inc();
}

/* Not protected by dict_table_stats_lock() for performance
Expand Down Expand Up @@ -2146,11 +2146,11 @@ row_update_for_mysql(row_prebuilt_t* prebuilt)
dict_table_n_rows_dec(node->table);

update_statistics = !srv_stats_include_delete_marked;
srv_stats.n_rows_deleted.inc(size_t(trx->id));
srv_stats.n_rows_deleted.inc();
} else {
update_statistics
= !(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE);
srv_stats.n_rows_updated.inc(size_t(trx->id));
srv_stats.n_rows_updated.inc();
}

if (update_statistics) {
Expand All @@ -2171,17 +2171,17 @@ row_update_for_mysql(row_prebuilt_t* prebuilt)
dict_table_n_rows_dec(prebuilt->table);

if (table->is_system_db) {
srv_stats.n_system_rows_deleted.inc(size_t(trx->id));
srv_stats.n_system_rows_deleted.inc();
} else {
srv_stats.n_rows_deleted.inc(size_t(trx->id));
srv_stats.n_rows_deleted.inc();
}

update_statistics = !srv_stats_include_delete_marked;
} else {
if (table->is_system_db) {
srv_stats.n_system_rows_updated.inc(size_t(trx->id));
srv_stats.n_system_rows_updated.inc();
} else {
srv_stats.n_rows_updated.inc(size_t(trx->id));
srv_stats.n_rows_updated.inc();
}

update_statistics
Expand Down
3 changes: 1 addition & 2 deletions storage/innobase/row/row0sel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3283,8 +3283,7 @@ row_sel_get_clust_rec_for_mysql(
*out_rec = NULL;
trx = thr_get_trx(thr);

srv_stats.n_sec_rec_cluster_reads.inc(
thd_get_thread_id(trx->mysql_thd));
srv_stats.n_sec_rec_cluster_reads.inc();

row_build_row_ref_in_tuple(prebuilt->clust_ref, rec,
sec_index, *offsets, trx);
Expand Down