Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2543157imu; Wed, 21 Nov 2018 13:22:02 -0800 (PST) X-Google-Smtp-Source: AFSGD/UwI6Yqw9219iyCrIZJ7hlTZEHUcg0Au+aqBzkvgNriWRqzoEPnTV0oCCrGN3lKCqHC/7U1 X-Received: by 2002:a63:a91a:: with SMTP id u26mr7390252pge.349.1542835322044; Wed, 21 Nov 2018 13:22:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542835322; cv=none; d=google.com; s=arc-20160816; b=yJtv5IKxirT+rynFOV2/Kc74kdC4Z1uwnMisruhDINTXp1ijnu141wiqIqk+3dzp1n OBBryGwZIAiIoj0GzQEWIVZ1T0jTV6y7I0r/sguNIKjtlUgB1VUX8jmEqSAx12p47e+c QzjkXkCoJF3aXuzwKGAmpwMEaofJNpMpoLGqqS67YDkLERVLgLkOiyqeRdsaEZPI2Lyg ML24hq4p8M2fLtkFkoAfNrWKLWxyzByI4c2aSravS45Q7CRwVdI1wk6lSlZLpLVvmSZ+ 7NxOBgJeJvW7UEeHgIYVOFqYj0SDo/KLI8BRC7BKLYjw5/H2hkaWzRCEq8C6N6vd7kfz n6Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=ARIXVDaQRUZ7X2dcuyortmQVt+ZsZwR9tFSZPo3/9TE=; b=YZN2Am9WmFFHIsgJwTkfkea6pwATuj2QsoVrhPoGhs0Ys47PILeJkSES7vN8kAOK4f gr3Jmswh7dMe2zixOJob7vaVwgklucaFE5dXPDRa34HXH//0FHcrPN6+4CnHfEPBxlH0 X3poXycNcqjhjXW6OzDcPa7JbvjKe0E/gMpWOYYSSvSLJr69IExLzgCjBVx6aRe2rVk9 s61q6N8Lz1mNrnllto7wMcE7VyIOsipf8OOP+pgVRfXbKHbNa3n6Fp0fQOoZEsvmdYSk KaIg15lXsUoCqvtRN2TzwwwXP+c804wFFCbYmNsvEXNVIfJt7nOtArtnKRTSM0cUrp2D TGQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=NMI604Gb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u7si47900023pgg.357.2018.11.21.13.21.47; Wed, 21 Nov 2018 13:22:01 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=NMI604Gb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730250AbeKVFPT (ORCPT + 99 others); Thu, 22 Nov 2018 00:15:19 -0500 Received: from mail.efficios.com ([167.114.142.138]:55630 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726172AbeKVFPT (ORCPT ); Thu, 22 Nov 2018 00:15:19 -0500 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 6EB72133EAA; Wed, 21 Nov 2018 13:39:49 -0500 (EST) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id GQ5iIyF6Tm4u; Wed, 21 Nov 2018 13:39:48 -0500 (EST) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 9AFCD133E89; Wed, 21 Nov 2018 13:39:48 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 9AFCD133E89 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1542825588; bh=ARIXVDaQRUZ7X2dcuyortmQVt+ZsZwR9tFSZPo3/9TE=; h=From:To:Date:Message-Id; b=NMI604GbEyYkhvYdZWLyHiq8Xt+fKcyp8BmARrR19aKEsDP8aw80IF5W6xEL4sOVs iA1+5/WwcKygkiuP0kJ1LzOSXjGZImZWHs9/8QiF3q61NdocdhIjUfhn08mbefQFnX es0I9N5XND6dSUL/WPlMC2eZ85qn98ENBuwnSAgACkexKcnHPhQkIvof3AwvD7dhWy sMmrgBpVHMcD2TmBfflGnedv8QapMC+THI7EAJfvDc+i999Jb8O+G3ppSpYRdnH8TU c1tikxVjhPjdh/eRZQZMYBAajxGyWhYm7JQl50f5DtIl5SjbsUn9uYfK1i51aAdJ3w zTaiDepwOorlA== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id 9Z1FoayEA1yk; Wed, 21 Nov 2018 13:39:48 -0500 (EST) Received: from localhost.localdomain (192-222-157-41.qc.cable.ebox.net [192.222.157.41]) by mail.efficios.com (Postfix) with ESMTPSA id 4DF13133E7E; Wed, 21 Nov 2018 13:39:48 -0500 (EST) 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 v4 2/5] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux Date: Wed, 21 Nov 2018 13:39:33 -0500 Message-Id: <20181121183936.8176-2-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181121183936.8176-1-mathieu.desnoyers@efficios.com> References: <20181121183936.8176-1-mathieu.desnoyers@efficios.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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..e1a206075c 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 -- 2.17.1