github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/io/handlers/ioweight/ioweight_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2022 The Katalyst Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package ioweight
    21  
    22  import (
    23  	"context"
    24  	"strconv"
    25  
    26  	"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/commonstate"
    27  	coreconfig "github.com/kubewharf/katalyst-core/pkg/config"
    28  	dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
    29  	"github.com/kubewharf/katalyst-core/pkg/metaserver"
    30  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    31  	"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
    32  	cgroupmgr "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager"
    33  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    34  	"github.com/kubewharf/katalyst-core/pkg/util/native"
    35  )
    36  
    37  func applyIOWeightCgroupLevelConfig(conf *coreconfig.Configuration, emitter metrics.MetricEmitter) {
    38  	if conf.IOWeightCgroupLevelConfigFile == "" {
    39  		general.Errorf("IOWeightCgroupLevelConfigFile isn't configured")
    40  		return
    41  	}
    42  
    43  	ioWightCgroupLevelConfigs := make(map[string]uint64)
    44  	err := general.LoadJsonConfig(conf.IOWeightCgroupLevelConfigFile, &ioWightCgroupLevelConfigs)
    45  	if err != nil {
    46  		general.Errorf("load IOWeightCgroupLevelConfig failed with error: %v", err)
    47  		return
    48  	}
    49  
    50  	for relativeCgPath, weight := range ioWightCgroupLevelConfigs {
    51  		err := cgroupmgr.ApplyIOWeightWithRelativePath(relativeCgPath, defaultDevID, weight)
    52  		if err != nil {
    53  			general.Errorf("ApplyIOWeightWithRelativePath for devID: %s in relativeCgPath: %s failed with error: %v",
    54  				defaultDevID, relativeCgPath, err)
    55  		} else {
    56  			general.Infof("ApplyIOWeightWithRelativePath for devID: %s, weight: %d in relativeCgPath: %s successfully",
    57  				defaultDevID, weight, relativeCgPath)
    58  			_ = emitter.StoreInt64(metricNameIOWeight, int64(weight), metrics.MetricTypeNameRaw,
    59  				metrics.ConvertMapToTags(map[string]string{
    60  					"cgPath": relativeCgPath,
    61  				})...)
    62  
    63  		}
    64  	}
    65  }
    66  
    67  func applyIOWeightQoSLevelConfig(conf *coreconfig.Configuration,
    68  	emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer,
    69  ) {
    70  	if conf.IOWeightQoSLevelConfigFile == "" {
    71  		general.Infof("no IOWeightQoSLevelConfigFile found")
    72  		return
    73  	}
    74  
    75  	var extraControlKnobConfigs commonstate.ExtraControlKnobConfigs
    76  	if err := general.LoadJsonConfig(conf.IOWeightQoSLevelConfigFile, &extraControlKnobConfigs); err != nil {
    77  		general.Errorf("IOWeightQoSLevelConfigFile load failed:%v", err)
    78  		return
    79  	}
    80  	ctx := context.Background()
    81  	podList, err := metaServer.GetPodList(ctx, native.PodIsActive)
    82  	if err != nil {
    83  		general.Infof("get pod list failed: %v", err)
    84  		return
    85  	}
    86  
    87  	for _, pod := range podList {
    88  		if pod == nil {
    89  			general.Warningf("get nil pod from metaServer")
    90  			continue
    91  		}
    92  		if conf.QoSConfiguration == nil {
    93  			continue
    94  		}
    95  		qosConfig := conf.QoSConfiguration
    96  		qosLevel, err := qosConfig.GetQoSLevelForPod(pod)
    97  		if err != nil {
    98  			general.Warningf("GetQoSLevelForPod failed:%v", err)
    99  			continue
   100  		}
   101  		qosLevelDefaultValue, ok := extraControlKnobConfigs[controlKnobKeyIOWeight].QoSLevelToDefaultValue[qosLevel]
   102  		if !ok {
   103  			continue
   104  		}
   105  		for _, containerStatus := range pod.Status.ContainerStatuses {
   106  			podUID, containerID := string(pod.UID), native.TrimContainerIDPrefix(containerStatus.ContainerID)
   107  			err := cgroupmgr.ApplyUnifiedDataForContainer(podUID, containerID, extraControlKnobConfigs[controlKnobKeyIOWeight].CgroupSubsysName, cgroupIOWeightName, qosLevelDefaultValue)
   108  			if err != nil {
   109  				general.Warningf("ApplyUnifiedDataForContainer failed:%v", err)
   110  				continue
   111  			}
   112  
   113  			ioWeightValue, err := strconv.ParseInt(qosLevelDefaultValue, 10, 64)
   114  			if err != nil {
   115  				general.Warningf("strconv.ParseInt failed, string=%v, err=%v", qosLevelDefaultValue, err)
   116  				continue
   117  			}
   118  
   119  			_ = emitter.StoreInt64(metricNameIOWeight, ioWeightValue, metrics.MetricTypeNameRaw,
   120  				metrics.ConvertMapToTags(map[string]string{
   121  					"podUID":      podUID,
   122  					"containerID": containerID,
   123  				})...)
   124  		}
   125  	}
   126  }
   127  
   128  func IOWeightTaskFunc(conf *coreconfig.Configuration,
   129  	_ interface{}, _ *dynamicconfig.DynamicAgentConfiguration,
   130  	emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer,
   131  ) {
   132  	general.Infof("called")
   133  
   134  	if conf == nil {
   135  		general.Errorf("nil extraConf")
   136  		return
   137  	} else if emitter == nil {
   138  		general.Errorf("nil emitter")
   139  		return
   140  	} else if metaServer == nil {
   141  		general.Errorf("nil metaServer")
   142  		return
   143  	}
   144  
   145  	// SettingIOWeight featuregate.
   146  	if !conf.EnableSettingIOWeight {
   147  		general.Infof("EnableSettingIOWeight disabled")
   148  		return
   149  	}
   150  
   151  	if !common.CheckCgroup2UnifiedMode() {
   152  		general.Infof("skip IOWeightTaskFunc in cg1 env")
   153  		return
   154  	}
   155  
   156  	// checking qos-level io.weight configuration.
   157  	if len(conf.IOWeightQoSLevelConfigFile) > 0 {
   158  		applyIOWeightQoSLevelConfig(conf, emitter, metaServer)
   159  	}
   160  
   161  	// checking cgroup-level io.weight configuration.
   162  	if len(conf.IOWeightCgroupLevelConfigFile) > 0 {
   163  		applyIOWeightCgroupLevelConfig(conf, emitter)
   164  	}
   165  }