Discussion:
Getting pthread names
Manuel Stühn
2018-04-30 11:14:34 UTC
Permalink
Hi,

for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Richard Yao
2018-04-30 13:25:14 UTC
Permalink
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for retrieving the name i found nothing. Is there any api like pthread_getname_np for FreeBSD? Or is there another way to retrieve the threads name within an application?
The only time that I have ever seen the names listed was when attaching gdb to a process on Linux and listing the threads. I have not tried it on FreeBSD. I suggest attaching gdb to see if it displays them. If it works, then you could look into how gdb gets them.

I hope that helps.
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
Konstantin Belousov
2018-04-30 13:57:11 UTC
Permalink
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Not like pthread_getname_np(), but still something. You can use
(binary) sysctl kern.proc.pid.<pid> to get struct kinfo_proc for all
threads. In the structure, the ki_tdname() member contains the thread
name as set by pthread_set_name_np(3).
Richard Yao
2018-04-30 14:05:13 UTC
Permalink
It sounds like someone could use that to implement pthread_getname_np(3).

https://www.freebsd.org/cgi/man.cgi?query=sysctl&sektion=3&apropos=0&manpath=FreeBSD+11.1-RELEASE+and+Ports
Post by Konstantin Belousov
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Not like pthread_getname_np(), but still something. You can use
(binary) sysctl kern.proc.pid.<pid> to get struct kinfo_proc for all
threads. In the structure, the ki_tdname() member contains the thread
name as set by pthread_set_name_np(3).
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
Manuel Stühn
2018-04-30 19:25:06 UTC
Permalink
Post by Konstantin Belousov
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Not like pthread_getname_np(), but still something. You can use
(binary) sysctl kern.proc.pid.<pid> to get struct kinfo_proc for all
threads. In the structure, the ki_tdname() member contains the thread
name as set by pthread_set_name_np(3).
Thank you (all) for your answer(s) and hints! I'll have a look into
this.

BR
Manuel
Willem Jan Withagen
2018-05-01 20:10:32 UTC
Permalink
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Not like pthread_getname_np(), but still something.  You can use
(binary) sysctl kern.proc.pid.<pid> to get struct kinfo_proc for all
threads.  In the structure, the ki_tdname() member contains the thread
name as set by pthread_set_name_np(3).
Thank you (all) for your answer(s) and hints! I'll have a look into this.
I asked the same question some time ago, because Ceph also uses this in
all kinds of analysis. It is/was still on my todo list, but since it is
only informational, it is not high on priority.

But would be more than interested to test/use an implementation.

--WjW
Julian Elischer
2018-05-02 02:38:55 UTC
Permalink
Post by Konstantin Belousov
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Not like pthread_getname_np(), but still something. You can use
(binary) sysctl kern.proc.pid.<pid> to get struct kinfo_proc for all
threads. In the structure, the ki_tdname() member contains the thread
name as set by pthread_set_name_np(3).
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
and ps in the kernel debugger shows it as well from memory

check top and ps in "show thread" mode.... at one stage I remember
writing code to show thread names

but I can't remember what programs it went into..  (around 2001),


I suspect that you are expected to remember your own name. the name
writing is so other processes can get that information.
Daniel Eischen
2018-05-02 09:03:25 UTC
Permalink
Post by Julian Elischer
Post by Konstantin Belousov
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for
retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve the
threads name within an application?
Not like pthread_getname_np(), but still something. You can use
(binary) sysctl kern.proc.pid.<pid> to get struct kinfo_proc for all
threads. In the structure, the ki_tdname() member contains the thread
name as set by pthread_set_name_np(3).
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
and ps in the kernel debugger shows it as well from memory
check top and ps in "show thread" mode.... at one stage I remember writing code to show thread names
but I can't remember what programs it went into.. (around 2001),
I suspect that you are expected to remember your own name. the name writing is so other processes can get that information.
I think I toyed with idea of adding pthread_set_name_np() back in the old libc_r days. I'm not sure why I didn't?

