package vmstart import ( "reflect" "testing" "github.com/Nwflower/reims-go/internal/host" ) func TestVMAffinityRoundTripAndResolve(t *testing.T) { dir := t.TempDir() // Default when sidecar does not exist. def := ReadVMAffinity(dir) if def.Mode != AffinityAuto || !def.ShouldDisableEcoQoS() { t.Fatalf("unexpected default: %+v", def) } // Custom mode rejects empty selection. if _, err := SaveVMAffinity(dir, VMAffinityConfig{Mode: AffinityCustom}, 16); err == nil { t.Fatalf("expected error for empty custom CPU list") } // Save custom deduplicated/sorted CPUs with EcoQoS disabled=false. eco := false saved, err := SaveVMAffinity(dir, VMAffinityConfig{ Mode: AffinityCustom, CPUs: []int{5, 2, 2, 0, 99, -1}, DisableEcoQoS: &eco, }, 16) if err != nil { t.Fatalf("SaveVMAffinity: %v", err) } wantCPUs := []int{0, 2, 5} if !reflect.DeepEqual(saved.CPUs, wantCPUs) || saved.ShouldDisableEcoQoS() { t.Fatalf("saved = %+v, want cpus %v eco=false", saved, wantCPUs) } loaded := ReadVMAffinity(dir) if loaded.Mode != AffinityCustom || !reflect.DeepEqual(loaded.CPUs, wantCPUs) || loaded.ShouldDisableEcoQoS() { t.Fatalf("loaded = %+v", loaded) } // Resolve on homogeneous 16-thread CPU. amd := host.Report{LogicalCPUs: 16} res := ResolveVMAffinity(loaded, true, amd, 4) if res.EffectiveMode != AffinityCustom || res.Mask != 0x25 || !res.ShouldPin { t.Fatalf("custom resolve = %+v, want mask 0x25", res) } // Resolve auto on 3-tier Arrow Lake. arrowLake := host.Report{ LogicalCPUs: 16, HasHybridCores: true, PCoreCount: 6, ECoreCount: 8, LPECoreCount: 2, PCoreMask: 0x3C03, PCoreLogicalCPUs: []int{0, 1, 10, 11, 12, 13}, NonLPEMask: 0x3FFF, NonLPELogicalCPUs: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, } r4 := ResolveVMAffinity(DefaultVMAffinityConfig(), true, arrowLake, 4) if r4.EffectiveMode != AffinityPCores || r4.Mask != 0x3C03 || !r4.ShouldPin { t.Fatalf("auto 4 vCPU = %+v", r4) } r8 := ResolveVMAffinity(DefaultVMAffinityConfig(), true, arrowLake, 8) if r8.EffectiveMode != AffinityNonLPE || r8.Mask != 0x3FFF || !r8.ShouldPin { t.Fatalf("auto 8 vCPU = %+v", r8) } r14 := ResolveVMAffinity(DefaultVMAffinityConfig(), true, arrowLake, 14) if r14.EffectiveMode != AffinityAll || !r14.AutoSkipped || r14.ShouldPin { t.Fatalf("auto 14 vCPU = %+v", r14) } // Explicit pcores overrides auto headroom limit when user explicitly asks for it. rExplicitP := ResolveVMAffinity(VMAffinityConfig{Mode: AffinityPCores}, false, arrowLake, 8) if rExplicitP.EffectiveMode != AffinityPCores || rExplicitP.Mask != 0x3C03 || !rExplicitP.ShouldPin { t.Fatalf("explicit pcores = %+v", rExplicitP) } // Explicit all returns full mask and ShouldPin=false (forceApplyAll handles live reset). rAll := ResolveVMAffinity(VMAffinityConfig{Mode: AffinityAll}, true, arrowLake, 4) if rAll.EffectiveMode != AffinityAll || rAll.Mask != 0xFFFF || rAll.ShouldPin { t.Fatalf("explicit all = %+v", rAll) } }