github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/io/handlers/dirtymem/wbt_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 dirtymem
    21  
    22  import (
    23  	"fmt"
    24  	"io/ioutil"
    25  	"os"
    26  
    27  	coreconfig "github.com/kubewharf/katalyst-core/pkg/config"
    28  	coreconsts "github.com/kubewharf/katalyst-core/pkg/consts"
    29  	"github.com/kubewharf/katalyst-core/pkg/metaserver"
    30  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/helper"
    31  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    32  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    33  )
    34  
    35  func SetWBTLimit(conf *coreconfig.Configuration,
    36  	emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer,
    37  ) {
    38  	general.Infof("called")
    39  	if conf == nil {
    40  		general.Errorf("nil Conf")
    41  		return
    42  	} else if emitter == nil {
    43  		general.Errorf("nil emitter")
    44  		return
    45  	} else if metaServer == nil {
    46  		general.Errorf("nil metaServer")
    47  		return
    48  	}
    49  	dir, err := ioutil.ReadDir(sysDiskPrefix)
    50  	if err != nil {
    51  		general.Errorf("failed to readdir:%v, err:%v", sysDiskPrefix, err)
    52  		return
    53  	}
    54  	for _, entry := range dir {
    55  		diskType, err := helper.GetDeviceMetric(metaServer.MetricsFetcher, emitter, coreconsts.MetricIODiskType, entry.Name())
    56  		if err != nil {
    57  			continue
    58  		}
    59  		wbtValue := 0
    60  		if diskType == coreconsts.DiskTypeHDD {
    61  			// -1 means skip it.
    62  			if conf.WBTValueHDD == -1 {
    63  				continue
    64  			}
    65  			wbtValue = conf.WBTValueHDD
    66  		} else if diskType == coreconsts.DiskTypeSSD {
    67  			// -1 means skip it.
    68  			if conf.WBTValueSSD == -1 {
    69  				continue
    70  			}
    71  			wbtValue = conf.WBTValueSSD
    72  		} else if diskType == coreconsts.DiskTypeNVME {
    73  			// -1 means skip it.
    74  			if conf.WBTValueNVME == -1 {
    75  				continue
    76  			}
    77  			wbtValue = conf.WBTValueNVME
    78  		} else {
    79  			continue // currently, only SSD/HDD/NVME were supported.
    80  		}
    81  
    82  		oldWBTValue, err := helper.GetDeviceMetric(metaServer.MetricsFetcher, emitter, coreconsts.MetricIODiskWBTValue, entry.Name())
    83  		if err != nil {
    84  			continue
    85  		}
    86  
    87  		if oldWBTValue == float64(wbtValue) {
    88  			continue // no need to set it.
    89  		}
    90  
    91  		wbtFilePath := sysDiskPrefix + "/" + entry.Name() + "/" + wbtSuffix
    92  		general.Infof("Apply WBT, device=%v, old value=%v, new value=%v", entry.Name(), oldWBTValue, wbtValue)
    93  		err = os.WriteFile(wbtFilePath, []byte(fmt.Sprintf("%d", wbtValue)), 0o644)
    94  		if err != nil {
    95  			general.Errorf("failed to write new wbt:%v to :%v, err:%v", wbtValue, entry.Name(), err)
    96  			continue
    97  		}
    98  		_ = emitter.StoreInt64(metricNameDiskWBT, int64(wbtValue), metrics.MetricTypeNameRaw,
    99  			metrics.ConvertMapToTags(map[string]string{
   100  				"diskName": entry.Name(),
   101  			})...)
   102  	}
   103  }