numa: reduce code duplication by adding helper numa_get_node_for_cpu()

Replace repeated pattern

    for (i = 0; i < nb_numa_nodes; i++) {
        if (test_bit(idx, numa_info[i].node_cpu)) {
           ...
           break;

with a helper function to lookup numa node index for cpu.

Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Shannon Zhao <shannon.zhao@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Igor Mammedov 2016-10-05 17:51:23 +02:00 committed by Michael S. Tsirkin
parent a06b1dae47
commit 6bea1ddf8b
7 changed files with 27 additions and 22 deletions

View File

@ -427,11 +427,9 @@ build_srat(GArray *table_data, BIOSLinker *linker, VirtGuestInfo *guest_info)
uint32_t *cpu_node = g_malloc0(guest_info->smp_cpus * sizeof(uint32_t)); uint32_t *cpu_node = g_malloc0(guest_info->smp_cpus * sizeof(uint32_t));
for (i = 0; i < guest_info->smp_cpus; i++) { for (i = 0; i < guest_info->smp_cpus; i++) {
for (j = 0; j < nb_numa_nodes; j++) { j = numa_get_node_for_cpu(i);
if (test_bit(i, numa_info[j].node_cpu)) { if (j < nb_numa_nodes) {
cpu_node[i] = j; cpu_node[i] = j;
break;
}
} }
} }

View File

@ -413,10 +413,9 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
armcpu->mp_affinity); armcpu->mp_affinity);
} }
for (i = 0; i < nb_numa_nodes; i++) { i = numa_get_node_for_cpu(cpu);
if (test_bit(cpu, numa_info[i].node_cpu)) { if (i < nb_numa_nodes) {
qemu_fdt_setprop_cell(vbi->fdt, nodename, "numa-node-id", i); qemu_fdt_setprop_cell(vbi->fdt, nodename, "numa-node-id", i);
}
} }
g_free(nodename); g_free(nodename);

View File

@ -2410,18 +2410,15 @@ build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
srat->reserved1 = cpu_to_le32(1); srat->reserved1 = cpu_to_le32(1);
for (i = 0; i < apic_ids->len; i++) { for (i = 0; i < apic_ids->len; i++) {
int j; int j = numa_get_node_for_cpu(i);
int apic_id = apic_ids->cpus[i].arch_id; int apic_id = apic_ids->cpus[i].arch_id;
core = acpi_data_push(table_data, sizeof *core); core = acpi_data_push(table_data, sizeof *core);
core->type = ACPI_SRAT_PROCESSOR_APIC; core->type = ACPI_SRAT_PROCESSOR_APIC;
core->length = sizeof(*core); core->length = sizeof(*core);
core->local_apic_id = apic_id; core->local_apic_id = apic_id;
for (j = 0; j < nb_numa_nodes; j++) { if (j < nb_numa_nodes) {
if (test_bit(i, numa_info[j].node_cpu)) {
core->proximity_lo = j; core->proximity_lo = j;
break;
}
} }
memset(core->proximity_hi, 0, 3); memset(core->proximity_hi, 0, 3);
core->local_sapic_eid = 0; core->local_sapic_eid = 0;

View File

@ -779,11 +779,9 @@ static FWCfgState *bochs_bios_init(AddressSpace *as, PCMachineState *pcms)
for (i = 0; i < max_cpus; i++) { for (i = 0; i < max_cpus; i++) {
unsigned int apic_id = x86_cpu_apic_id_from_index(i); unsigned int apic_id = x86_cpu_apic_id_from_index(i);
assert(apic_id < pcms->apic_id_limit); assert(apic_id < pcms->apic_id_limit);
for (j = 0; j < nb_numa_nodes; j++) { j = numa_get_node_for_cpu(i);
if (test_bit(i, numa_info[j].node_cpu)) { if (j < nb_numa_nodes) {
numa_fw_cfg[apic_id + 1] = cpu_to_le64(j); numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
break;
}
} }
} }
for (i = 0; i < nb_numa_nodes; i++) { for (i = 0; i < nb_numa_nodes; i++) {

View File

@ -69,11 +69,9 @@ void spapr_cpu_init(sPAPRMachineState *spapr, PowerPCCPU *cpu, Error **errp)
} }
/* Set NUMA node for the added CPUs */ /* Set NUMA node for the added CPUs */
for (i = 0; i < nb_numa_nodes; i++) { i = numa_get_node_for_cpu(cs->cpu_index);
if (test_bit(cs->cpu_index, numa_info[i].node_cpu)) { if (i < nb_numa_nodes) {
cs->numa_node = i; cs->numa_node = i;
break;
}
} }
xics_cpu_setup(spapr->xics, cpu); xics_cpu_setup(spapr->xics, cpu);

View File

@ -32,4 +32,7 @@ void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node);
void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node); void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node);
uint32_t numa_get_node(ram_addr_t addr, Error **errp); uint32_t numa_get_node(ram_addr_t addr, Error **errp);
/* on success returns node index in numa_info,
* on failure returns nb_numa_nodes */
int numa_get_node_for_cpu(int idx);
#endif #endif

12
numa.c
View File

@ -550,3 +550,15 @@ MemdevList *qmp_query_memdev(Error **errp)
object_child_foreach(obj, query_memdev, &list); object_child_foreach(obj, query_memdev, &list);
return list; return list;
} }
int numa_get_node_for_cpu(int idx)
{
int i;
for (i = 0; i < nb_numa_nodes; i++) {
if (test_bit(idx, numa_info[i].node_cpu)) {
break;
}
}
return i;
}