Message ID | 2D7KNXY512R77.2ELCGV3AMTY0A@8pit.net |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f406:0:0:0:0:0 with SMTP id g6csp181167wro; Sat, 20 Aug 2022 03:32:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR4YHkcIeaK9TqTq+nIVQL7lOfFnueMTntjRMW8S8eBKDN1zRNHhgYPrSBPYHH9nbo7P3yha X-Received: by 2002:a17:906:c154:b0:733:197:a8c with SMTP id dp20-20020a170906c15400b0073301970a8cmr7574156ejc.483.1660991541661; Sat, 20 Aug 2022 03:32:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660991541; cv=none; d=google.com; s=arc-20160816; b=uKSr82IUFIPM5/H8QV6R+v9mVk6/bRY/FbgEVdCfQ4N/8HB4Jf7TFmuWxsPCtayNQ2 N9Ac4KpX3xB4NZDJM8PhVfd8Cy71j4w3pm8pFHUz1y0CdJe7fLfSvfaUQGV2D4KduwDa 9nhl6ZsClYp3KEXkQwtjIR9Lwwwu7vIsntyWDp6FylGd25GzLMf+isGqN0eTOa1kCa6u HkLOT8zq+G4cZ3MFrh5eKxKXIpev7ZRaewxQWYMeTPmhWMEzLIp3xgfENzb4pKuP5Z16 54mHkEN+9mZN9/bQW9I2320PsWgbkeqb/lJBhB6zQI/2A6ZdqT0bxrfUMYhCQIGUdgGn DG9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:in-reply-to :references:subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=CwAW1oxMwpCP07LZy/eVAY8VAyrCFb5e9q43SUAkv4Q=; b=vBxCgKGE8nj3jM3IMqY7uEAAXmOaTnkrwr30wlPRW1k/qa/f8hKk8Q0YJMv5FNE7AN M0L1SVLLyIh8Why88aKlmZFFxYP6BIzSl9/NcGvH8qnzNihecxEsJIeXXBb4r/6eDWiI 3F4lACfLc1R0PnzxqnLgJ6+vpft76mJfTJsalinKEmSJcwvXzru68f4LTd/x9Y3loaDI oZaMkXMSN9IFeNrGabQNRX/KWi6BaUx65yzFu5LFVoTzFY3vtyr7VvScpceetntrCTMl rx2+swJVImAssxeN3EJvL2zrvyXlXFWJv7sP5uHDRNAwt4y5q2Ks3z0G0aXYBJfTptnU xsYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Q1zRXulM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id jg27-20020a170907971b00b0072b02ef4ae9si5072816ejc.618.2022.08.20.03.32.21 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Aug 2022 03:32:21 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Q1zRXulM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 010C83858413 for <ouuuleilei@gmail.com>; Sat, 20 Aug 2022 10:32:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 010C83858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660991540; bh=CwAW1oxMwpCP07LZy/eVAY8VAyrCFb5e9q43SUAkv4Q=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Q1zRXulMaiQrX0Wsx6loYrkEiMYjI8OHlUTXXPi7gQVV+3ajXEUVZhoyDWDeLYaz3 3K6JhQmUOdWAJBET0sCkjDqBQ4O2vxO3vMRLcHG1ySRdvp1fja2e6MxAN8NYU0m2jK srhyxnQFl+NVF3tEj+d6V0jZv58ULzPJuZ66HQfU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from magnesium.8pit.net (magnesium.8pit.net [45.76.88.171]) by sourceware.org (Postfix) with ESMTPS id C0D553858D39 for <gcc-patches@gcc.gnu.org>; Sat, 20 Aug 2022 10:31:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C0D553858D39 Received: from localhost (p200300f5ff05aa009d746ddc22c1eda1.dip0.t-ipconnect.de [2003:f5:ff05:aa00:9d74:6ddc:22c1:eda1]) by magnesium.8pit.net (OpenSMTPD) with ESMTPSA id 1bdd8cc7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:YES); Sat, 20 Aug 2022 12:31:32 +0200 (CEST) Date: Sat, 20 Aug 2022 12:31:27 +0200 To: Ian Lance Taylor <iant@golang.org> Subject: [PATCH] libgo: Access to glibc-specific field in struct sigevent References: <3P4Q20UQY09GV.2ZECSRTOW6FRW@8pit.net> <CAOyqgcVgWaCvV6cTpZF+Q0rSV5DAdsdCTxOd36L0SWoo302L7Q@mail.gmail.com> In-Reply-To: <CAOyqgcVgWaCvV6cTpZF+Q0rSV5DAdsdCTxOd36L0SWoo302L7Q@mail.gmail.com> Message-Id: <2D7KNXY512R77.2ELCGV3AMTY0A@8pit.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: =?utf-8?q?S=C3=B6ren_Tempel_via_Gcc-patches?= <gcc-patches@gcc.gnu.org> Reply-To: =?utf-8?b?U8O2cmVu?= Tempel <soeren@soeren-tempel.net> Cc: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1741675866474741648?= X-GMAIL-MSGID: =?utf-8?q?1741675866474741648?= |
Series |
libgo: Access to glibc-specific field in struct sigevent
|
|
Commit Message
Sören Tempel
Aug. 20, 2022, 10:31 a.m. UTC
Hi Ian, Thanks for your input! 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. Sincerely, Sören Ian Lance Taylor <iant@golang.org> wrote: > On Fri, Aug 12, 2022 at 10:21 AM Sören Tempel <soeren@soeren-tempel.net> 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. Hence > > reporting it here. > > > > The setThreadCPUProfiler implementation in libgo/go/runtime/os_linux.go > > contains the following code: > > > > var sevp _sigevent > > sevp.sigev_notify = _SIGEV_THREAD_ID > > sevp.sigev_signo = _SIGPROF > > *((*int32)(unsafe.Pointer(&sevp._sigev_un))) = int32(mp.procid) > > > > I am not entirely sure, but I assume the last line is supposed to access > > 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). > > > > 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 is 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 requires 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 solution > > for this issue? > > I think you're right that a tiny C function is the way to go here. > > Ian
Comments
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ören Sören Tempel <soeren@soeren-tempel.net> wrote: > Hi Ian, > > Thanks for your input! > > 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. > > Sincerely, > Sören > > 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 > } > > +func setProcID(uintptr, int32) > + > func getProcID() uint64 { > return uint64(gettid()) > } > @@ -365,7 +367,7 @@ func setThreadCPUProfiler(hz int32) { > var sevp _sigevent > sevp.sigev_notify = _SIGEV_THREAD_ID > sevp.sigev_signo = _SIGPROF > - *((*int32)(unsafe.Pointer(&sevp._sigev_un))) = int32(mp.procid) > + setProcID(uintptr(unsafe.Pointer(&sevp)), int32(mp.procid)) > ret := timer_create(_CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid) > if ret != 0 { > // If we cannot create a timer for this M, leave profileTimerValid 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 handler) > sa->sa_sigaction = (void*)(handler); > } > > +void setProcID(uintptr_t, int32_t) > + __asm__ (GOSYM_PREFIX "runtime.setProcID"); > + > +void > +setProcID(uintptr_t ptr, int32_t v) > +{ > + struct sigevent *s = (void *)ptr; > + s->sigev_notify_thread_id = v; > +} > + > // C code to fetch values from the siginfo_t and ucontext_t pointers > // passed to a signal handler. > > Ian Lance Taylor <iant@golang.org> wrote: > > On Fri, Aug 12, 2022 at 10:21 AM Sören Tempel <soeren@soeren-tempel.net> 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. Hence > > > reporting it here. > > > > > > The setThreadCPUProfiler implementation in libgo/go/runtime/os_linux.go > > > contains the following code: > > > > > > var sevp _sigevent > > > sevp.sigev_notify = _SIGEV_THREAD_ID > > > sevp.sigev_signo = _SIGPROF > > > *((*int32)(unsafe.Pointer(&sevp._sigev_un))) = int32(mp.procid) > > > > > > I am not entirely sure, but I assume the last line is supposed to access > > > 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). > > > > > > 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 is 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 requires 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 solution > > > for this issue? > > > > I think you're right that a tiny C function is the way to go here. > > > > Ian
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 } +func setProcID(uintptr, int32) + func getProcID() uint64 { return uint64(gettid()) } @@ -365,7 +367,7 @@ func setThreadCPUProfiler(hz int32) { var sevp _sigevent sevp.sigev_notify = _SIGEV_THREAD_ID sevp.sigev_signo = _SIGPROF - *((*int32)(unsafe.Pointer(&sevp._sigev_un))) = int32(mp.procid) + setProcID(uintptr(unsafe.Pointer(&sevp)), int32(mp.procid)) ret := timer_create(_CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid) if ret != 0 { // If we cannot create a timer for this M, leave profileTimerValid 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 handler) sa->sa_sigaction = (void*)(handler); } +void setProcID(uintptr_t, int32_t) + __asm__ (GOSYM_PREFIX "runtime.setProcID"); + +void +setProcID(uintptr_t ptr, int32_t v) +{ + struct sigevent *s = (void *)ptr; + s->sigev_notify_thread_id = v; +} + // C code to fetch values from the siginfo_t and ucontext_t pointers // passed to a signal handler.