代码拉取完成,页面将自动刷新
同步操作将从 Ascend/mind-cluster 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/* 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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。