From b25aa764a58f34f248ce3e4557b3ddb0dfbd4cc4 Mon Sep 17 00:00:00 2001 From: Martin Sivak Date: Mon, 2 Dec 2024 17:13:34 +0100 Subject: [PATCH] OCPBUGS-45264: Normalize cpu sets when rendering to Tuned profiles Previously the isolated cpu list was passed verbatim as the user entered it. This could have resulted in very long kernel command line. Example: 10,9,8,7,6,5,4,3,2,1 (imagine 500+ cpus) This change consolidates the parsing logic in the Tuned templating and generates simply the following in such case. Example: 1-10 --- .../components/tuned/tuned.go | 32 ++++++++----------- .../components/tuned/tuned_test.go | 15 +++++++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned.go b/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned.go index ce56d0230f..7d7f8ef4c3 100644 --- a/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned.go +++ b/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned.go @@ -64,32 +64,28 @@ func NewNodePerformance(profile *performancev2.PerformanceProfile) (*tunedv1.Tun templateArgs[templatePerformanceProfileName] = profile.Name if profile.Spec.CPU.Isolated != nil { - templateArgs[templateIsolatedCpus] = string(*profile.Spec.CPU.Isolated) - if profile.Spec.CPU.BalanceIsolated != nil && !*profile.Spec.CPU.BalanceIsolated { - templateArgs[templateStaticIsolation] = strconv.FormatBool(true) + minifiedCpuSet, err := cpuset.Parse(string(*profile.Spec.CPU.Isolated)) + if err != nil { + return nil, fmt.Errorf("cannot parse isolated cpuset: %v", err) } + templateArgs[templateIsolatedCpus] = minifiedCpuSet.String() + templateArgs[templateIsolatedCpuList] = minifiedCpuSet.List() } - if profile.Spec.HardwareTuning != nil { - isolatedCpuSet, err := cpuset.Parse(string(*profile.Spec.CPU.Isolated)) + if profile.Spec.CPU.Reserved != nil { + minifiedCpuSet, err := cpuset.Parse(string(*profile.Spec.CPU.Reserved)) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot parse reserved cpuset: %v", err) } - isolatedCpuString := components.ListToString(isolatedCpuSet.List()) - // converts a string to a string slice - isolatedCpuList := strings.SplitN(isolatedCpuString, ",", len(isolatedCpuString)) + templateArgs[templateReservedCpuList] = minifiedCpuSet.List() + } - reservedSet, err := cpuset.Parse(string(*profile.Spec.CPU.Reserved)) - if err != nil { - return nil, err - } - reservedCpuString := components.ListToString(reservedSet.List()) - // converts a string to a string slice - reservedCpuList := strings.SplitN(reservedCpuString, ",", len(reservedCpuString)) + if profile.Spec.CPU.BalanceIsolated != nil && !*profile.Spec.CPU.BalanceIsolated { + templateArgs[templateStaticIsolation] = strconv.FormatBool(true) + } + if profile.Spec.HardwareTuning != nil { templateArgs[templateHardwareTuning] = strconv.FormatBool(true) - templateArgs[templateIsolatedCpuList] = isolatedCpuList - templateArgs[templateReservedCpuList] = reservedCpuList templateArgs[templateIsolatedCpuMaxFreq] = int(*profile.Spec.HardwareTuning.IsolatedCpuFreq) templateArgs[templateReservedCpuMaxFreq] = int(*profile.Spec.HardwareTuning.ReservedCpuFreq) } diff --git a/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned_test.go b/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned_test.go index 47227d8d77..2b875dece7 100644 --- a/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned_test.go +++ b/pkg/performanceprofile/controller/performanceprofile/components/tuned/tuned_test.go @@ -129,6 +129,21 @@ var _ = Describe("Tuned", func() { Expect(bootLoaderSection.Key("cmdline_realtime_common").String()).To(Equal(cmdlineRealtimeCommon)) }) + It("should normalize cpusets", func() { + test_profile := testutils.NewPerformanceProfile("test") + isolated_cpus := performancev2.CPUSet("10,9,8,7,6,5,4,3,2,1,21,22,23,24,25,26,27,28") + reserved_cpus := performancev2.CPUSet("11-15,19,18,17,16") + + test_profile.Spec.CPU.Isolated = &isolated_cpus + test_profile.Spec.CPU.Reserved = &reserved_cpus + + tunedData := getTunedStructuredData(test_profile, components.ProfileNamePerformance) + isolated, err := tunedData.GetSection("variables") + Expect(err).ToNot(HaveOccurred()) + + Expect(isolated.Key("isolated_cores").String()).To(Equal("1-10,21-28")) + }) + Context("default profile default tuned", func() { It("should [cpu] section in tuned", func() { tunedData := getTunedStructuredData(profile, components.ProfileNamePerformance)