From patchwork Mon Jan 21 21:35:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 1028919 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-99470-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="Dm3f/r6o"; dkim=pass (2048-bit key; unprotected) header.d=efficios.com header.i=@efficios.com header.b="Jizh9qBr"; 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 43k4bw0cLwz9s55 for ; Tue, 22 Jan 2019 08:35:55 +1100 (AEDT) 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=TOdF0CuX5xAFJaPsp+zTcNxF9uzoKma c3+hzcuLqm8HAmd0ct8Hyfq41EB68qzLdavD9suPZBNVcz7fVX/m9qG6P1z809CD rgwVxskV2PTQ2dABbR7mbuWsLNoYJ77uM43Cmx1xDLqpuLDX9Z+MeJr5t+wmr6qN UcGYbQzhnQi0= 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=HQ/R83LY+Mf40KjdjvKHMYaQd+o=; b=Dm3f/ r6ofMJwJUOJbIwU6MYsPkhbHy/l9Y+Ghbc4Bdc13hG2uFEEmPr65C+A3s1vkOlTa s2UG23HV/v86eZHh9aYHC4eNkPOZe4J6CmNsELehf7tbeZkxmZD9dYKGjvPUNScZ j6Rdx1Si+qWBD9XO3tfhaqSo0Z/Iv3Wbzood6Y= Received: (qmail 120297 invoked by alias); 21 Jan 2019 21:35:40 -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 120273 invoked by uid 89); 21 Jan 2019 21:35:39 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=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.2 spammy=ben, speedup, Ben, Peter X-HELO: mail.efficios.com DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 6663CB4FCD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1548106535; bh=emmEvvmvg4LpJ76voI4pvfqiprYl4MAXxEB0bZnS5JQ=; h=From:To:Date:Message-Id; b=Jizh9qBrya/Q0V864umhXR3X0rA2rEM5kNDJ3OwNfSQCHqxi9QF9i+Z5J8pTjDDUB gqoy12CgBmnnSb7Ux8D/7LFIvxbo7g68ZBYmAmmBhbhXVwWMphrFPyDyx0QTwcz8nf WQFxbmPWLb2tP545uqfRNwOuVnsjpvwZQjasZdFf7c2YBgCyWMsguiMeXz3LxsL4Ro /YZ6ydxjVt0LEgAAxvbNNURl7wCy12DYxgZxG15/4Mv2JVvz5bC5NY9tDRoIcQFImD ci2aKWaX039ObPA1shUO/smP0n//OfqBTeCPaHBR4fJJiYf2TYgTmiOQYJeKA+Lx6X EZneLj37YhuxA== 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: [RFC PATCH glibc 2/4] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux Date: Mon, 21 Jan 2019 16:35:28 -0500 Message-Id: <20190121213530.23803-2-mathieu.desnoyers@efficios.com> In-Reply-To: <20190121213530.23803-1-mathieu.desnoyers@efficios.com> References: <20190121213530.23803-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 --- sysdeps/unix/sysv/linux/sched_getcpu.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c index b69eeda15c..37fda59d36 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,24 @@ sched_getcpu (void) return -1; #endif } + +#ifdef __NR_rseq +#include + +extern __attribute__ ((tls_model ("initial-exec"))) +__thread volatile struct rseq __rseq_abi; + +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