From patchwork Fri May 3 18:42:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 1095014 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-101733-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="sZVHqLGB"; dkim=pass (2048-bit key; unprotected) header.d=efficios.com header.i=@efficios.com header.b="jBGUXCdz"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44wgx85CVvz9s9y for ; Sat, 4 May 2019 04:42:52 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=h1wTEAEHD3uxoL8mFK+G7TD3yDhW6fl JQPtNkOg27EomfQ9Yqnku2Ohlb3rXWGNe4/BAB4Pieq17Fi54mBwKt9c16f3zj+E eLpGM46S2sFdkQpvSlHr732tFXF0SvQyE3wXNewUUUygqMypZNVtkZ75T7UbAZIj 739clq9kQkCA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=GvOYmrg4t4pxRajD9Ih2WlWkIo0=; b=sZVHq LGBR9dWqOW5hYopPwRjVuMjtDBoYA7VZMeg4mHWXpcxJkBLlXhbWoJ1qMY0a5cwO uc2scopOPv6bGIb7WKNHbucYW2Ln4G+2njCcxBkPRzSNcjbBnoiLfiqLrWqAc6uM 9shRNbBIREyBA0x9/YxnQ4t89dl9P+m9WQUGRg= Received: (qmail 67355 invoked by alias); 3 May 2019 18:42:30 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 67278 invoked by uid 89); 3 May 2019 18:42:29 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HDKIM-Filter:v2.10.3, 0.8, feng, NEWS X-HELO: mail.efficios.com DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 7DCDC1DD9C8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1556908945; bh=8IWdbQsdeFFX/LTHcnOdmakbfhLqku06FAcvzm3c+/g=; h=From:To:Date:Message-Id; b=jBGUXCdzaOlh55LoRWCf1nvd74iZTCfXIqyCRLatV6CiIYO2bd5WETT0jh0JbNDpV FGU/lWvAClugrSmAmIn1ubOSeGow3nQcRzXAxZzI+5lkg6u+TPNQVU9wmPMn4ydXEu 9xHy3SxjOhF4OXeex817PKFJSrR24/X9EqrSOx08nZ/640VGMfmg3e4uuXLslNFAVu 9q3lgY1WXCyFD99L6wh9aOF3mOZhCz/CLW+Rnoi50A0wySzvrLFqa0b6Wm4sEwt2ot IQLnxCHadMNaLgWEYKvpT+ZKXNWAnN8wjpk3gJyLaMamDX5t98XMcYhj35bNc2jfy2 eVWWhcNtfCamw== From: Mathieu Desnoyers To: Carlos O'Donell Cc: Florian Weimer , Joseph Myers , Szabolcs Nagy , libc-alpha@sourceware.org, Mathieu Desnoyers , Thomas Gleixner , Ben Maurer , Peter Zijlstra , "Paul E. McKenney" , Boqun Feng , Will Deacon , Dave Watson , Paul Turner , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH 2/5] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux (v4) Date: Fri, 3 May 2019 14:42:16 -0400 Message-Id: <20190503184219.19266-3-mathieu.desnoyers@efficios.com> In-Reply-To: <20190503184219.19266-1-mathieu.desnoyers@efficios.com> References: <20190503184219.19266-1-mathieu.desnoyers@efficios.com> When available, use the cpu_id field from __rseq_abi on Linux to implement sched_getcpu(). Fall-back on the vgetcpu vDSO if unavailable. Benchmarks: x86-64: Intel E5-2630 v3@2.40GHz, 16-core, hyperthreading glibc sched_getcpu(): 13.7 ns (baseline) glibc sched_getcpu() using rseq: 2.5 ns (speedup: 5.5x) inline load cpuid from __rseq_abi TLS: 0.8 ns (speedup: 17.1x) Signed-off-by: Mathieu Desnoyers CC: Carlos O'Donell CC: Florian Weimer CC: Joseph Myers CC: Szabolcs Nagy CC: Thomas Gleixner CC: Ben Maurer CC: Peter Zijlstra CC: "Paul E. McKenney" CC: Boqun Feng CC: Will Deacon CC: Dave Watson CC: Paul Turner CC: libc-alpha@sourceware.org CC: linux-kernel@vger.kernel.org CC: linux-api@vger.kernel.org --- Changes since v1: - rseq is only used if both __NR_rseq and RSEQ_SIG are defined. Changes since v2: - remove duplicated __rseq_abi extern declaration. Changes since v3: - update ChangeLog. --- ChangeLog | 5 +++++ sysdeps/unix/sysv/linux/sched_getcpu.c | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 459af8f1a5..3e8ec0de1c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2019-04-23 Mathieu Desnoyers + + * sysdeps/unix/sysv/linux/sched_getcpu.c: use rseq cpu_id TLS on + Linux. + 2019-04-23 Mathieu Desnoyers * NEWS: Add Restartable Sequences feature description. diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c index fb0d317f83..167976fab7 100644 --- a/sysdeps/unix/sysv/linux/sched_getcpu.c +++ b/sysdeps/unix/sysv/linux/sched_getcpu.c @@ -24,8 +24,8 @@ #endif #include -int -sched_getcpu (void) +static int +vsyscall_sched_getcpu (void) { #ifdef __NR_getcpu unsigned int cpu; @@ -37,3 +37,23 @@ sched_getcpu (void) return -1; #endif } + +#ifdef __NR_rseq +#include +#endif + +#if defined __NR_rseq && defined RSEQ_SIG +int +sched_getcpu (void) +{ + int cpu_id = __rseq_abi.cpu_id; + + return cpu_id >= 0 ? cpu_id : vsyscall_sched_getcpu (); +} +#else +int +sched_getcpu (void) +{ + return vsyscall_sched_getcpu (); +} +#endif