Skip to content

Commit 28cdb2f

Browse files
authored
Merge pull request #16 from mfreeman451/main
💄 compile regex one time
2 parents 52b618c + a2c734c commit 28cdb2f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ var (
6161
updateInterval = 1000
6262
)
6363

64+
var (
65+
dataRegex = regexp.MustCompile(`(?m)^\s*(\S.*?)\s+(\d+)\s+(\d+\.\d+)\s+\d+\.\d+\s+`)
66+
outRegex = regexp.MustCompile(`out:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
67+
inRegex = regexp.MustCompile(`in:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
68+
readRegex = regexp.MustCompile(`read:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
69+
writeRegex = regexp.MustCompile(`write:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
70+
residencyRe = regexp.MustCompile(`(\w+-Cluster)\s+HW active residency:\s+(\d+\.\d+)%`)
71+
frequencyRe = regexp.MustCompile(`(\w+-Cluster)\s+HW active frequency:\s+(\d+)\s+MHz`)
72+
re = regexp.MustCompile(`GPU\s*(HW)?\s*active\s*(residency|frequency):\s+(\d+\.\d+)%?`)
73+
freqRe = regexp.MustCompile(`(\d+)\s*MHz:\s*(\d+)%`)
74+
)
75+
6476
func setupUI() {
6577
appleSiliconModel := getSOCInfo()
6678
modelText = w.NewParagraph()
@@ -525,7 +537,6 @@ func updateProcessUI(processMetrics []ProcessMetrics) {
525537

526538
func parseProcessMetrics(powermetricsOutput string, processMetrics []ProcessMetrics) []ProcessMetrics {
527539
lines := strings.Split(powermetricsOutput, "\n")
528-
dataRegex := regexp.MustCompile(`(?m)^\s*(\S.*?)\s+(\d+)\s+(\d+\.\d+)\s+\d+\.\d+\s+`)
529540
seen := make(map[int]bool) // Map to track seen process IDs
530541
for _, line := range lines {
531542
matches := dataRegex.FindStringSubmatch(line)
@@ -554,8 +565,7 @@ func parseProcessMetrics(powermetricsOutput string, processMetrics []ProcessMetr
554565
}
555566

556567
func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetrics) NetDiskMetrics {
557-
outRegex := regexp.MustCompile(`out:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
558-
inRegex := regexp.MustCompile(`in:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
568+
559569
outMatches := outRegex.FindStringSubmatch(powermetricsOutput)
560570
inMatches := inRegex.FindStringSubmatch(powermetricsOutput)
561571
if len(outMatches) == 3 {
@@ -566,8 +576,7 @@ func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetri
566576
netdiskMetrics.InPacketsPerSec, _ = strconv.ParseFloat(inMatches[1], 64)
567577
netdiskMetrics.InBytesPerSec, _ = strconv.ParseFloat(inMatches[2], 64)
568578
}
569-
readRegex := regexp.MustCompile(`read:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
570-
writeRegex := regexp.MustCompile(`write:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
579+
571580
readMatches := readRegex.FindStringSubmatch(powermetricsOutput)
572581
writeMatches := writeRegex.FindStringSubmatch(powermetricsOutput)
573582
if len(readMatches) == 3 {
@@ -587,8 +596,7 @@ func parseCPUMetrics(powermetricsOutput string, cpuMetrics CPUMetrics, modelName
587596
pCores := []int{}
588597
var eClusterActiveSum, pClusterActiveSum, eClusterFreqSum, pClusterFreqSum float64
589598
var eClusterCount, pClusterCount, eClusterActiveTotal, pClusterActiveTotal, eClusterFreqTotal, pClusterFreqTotal int
590-
residencyRe := regexp.MustCompile(`(\w+-Cluster)\s+HW active residency:\s+(\d+\.\d+)%`)
591-
frequencyRe := regexp.MustCompile(`(\w+-Cluster)\s+HW active frequency:\s+(\d+)\s+MHz`)
599+
592600
if modelName == "Apple M3 Max" || modelName == "Apple M2 Max" { // For the M3/M2 Max, we need to manually parse the CPU Usage from the powermetrics output (as current bug in Apple's powermetrics)
593601
for _, line := range lines {
594602

@@ -808,8 +816,7 @@ func max(nums ...int) int {
808816
}
809817

810818
func parseGPUMetrics(powermetricsOutput string, gpuMetrics GPUMetrics) GPUMetrics {
811-
re := regexp.MustCompile(`GPU\s*(HW)?\s*active\s*(residency|frequency):\s+(\d+\.\d+)%?`)
812-
freqRe := regexp.MustCompile(`(\d+)\s*MHz:\s*(\d+)%`)
819+
813820
lines := strings.Split(powermetricsOutput, "\n")
814821

815822
for _, line := range lines {

0 commit comments

Comments
 (0)