github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/agent/qrm/memory_plugin.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package qrm
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"sync"
    23  
    24  	"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent"
    25  	phconsts "github.com/kubewharf/katalyst-core/pkg/agent/utilcomponent/periodicalhandler/consts"
    26  	"github.com/kubewharf/katalyst-core/pkg/config"
    27  )
    28  
    29  const (
    30  	QRMPluginNameMemory = "qrm_memory_plugin"
    31  )
    32  
    33  var (
    34  	// memoryPolicyInitializers is used to store the initializing function for cpu resource plugin policies
    35  	memoryPolicyInitializers sync.Map
    36  
    37  	QRMMemoryPluginPeriodicalHandlerGroupName = strings.Join([]string{
    38  		QRMPluginNameMemory,
    39  		phconsts.PeriodicalHandlersGroupNameSuffix,
    40  	}, phconsts.GroupNameSeparator)
    41  )
    42  
    43  // RegisterMemoryPolicyInitializer is used to register user-defined resource plugin init functions
    44  func RegisterMemoryPolicyInitializer(name string, initFunc agent.InitFunc) {
    45  	memoryPolicyInitializers.Store(name, initFunc)
    46  }
    47  
    48  // getMemoryPolicyInitializers returns those policies with initialized functions
    49  func getMemoryPolicyInitializers() map[string]agent.InitFunc {
    50  	agents := make(map[string]agent.InitFunc)
    51  	memoryPolicyInitializers.Range(func(key, value interface{}) bool {
    52  		agents[key.(string)] = value.(agent.InitFunc)
    53  		return true
    54  	})
    55  	return agents
    56  }
    57  
    58  func InitQRMMemoryPlugins(agentCtx *agent.GenericContext, conf *config.Configuration, extraConf interface{}, agentName string) (bool, agent.Component, error) {
    59  	initializers := getMemoryPolicyInitializers()
    60  	policyName := conf.MemoryQRMPluginConfig.PolicyName
    61  
    62  	initFunc, ok := initializers[policyName]
    63  	if !ok {
    64  		return false, agent.ComponentStub{}, fmt.Errorf("invalid policy name %v for memory resource plugin", policyName)
    65  	}
    66  
    67  	return initFunc(agentCtx, conf, extraConf, agentName)
    68  }