Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp4232071imj; Tue, 12 Feb 2019 12:07:22 -0800 (PST) X-Google-Smtp-Source: AHgI3IZNZd/bVQCcchDTJTEAYctmjG5W0lNGtDs7ddzghC3miMmpTxaGzVBu603/WyckdApMJM8m X-Received: by 2002:a17:902:8f98:: with SMTP id z24mr5631895plo.40.1550002041949; Tue, 12 Feb 2019 12:07:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550002041; cv=none; d=google.com; s=arc-20160816; b=WItMCxLRywbfDhk5uJhEkTA5QG1PjSwDbAQ6Uw+0AJGj2v0vJMBEJR/aq+WQSaxj8S nfqtAaoFYC7LczhnEIaUUbMlrpmgLfuJbgyF3QrRLXGMb+mSMBsgpAxpiMm0FsgrTGtF GS+uVsKs+twGuBGoCKyPpBoUP6fkHy0C5mjI+goMsd/TDJl/DURFKpMaRUhNfcHRxMqx 2/2xx32vXh8dSALyuyzRbCSwqQETpinrjkrt8atMxOwDQ+/VTZI9/wmmhO/fmt0PggZa i10xyzrA48l01YnP+RF5R5w4nyYkDAj8dUCy6wk7K7SBlOIXkxZxtagMsucCNVcrSBYc jMhg== 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=AWYbTmbbUu9whfET+jatobpOKB+ZZRz448Bik1cpDV0=; b=P7a8hijHYmlfxPJpw88C6by4DFewJOYLwzxt+IOiBboeeDJEEnYr/sLkQMLLL4Cnlu G+AySQsxy7QaaOkphR6mOOP2gjs7m2x0Rx8BacUlFYRoa5H2ZCJtuR6rbEApVHKRHZah LJRq5lv9BKRfAFuzdPJNBSg0RqvoYSGTtKGd0Sryu5KlZhV0Jw1kKI2mltlR4iAE5WXc oh6hYu9zoZ4YfVppNz8y8oGvZHkfgi9RlfQbig+3E090rSabA7tgmQbT92PLZtFkXHiH HdBG2epdPqAU0iA5+A5whVe26z4z+PTdu3+4LTt0Vz519byhujLV/d0r4otjTwJW2lo9 HIuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=Li0mLark; 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 h3si13027457pgl.468.2019.02.12.12.07.05; Tue, 12 Feb 2019 12:07:21 -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=Li0mLark; 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 S1732015AbfBLTnM (ORCPT + 99 others); Tue, 12 Feb 2019 14:43:12 -0500 Received: from mail.efficios.com ([167.114.142.138]:57380 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727343AbfBLTnL (ORCPT ); Tue, 12 Feb 2019 14:43:11 -0500 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 88ED91A1B1B; Tue, 12 Feb 2019 14:43:10 -0500 (EST) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id h1VCgYn5zwSv; Tue, 12 Feb 2019 14:43:10 -0500 (EST) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id E04E21A1AFF; Tue, 12 Feb 2019 14:43:09 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com E04E21A1AFF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1550000589; bh=AWYbTmbbUu9whfET+jatobpOKB+ZZRz448Bik1cpDV0=; h=From:To:Date:Message-Id; b=Li0mLarkExrbEuurvDqnwcBnczDCJsAjyQMUiJ2vOiXPVanOiQ51nhdi4I6gxyzoj VtiTKYDxMRN/uGT2fLR8xt/7BLgbaVBZuXtdnLocaqcC0RLxWPy03E26fC0E8ljNeE pjlC9oDWEx54El2mDh5PB9Vxr1angPTVgyabDd0NQkte6hemd9fkQXDUOvr7tMuVNH i9pPFFAbJwxjNMmDBFRsKHYCZivGXSChQgbfpkug+tKuGmmvXhnwGwM7QScYCnwfHR SQ+x3MPlWXVXj0WcUMXiIk5XDRM4vm4OQTRL3yDs4fvjYAJOxg6zby5X1NOsjHRrxy rgpXpNiHSo1Gg== 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 IBjfZR3O_V4t; Tue, 12 Feb 2019 14:43:09 -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 93EB91A1AEB; Tue, 12 Feb 2019 14:43:09 -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: [PATCH 2/4] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux Date: Tue, 12 Feb 2019 14:42:51 -0500 Message-Id: <20190212194253.1951-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190212194253.1951-1-mathieu.desnoyers@efficios.com> References: <20190212194253.1951-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 fb0d317f83..8bfb03778b 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