Skip to content

Commit 79b8d17

Browse files
authored
feat: Support gpu of schema v11 (#7987)
1 parent ec746c0 commit 79b8d17

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

agent/utils/ai_tools/gpu/gpu.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"github.com/1Panel-dev/1Panel/agent/global"
1414
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/common"
15-
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/schema_v12"
15+
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/schema"
1616
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
1717
)
1818

@@ -28,7 +28,7 @@ func (n NvidiaSMI) LoadGpuInfo() (*common.GpuInfo, error) {
2828
return nil, fmt.Errorf("calling nvidia-smi failed, err: %w", err)
2929
}
3030
data := []byte(itemData)
31-
schema := "v11"
31+
version := "v11"
3232

3333
buf := bytes.NewBuffer(data)
3434
decoder := xml.NewDecoder(buf)
@@ -51,15 +51,18 @@ func (n NvidiaSMI) LoadGpuInfo() (*common.GpuInfo, error) {
5151
parts := strings.Split(directive, " ")
5252
s := strings.Trim(parts[len(parts)-1], "\" ")
5353
if strings.HasPrefix(s, "nvsmi_device_") && strings.HasSuffix(s, ".dtd") {
54-
schema = strings.TrimSuffix(strings.TrimPrefix(s, "nvsmi_device_"), ".dtd")
54+
version = strings.TrimSuffix(strings.TrimPrefix(s, "nvsmi_device_"), ".dtd")
5555
} else {
5656
global.LOG.Debugf("Cannot find schema version in %q", directive)
5757
}
5858
break
5959
}
6060

61-
if schema != "v12" {
62-
return &common.GpuInfo{}, nil
61+
if version == "v12" || version == "v11" {
62+
return schema.Parse(data, version)
63+
} else {
64+
global.LOG.Errorf("don't support such schema version %s", version)
6365
}
64-
return schema_v12.Parse(data)
66+
67+
return &common.GpuInfo{}, nil
6568
}

agent/utils/ai_tools/gpu/schema_v12/parser.go renamed to agent/utils/ai_tools/gpu/schema/parser.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package schema_v12
1+
package schema
22

33
import (
44
"encoding/xml"
55

66
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/common"
77
)
88

