From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112a.google.com (mail-yw1-x112a.google.com [IPv6:2607:f8b0:4864:20::112a]) by sourceware.org (Postfix) with ESMTPS id 9BEF23846410 for ; Fri, 5 Apr 2024 12:57:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9BEF23846410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9BEF23846410 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712321875; cv=none; b=AQKxLZp/6XzEL6ZV5VPOIRGGp+Znqh6R9SsbTk8EFEhlrJLsWRGHDl+NXNNvf2ky8dGppRitB8NGOVKODjj265M1ngLJboBlX6xnmgrs45JIkV+K77+qqqwCipRG55AHVYHJu9dRsKemDoqo5dFjvQNZC+hXFvrLTHKuAHhiBhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712321875; c=relaxed/simple; bh=xp7h9cGy6CcCMKT90g038CnnaQnXGWmM4lVFMPRJLbs=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=ksN99ASOf0vsMblkNnhJFPfyeyJ1RhEIGhem6E5yeJU9B1tsbEQY36BOLK3f/iIFclg5z2/eaRRhrNQCAAJsMx7eryV8hUbKbznWmkgD0eXg29VhMxZwcqwa7AlYn3yqvpyx28QzjPj5rXsMAgs+wXCRGvPhrzZUQFG4dt361b4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-617cd7bd929so6747047b3.3 for ; Fri, 05 Apr 2024 05:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712321872; x=1712926672; darn=sourceware.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=/4+cL9WKnsk1B3PwwY8HMYkDxMyCOQv6pGUjt57bVB4=; b=TCcCBhb6suA6cl3vqHd62mMnVrDmyNkkO4WNiNlhxIJniSwEYfbSrkKtfBI9Wwkg3X v4B0hRLJeULaBJR+QCPNnoIwz6Piyi5Gfp9LQ36C9Z0EBjm5xtxAjUxvayRMVuFtPztz oUKrUNgEmWd9wrZAuNOw2UAPrWE10qNo2OwgE1ADKAcar+WVF4fiMGW2W0NaHr9uLJ2u vgACx/AHmkEduWwhRKunKjrHFc9UmP4idMpv6YUv5n4qSnIpKPEiMwVhWdhpBiJLGgC1 ZYwU9mcCOOEHIzdhSW/uSTD7jHqAny8iPA0rBxR0UVR+7vgkg5z4T4GHJRKfm9hPLg5R d3LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712321872; x=1712926672; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/4+cL9WKnsk1B3PwwY8HMYkDxMyCOQv6pGUjt57bVB4=; b=P7uA6wo5Ss6E2PRfs1rocDQ1V/mBz5U/LCA+vCMSYmpqWPaY6Sz4RWMj+ZwrgfbQFT Oulos2OLH0xU9mUtScIXP35WyDTLV52S4Lp9nEL4LEH4Nju3rE5NRxdqXFRvxnwqhzpR lpNyC5mpdlPY2V/mCVusQ+U5tOtqvmc7cfYpftfcrXJr04i6LdmaGlsWY2Ds+bb2tc88 wwDa7UdspwRhOoAKidK2AOVUitC0I9+/be4NzuAW6H89+C+vgbxYpbIDcEYOwIc2c0lu SiWDLn0g/nlhNvvY0VF7K+nUnU4/OfDYoKjkLVP7crLHvxA7KKBZrToVF65TI139QH2Q 0WAA== X-Gm-Message-State: AOJu0Yy964ddNq6BClARf1gJFyER7FETFAZv+ss6HSntsMZYYfYTQG1m 0aEok9MibDjrkou6MZk+pNHAz+IPOc/ZcpWlFzjmj18bc/A91znXlN5DiXHGwsy7w2B1qqcGhF8 zJ9DE63J7PrkVC75d6jolNZL29smoiVEI X-Google-Smtp-Source: AGHT+IGr5u7uVFw4/zfTTNjVzAl9EZUqE1jkmfAEupbPi1rlZ8l2QczBOnQSaG1q9AVnLpOmaS5kyonUkunOx1bCohc= X-Received: by 2002:a5b:ec6:0:b0:dcf:9019:a2fe with SMTP id a6-20020a5b0ec6000000b00dcf9019a2femr956903ybs.64.1712321871849; Fri, 05 Apr 2024 05:57:51 -0700 (PDT) MIME-Version: 1.0 References: <193e1a53ba530e4a7ec909a11662ec733196d659.1712312063.git.fweimer@redhat.com> In-Reply-To: <193e1a53ba530e4a7ec909a11662ec733196d659.1712312063.git.fweimer@redhat.com> From: "H.J. Lu" Date: Fri, 5 Apr 2024 05:57:15 -0700 Message-ID: Subject: Re: [PATCH v3 1/3] elf: Add CPU iteration support for future use in ld.so diagnostics To: Florian Weimer Cc: libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3019.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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Apr 5, 2024 at 3:17=E2=80=AFAM Florian Weimer = wrote: > > --- > elf/dl-iterate_cpu.h | 135 ++++++++++++++++++++++++++ > sysdeps/generic/dl-affinity.h | 54 +++++++++++ > sysdeps/unix/sysv/linux/dl-affinity.h | 46 +++++++++ > 3 files changed, 235 insertions(+) > create mode 100644 elf/dl-iterate_cpu.h > create mode 100644 sysdeps/generic/dl-affinity.h > create mode 100644 sysdeps/unix/sysv/linux/dl-affinity.h > > diff --git a/elf/dl-iterate_cpu.h b/elf/dl-iterate_cpu.h > new file mode 100644 > index 0000000000..772efffae1 > --- /dev/null > +++ b/elf/dl-iterate_cpu.h > @@ -0,0 +1,135 @@ > +/* Iterate over all CPUs, for CPU-specific diagnostics. > + Copyright (C) 2024 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 > + . */ > + > +#ifndef DL_ITERATE_CPU_H > +#define DL_ITERATE_CPU_H > + > +#include > + > +struct dl_iterate_cpu > +{ > + /* Sequential iteration count, starting at 0. */ > + unsigned int processor_index; > + > + /* Requested CPU. Can be -1 if affinity could not be set. */ > + int requested_cpu; > + > + /* Observed current CPU. -1 if unavailable. */ > + int actual_cpu; > + > + /* Observed node ID for the CPU. -1 if unavailable. */ > + int actual_node; > + > + /* Internal fields to implement the iteration. */ > + > + /* Affinity as obtained by _dl_iterate_cpu_init, using > + _dl_getaffinity. Space for 8,192 CPUs. */ > + unsigned long int mask_reference[8192 / sizeof (unsigned long int) / 8= ]; > + > + /* This array is used by _dl_setaffinity calls. */ > + unsigned long int mask_request[8192 / sizeof (unsigned long int) / 8]; > + > + /* Return value from the initial _dl_getaffinity call. */ > + int length_reference; > +}; > + > +static void > +_dl_iterate_cpu_init (struct dl_iterate_cpu *dic) > +{ > + dic->length_reference > + =3D _dl_getaffinity (dic->mask_reference, sizeof (dic->mask_referenc= e)); > + /* Prepare for the first _dl_iterate_cpu_next call. */ > + dic->processor_index =3D -1; > + dic->requested_cpu =3D -1; > +} > + > +static bool > +_dl_iterate_cpu_next (struct dl_iterate_cpu *dic) > +{ > + ++dic->processor_index; > + > + if (dic->length_reference > 0) > + { > + /* Search for the next CPU to switch to. */ > + while (true) > + { > + ++dic->requested_cpu; > + > + /* Array index and bit number within the array. */ > + unsigned int long_index > + =3D dic->requested_cpu / sizeof (unsigned long int) / 8; > + unsigned int bit_index > + =3D dic->requested_cpu % (sizeof (unsigned long int) * 8); > + > + if (long_index * sizeof (unsigned long int) >=3D dic->length_r= eference) > + /* All possible CPUs have been covered. */ > + return false; > + > + unsigned long int bit =3D 1UL << bit_index; > + if (dic->mask_reference[long_index] & bit) > + { > + /* The CPU is available. Try to select it. */ > + dic->mask_request[long_index] =3D bit; > + if (_dl_setaffinity (dic->mask_request, > + (long_index + 1) > + * sizeof (unsigned long int)) < 0) > + { > + /* Record that we could not perform a CPU request. */ > + dic->length_reference =3D -1; > + > + if (dic->processor_index > 0) > + /* We already reported something. There is no need = to > + continue because the new data is probably not use= ful. */ > + return false; > + } > + > + /* Clear the bit in case the next iteration switches to th= e > + next long value. */ > + dic->mask_request[long_index] =3D 0; > + > + /* We found a CPU to run on. */ > + break; > + } > + } > + } > + else > + { > + /* No way to set CPU affinity. Iterate just once. */ > + if (dic->processor_index > 0) > + return false; > + } > + > + /* Fill in the actual CPU information. CPU pinning may not actually > + be effective, depending on the container host. */ > + unsigned int cpu, node; > + if (_dl_getcpu (&cpu, &node) < 0) > + { > + /* No CPU information available. */ > + dic->actual_cpu =3D -1; > + dic->actual_node =3D -1; > + } > + else > + { > + dic->actual_cpu =3D cpu; > + dic->actual_node =3D node; > + } > + > + return true; > +} > + > +#endif /* DL_ITERATE_CPU_H */ > diff --git a/sysdeps/generic/dl-affinity.h b/sysdeps/generic/dl-affinity.= h > new file mode 100644 > index 0000000000..d117f737e9 > --- /dev/null > +++ b/sysdeps/generic/dl-affinity.h > @@ -0,0 +1,54 @@ > +/* CPU affinity handling for the dynamic linker. Stub version. > + Copyright (C) 2024 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 > + . */ > + > +#ifndef DL_AFFINITY_H > +#define DL_AFFINITY_H > + > +#include > +#include > + > +/* On success, write the current CPU ID to *CPU, and the current node > + ID to *NODE, and return 0. Return a negative error code on > + failure. */ > +static inline int > +_dl_getcpu (unsigned int *cpu, unsigned int *node) > +{ > + return -ENOSYS; > +} > + > +/* On success, write CPU ID affinity bits for the current thread to > + *BITS, which must be SIZE bytes long, and return the number of > + bytes updated, a multiple of sizeof (unsigned long int). On > + failure, return a negative error code. */ > +static int > +_dl_getaffinity (unsigned long int *bits, size_t size) > +{ > + return -ENOSYS; > +} > + > +/* Set the CPU affinity mask for the current thread to *BITS, using > + the SIZE bytes from that array, which should be a multiple of > + sizeof (unsigned long int). Return 0 on success, and a negative > + error code on failure. */ > +static int > +_dl_setaffinity (const unsigned long int *bits, size_t size) > +{ > + return -ENOSYS; > +} > + > +#endif /* DL_AFFINITY_H */ > diff --git a/sysdeps/unix/sysv/linux/dl-affinity.h b/sysdeps/unix/sysv/li= nux/dl-affinity.h > new file mode 100644 > index 0000000000..bbfede7750 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/dl-affinity.h > @@ -0,0 +1,46 @@ > +/* CPU affinity handling for the dynamic linker. Linux version. > + Copyright (C) 2024 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 > + . */ > + > +/* See sysdeps/generic/dl-affinity.h for documentation of these interfac= es. */ > + > +#ifndef DL_AFFINITY_H > +#define DL_AFFINITY_H > + > +#include > +#include > +#include > + > +static inline int > +_dl_getcpu (unsigned int *cpu, unsigned int *node) > +{ > + return INTERNAL_SYSCALL_CALL (getcpu, cpu, node); > +} > + > +static int > +_dl_getaffinity (unsigned long int *bits, size_t size) > +{ > + return INTERNAL_SYSCALL_CALL (sched_getaffinity, /* TID */ 0, size, bi= ts); > +} > + > +static int > +_dl_setaffinity (const unsigned long int *bits, size_t size) > +{ > + return INTERNAL_SYSCALL_CALL (sched_setaffinity, /* TID */ 0, size, bi= ts); > +} > + > +#endif /* DL_AFFINITY_H */ > -- > 2.44.0 > > LGTM. Reviewed-by: H.J. Lu Thanks. --=20 H.J.