Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2023859imd; Fri, 2 Nov 2018 04:53:48 -0700 (PDT) X-Google-Smtp-Source: AJdET5fXDX3wMixk97rNE8YT9Y450LUCBH9H+drOWf/BJIWV8iTKl9sSz4cOrw6VDKqcrQgAL2AP X-Received: by 2002:a17:902:27a8:: with SMTP id d37-v6mr9775000plb.0.1541159628436; Fri, 02 Nov 2018 04:53:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541159628; cv=none; d=google.com; s=arc-20160816; b=p1RuJlfd3Bu+/FqJcWmpsQlMI5Q3ISFvGrv0M/brgE5RG4WyA28WUlNtbc9NRIaiqi Ggu85afjEwUl88dKc9GXkn/KyVm7AzLuQQM4jCo1kv6mN0uLspJRDTRTYergXCZiOHsJ CSI15owVw2AZSxVJhlOZIoymh3J4WgV+wirGA2F01gfEsIVBToxYv6nCpYsc5YOJDHfY ihhOfp0HE+BRUY66rBoLwUJec5vCmsb4RhG96gfGe0jha0KKlLjKyvLgHmUJzmH7KRV+ LMkQkOMuMpA3W9vLvMvb8u/oqR1M2Mw4jHZWJQPgjA1wjMYfBIjauqAdB03IE22/cS7c q32w== 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=pMkAJ9vXhyTi3jnZBr+WZQpa6uBYTuxn8TXb5sfGUqlvNOeP2C5CnmcQIg3BoWWeRj wqv7ht9cGjzObfc9TKXJ/ZnteAOsZaj2E7uagH3EinFH0Cb40NDqVNJLOa+njmq32OB7 sA0MpTnnZmhtWuMSv45mjnrwVHGiZfv3iwr31go/83Go61Yjjbu2SbywQKjHUG2THAhN lAE7pID1868UAilleH3TlkXpwxo7/huMh82dR4bWAdTvvu9QBuXK61bU6DJvwJcEGna4 HA1/ykzHbtRZf4k/vuspEYmHuNr6/J5giFPMBI9X/cqnEB5elrDGlFeBxSkjbpaQJqc7 4kJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=ZZSYwnaW; 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 u5-v6si34104331pgm.268.2018.11.02.04.53.32; Fri, 02 Nov 2018 04:53:48 -0700 (PDT) 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=ZZSYwnaW; 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 S1726770AbeKBVAE (ORCPT + 99 others); Fri, 2 Nov 2018 17:00:04 -0400 Received: from mail.efficios.com ([167.114.142.138]:45244 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726016AbeKBVAD (ORCPT ); Fri, 2 Nov 2018 17:00:03 -0400 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id E64732488C0; Fri, 2 Nov 2018 07:53:06 -0400 (EDT) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id skhbbq-SB2sz; Fri, 2 Nov 2018 07:53:06 -0400 (EDT) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 94FA52488BB; Fri, 2 Nov 2018 07:53:06 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 94FA52488BB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1541159586; bh=ARIXVDaQRUZ7X2dcuyortmQVt+ZsZwR9tFSZPo3/9TE=; h=From:To:Date:Message-Id; b=ZZSYwnaWvaHeKXt1fPCsxFWd4tkBzWSXDVX2O5uWZ/j5MuURmLQG4GBpLDoOUVmxB tHmhi9imrNCjYMmvtsKWDy69aD3p4bSDk8EiXgqK34Oztt/FrE3nferm9K9BA6v4Rl Z8+cst2yRmsOjTnPwWBgQ7WOFsZTW/ssXzJsZr9gd/8yKDu0fICERrSJFUtBntZaDu QzRMaM6XZJnMSXIsSzVrZRIwTYhF/SV/JweutFJgVIxLkyUrTdM+XdAfU6BLTzBfVN ELSuMv5ldHA8ms7AHygqxqKNrQrGQeVs6UOhDb68mgHqhJ6HxKATh/yKRRnn9wjTHx j3/WmM1Hd+mrw== 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 r582tG_CYFPt; Fri, 2 Nov 2018 07:53:06 -0400 (EDT) Received: from localhost.localdomain (192-222-157-41.qc.cable.ebox.net [192.222.157.41]) by mail.efficios.com (Postfix) with ESMTPSA id 4EA4B2488AF; Fri, 2 Nov 2018 07:53:06 -0400 (EDT) From: Mathieu Desnoyers To: libc-alpha@sourceware.org Cc: Mathieu Desnoyers , Carlos O'Donell , Florian Weimer , Joseph Myers , Szabolcs Nagy , 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 2/2] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux Date: Fri, 2 Nov 2018 07:52:59 -0400 Message-Id: <20181102115259.11383-2-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181102115259.11383-1-mathieu.desnoyers@efficios.com> References: <20181102115259.11383-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