[v1] x86: Fix slight bug in `shared_per_thread` cache size calculation.

Message ID 20230718041433.217111-1-goldstein.w.n@gmail.com
State Committed
Commit 47f747217811db35854ea06741be3685e8bbd44d
Headers
Series [v1] x86: Fix slight bug in `shared_per_thread` cache size calculation. |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 warning Patch failed to apply
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 warning Patch failed to apply
linaro-tcwg-bot/tcwg_glibc_check--master-arm warning Patch failed to apply

Commit Message

Noah Goldstein July 18, 2023, 4:14 a.m. UTC
  After:
```
    commit af992e7abdc9049714da76cae1e5e18bc4838fb8
    Author: Noah Goldstein <goldstein.w.n@gmail.com>
    Date:   Wed Jun 7 13:18:01 2023 -0500

        x86: Increase `non_temporal_threshold` to roughly `sizeof_L3 / 4`
```

Split `shared` (cumulative cache size) from `shared_per_thread` (cache
size per socket), the `shared_per_thread` *can* be slightly off from
the previous calculation.

Previously we added `core` even if `threads_l2` was invalid, and only
used `threads_l2` to divide `core` if it was present. The changed
version only included `core` if `threads_l2` was valid.

This change restores the old behavior if `threads_l2` is invalid by
adding the entire value of `core`.
---
 sysdeps/x86/dl-cacheinfo.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Noah Goldstein July 18, 2023, 4:15 a.m. UTC | #1
On Mon, Jul 17, 2023 at 11:14 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>
> After:
> ```
>     commit af992e7abdc9049714da76cae1e5e18bc4838fb8
>     Author: Noah Goldstein <goldstein.w.n@gmail.com>
>     Date:   Wed Jun 7 13:18:01 2023 -0500
>
>         x86: Increase `non_temporal_threshold` to roughly `sizeof_L3 / 4`
> ```
>
> Split `shared` (cumulative cache size) from `shared_per_thread` (cache
> size per socket), the `shared_per_thread` *can* be slightly off from
> the previous calculation.
>
> Previously we added `core` even if `threads_l2` was invalid, and only
> used `threads_l2` to divide `core` if it was present. The changed
> version only included `core` if `threads_l2` was valid.
>
> This change restores the old behavior if `threads_l2` is invalid by
> adding the entire value of `core`.
> ---
>  sysdeps/x86/dl-cacheinfo.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/sysdeps/x86/dl-cacheinfo.h b/sysdeps/x86/dl-cacheinfo.h
> index c98fa57a7b..43be2c1229 100644
> --- a/sysdeps/x86/dl-cacheinfo.h
> +++ b/sysdeps/x86/dl-cacheinfo.h
> @@ -614,8 +614,8 @@ get_common_cache_info (long int *shared_ptr, long int * shared_per_thread_ptr, u
>    /* Account for non-inclusive L2 and L3 caches.  */
>    if (!inclusive_cache)
>      {
> -      if (threads_l2 > 0)
> -       shared_per_thread += core / threads_l2;
> +      long int core_per_thread = threads_l2 > 0 ? (core / threads_l2) : core;
> +      shared_per_thread += core_per_thread;
>        shared += core;
>      }
>
> --
> 2.34.1
>

Noticed this when working on the AMD patch, but think the
fix should be seperate.
  
DJ Delorie July 18, 2023, 10:14 p.m. UTC | #2
Noah Goldstein via Libc-alpha <libc-alpha@sourceware.org> writes:
>    if (!inclusive_cache)
>      {
> -      if (threads_l2 > 0)
> -	shared_per_thread += core / threads_l2;
> +      long int core_per_thread = threads_l2 > 0 ? (core / threads_l2) : core;
> +      shared_per_thread += core_per_thread;
>        shared += core;
>      }

So the original code read:

  if (!inclusive_cache)
     {
       if (threads_l2 > 0)
        core /= threads_l2;
       shared += core;
     }
 
And the new code reads:

  if (!inclusive_cache)
    {
      long int core_per_thread = threads_l2 > 0 ? (core / threads_l2) : core;
      shared_per_thread += core_per_thread;
      shared += core;
    }

"core_per_thread" is thus either all of core, or, if we have the data,
core/threads_l2.  But, "core" doesn't change, so the value stored in
"shared" is different than way-back-when.  However, the original change
also changed the temporal threshold from "shared" to
"shared_per_thread", which is initialized to "core".  So the resulting
value used later should be effectively the same.

In the interim code:

 if (!inclusive_cache)
   {
     if (threads_l2 > 0)
       shared_per_thread += core / threads_l2;
     shared += core;
   }

the new "shared_per_thread" doesn't get the default value of "core" when
threads_l2 is zero.

This all seems right to me.

LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>
  

Patch

diff --git a/sysdeps/x86/dl-cacheinfo.h b/sysdeps/x86/dl-cacheinfo.h
index c98fa57a7b..43be2c1229 100644
--- a/sysdeps/x86/dl-cacheinfo.h
+++ b/sysdeps/x86/dl-cacheinfo.h
@@ -614,8 +614,8 @@  get_common_cache_info (long int *shared_ptr, long int * shared_per_thread_ptr, u
   /* Account for non-inclusive L2 and L3 caches.  */
   if (!inclusive_cache)
     {
-      if (threads_l2 > 0)
-	shared_per_thread += core / threads_l2;
+      long int core_per_thread = threads_l2 > 0 ? (core / threads_l2) : core;
+      shared_per_thread += core_per_thread;
       shared += core;
     }