9-
func Parse(buf []byte) (*common.GpuInfo, error) {
9+
func Parse(buf []byte, version string) (*common.GpuInfo, error) {
1010
var (
1111
s smi
1212
info common.GpuInfo
@@ -33,8 +33,13 @@ func Parse(buf []byte) (*common.GpuInfo, error) {
3333

3434
gpuItem.Temperature = s.Gpu[i].Temperature.GpuTemp
3535
gpuItem.PerformanceState = s.Gpu[i].PerformanceState
36-
gpuItem.PowerDraw = s.Gpu[i].GpuPowerReadings.PowerDraw
37-
gpuItem.MaxPowerLimit = s.Gpu[i].GpuPowerReadings.MaxPowerLimit
36+
if version == "v12" {
37+
gpuItem.PowerDraw = s.Gpu[i].GpuPowerReadings.PowerDraw
38+
gpuItem.MaxPowerLimit = s.Gpu[i].GpuPowerReadings.MaxPowerLimit
39+
} else {
40+
gpuItem.PowerDraw = s.Gpu[i].PowerReadings.PowerDraw
41+
gpuItem.MaxPowerLimit = s.Gpu[i].PowerReadings.MaxPowerLimit
42+
}
3843
gpuItem.MemUsed = s.Gpu[i].FbMemoryUsage.Used
3944
gpuItem.MemTotal = s.Gpu[i].FbMemoryUsage.Total
4045
gpuItem.GPUUtil = s.Gpu[i].Utilization.GpuUtil

agent/utils/ai_tools/gpu/schema_v12/types.go renamed to agent/utils/ai_tools/gpu/schema/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package schema_v12
1+
package schema
22

33
type smi struct {
44
AttachedGpus string `xml:"attached_gpus"`

agent/utils/cmd/cmd.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) {
3636
}
3737

3838
func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
39+
env := os.Environ()
3940
cmd := exec.Command("bash", "-c", cmdStr)
4041
var stdout, stderr bytes.Buffer
4142
cmd.Stdout = &stdout
4243
cmd.Stderr = &stderr
44+
cmd.Env = env
4345
if err := cmd.Start(); err != nil {
4446
return "", err
4547
}
@@ -62,6 +64,7 @@ func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
6264
}
6365

6466
func ExecWithLogFile(cmdStr string, timeout time.Duration, outputFile string) error {
67+
env := os.Environ()
6568
cmd := exec.Command("bash", "-c", cmdStr)
6669

6770
outFile, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, constant.DirPerm)
@@ -72,6 +75,7 @@ func ExecWithLogFile(cmdStr string, timeout time.Duration, outputFile string) er
7275

7376
cmd.Stdout = outFile
7477
cmd.Stderr = outFile
78+
cmd.Env = env
7579

7680
if err := cmd.Start(); err != nil {
7781
return err
@@ -109,6 +113,7 @@ func ExecContainerScript(containerName, cmdStr string, timeout time.Duration) er
109113
}
110114

111115
func ExecShell(outPath string, timeout time.Duration, name string, arg ...string) error {
116+
env := os.Environ()
112117
file, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE, constant.FilePerm)
113118
if err != nil {
114119
return err
@@ -118,6 +123,7 @@ func ExecShell(outPath string, timeout time.Duration, name string, arg ...string
118123
cmd := exec.Command(name, arg...)
119124
cmd.Stdout = file
120125
cmd.Stderr = file
126+
cmd.Env = env
121127
if err := cmd.Start(); err != nil {
122128
return err
123129
}
@@ -147,10 +153,12 @@ func (cw *CustomWriter) Write(p []byte) (n int, err error) {
147153
return len(p), nil
148154
}
149155
func ExecShellWithTask(taskItem *task.Task, timeout time.Duration, name string, arg ...string) error {
156+
env := os.Environ()
150157
customWriter := &CustomWriter{taskItem: taskItem}
151158
cmd := exec.Command(name, arg...)
152159
cmd.Stdout = customWriter
153160
cmd.Stderr = customWriter
161+
cmd.Env = env
154162
if err := cmd.Start(); err != nil {
155163
return err
156164
}
@@ -172,10 +180,12 @@ func ExecShellWithTask(taskItem *task.Task, timeout time.Duration, name string,
172180
}
173181

174182
func Execf(cmdStr string, a ...interface{}) (string, error) {
183+
env := os.Environ()
175184
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
176185
var stdout, stderr bytes.Buffer
177186
cmd.Stdout = &stdout
178187
cmd.Stderr = &stderr
188+
cmd.Env = env
179189
err := cmd.Run()
180190
if err != nil {
181191
return handleErr(stdout, stderr, err)
@@ -184,10 +194,12 @@ func Execf(cmdStr string, a ...interface{}) (string, error) {
184194
}
185195

186196
func ExecWithCheck(name string, a ...string) (string, error) {
197+
env := os.Environ()
187198
cmd := exec.Command(name, a...)
188199
var stdout, stderr bytes.Buffer
189200
cmd.Stdout = &stdout
190201
cmd.Stderr = &stderr
202+
cmd.Env = env
191203
err := cmd.Run()
192204
if err != nil {
193205
return handleErr(stdout, stderr, err)
@@ -196,11 +208,13 @@ func ExecWithCheck(name string, a ...string) (string, error) {
196208
}
197209

198210
func ExecScript(scriptPath, workDir string) (string, error) {
211+
env := os.Environ()
199212
cmd := exec.Command("bash", scriptPath)
200213
var stdout, stderr bytes.Buffer
201214
cmd.Dir = workDir
202215
cmd.Stdout = &stdout
203216
cmd.Stderr = &stderr
217+
cmd.Env = env
204218
if err := cmd.Start(); err != nil {
205219
return "", err
206220
}
@@ -279,13 +293,15 @@ func Which(name string) bool {
279293
}
280294

281295
func ExecShellWithTimeOut(cmdStr, workdir string, logger *log.Logger, timeout time.Duration) error {
296+
env := os.Environ()
282297
ctx, cancel := context.WithTimeout(context.Background(), timeout)
283298
defer cancel()
284299

285300
cmd := exec.CommandContext(ctx, "bash", "-c", cmdStr)
286301
cmd.Dir = workdir
287302
cmd.Stdout = logger.Writer()
288303
cmd.Stderr = logger.Writer()
304+
cmd.Env = env
289305
if err := cmd.Start(); err != nil {
290306
return err
291307
}

frontend/src/views/ai/gpu/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ const loadEcc = (val: string) => {
329329
if (val === 'Enabled') {
330330
return i18n.global.t('aiTools.gpu.enabled');
331331
}
332-
return val;
332+
return val || 0;
333333
};
334334
335335
const loadProcessType = (val: string) => {

0 commit comments

Comments
 (0)