Skip to content

Commit

Permalink
change method of running thread count
Browse files Browse the repository at this point in the history
  • Loading branch information
zhang-lujie committed Oct 11, 2021
1 parent 7d10c82 commit e1c28bf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
2 changes: 0 additions & 2 deletions sql/event_scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ bool post_init_event_thread(THD *thd) {

Global_THD_manager *thd_manager = Global_THD_manager::get_instance();
thd_manager->add_thd(thd);
thd_manager->inc_thread_running();
return false;
}

Expand All @@ -190,7 +189,6 @@ void deinit_event_thread(THD *thd) {
DBUG_PRINT("exit", ("Event thread finishing"));
thd->release_resources();
thd_manager->remove_thd(thd);
thd_manager->dec_thread_running();
delete thd;
}

Expand Down
21 changes: 20 additions & 1 deletion sql/mysqld_thd_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ Global_THD_manager::Global_THD_manager()
THD_array(PSI_INSTRUMENT_ME),
},
thread_ids(PSI_INSTRUMENT_ME),
atomic_num_thread_running(0),
atomic_thread_created(0),
thread_id_counter(reserved_thread_id + 1),
unit_test(false)
Expand Down Expand Up @@ -315,6 +314,26 @@ THD *Global_THD_manager::find_thd(Find_thd_with_id *func) {
return nullptr;
}

class Running_thread_count : public Do_THD_Impl {
public:
Running_thread_count() : m_count(0) {}
virtual void operator()(THD *thd) {
if (thd->get_command() != COM_SLEEP) {
m_count++;
}
}
int get_count() { return m_count; }

private:
int m_count;
};

int Global_THD_manager::get_num_thread_running() {
Running_thread_count thread_running;
Global_THD_manager::get_instance()->do_for_all_thd(&thread_running);
return thread_running.get_count();
}

void inc_thread_created() {
Global_THD_manager::get_instance()->inc_thread_created();
}
Expand Down
15 changes: 1 addition & 14 deletions sql/mysqld_thd_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,7 @@ class Global_THD_manager {
Retrieves thread running statistic variable.
@return int Returns the total number of threads currently running
*/
int get_num_thread_running() const { return atomic_num_thread_running; }

/**
Increments thread running statistic variable.
*/
void inc_thread_running() { atomic_num_thread_running++; }

/**
Decrements thread running statistic variable.
*/
void dec_thread_running() { atomic_num_thread_running--; }
int get_num_thread_running();

/**
Retrieves thread created statistic variable.
Expand Down Expand Up @@ -267,9 +257,6 @@ class Global_THD_manager {
// Mutex protecting thread_ids
mysql_mutex_t LOCK_thread_ids;

// Count of active threads which are running queries in the system.
std::atomic<int> atomic_num_thread_running;

// Cumulative number of threads created by mysqld daemon.
std::atomic<ulonglong> atomic_thread_created;

Expand Down
3 changes: 0 additions & 3 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,6 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data,
}
thd->set_query_id(next_query_id());
thd->reset_rewritten_query();
thd_manager->inc_thread_running();

if (!(server_command_flags[command] & CF_SKIP_QUESTIONS))
thd->status_var.questions++;
Expand Down Expand Up @@ -2195,8 +2194,6 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data,
/* Prevent rewritten query from getting "stuck" in SHOW PROCESSLIST. */
thd->reset_rewritten_query();

thd_manager->dec_thread_running();

/* Freeing the memroot will leave the THD::work_part_info invalid. */
thd->work_part_info = nullptr;

Expand Down
8 changes: 0 additions & 8 deletions unittest/gunit/thd_manager-t.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ TEST_F(ThreadManagerTest, AddRemoveTHDWithGuard) {
EXPECT_EQ(0U, thd_manager->get_thd_count());
}

TEST_F(ThreadManagerTest, IncDecThreadRunning) {
EXPECT_EQ(0, thd_manager->get_num_thread_running());
thd_manager->inc_thread_running();
EXPECT_EQ(1, thd_manager->get_num_thread_running());
thd_manager->dec_thread_running();
EXPECT_EQ(0, thd_manager->get_num_thread_running());
}

TEST_F(ThreadManagerTest, IncThreadCreated) {
EXPECT_EQ(0U, thd_manager->get_num_thread_created());
thd_manager->inc_thread_created();
Expand Down

0 comments on commit e1c28bf

Please sign in to comment.