The problem with remembering to
what you set each thread's name, is that the names may be set by libraries or code out of your control. Maybe not a common use case, though. The set name function is void return, so you can't even get the name to see if the setter actually worked. I think the libc_r version just did a strdup() of sorts, so it could be as long as necessary (bug or feature?). The current limit seems to be MAXCOMLEN, which is 19, and I think just silently fails if more than that.

--
DE
Eric van Gyzen
2018-05-02 14:23:47 UTC
Permalink
The current limit seems to be MAXCOMLEN, which is 19, and I think
just silently fails if more than that.
It used to silently fail, but now it silently truncates.

https://svnweb.freebsd.org/base?view=revision&revision=309460

Eric

Ian Lepore
2018-04-30 14:17:09 UTC
Permalink
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but
for 
retrieving the name i found nothing. Is there any api like 
pthread_getname_np for FreeBSD? Or is there another way to retrieve
the 
threads name within an application?
The applications I know of that can display thread names (ps, top,
procstat) retrieve it using libprocstat and the procstat_getprocs(3)
function. The libprocstat functions access the information via /dev/kvm
or sysctl, but those interfaces return raw binary kernel data that
userland should not try to directly interpret for themselves.

-- Ian
Jan Knepper
2018-04-30 14:24:36 UTC
Permalink
I think the pthread_set_name_np(3) function is for debugging (reporting)
purposes.

The names show up when you use 'procstat', 'top', 'ps', etc.

If you want to give a thread a name and use that name later in code why
not keep that internally in you thread management structures/code and
use that same name in the call to pthread_set_name_np?

ManiaC++
Jan Knepper
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but
for retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve
the threads name within an application?
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to
Richard Yao
2018-04-30 14:53:26 UTC
Permalink
I think the pthread_set_name_np(3) function is for debugging (reporting) purposes.
It is.
The names show up when you use 'procstat', 'top', 'ps', etc.
If you want to give a thread a name and use that name later in code why not keep that internally in you thread management structures/code and use that same name in the call to pthread_set_name_np?
I cannot speak for him, but sometimes it is easier to just let the OS do stuff for you. That being said, if that is what he wants, it would be trivial to do using thread specific data:

https://docs.oracle.com/cd/E19120-01/open.solaris/816-5137/tlib-40012/index.html

The GNU extension is just a special case of TSD that was implemented to aid debugging as far as I can tell. It is separate from the pthreads functionality for implementing this though.
ManiaC++
Jan Knepper
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but for retrieving the name i found nothing. Is there any api like pthread_getname_np for FreeBSD? Or is there another way to retrieve the threads name within an application?
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
Jan Knepper
2018-04-30 17:49:54 UTC
Permalink
Post by Richard Yao
Post by Jan Knepper
I think the pthread_set_name_np(3) function is for debugging
(reporting) purposes.
It is.
:-)
Post by Richard Yao
Post by Jan Knepper
The names show up when you use 'procstat', 'top', 'ps', etc.
If you want to give a thread a name and use that name later in code
why not keep that internally in you thread management structures/code
and use that same name in the call to pthread_set_name_np?
I cannot speak for him, but sometimes it is easier to just let the OS
do stuff for you. That being said, if that is what he wants, it would
My experience as a software engineer is... If the OS or (standard)
library can do something for you, let the OS or (standard) library do
it. So, agreed.

It is also 'trivial' to do when you have any form or thread management
or any form of thread data. Indeed using a 'thread specific data'
solution might be helpful.
Post by Richard Yao
Post by Jan Knepper
Post by Manuel Stühn
Hi,
for setting a name for pthreads i found pthread_set_name_np(3), but
for retrieving the name i found nothing. Is there any api like
pthread_getname_np for FreeBSD? Or is there another way to retrieve
the threads name within an application?
_______________________________________________
ManiaC++
Jan Knepper
Loading...