From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 779B83858419 for ; Mon, 21 Feb 2022 03:12:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 779B83858419 Received: by mail-io1-xd34.google.com with SMTP id c18so11929846ioc.6 for ; Sun, 20 Feb 2022 19:12:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=nwlbWQEahtH6fqcP0AGtle+j/d/Guy6hVnsM3p1M0dk=; b=I6AsImYUeRnVhZCfA4naVZ8yHj8D7lu3huZNJ28nUBC045gCKtNcrs5AHslPSW/MW2 OX3HVdKA4/jqVs6tIKGptQeZyaRgFC2OQr2b5ZRxsiINL37gH6Wjqs8y22J9iQTAaABq wY4PIIPfr5VCgrzOA+Xandl/VUvImoLKE9KTWglkCCl9MOpFTmpR8h6NuMd1n0WEgBxh 71TvyY9ShMPE7n7j7pjFKdiR9YffHXvURvR4czjqSmQqAgJlpJlPXeNqRucCDfUA3Lz0 uT+AFRk5ECVowZznWNoBxZ9zymxgTgRiZClbN1VOdK/Gx+VFmhVmClFmeyrOQ4kuJHPL L0hg== X-Gm-Message-State: AOAM532fajWqy5v8C+Omdz2/EBU1PxeiGNjfxNp25end8xHMnKJ/Tajo C1VKsjvlZ52HWG/LE0wmdyFNe6Ka8Tg= X-Google-Smtp-Source: ABdhPJxlTLmzX4Maty0SRYNJnCaMXublQFpnoM4PZl0VF+NG9JEJRLqe5xilZLzszIg25NdvmA6wMg== X-Received: by 2002:a02:a1d6:0:b0:314:af71:b38a with SMTP id o22-20020a02a1d6000000b00314af71b38amr10511822jah.95.1645413144703; Sun, 20 Feb 2022 19:12:24 -0800 (PST) Received: from localhost.localdomain (node-17-161.flex.volo.net. [76.191.17.161]) by smtp.googlemail.com with ESMTPSA id s15sm1689152ilq.66.2022.02.20.19.12.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Feb 2022 19:12:24 -0800 (PST) From: Noah Goldstein To: libc-alpha@sourceware.org Subject: [PATCH v1] x86: Add version of sched_getcpu that uses `rdpid` Date: Sun, 20 Feb 2022 21:12:16 -0600 Message-Id: <20220221031216.328713-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Feb 2022 03:12:27 -0000 The `rdpid` version is slightly faster: Measured on Tigerlake 2.8 GHz rdpid -> 0.215 Calls / ns rseq -> 0.312 Calls / ns tst-skeleton-thread-affinity and tst-skeleton-affinity pass w and w.o multiarch. --- sysdeps/unix/sysv/linux/sched_getcpu.c | 7 +++- sysdeps/unix/sysv/linux/x86/sched_getcpu.c | 48 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/x86/sched_getcpu.c diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c index 5c3301004c..e659aeaaf8 100644 --- a/sysdeps/unix/sysv/linux/sched_getcpu.c +++ b/sysdeps/unix/sysv/linux/sched_getcpu.c @@ -35,15 +35,18 @@ vsyscall_sched_getcpu (void) #ifdef RSEQ_SIG int -sched_getcpu (void) +__sched_getcpu (void) { int cpu_id = THREAD_GETMEM_VOLATILE (THREAD_SELF, rseq_area.cpu_id); return __glibc_likely (cpu_id >= 0) ? cpu_id : vsyscall_sched_getcpu (); } #else /* RSEQ_SIG */ int -sched_getcpu (void) +__sched_getcpu (void) { return vsyscall_sched_getcpu (); } #endif /* RSEQ_SIG */ +#ifndef USE_IFUNC_SCHED_GETCPU +weak_alias (__sched_getcpu, sched_getcpu) +#endif /* !USE_IFUNC_SCHED_GETCPU */ diff --git a/sysdeps/unix/sysv/linux/x86/sched_getcpu.c b/sysdeps/unix/sysv/linux/x86/sched_getcpu.c new file mode 100644 index 0000000000..38afed78c6 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/sched_getcpu.c @@ -0,0 +1,48 @@ +/* sched_getcpu -- Get current processor. Linux/x86 version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + + + +#if defined __x86_64__ && defined USE_MULTIARCH && IS_IN (libc) +# define USE_IFUNC_SCHED_GETCPU + +# include +int __sched_getcpu (void); + +int +__sched_getcpu_rdpid (void) +{ +# if __has_builtin (__builtin_ia32_rdpid) + return __builtin_ia32_rdpid(); +# else + unsigned long cpu; + asm volatile("rdpid %[cpu]" : [cpu] "=r" (cpu) : :); + return cpu; +# endif +} + +# undef INIT_ARCH +# define INIT_ARCH() \ + const struct cpu_features *cpu_features = __get_cpu_features (); + +libc_ifunc (sched_getcpu, CPU_FEATURE_USABLE_P (cpu_features, RDPID) + ? (void *) __sched_getcpu_rdpid + : (void *) __sched_getcpu) + +#endif +#include -- 2.25.1