Skip to content

Commit bb6bcd3

Browse files
author
Tor Didriksen
committed
Bug#14771006 SHOW PROCESSLIST SORT ORDER CHANGED IN 5.6
'SHOW PROCESSLIST' has traditionally returned the result ordered by thread_id, so do the same now, even if the THD-list is a std::set. Solution: Convert thread_infos from intrusive list, to array of pointers, and std::sort it.
1 parent d129bc7 commit bb6bcd3

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

sql/global_threads.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class THD;
2525
extern mysql_mutex_t LOCK_thread_count;
2626
extern mysql_cond_t COND_thread_count;
2727

28-
/*
28+
/**
2929
We maintail a set of all registered threads.
3030
We provide acccessors to iterate over all threads.
31+
There is no guarantee on the order of THDs when iterating.
3132
3233
We also provide mutators for inserting, and removing an element:
3334
add_global_thread() inserts a THD into the set, and increments the counter.

sql/sql_show.cc

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,8 @@ view_store_create_info(THD *thd, TABLE_LIST *table, String *buff)
19731973
returns for each thread: thread id, user, host, db, command, info
19741974
****************************************************************************/
19751975

1976-
class thread_info :public ilink<thread_info> {
1976+
class thread_info
1977+
{
19771978
public:
19781979
static void *operator new(size_t size)
19791980
{
@@ -1990,6 +1991,17 @@ class thread_info :public ilink<thread_info> {
19901991
CSET_STRING query_string;
19911992
};
19921993

1994+
// For sorting by thread_id.
1995+
class thread_info_compare :
1996+
public std::binary_function<const thread_info*, const thread_info*, bool>
1997+
{
1998+
public:
1999+
bool operator() (const thread_info* p1, const thread_info* p2)
2000+
{
2001+
return p1->thread_id < p2->thread_id;
2002+
}
2003+
};
2004+
19932005
static const char *thread_state_info(THD *tmp)
19942006
{
19952007
#ifndef EMBEDDED_LIBRARY
@@ -2018,7 +2030,7 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
20182030
{
20192031
Item *field;
20202032
List<Item> field_list;
2021-
I_List<thread_info> thread_infos;
2033+
Mem_root_array<thread_info*, true> thread_infos(thd->mem_root);
20222034
ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
20232035
PROCESS_LIST_WIDTH);
20242036
Protocol *protocol= thd->protocol;
@@ -2043,6 +2055,7 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
20432055
if (!thd->killed)
20442056
{
20452057
mysql_mutex_lock(&LOCK_thread_count);
2058+
thread_infos.reserve(get_thread_count());
20462059
Thread_iterator it= global_thread_list_begin();
20472060
Thread_iterator end= global_thread_list_end();
20482061
for (; it != end; ++it)
@@ -2092,16 +2105,19 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
20922105
}
20932106
mysql_mutex_unlock(&tmp->LOCK_thd_data);
20942107
thd_info->start_time= tmp->start_time.tv_sec;
2095-
thread_infos.push_front(thd_info);
2108+
thread_infos.push_back(thd_info);
20962109
}
20972110
}
20982111
mysql_mutex_unlock(&LOCK_thread_count);
20992112
}
21002113

2101-
thread_info *thd_info;
2114+
// Return list sorted by thread_id.
2115+
std::sort(thread_infos.begin(), thread_infos.end(), thread_info_compare());
2116+
21022117
time_t now= my_time(0);
2103-
while ((thd_info=thread_infos.get()))
2118+
for (size_t ix= 0; ix < thread_infos.size(); ++ix)
21042119
{
2120+
thread_info *thd_info= thread_infos.at(ix);
21052121
protocol->prepare_for_resend();
21062122
protocol->store((ulonglong) thd_info->thread_id);
21072123
protocol->store(thd_info->user, system_charset_info);

0 commit comments

Comments
 (0)