From d76903e9e3caf09b62c637c2b4926cdf65a27301 Mon Sep 17 00:00:00 2001 From: "maofeng.huang" Date: Tue, 11 Feb 2025 16:01:52 +0800 Subject: [PATCH] update samples and fix the issue of getPidName --- pkg/ixml/utils.go | 2 + samples/attributes/main.go | 7 ++-- samples/gpmmetrics/main.go | 28 +++++++------- samples/metrics/main.go | 77 ++++++++++++++++++++++++------------- samples/processinfo/main.go | 7 ++-- samples/sameboard/main.go | 11 +++--- samples/system/main.go | 1 - 7 files changed, 79 insertions(+), 54 deletions(-) diff --git a/pkg/ixml/utils.go b/pkg/ixml/utils.go index 31c078c..dfa57df 100644 --- a/pkg/ixml/utils.go +++ b/pkg/ixml/utils.go @@ -18,6 +18,7 @@ limitations under the License. package ixml import ( + "bytes" "fmt" "os" "strings" @@ -41,5 +42,6 @@ func getPidName(pid uint32) string { if err != nil { return "" } + data = bytes.ReplaceAll(data, []byte{0}, []byte{' '}) return strings.TrimSuffix(string(data), "\x00") } diff --git a/samples/attributes/main.go b/samples/attributes/main.go index b2944e2..0478ea1 100644 --- a/samples/attributes/main.go +++ b/samples/attributes/main.go @@ -32,7 +32,6 @@ func main() { ret := ixml.Init() if ret != ixml.SUCCESS { log.Fatalf("Unable to initialize IXML: %v", ret) - return } defer func() { ret := ixml.Shutdown() @@ -60,13 +59,13 @@ func main() { Integer, Decimal, ret := device.GetGPUVoltage() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get Integer, Decimal %v", ret) + log.Fatalf("Unable to get GPU Voltage: %v", ret) } - fmt.Printf("Integer, Decimal: %v.%v\n", Integer, Decimal) + fmt.Printf("GPU Voltage: %v.%v\n", Integer, Decimal) pos, ret := device.GetBoardPosition() if ret == ixml.ERROR_NOT_SUPPORTED { - fmt.Printf("position interface: Not supported\n") + fmt.Printf("GetBoardPosition interface is not supported\n") } else if ret != ixml.SUCCESS { log.Fatalf("Unable to get BoardPosition %v", ret) } else { diff --git a/samples/gpmmetrics/main.go b/samples/gpmmetrics/main.go index f090045..ef2938f 100644 --- a/samples/gpmmetrics/main.go +++ b/samples/gpmmetrics/main.go @@ -28,23 +28,22 @@ import ( func main() { ret := ixml.AbsInit("/usr/local/corex/lib/libixml.so") if ret != ixml.SUCCESS { - log.Fatalf("Unable to initialize IXML: %v", ret) - return + log.Fatalf("failed to initialize IXML: %v", ret) } defer func() { ret := ixml.Shutdown() if ret != ixml.SUCCESS { - log.Fatalf("Unable to shutdown IXML: %v", ret) + log.Fatalf("failed to shutdown IXML: %v", ret) } }() count, ret := ixml.DeviceGetCount() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get device count %v", ret) + log.Fatalf("failed to get device count: %v", ret) } for i := uint(0); i < count; i++ { if err := collectGPMMetrics(i); err != nil { - log.Fatalf("Unable to collect metrics for device %d: %v", i, err) + log.Printf("failed to collect metrics for device %d: %v", i, err) } } @@ -54,38 +53,38 @@ func collectGPMMetrics(i uint) error { var device ixml.Device ret := ixml.DeviceGetHandleByIndex(i, &device) if ret != ixml.SUCCESS { - log.Fatalf("Unable to get device at index %d: %v", i, ret) + return fmt.Errorf("failed to get device at index %d: %v", i, ret) } gpuQuerySupport, ret := device.GpmQueryDeviceSupport() if ret != ixml.SUCCESS { - return fmt.Errorf("could not query GPM support: %w", ret) + return fmt.Errorf("failed to query GPM support: %v", ret) } if gpuQuerySupport.IsSupportedDevice == 0 { return fmt.Errorf("GPM queries are not supported") } - fmt.Printf("GPM queries are supported\n") + fmt.Printf("GPM queries are supported for device %d\n", i) sample1, ret := ixml.GpmSampleAlloc() if ret != ixml.SUCCESS { - return fmt.Errorf("could not allocate GPM sample: %w", ret) + return fmt.Errorf("failed to allocate GPM sample1: %v", ret) } defer func() { _ = sample1.Free() }() sample2, ret := ixml.GpmSampleAlloc() if ret != ixml.SUCCESS { - return fmt.Errorf("could not allocate GPM sample: %w", ret) + return fmt.Errorf("failed to allocate GPM sample2: %v", ret) } defer func() { _ = sample2.Free() }() if ret := device.GpmSampleGet(sample1); ret != ixml.SUCCESS { - return fmt.Errorf("could not get GPM sample: %w", ret) + return fmt.Errorf("failed to get GPM sample1: %v", ret) } time.Sleep(1 * time.Second) if ret := device.GpmSampleGet(sample2); ret != ixml.SUCCESS { - return fmt.Errorf("could not get GPM sample: %w", ret) + return fmt.Errorf("failed to get GPM sample2: %v", ret) } gpmMetric := ixml.GpmMetricsGetType{ @@ -107,12 +106,13 @@ func collectGPMMetrics(i uint) error { ret = ixml.GpmMetricsGet(&gpmMetric) if ret != ixml.SUCCESS { - return fmt.Errorf("failed to get gpm metric: %w", ret) + return fmt.Errorf("failed to get gpm metric: %v", ret) } + fmt.Printf("success to get gpm metric for device %d\n", i) for i := 0; i < int(gpmMetric.NumMetrics); i++ { if gpmMetric.Metrics[i].MetricId > 0 { - fmt.Printf("gpmMetric id: %v, value: %v\n", gpmMetric.Metrics[i].MetricId, gpmMetric.Metrics[i].Value) + fmt.Printf("gpm metric id: %+4v, value: %v\n", gpmMetric.Metrics[i].MetricId, gpmMetric.Metrics[i].Value) } } diff --git a/samples/metrics/main.go b/samples/metrics/main.go index c736730..4c7e63e 100644 --- a/samples/metrics/main.go +++ b/samples/metrics/main.go @@ -28,7 +28,6 @@ func main() { ret := ixml.Init() if ret != ixml.SUCCESS { log.Fatalf("Unable to initialize IXML: %v", ret) - return } defer func() { ret := ixml.Shutdown() @@ -47,72 +46,98 @@ func main() { var device ixml.Device ret = ixml.DeviceGetHandleByIndex(i, &device) if ret != ixml.SUCCESS { - log.Fatalf("Unable to get device at index %d: %v", i, ret) + fmt.Printf("Unable to get device at index %d: %v\n", i, ret) + } else { + fmt.Printf("Get device at index %d\n", i) } Uuid, ret := device.GetUUID() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU Uuid of device %d: %v", i, ret) + fmt.Printf("Unable to get GPU Uuid of device %d: %v\n", i, ret) + } else { + fmt.Printf("Uuid of device %d: %s\n", i, Uuid) } - fmt.Printf("Uuid of device %d: %s\n", i, Uuid) MinorNumber, ret := device.GetMinorNumber() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU MinorNumber of device %d: %v", i, ret) + fmt.Printf("Unable to get GPU MinorNumber of device %d: %v\n", i, ret) + } else { + fmt.Printf("MinorNumber of device %d: %d\n", i, MinorNumber) } - fmt.Printf("MinorNumber of device %d: %d\n", i, MinorNumber) temperature, ret := device.GetTemperature() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU temperature of device %d: %v", i, ret) + fmt.Printf("Unable to get GPU temperature of device %d: %v\n", i, ret) + } else { + fmt.Printf("temperature of device %d: %d\n", i, temperature) } - fmt.Printf("temperature of device %d: %d\n", i, temperature) FanSpeed, ret := device.GetFanSpeed() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU FanSpeed of device %d: %v", i, ret) + fmt.Printf("Unable to get GPU FanSpeed of device %d: %v\n", i, ret) + } else { + fmt.Printf("FanSpeed of device %d: %d\n", i, FanSpeed) } - fmt.Printf("FanSpeed of device %d: %d\n", i, FanSpeed) ClockInfo, ret := device.GetClockInfo() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU MemClock of device %d: %v", i, ret) + fmt.Printf("Unable to get GPU MemClock of device %d: %v\n", i, ret) + } else { + fmt.Printf("MemClock of device %d: %d\n", i, ClockInfo.Mem) } - fmt.Printf("MemClock of device %d: %d\n", i, ClockInfo.Mem) MemoryInfo, ret := device.GetMemoryInfo() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU MemoryInfo of device %d: %v", i, ret) + fmt.Printf("Unable to get MemoryInfo of device %d: %v\n", i, ret) + } else { + fmt.Printf("MemoryInfo totalMem of device %d: %d (MiB)\n", i, MemoryInfo.Total) + fmt.Printf("MemoryInfo usedMem of device %d: %d (MiB)\n", i, MemoryInfo.Used) + fmt.Printf("MemoryInfo freeMem of device %d: %v (MiB)\n", i, MemoryInfo.Free) } - fmt.Printf("MemoryInfo totalMem of device %d: %d (MiB)\n", i, MemoryInfo.Total) - fmt.Printf("MemoryInfo usedMem of device %d: %d (MiB)\n", i, MemoryInfo.Used) - fmt.Printf("MemoryInfo freeMem of device %d: %v (MiB)\n", i, MemoryInfo.Free) utilizationRates, ret := device.GetUtilizationRates() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU utilizationRates of device %d: %v", i, ret) + fmt.Printf("Unable to get UtilizationRates of device %d: %v\n", i, ret) + } else { + fmt.Printf("Mem utilizationRates of device %d: %d\n", i, utilizationRates.Memory) + fmt.Printf("GPU utilizationRates of device %d: %d\n", i, utilizationRates.Gpu) } - fmt.Printf("Mem utilizationRates of device %d: %d\n", i, utilizationRates.Memory) - fmt.Printf("GPU utilizationRates of device %d: %d\n", i, utilizationRates.Gpu) PciInfo, ret := device.GetPciInfo() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU PciInfo of device %d: %v", i, ret) + fmt.Printf("Unable to get PciInfo of device %d: %v\n", i, ret) + } else { + fmt.Printf("PciInfo of device %d: %v\n", i, PciInfo.BusId) + } + + pcieReplyCnt, ret := device.GetPcieReplayCounter() + if ret != ixml.SUCCESS { + fmt.Printf("Unable to get PcieReplayCounter of device %d: %v\n", i, ret) + } else { + fmt.Printf("PcieReplayCounter of device %d: %v\n", i, pcieReplyCnt) } - fmt.Printf("PciInfo of device %d: %v\n", i, PciInfo.BusId) clocksThrottleReasons, ret := device.GetCurrentClocksThrottleReasons() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get GPU clocksThrottleReasons of device %d: %v", i, ret) + fmt.Printf("Unable to get GPU ClocksThrottleReasons of device %d: %v\n", i, ret) + } else { + fmt.Printf("ClocksThrottleReasons of device %d: %v\n", i, clocksThrottleReasons) } - fmt.Printf("clocksThrottleReasons of device %d: %v\n", i, clocksThrottleReasons) singleErr, doubleErr, ret := device.GetEccErros() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get ecc errors %d: %v", i, ret) + fmt.Printf("Unable to get ecc errors %d: %v\n", i, ret) + } else { + fmt.Printf("singleErr: %d, doubleErr: %d\n", singleErr, doubleErr) + } + + minLimit, maxLimit, ret := device.GetPowerManagementLimitConstraints() + if ret != ixml.SUCCESS { + fmt.Printf("Unable to get PowerManagementLimitConstraints of device %d: %v\n", i, ret) + } else { + fmt.Printf("minLimit: %d, maxLimit: %d\n", minLimit, maxLimit) } - fmt.Printf("singleErr: %d, doubleErr: %d\n", singleErr, doubleErr) - fmt.Println("========================================") + fmt.Println("------------------------------------") } } diff --git a/samples/processinfo/main.go b/samples/processinfo/main.go index 26832ac..359a39a 100644 --- a/samples/processinfo/main.go +++ b/samples/processinfo/main.go @@ -28,7 +28,6 @@ func main() { ret := ixml.AbsInit("/usr/local/corex/lib/libixml.so") if ret != ixml.SUCCESS { log.Fatalf("Unable to initialize IXML: %v", ret) - return } defer func() { ret := ixml.Shutdown() @@ -45,12 +44,14 @@ func main() { var device ixml.Device ret := ixml.DeviceGetHandleByIndex(i, &device) if ret != ixml.SUCCESS { - log.Fatalf("Unable to get device at index %d: %v", i, ret) + fmt.Printf("Unable to get device at index %d: %v\n", i, ret) + continue } processInfos, ret := device.GetComputeRunningProcesses() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get processInfos %d: %v", i, ret) + fmt.Printf("Unable to get processInfos %d: %v\n", i, ret) + continue } fmt.Printf("found processInfos %d on device %d\n", len(processInfos), i) diff --git a/samples/sameboard/main.go b/samples/sameboard/main.go index 28d08ed..48a536c 100644 --- a/samples/sameboard/main.go +++ b/samples/sameboard/main.go @@ -39,7 +39,6 @@ func main() { ret := ixml.Init() if ret != ixml.SUCCESS { log.Fatalf("Unable to initialize IXML: %v", ret) - return } defer func() { ret := ixml.Shutdown() @@ -50,12 +49,12 @@ func main() { device1, ret := ixml.GetHandleByUUID(defalutGpu1) if ret != ixml.SUCCESS { - log.Fatalf("Unable to get Handle by uuid %v", ret) + log.Fatalf("Unable to get handle by uuid %v", ret) } name1, ret := device1.GetName() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get name1 %v", ret) + log.Fatalf("Unable to get name of Device1: %v", ret) } fmt.Printf("name1: %s\n", name1) @@ -66,7 +65,7 @@ func main() { name2, ret := device2.GetName() if ret != ixml.SUCCESS { - log.Fatalf("Unable to get name2 %v", ret) + log.Fatalf("Unable to get name of Device2: %v", ret) } fmt.Printf("name2: %s\n", name2) @@ -74,9 +73,9 @@ func main() { if ret == ixml.ERROR_NOT_SUPPORTED { fmt.Printf("GetOnSameBoard: Not supported\n") } else if ret != ixml.SUCCESS { - log.Fatalf("Unable to get OnSameBoard %v", ret) + log.Printf("Device1 and Device2 Not On Same Board: %v\n", ret) } else { - fmt.Printf("device1 && device2 On Same Board: %d\n", OnSameBoard) + fmt.Printf("Device1 and Device2 On Same Board: %d\n", OnSameBoard) } fmt.Println("========================================") diff --git a/samples/system/main.go b/samples/system/main.go index 05d7fc3..e43998c 100644 --- a/samples/system/main.go +++ b/samples/system/main.go @@ -28,7 +28,6 @@ func main() { ret := ixml.Init() if ret != ixml.SUCCESS { log.Fatalf("Unable to initialize IXML: %v", ret) - return } defer func() { ret := ixml.Shutdown() -- Gitee