1 Star 0 Fork 116

elddriver/mind-cluster

forked from Ascend/mind-cluster
暂停
 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ascend_used_chips.go 5.05 KB
一键复制 编辑 原始数据 按行查看 历史
lirui238 提交于 2025-04-08 14:20 +08:00 . !762 v7.0.RC1分支 回合master
/* Copyright(C) 2025. Huawei Technologies Co.,Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package device a series of device function
package device
import (
"context"
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/namespaces"
"k8s.io/apimachinery/pkg/util/sets"
"Ascend-device-plugin/pkg/common"
"ascend-common/common-utils/hwlog"
)
// GetUsedChips return chips used by process and containerd
func (tool *AscendTools) GetUsedChips() sets.String {
procUsedChips := tool.getChipsUsedByProcess()
containerUsedChips := tool.getChipsUsedByContainerd()
usedChips := procUsedChips.Union(containerUsedChips)
hwlog.RunLog.Debugf("get used chips: %v", usedChips)
return usedChips
}
// getChipsUsedByProcess return chips used by process
func (tool *AscendTools) getChipsUsedByProcess() sets.String {
if !common.ParamOption.PresetVDevice {
return sets.String{}
}
_, logicIDs, err := tool.dmgr.GetDeviceList()
if err != nil {
hwlog.RunLog.Warnf("get device list failed, err: %v", err)
return sets.String{}
}
if len(logicIDs) < 1 {
hwlog.RunLog.Warn("get device list failed, logicID is empty")
return sets.String{}
}
usedChips := make([]string, 0, len(logicIDs))
for _, logicID := range logicIDs {
chipInfo, err := tool.dmgr.GetDevProcessInfo(logicID)
if err != nil {
// use vnpu will report an 8255 error
hwlog.RunLog.Debugf("get device process info failed, err: %v", err)
continue
}
if chipInfo.ProcNum != 0 {
hwlog.RunLog.Debugf("the card logicID:[%d] is used, chipInfo: %#v", logicID, chipInfo)
davinCidev, err := tool.getDavinCiDev(logicID)
if err != nil {
hwlog.RunLog.Errorf("get davinci dev by logicID:[%d] failed, err: %v", logicID, err)
continue
}
chipName := fmt.Sprintf("%s-%d", tool.name, davinCidev.PhyID)
usedChips = append(usedChips, chipName)
}
}
curProcUsedChips := sets.NewString(usedChips...)
if !curProcUsedChips.Equal(tool.lastUsedChipsByProcess) {
hwlog.RunLog.Infof("process used chips: %v", usedChips)
tool.lastUsedChipsByProcess = curProcUsedChips
}
return curProcUsedChips
}
// getChipsUsedByContainerd return chips used by process
func (tool *AscendTools) getChipsUsedByContainerd() sets.String {
usedChips := sets.NewString()
if tool.containerdClient == nil {
hwlog.RunLog.Debug("containerd client is nil")
return usedChips
}
nss, err := tool.containerdClient.NamespaceService().List(context.Background())
if err != nil {
hwlog.RunLog.Warnf("failed to get namespace list: %v", err)
return usedChips
}
hwlog.RunLog.Debugf("containerd namespace list: %v", nss)
curUsedChipsContainerMap := make(map[string]sets.String)
for _, ns := range nss {
ctx := namespaces.WithNamespace(context.Background(), ns)
taskList, err := tool.containerdClient.TaskService().List(ctx, &tasks.ListTasksRequest{})
if err != nil {
hwlog.RunLog.Warnf("failed to get task list: %v", err)
continue
}
if len(taskList.Tasks) == 0 {
hwlog.RunLog.Debugf("no tasks found in namespace %s", ns)
continue
}
for _, taskInfo := range taskList.Tasks {
hwlog.RunLog.Debugf("Task ID: %s, PID: %d", taskInfo.ID, taskInfo.Pid)
containerObj, err := tool.containerdClient.LoadContainer(ctx, taskInfo.ID)
if err != nil {
hwlog.RunLog.Warnf("failed to load container %s, err: %v", taskInfo.ID, err)
continue
}
usedChipsWithoutAscendRuntime := tool.getDeviceWithoutAscendRuntime(containerObj, ctx)
if usedChipsWithoutAscendRuntime.Len() > 0 {
curUsedChipsContainerMap[taskInfo.ID] = usedChipsWithoutAscendRuntime
}
usedChips = usedChips.Union(usedChipsWithoutAscendRuntime)
}
}
if !common.CompareStringSetMap(curUsedChipsContainerMap, tool.lastUsedChipsContainerMap) {
hwlog.RunLog.Infof("containerd used chips: %v", curUsedChipsContainerMap)
tool.lastUsedChipsContainerMap = curUsedChipsContainerMap
}
return usedChips
}
func (tool *AscendTools) getDeviceWithoutAscendRuntime(containerObj containerd.Container,
ctx context.Context) sets.String {
usedChips := sets.NewString()
spec, err := getContainerValidSpec(containerObj, ctx)
if err != nil {
hwlog.RunLog.Debugf("failed to get container valid spec: %v", err)
return usedChips
}
deviceIDs, err := filterNPUDevices(spec)
if err != nil {
hwlog.RunLog.Debugf("failed to get device ids: %v", err)
return usedChips
}
hwlog.RunLog.Debugf("filter npu devices get deviceIDs: %v", deviceIDs)
for _, deviceID := range deviceIDs {
chipName := fmt.Sprintf("%s-%d", tool.name, deviceID)
usedChips.Insert(chipName)
}
return usedChips
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/elddriver/mind-cluster.git
git@gitee.com:elddriver/mind-cluster.git
elddriver
mind-cluster
mind-cluster
366d0f7fc38e

搜索帮助