Re: [RFC PATCH v2 12/23] sched/cache: Introduce rq affinity gain calculation

From: Peter Zijlstra

Date: Tue Sep 01 2026 - 06:34:46 EST


On Thu, Aug 27, 2026 at 08:28:05PM +0800, Jianyong Wu wrote:

> +static int get_affi_llcs(struct sched_domain *sd, int src_llc, int dst_llc,
> + int *affi_llcs, int *affi)
> +{

> + if (src_llc > dst_llc) {
> + affi[j] = clamp(src_llc - dst_llc, 1, 1024);
> + affi_llcs[j++] = i;
> + }

> + return j;
> +}
> +
> +static int get_affi_numas(int src_node, int dst_node, int *affi_nodes, int *affi)
> +{

> + if (src_dist > dst_dist) {
> + affi[j] = clamp(src_dist - dst_dist, 4, 1024);
> + affi_nodes[j++] = node;
> + }

> + return j;
> +}
> +
> +static int calc_affinity_numa_score(struct sched_domain *sd, int src_cpu, int dst_cpu,
> + int *affi_node, int *affi, int *last_node, int *num)
> +{
> + int src_node, dst_node, score = 0;
> +
> + src_node = cpu_to_node(src_cpu);
> + dst_node = cpu_to_node(dst_cpu);
> + if (src_node != *last_node) {
> + *last_node = src_node;
> + memset(affi_node, 0, (max_lid + 1) * sizeof(int));
> + memset(affi, 0, (max_lid + 1) * sizeof(int));

This and..

> + *num = get_affi_numas(src_node, dst_node, affi_node, affi);
> + }
> +
> + for (int i = 0; i < *num; i++) {
> + if ((unsigned int)affi_node[i] < nr_node_ids)
> + score += sd->numa_counts[affi_node[i]] * affi[i];
> + }
> +
> + return score;
> +}
> +
> +static int calc_affinity_llc_score(struct sched_domain *sd_cur, struct sched_domain *sd,
> + int src_cpu, int dst_cpu, int *affi_llc,
> + int *affi, int *last_llc, int *num)
> +{
> + int src_llc, dst_llc, score = 0;
> +
> + src_llc = llc_id(src_cpu);
> + dst_llc = llc_id(dst_cpu);
> +
> + if (src_llc != *last_llc) {
> + *last_llc = src_llc;
> + memset(affi_llc, 0, (max_lid + 1) * sizeof(int));
> + memset(affi, 0, (max_lid + 1) * sizeof(int));

... this. Why do we need the memset()? AFAICT the get_affi_*() functions
use direct assignment and the sum is limited to the number returned.

> + *num = get_affi_llcs(sd_cur, src_llc, dst_llc, affi_llc, affi);
> + }
> +
> + for (int i = 0; i < *num; i++) {
> + if ((unsigned int)affi_llc[i] < sd->llc_max)
> + score += sd->llc_counts[affi_llc[i]] * affi[i];
> + }
> +
> + return score;
> +}