Skip to content

Commit

Permalink
make Stat part of Cpu structure
Browse files Browse the repository at this point in the history
replace cpuStat method with stat function

Signed-off-by: Niranjan M.R <mrniranjan@redhat.com>
  • Loading branch information
Niranjan M.R committed Mar 13, 2024
1 parent 1f9effc commit 36c5e63
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
Expand Up @@ -645,10 +645,10 @@ var _ = Describe("[rfe_id:27363][performance] CPU Management", Ordered, func() {
})

It("[test_id: 72081] Verify cpu assigned to pod with quota disabled is not throttled", func() {
cpuStat := &controller.CpuStat{}
err := getter.Container(ctx, testpod, testpod.Spec.Containers[0].Name, cpuStat)
cpuCfg := &controller.Cpu{}
err := getter.Container(ctx, testpod, testpod.Spec.Containers[0].Name, cpuCfg)
Expect(err).ToNot(HaveOccurred())
Expect(cpuStat.Stat["nr_throttled"]).To(Equal("0"), "cpu throttling not disabled on pod=%q, container=%q", client.ObjectKeyFromObject(testpod), testpod.Spec.Containers[0].Name)
Expect(cpuCfg.Stat["nr_throttled"]).To(Equal("0"), "cpu throttling not disabled on pod=%q, container=%q", client.ObjectKeyFromObject(testpod), testpod.Spec.Containers[0].Name)
})
})
})
Expand Down
Expand Up @@ -16,8 +16,5 @@ type CpuSet struct {
type Cpu struct {
Quota string
Period string
}

type CpuStat struct {
Stat map[string]string
Stat map[string]string
}
25 changes: 10 additions & 15 deletions test/e2e/performanceprofile/functests/utils/cgroup/v1/v1.go
Expand Up @@ -57,33 +57,34 @@ func (cm *ControllersManager) Cpu(ctx context.Context, pod *corev1.Pod, containe
output := strings.Split(string(b), "\r\n")
cfg.Quota = output[0]
cfg.Period = output[1]
cfg.Stat, err = stat(cm.k8sClient, pod, containerName, childName)
return cfg, nil
}

func (cm *ControllersManager) CpuStat(ctx context.Context, pod *corev1.Pod, containerName, childName, runtimeType string) (*controller.CpuStat, error) {
cfg := &controller.CpuStat{}
cfg.Stat = make(map[string]string)
//stat fetch cpu.stat values
func stat(k8sclient *kubernetes.Clientset, pod *corev1.Pod, containerName, childName string) (map[string]string, error) {
cpuStat := make(map[string]string)
dirPath := path.Join(controller.CgroupMountPoint, childName)
cmd := []string{
"/bin/cat",
dirPath + "/cpu/cpu.stat",
}
b, err := pods.ExecCommandOnPod(cm.k8sClient, pod, containerName, cmd)
statBytes, err := pods.ExecCommandOnPod(k8sclient, pod, containerName, cmd)
if err != nil {
return nil, fmt.Errorf("failed to retrieve cgroup config for pod. pod=%q, container=%q; %w", client.ObjectKeyFromObject(pod).String(), containerName, err)
}
output := strings.TrimSpace(string(b))
output := strings.TrimSpace(string(statBytes))
interfacevalues := strings.Split(output, "\r\n")
// cpu.stat always contains 3 stats usage_usec, user_usec, system_usec
// only when cpu controller is enabled other stats like nr_periods etc are enabled
if len(interfacevalues) < 4 {
return nil, fmt.Errorf("cpu.stat doesn't contain all the cpu metrics required")
return nil, fmt.Errorf("CPU Controller is not enabled")
}
for _, v := range interfacevalues {
stat := strings.Split(v, " ")
cfg.Stat[stat[0]] = stat[1]
values := strings.Split(v, " ")
cpuStat[values[0]] = values[1]
}
return cfg, nil
return cpuStat, nil
}

func (cm *ControllersManager) Pod(ctx context.Context, pod *corev1.Pod, controllerConfig interface{}) error {
Expand All @@ -109,12 +110,6 @@ func (cm *ControllersManager) Container(ctx context.Context, pod *corev1.Pod, co
return err
}
*cc = *cfg
case *controller.CpuStat:
cfg, err := cm.CpuStat(ctx, pod, containerName, "", runtimeType)
if err != nil {
return err
}
*cc = *cfg
default:
return fmt.Errorf("failed to get the controller config type")
}
Expand Down
27 changes: 11 additions & 16 deletions test/e2e/performanceprofile/functests/utils/cgroup/v2/v2.go
Expand Up @@ -57,33 +57,34 @@ func (cm *ControllersManager) Cpu(ctx context.Context, pod *corev1.Pod, containe
quotaAndPeriod := strings.Split(output[0], " ")
cfg.Quota = quotaAndPeriod[0]
cfg.Period = quotaAndPeriod[1]
cfg.Stat, err = stat(cm.k8sClient, pod, containerName, childName)
return cfg, nil
}

func (cm *ControllersManager) CpuStat(ctx context.Context, pod *corev1.Pod, containerName, childName, runtimeType string) (*controller.CpuStat, error) {
cfg := &controller.CpuStat{}
cfg.Stat = make(map[string]string)
// stat fetch cpu.stat values
func stat(k8sclient *kubernetes.Clientset, pod *corev1.Pod, containerName, childName string) (map[string]string, error) {
cpuStat := make(map[string]string)
dirPath := path.Join(controller.CgroupMountPoint, childName)
cmd := []string{
"/bin/cat",
dirPath + "/cpu.stat",
dirPath + "/cpu/cpu.stat",
}
b, err := pods.ExecCommandOnPod(cm.k8sClient, pod, containerName, cmd)
statBytes, err := pods.ExecCommandOnPod(k8sclient, pod, containerName, cmd)
if err != nil {
return nil, fmt.Errorf("failed to retrieve cgroup config for pod. pod=%q, container=%q; %w", client.ObjectKeyFromObject(pod).String(), containerName, err)
}
output := strings.TrimSpace(string(b))
output := strings.TrimSpace(string(statBytes))
interfacevalues := strings.Split(output, "\r\n")
// cpu.stat always contains 3 stats usage_usec, user_usec, system_usec
// only when cpu controller is enabled other stats like nr_periods etc are enabled
if len(interfacevalues) < 4 {
return nil, fmt.Errorf("cpu.stat doesn't contain all the cpu metrics required")
return nil, fmt.Errorf("CPU Controller is not enabled")
}
for _, v := range interfacevalues {
stat := strings.Split(v, " ")
cfg.Stat[stat[0]] = stat[1]
values := strings.Split(v, " ")
cpuStat[values[0]] = values[1]
}
return cfg, nil
return cpuStat, nil
}

func (cm *ControllersManager) Pod(ctx context.Context, pod *corev1.Pod, controllerConfig interface{}) error {
Expand Down Expand Up @@ -113,12 +114,6 @@ func (cm *ControllersManager) Child(ctx context.Context, pod *corev1.Pod, contai
return err
}
*cc = *cfg
case *controller.CpuStat:
cfg, err := cm.CpuStat(ctx, pod, containerName, childName, runtimeType)
if err != nil {
return err
}
*cc = *cfg
default:
return fmt.Errorf("failed to get the controller config type")
}
Expand Down

0 comments on commit 36c5e63

Please sign in to comment.