@@ -718,19 +718,31 @@ func maxInt(nums []int) int {
718718}
719719
720720func parseGPUMetrics (powermetricsOutput string , gpuMetrics GPUMetrics ) GPUMetrics {
721- re := regexp .MustCompile (`GPU HW active residency:\s+(\d+\.\d+)%` ) // Regex to capture the floating-point number followed by '%'
721+ re := regexp .MustCompile (`GPU\s*(HW)?\s*active\s*(residency|frequency):\s+(\d+\.\d+)%?` )
722+ freqRe := regexp .MustCompile (`(\d+)\s*MHz:\s*(\d+)%` )
722723 lines := strings .Split (powermetricsOutput , "\n " )
723724
724725 for _ , line := range lines {
725- if strings .Contains (line , "GPU HW active residency " ) {
726+ if strings .Contains (line , "GPU active" ) || strings . Contains ( line , "GPU HW active" ) {
726727 matches := re .FindStringSubmatch (line )
727- if len (matches ) > 1 {
728- gpuMetrics .Active , _ = strconv .ParseFloat (matches [1 ], 64 ) // matches[1] contains the first captured group, the percentage
728+ if len (matches ) > 3 {
729+ if strings .Contains (matches [2 ], "residency" ) {
730+ gpuMetrics .Active , _ = strconv .ParseFloat (matches [3 ], 64 )
731+ } else if strings .Contains (matches [2 ], "frequency" ) {
732+ gpuMetrics .FreqMHz , _ = strconv .Atoi (strings .TrimSuffix (matches [3 ], "MHz" ))
733+ }
729734 }
730- } else if strings .Contains (line , "GPU HW active frequency" ) {
731- fields := strings .Fields (line )
732- if len (fields ) >= 5 {
733- gpuMetrics .FreqMHz , _ = strconv .Atoi (strings .TrimSuffix (fields [4 ], "MHz" ))
735+
736+ freqMatches := freqRe .FindAllStringSubmatch (line , - 1 )
737+ for _ , match := range freqMatches {
738+ if len (match ) == 3 {
739+ freq , _ := strconv .Atoi (match [1 ])
740+ residency , _ := strconv .ParseFloat (match [2 ], 64 )
741+ if residency > 0 {
742+ gpuMetrics .FreqMHz = freq
743+ break
744+ }
745+ }
734746 }
735747 }
736748 }
0 commit comments