From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from magnesium.8pit.net (magnesium.8pit.net [45.76.88.171]) by sourceware.org (Postfix) with ESMTPS id 6AB1B385AC30 for ; Mon, 5 Sep 2022 12:31:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6AB1B385AC30 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=soeren-tempel.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=soeren-tempel.net DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; s=opensmtpd; bh=UJBAnmX+uE XBBklAIpqsJEbjW/f1MPp0iLnI7Ze4ElQ=; h=in-reply-to:references:from: subject:cc:to:date; d=soeren-tempel.net; b=C1iB6Q6Ib+wNWLavV2/ojLN0BRk qFKOHeyMDHW4Flyk1lKbXWviACLU4jgszLDz0MH9nNAAlz+G5p49FjnkuN1KoiC16zR6Jy kxDwf/xSwXh8s341clsAKxUwAiwuBfthjGWrKDCWis29FZVLEPHgEgKlNpgkWMZPxOuHCj feik= Received: from localhost (ws117.exhibition.haw-hamburg.de [141.22.222.117]) by magnesium.8pit.net (OpenSMTPD) with ESMTPSA id c18f80be (TLSv1.3:TLS_AES_256_GCM_SHA384:256:YES); Mon, 5 Sep 2022 14:31:12 +0200 (CEST) Date: Mon, 05 Sep 2022 14:31:07 +0200 To: Ian Lance Taylor Cc: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: Re: [PATCH] libgo: Access to glibc-specific field in struct sigevent From: =?UTF-8?Q?S=C3=B6ren?= Tempel References: <3P4Q20UQY09GV.2ZECSRTOW6FRW@8pit.net> <2D7KNXY512R77.2ELCGV3AMTY0A@8pit.net> In-Reply-To: <2D7KNXY512R77.2ELCGV3AMTY0A@8pit.net> Message-Id: <31V5NDVG5VHCB.3UZCG4NASKJ94@8pit.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hello Ian, Have you had the chance to look at this yet? Let me know if there is a better way to do this. Greetings, S=C3=B6ren S=C3=B6ren Tempel wrote: > Hi Ian, >=20 > Thanks for your input! >=20 > I am not familiar with Go runtime internals at all, but the patch below > at least compiles for me. Let me know if this works for you too / if > there is a better way to do this. The untyped uintptr_t seems to be > necessary as otherwise I encountered errors about &sevp escaping to the > heap. >=20 > Sincerely, > S=C3=B6ren >=20 > diff --git a/libgo/go/runtime/os_linux.go b/libgo/go/runtime/os_linux.go > index 96fb1788..6653d85e 100644 > --- a/libgo/go/runtime/os_linux.go > +++ b/libgo/go/runtime/os_linux.go > @@ -22,6 +22,8 @@ type mOS struct { > profileTimerValid uint32 > } >=20 > +func setProcID(uintptr, int32) > + > func getProcID() uint64 { > return uint64(gettid()) > } > @@ -365,7 +367,7 @@ func setThreadCPUProfiler(hz int32) { > var sevp _sigevent > sevp.sigev_notify =3D _SIGEV_THREAD_ID > sevp.sigev_signo =3D _SIGPROF > - *((*int32)(unsafe.Pointer(&sevp._sigev_un))) =3D int32(mp.procid)= > + setProcID(uintptr(unsafe.Pointer(&sevp)), int32(mp.procid)) > ret :=3D timer_create(_CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid) > if ret !=3D 0 { > // If we cannot create a timer for this M, leave profileT= imerValid false > diff --git a/libgo/runtime/go-signal.c b/libgo/runtime/go-signal.c > index 528d9b6d..347b24e2 100644 > --- a/libgo/runtime/go-signal.c > +++ b/libgo/runtime/go-signal.c > @@ -183,6 +183,16 @@ setSigactionHandler(struct sigaction* sa, uintptr ha= ndler) > sa->sa_sigaction =3D (void*)(handler); > } >=20 > +void setProcID(uintptr_t, int32_t) > + __asm__ (GOSYM_PREFIX "runtime.setProcID"); > + > +void > +setProcID(uintptr_t ptr, int32_t v) > +{ > + struct sigevent *s =3D (void *)ptr; > + s->sigev_notify_thread_id =3D v; > +} > + > // C code to fetch values from the siginfo_t and ucontext_t pointers > // passed to a signal handler. >=20 > Ian Lance Taylor wrote: > > On Fri, Aug 12, 2022 at 10:21 AM S=C3=B6ren Tempel wrote: > > > > > > This is one of the few remaining issues regarding musl compatibility = for > > > gofrontend. Unfortunately, I am not sure how to best fix this one. He= nce > > > reporting it here. > > > > > > The setThreadCPUProfiler implementation in libgo/go/runtime/os_linux.= go > > > contains the following code: > > > > > > var sevp _sigevent > > > sevp.sigev_notify =3D _SIGEV_THREAD_ID > > > sevp.sigev_signo =3D _SIGPROF > > > *((*int32)(unsafe.Pointer(&sevp._sigev_un))) =3D int32(mp.pro= cid) > > > > > > I am not entirely sure, but I assume the last line is supposed to acc= ess > > > the thread ID in struct sigevent. Unfortunately, it does so via the > > > glibc-specific _sigev_un member which doesn't work on musl libc (the > > > union is named __sev_fields on musl and hence causes a build failure)= =2E > > > > > > A portable way to access the thread ID in struct sigevent is via > > > sigev_notify_thread_id which is documented in timer_create(2). This i= s a > > > macro provided in siginfo.h from linux-headers for glibc [1] and in > > > signal.h for musl [2]. However, since it is macro it probably require= s a > > > C wrapper function in order to be usable in libgo. Would be possible = to > > > add that somehow to libgo or is there an alternative feasible solutio= n > > > for this issue? > >=20 > > I think you're right that a tiny C function is the way to go here. > >=20 > > Ian