Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: 修复用户空间的单节点内存不显示的问题 #28

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions services/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func (s *ServerConfig) GetClusterConfig(ctx context.Context, in *pb.GetClusterCo
totalGpus uint32
comment string
qos []string
totalMems int
totalCpus string
totalMemsTmp string
totalNodes string
Expand Down Expand Up @@ -173,20 +172,16 @@ func (s *ServerConfig) GetClusterConfig(ctx context.Context, in *pb.GetClusterCo
}
}

if strings.Contains(totalMemsTmp, "M") {
totalMemsInt, _ := strconv.Atoi(strings.Split(totalMemsTmp, "M")[0])
totalMems = totalMemsInt
} else if strings.Contains(totalMemsTmp, "G") {
totalMemsInt, _ := strconv.Atoi(strings.Split(totalMemsTmp, "G")[0])
totalMems = totalMemsInt * 1024
} else if strings.Contains(totalMemsTmp, "T") {
totalMemsInt, _ := strconv.Atoi(strings.Split(totalMemsTmp, "T")[0])
totalMems = totalMemsInt * 1024 * 1024
totalMemInt, err = utils.ConvertMemory(totalMemsTmp)
if err != nil {
errInfo := &errdetails.ErrorInfo{
Reason: "CONVERT_MEMORY_FAILED",
}
st := status.New(codes.Internal, "convert memory error")
st, _ = st.WithDetails(errInfo)
caller.Logger.Errorf("GetClusterConfig failed: %v", st.Err())
return nil, st.Err()
}

// 将字符串转换为int
totalCpuInt, _ = strconv.Atoi(totalCpus)
totalMemInt = totalMems
totalNodeNumInt, _ = strconv.Atoi(totalNodes)
} else if err != nil && !utils.CheckSlurmStatus(output) {
// 获取总cpu、总内存、总节点数
Expand Down Expand Up @@ -519,7 +514,6 @@ func (s *ServerConfig) GetAvailablePartitions(ctx context.Context, in *pb.GetAva
}
for _, partition := range partitions {
var (
totalMems int
totalGpus uint32
comment string
qos []string
Expand Down Expand Up @@ -587,17 +581,16 @@ func (s *ServerConfig) GetAvailablePartitions(ctx context.Context, in *pb.GetAva
return nil, st.Err()
}

if strings.Contains(totalMemsTmp, "M") {
totalMemsInt, _ := strconv.Atoi(strings.Split(totalMemsTmp, "M")[0])
totalMems = totalMemsInt
} else if strings.Contains(totalMemsTmp, "G") {
totalMemsInt, _ := strconv.Atoi(strings.Split(totalMemsTmp, "G")[0])
totalMems = totalMemsInt * 1024
} else if strings.Contains(totalMemsTmp, "T") {
totalMemsInt, _ := strconv.Atoi(strings.Split(totalMemsTmp, "T")[0])
totalMems = totalMemsInt * 1024 * 1024
totalMemInt, err = utils.ConvertMemory(totalMemsTmp)
if err != nil {
errInfo := &errdetails.ErrorInfo{
Reason: "CONVERT_MEMORY_FAILED",
}
st := status.New(codes.Internal, "convert memory error")
st, _ = st.WithDetails(errInfo)
caller.Logger.Errorf("GetClusterConfig failed: %v", st.Err())
return nil, st.Err()
}
totalMemInt = totalMems
} else {
// 取节点名,默认取第一个元素,在判断有没有[特殊符合
getPartitionNodeNameCmd := fmt.Sprintf("scontrol show partition=%s | grep -i ' Nodes=' | awk -F'=' '{print $2}'", partition)
Expand Down
41 changes: 41 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,44 @@ func ExtractValue(input, key string) string {
}
return ""
}

func ConvertMemory(memoryString string) (int, error) {
// 使用正则表达式提取数字部分和单位
re := regexp.MustCompile(`^(\d+(?:\.\d+)?)([A-Z]+)$`)
matches := re.FindStringSubmatch(memoryString)

if len(matches) != 3 {
return 0, fmt.Errorf("illegal memory format")
}

// 提取数字部分
memorySizeStr := matches[1]

// 提取单位
unit := matches[2]

// 将字符串转换为float64
memorySize, err := strconv.ParseFloat(memorySizeStr, 64)
if err != nil {
return 0, fmt.Errorf("convert err: %v", err)
}

// 将TB转换为MB,1TB = 1024 * 1024MB
const bytesInMB = 1024 * 1024
var memorySizeMB float64

switch unit {
case "P":
memorySizeMB = memorySize * bytesInMB * 1024
case "T":
memorySizeMB = memorySize * bytesInMB
case "G":
memorySizeMB = memorySize * bytesInMB / 1024
case "M":
memorySizeMB = memorySize
default:
return 0, fmt.Errorf("unknown memory unit")
}

return int(memorySizeMB), nil
}
Loading