From owner-freebsd-hackers@freebsd.org Mon Aug 13 00:24:09 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 CE1EA105C63E for ; Mon, 13 Aug 2018 00:24:09 +0000 (UTC) (envelope-from yuripv@yuripv.net) Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 5CF3A811FC for ; Mon, 13 Aug 2018 00:24:09 +0000 (UTC) (envelope-from yuripv@yuripv.net) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 8967E21161; Sun, 12 Aug 2018 20:24:08 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Sun, 12 Aug 2018 20:24:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yuripv.net; h=cc :content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=KSfyyikK5Fc5Th3kSexCP/Q9y0LpiJH1638FMmQqvlw=; b=VcfCNW7w CREfr8cPg13gYqnX9wRk+kD86s8gOAqG32YxaXOkNkpdo83t052N40iaSIpEVSdn qSnuHITKTKrDb9UgXD1FNRnDME82gl5sCrfl7zcm2BHIdmW5uWaUU3w8U/Z2GEQY 5+5Xb5xawPXBaZxM6n81Jd0LhbEbBCH9k8V1CYdZi9UWW/RGrdV/csFfbCiJIP8k Zf7qy/yEUIC8HM+aboqD2Y4I7Av5pm44827Q87AE5OovkVnpSLocqSA39Yn0/Dzg iuoY09hYkxYOj7l3rub18agX2n+wH0FM2hhDmr7gVaQss4h6kVg1v6WgpPESXf2R 6hxCE489pumn8w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=KSfyyikK5Fc5Th3kSexCP/Q9y0Lpi JH1638FMmQqvlw=; b=wBQ7SRFZdDf3CCv30UUU7EV3wGmQN7vgUnuSt3jmpfKRe /SNzlRyqrQzss43J4+sP93AHOAEXprdYAdRSCW79ndT74g9oWCyZnhvVxZZgmFVk Xv3dXvcAjsgYNTuHVkAoRhqCfjq6Gwi/fesenxOdzqfMVZLrxc9M6rtnZ/Zjk8+e LefjJtSbagBLjF/Pfv/CZCmkqcKUNugQSRjTHWM0MJSonbW2uI5jpUcf3xGlCWsz JR1YHOplSHOp1tyKa/uXfALvF2nJoWFMCE1+AIkNboCqu62uU6XVY9nPlYv5wkI2 VtDe50TvHO35zheNh0Qz4L/g2hEvtqIvKRkmJQWwA== X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.2] (unknown [62.183.125.27]) by mail.messagingengine.com (Postfix) with ESMTPA id 7FF04E454E; Sun, 12 Aug 2018 20:24:06 -0400 (EDT) Subject: Re: Write a version for pthread_get_name_np To: Konstantin Belousov , Willem Jan Withagen 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> From: Yuri Pankov Message-ID: <34158602-9806-0553-af1c-e8dacbb0ffa0@yuripv.net> Date: Mon, 13 Aug 2018 03:24:03 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 In-Reply-To: <20180812234800.GD2340@kib.kiev.ua> Content-Type: multipart/mixed; boundary="------------1453E18930BCF03D3E4A4C2E" Content-Language: en-US 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 00:24:10 -0000 This is a multi-part message in MIME format. --------------1453E18930BCF03D3E4A4C2E Content-Type: text/plain; charset=koi8-r; format=flowed Content-Transfer-Encoding: 8bit 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? >> And even though (struct thread *) has a field: >> lwpid_t td_tid; /* (b) Thread ID. */ >> that field seems to be stuck at value td_tid = 32767 every time I look >> at it with gdb. >> >> So it is a pity that this is not going to work this way. --------------1453E18930BCF03D3E4A4C2E Content-Type: text/plain; charset=UTF-8; name="pthread.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="pthread.diff" diff --git a/share/man/man3/Makefile b/share/man/man3/Makefile index 6197b0affb4d..ff2cb02827d4 100644 --- a/share/man/man3/Makefile +++ b/share/man/man3/Makefile @@ -334,6 +334,7 @@ PTHREAD_MLINKS+=pthread_rwlock_rdlock.3 pthread_rwlock_tryrdlock.3 PTHREAD_MLINKS+=pthread_rwlock_wrlock.3 pthread_rwlock_trywrlock.3 PTHREAD_MLINKS+=pthread_schedparam.3 pthread_getschedparam.3 \ pthread_schedparam.3 pthread_setschedparam.3 +PTHREAD_MLINKS+=pthread_set_name_np.3 pthread_get_name_np.3 PTHREAD_MLINKS+=pthread_spin_init.3 pthread_spin_destroy.3 \ pthread_spin_lock.3 pthread_spin_trylock.3 \ pthread_spin_lock.3 pthread_spin_unlock.3 diff --git a/share/man/man3/pthread_set_name_np.3 b/share/man/man3/pthread_set_name_np.3 index c8e5cb4f62c0..cb32e52e2baf 100644 --- a/share/man/man3/pthread_set_name_np.3 +++ b/share/man/man3/pthread_set_name_np.3 @@ -24,17 +24,20 @@ .\" .\" $FreeBSD$ .\" -.Dd December 2, 2016 +.Dd August 12, 2018 .Dt PTHREAD_SET_NAME_NP 3 .Os .Sh NAME +.Nm pthread_get_name_np , .Nm pthread_set_name_np -.Nd set the thread name +.Nd set and retrieve the thread name .Sh LIBRARY .Lb libpthread .Sh SYNOPSIS .In pthread_np.h .Ft void +.Fn pthread_get_name_np "pthread_t thread" "char *name" "size_t len" +.Ft void .Fn pthread_set_name_np "pthread_t thread" "const char *name" .Sh DESCRIPTION The @@ -43,11 +46,30 @@ function applies a copy of the given .Fa name to the given .Fa thread . +.Pp +The +.Fn pthread_get_name_np +retrieves the +.Fa name +associated with +.Fa thread . +If +.Fn pthread_set_name_np +was not previously called for +.Fa thread , +the buffer pointed to by +.Fa name +will be empty. .Sh ERRORS -Because of the debugging nature of this function, all errors that may +Because of the debugging nature of these functions, all errors that may appear inside are silently ignored. .Sh SEE ALSO .Xr thr_set_name 2 +.Sh STANDARDS +.Fn pthread_set_name_np +and +.Fn pthread_get_name_np +functions are non-standard extensions. .Sh AUTHORS This manual page was written by .An Alexey Zelkin Aq Mt phantom@FreeBSD.org . --------------1453E18930BCF03D3E4A4C2E--