From owner-freebsd-hackers@freebsd.org Mon Aug 13 08:16:32 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 275E31069EFB for ; Mon, 13 Aug 2018 08:16:32 +0000 (UTC) (envelope-from wjw@digiware.nl) Received: from smtp.digiware.nl (smtp.digiware.nl [IPv6:2001:4cb8:90:ffff::3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B6E808F8EA for ; Mon, 13 Aug 2018 08:16:31 +0000 (UTC) (envelope-from wjw@digiware.nl) Received: from router.digiware.nl (localhost.digiware.nl [127.0.0.1]) by smtp.digiware.nl (Postfix) with ESMTP id 75B5E802C; Mon, 13 Aug 2018 10:16:29 +0200 (CEST) X-Virus-Scanned: amavisd-new at digiware.com Received: from smtp.digiware.nl ([127.0.0.1]) by router.digiware.nl (router.digiware.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZJS5wj8dj52m; Mon, 13 Aug 2018 10:16:28 +0200 (CEST) Received: from [192.168.11.152] (unknown [192.168.11.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.digiware.nl (Postfix) with ESMTPSA id 7734E8026; Mon, 13 Aug 2018 10:16:28 +0200 (CEST) Subject: Re: Write a version for pthread_get_name_np To: Yuri Pankov , Konstantin Belousov Cc: FreeBSD Hackers References: <7fa2b876-9397-da2b-cb29-56badf11b66d@ecoracks.nl> <42689feb-9bd4-429b-63d7-b9193123ee47@digiware.nl> <20180812205835.GB2340@kib.kiev.ua> <20180812225726.GC2340@kib.kiev.ua> <20180812234800.GD2340@kib.kiev.ua> <34158602-9806-0553-af1c-e8dacbb0ffa0@yuripv.net> From: Willem Jan Withagen Message-ID: Date: Mon, 13 Aug 2018 10:16:30 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <34158602-9806-0553-af1c-e8dacbb0ffa0@yuripv.net> Content-Type: text/plain; charset=koi8-r; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Aug 2018 08:16:32 -0000 On 13/08/2018 02:24, Yuri Pankov wrote: > Konstantin Belousov wrote: >> On Mon, Aug 13, 2018 at 01:36:28AM +0200, Willem Jan Withagen wrote: >>> On 13/08/2018 00:57, Konstantin Belousov wrote: >>>> On Mon, Aug 13, 2018 at 12:01:03AM +0200, Willem Jan Withagen wrote: >>>>> On 12/08/2018 22:58, Konstantin Belousov wrote: >>>>>> On Sun, Aug 12, 2018 at 10:33:06PM +0200, Willem Jan Withagen wrote: >>>>>>> Hi, >>>>>>> >>>>>>> For some porting I'm trying to write a pthread_get_name_np(), but >>>>>>> I have run into a snag.... >>>>>>> Tried some the the procstat code I would come up with: >>>>>>> >>>>>>> int pthread_get_name_np(pthread_t *thread, >>>>>>> ššš ššššššššššššššššššššššš char *name, size_t len){ >>>>>>> ššš ššš struct procstat *procstat; >>>>>>> ššš ššš int pid = getpid(); >>>>>>> ššš ššš unsigned int count, i; >>>>>>> ššš ššš struct kinfo_proc *kip, *kipp; >>>>>>> >>>>>>> ššš ššš procstat = procstat_open_sysctl(); >>>>>>> ššš ššš kip = procstat_getprocs(procstat, KERN_PROC_PID | >>>>>>> KERN_PROC_INC_THREAD, >>>>>>> ššš ššššššššššš pid, &count); >>>>>>> ššš ššš for (i = 0; i < count; i++) { >>>>>>> ššš ššš ššš kipp = &kip[i]; >>>>>>> ššš ššš ššš printf("thread: %ld(0x%lx), %ld(0x%lx)\n", thread, >>>>>>> thread, >>>>>>> kipp->ki_tid, kipp->ki_tid); >>>>>>> ššš ššš ššš if (thread == kipp->ki_tid) { >>>>>>> ššš ššš ššš ššš kinfo_proc_thread_name(kipp, name, len); >>>>>>> ššš ššš ššš } >>>>>>> ššš ššš } >>>>>>> ššš ššš return 0; >>>>>>> } >>>>>>> >>>>>>> Problem only is that the TID value in what procstat_getprocs >>>>>>> returns in >>>>>>> ki_tid is nowhere near >>>>>>> what pthread_self() returns. >>>>>>> But both manual page and the include file describe the value as >>>>>>> Thread ID. >>>>>>> Only in sys/user.h the type is >>>>>>> ššš ššš lwpid_t ki_tid;šššššššššššššššš /* XXXKSE thread id */ >>>>>>> >>>>>>> What do I need to translate one tid into the other so I can really >>>>>>> compare them? >>>>>> >>>>>> You need to cast pthread_t * to struct thread *, this is an internal >>>>>> libthr structure which represents the thread in userspace.š The >>>>>> structure field tid gives you the tid you want to pass to sysctl. >>>>> >>>>> mmmm, >>>>> >>>>> I'm afraid I'm not quit able to followup on your answer. >>>>> >>>>> Trying to find what to include to be able to cast this, I can only >>>>> find >>>>> a small bit defined in /usr/src/lib/libthr/thread/thr_private.h. >>>>> /* >>>>> ššš * lwpid_t is 32bit but kernel thr API exports tid as long type >>>>> ššš * to preserve the ABI for M:N model in very early date (r131431). >>>>> ššš */ >>>>> #define TID(thread)šššš ((uint32_t) ((thread)->tid)) >>>>> >>>>> But that I cannot just "include" that file without a lot of mess, and >>>>> then still it does not compile. >>>>> >>>>> So could you point me to where this private part of struct thread is >>>>> hidding? >>>> I do not understand your confusion. The thr_private.h header is only >>>> for use by libthr, the FreeBSD threading library implementation. The >>>> function that you are trying to implement, requires understanding of >>>> the >>>> libthr internals and can only be implemented as part of libthr. >>>> >>>> Any other attempt to translate libthr handle for thread into tid needs >>>> same access to the struct pthread. >>> >>> Oke, >>> >>> I don't have more knowledge of (p)threads than just how to use it. >>> I was trying to do this as a piece of code in the compat.cc of my >>> ceph port. >>> >>> But trying to reread what you write is boils down to: there is no way to >>> link a value returned by (struct pthread *)pthread_self() to the kd_tid >>> value that can be found in the (struct kinfo_proc *) describing that >>> same thread. >> No, this is not what I said. >> >> I am claiming that this functionality must be implemented in libthr. >> So if you want this function, it needs to be added to libthr, and it is >> quite trivial to do. >> >> You can code it, or you might provide (draft of the) man page, and I >> will code it according to the spec. > > How about the attached patch for man page part? Thanx, I guess that'll work. Although I was following the procstat way and when there was no name set we get a '-'. But if Konstatin is going to write it, it is his prerogative. And I would expect it to be equivalent partner of thr_{set,get}_name as well. But that could be added later in the man page. --WjW