From a663f2c11bb0e8ddc608944ed267dacac35ef2d2 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Thu, 18 Nov 2021 18:27:28 +0100 Subject: [PATCH] Allocate only the required CPUs --- emu.c | 41 +++++++++++++++++++++++++++++++++++------ emu.h | 2 +- emu_ovni.c | 2 +- ovni.h | 1 - 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/emu.c b/emu.c index 4c7bdcb..6c26796 100644 --- a/emu.c +++ b/emu.c @@ -540,11 +540,15 @@ add_new_cpu(struct ovni_emu *emu, struct ovni_loom *loom, int i, int phyid) { struct ovni_cpu *cpu; - /* The logical id must match our index */ - assert((size_t) i == loom->ncpus); - cpu = &loom->cpu[i]; + if(i < 0 || i >= (int) loom->ncpus) + { + err("CPU with index %d in loom %s is out of bounds\n", i, + loom->hostname); + abort(); + } + assert(cpu->state == CPU_ST_UNKNOWN); cpu->state = CPU_ST_READY; @@ -554,8 +558,6 @@ add_new_cpu(struct ovni_emu *emu, struct ovni_loom *loom, int i, int phyid) cpu->loom = loom; dbg("new cpu %d at phyid=%d\n", cpu->gindex, phyid); - - loom->ncpus++; } static void @@ -579,16 +581,41 @@ proc_load_cpus(struct ovni_emu *emu, struct ovni_loom *loom, struct ovni_eproc * assert(loom->ncpus == 0); - for(i = 0; i < json_array_get_count(cpuarray); i++) + loom->ncpus = json_array_get_count(cpuarray); + + if(loom->ncpus <= 0) + { + err("loom %s proc %d has read %ld cpus, but required > 0\n", + loom->hostname, proc->pid, loom->ncpus); + abort(); + } + + loom->cpu = calloc(loom->ncpus, sizeof(struct ovni_cpu)); + + if(loom->cpu == NULL) + { + perror("calloc failed"); + abort(); + } + + for(i = 0; i < loom->ncpus; i++) { cpu = json_array_get_object(cpuarray, i); + if(cpu == NULL) + { + err("json_array_get_object returned NULL\n"); + abort(); + } + index = (int) json_object_get_number(cpu, "index"); phyid = (int) json_object_get_number(cpu, "phyid"); add_new_cpu(emu, loom, index, phyid); } + /* If we reach this point, all CPUs are in the ready state */ + /* Init the vcpu as well */ vcpu = &loom->vcpu; assert(vcpu->state == CPU_ST_UNKNOWN); @@ -651,6 +678,8 @@ destroy_metadata(struct ovni_emu *emu) assert(proc->meta); json_value_free(proc->meta); } + + free(loom->cpu); } return 0; diff --git a/emu.h b/emu.h index 8b253b0..16aaaa3 100644 --- a/emu.h +++ b/emu.h @@ -381,7 +381,7 @@ struct ovni_loom { size_t max_phyid; size_t ncpus; size_t offset_ncpus; - struct ovni_cpu cpu[OVNI_MAX_CPU]; + struct ovni_cpu *cpu; int64_t clock_offset; diff --git a/emu_ovni.c b/emu_ovni.c index 642a727..f06aa29 100644 --- a/emu_ovni.c +++ b/emu_ovni.c @@ -187,7 +187,7 @@ update_cpu(struct ovni_cpu *cpu) struct ovni_cpu * emu_get_cpu(struct ovni_loom *loom, int cpuid) { - assert(cpuid < OVNI_MAX_CPU); + assert(cpuid < (int) loom->ncpus); if(cpuid < 0) { diff --git a/ovni.h b/ovni.h index 91e0cda..ee542b6 100644 --- a/ovni.h +++ b/ovni.h @@ -40,7 +40,6 @@ extern "C" { /* Hardcode the JSON_Value to avoid a dependency with janson */ typedef struct json_value_t JSON_Value; -#define OVNI_MAX_CPU 256 #define OVNI_TRACEDIR "ovni" #define OVNI_MAX_HOSTNAME 512