Bug #70858 Set thread name
Submitted: 8 Nov 2013 8:58 Modified: 19 Oct 2021 12:10
Reporter: Daniël van Eeden (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Performance Schema Severity:S4 (Feature request)
Version:5.7, 8.0 OS:Any
Assigned to: Marc ALFF CPU Architecture:Any

[8 Nov 2013 8:58] Daniël van Eeden
Description:
Firefox does set a name for each tread. This name will appear in top and other utilities. This is much more usefull than that all threads have the same name as the process.

How to repeat:
$ for task in $(ls /proc/$(pidof firefox)/task/); do name=$(cat /proc/$(pidof firefox)/task/${task}/comm); echo "TASK: ${task} (${name})"; done
TASK: 12620 (DNS Res~er #378)
TASK: 3533 (firefox)
TASK: 3538 (Gecko_IOThread)
TASK: 3539 (Socket Thread)
TASK: 3540 (JS GC Helper)
TASK: 3541 (JS Sour~ Thread)
TASK: 3542 (JS Watchdog)
TASK: 3543 (Hang Monitor)
TASK: 3545 (gdbus)
TASK: 3546 (DOM Worker)
TASK: 3547 (Timer)
TASK: 3548 (mozStorage #1)
TASK: 3550 (DOM Worker)
TASK: 3551 (DOM Worker)
TASK: 3552 (Cache I/O)
TASK: 3554 (HTML5 Parser)
TASK: 3564 (mozStorage #2)
TASK: 3571 (mozStorage #3)
TASK: 3572 (mozStorage #4)
TASK: 3579 (Cert Verify)
TASK: 3580 (Proxy R~olution)
TASK: 3582 (URL Classifier)
TASK: 3588 (localStorage DB)
TASK: 3603 (mozStorage #5)
TASK: 3618 (mozStorage #6)
TASK: 3631 (Analysis Helper)
TASK: 3632 (Analysis Helper)
TASK: 3633 (Analysis Helper)
TASK: 3642 (DOM Worker)
TASK: 3646 (Image Scaler)
TASK: 3655 (MediaManager)
TASK: 3661 (mozStorage #7)
TASK: 3663 (mozStorage #8)
TASK: 3713 (mozStorage #9)
TASK: 3728 (mozStorage #10)
TASK: 3729 (mozStorage #11)
TASK: 3845 (threaded-ml)
TASK: 4852 (gmain)
$ for task in $(ls /proc/$(pidof mysqld)/task/); do name=$(cat /proc/$(pidof mysqld)/task/${task}/comm); echo "TASK: ${task} (${name})"; done
TASK: 12522 (mysqld)
TASK: 12523 (mysqld)
TASK: 12524 (mysqld)
TASK: 12525 (mysqld)
TASK: 12526 (mysqld)
TASK: 12527 (mysqld)
TASK: 12528 (mysqld)
TASK: 12529 (mysqld)
TASK: 12530 (mysqld)
TASK: 12531 (mysqld)
TASK: 12532 (mysqld)
TASK: 12535 (mysqld)
TASK: 12536 (mysqld)
TASK: 12537 (mysqld)
TASK: 12538 (mysqld)
TASK: 12539 (mysqld)
TASK: 12540 (mysqld)
TASK: 12541 (mysqld)
TASK: 12542 (mysqld)
TASK: 12543 (mysqld)
TASK: 12544 (mysqld)

Suggested fix:
Set a usefull name for each thread, but don't leak security sensitive information.

I would expect names like:
- main thread
- purge thread
- connection thread
- slave worker
- dump thread
[8 Nov 2013 9:10] Daniël van Eeden
top will show this (Use 'H' to show threads)

To show this info with ps:
ps -Lo cmd:50,comm -p $(pidof mysqld),$(pidof firefox)
[1 Jan 2014 10:14] MySQL Verification Team
Thanks for the feature request.  The question is, what do we call the threads that  are executing user queries?

FWIW, at least on windows, the background threads already have appropriate names (see attached image).
[1 Jan 2014 10:15] MySQL Verification Team
example

Attachment: threads_on_windows_5.6.15.png (image/png, text), 52.06 KiB.

[2 Jul 2015 6:16] Daniël van Eeden
Related: https://github.com/dveeden/udf_pthread_name

mysql> SELECT set_pthread_name('foobarbaz');
+-------------------------------+
| set_pthread_name('foobarbaz') |
+-------------------------------+
|                             0 |
+-------------------------------+
1 row in set (0.00 sec)
mysql> \! ps -eLo pid,tid,comm | grep $(pgrep -x mysqld)
3880  3880 mysqld
3880  3881 mysqld
3880  3882 mysqld
3880  3883 mysqld
3880  3884 mysqld
3880  3885 mysqld
3880  3886 mysqld
3880  3887 mysqld
3880  3888 mysqld
3880  3889 mysqld
3880  3890 mysqld
3880  3893 mysqld
3880  3894 mysqld
3880  3895 mysqld
3880  3896 mysqld
3880  3897 mysqld
3880  3898 mysqld
3880  3899 mysqld
3880  3900 mysqld
3880  3901 mysqld
3880  3902 mysqld
3880  3911 foobarbaz

This however only works for the current thread.
[18 Apr 2018 12:24] Daniël van Eeden
The same but for ndbmtd:
Bug #90495 	Set thread name for ndbmtd
[19 Oct 2021 12:10] Daniël van Eeden
Looks like this is fixed in 8.0.27 ( https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-27.html )

"To assist monitoring and troubleshooting, the Performance Schema instrumentation is now used to export names of instrumented threads to the operating system. This enables utilities that display thread names, such as debuggers and the Unix ps command, to display distinct mysqld thread names rather than “mysqld”. This feature is supported only on Linux, macOS, and Windows. For more information, see The setup_instruments Table."