-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
994 lines (925 loc) · 33.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
// Copyright (c) 2024 Carsen Klock under MIT License
// mactop is a simple terminal based Apple Silicon power monitor written in Go Lang!
// github.com/context-labs/mactop
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"os/exec"
"os/signal"
"regexp"
"sort"
"strconv"
"strings"
"syscall"
"time"
ui "github.com/gizak/termui/v3"
w "github.com/gizak/termui/v3/widgets"
"github.com/shirou/gopsutil/mem"
)
type CPUMetrics struct {
EClusterActive, EClusterFreqMHz, PClusterActive, PClusterFreqMHz int
ECores, PCores []int
ANEW, CPUW, GPUW, PackageW float64
E0ClusterActive, E0ClusterFreqMHz, E1ClusterActive, E1ClusterFreqMHz, P0ClusterActive, P0ClusterFreqMHz, P1ClusterActive, P1ClusterFreqMHz, P2ClusterActive, P2ClusterFreqMHz, P3ClusterActive, P3ClusterFreqMHz int
}
type NetDiskMetrics struct {
OutPacketsPerSec, OutBytesPerSec, InPacketsPerSec, InBytesPerSec, ReadOpsPerSec, WriteOpsPerSec, ReadKBytesPerSec, WriteKBytesPerSec float64
}
type GPUMetrics struct {
FreqMHz int
Active float64
}
type ProcessMetrics struct {
ID int
Name string
CPUUsage float64
}
type MemoryMetrics struct {
Total, Used, Available, SwapTotal, SwapUsed uint64
}
type EventThrottler struct {
timer *time.Timer
gracePeriod time.Duration
C chan struct{}
}
func NewEventThrottler(gracePeriod time.Duration) *EventThrottler {
return &EventThrottler{
timer: nil,
gracePeriod: gracePeriod,
C: make(chan struct{}, 1),
}
}
func (e *EventThrottler) Notify() {
if e.timer != nil {
return
}
e.timer = time.AfterFunc(e.gracePeriod, func() {
e.timer = nil
select {
case e.C <- struct{}{}:
default:
}
})
}
var (
cpu1Gauge, cpu2Gauge, gpuGauge, aneGauge *w.Gauge
TotalPowerChart *w.BarChart
memoryGauge *w.Gauge
modelText, PowerChart, NetworkInfo, ProcessInfo, helpText *w.Paragraph
grid *ui.Grid
powerValues []float64
lastUpdateTime time.Time
stderrLogger = log.New(os.Stderr, "", 0)
currentGridLayout = "default"
showHelp = false
updateInterval = 1000
)
var (
dataRegex = regexp.MustCompile(`(?m)^\s*(\S.*?)\s+(\d+)\s+(\d+\.\d+)\s+\d+\.\d+\s+`)
outRegex = regexp.MustCompile(`out:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
inRegex = regexp.MustCompile(`in:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
readRegex = regexp.MustCompile(`read:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
writeRegex = regexp.MustCompile(`write:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
residencyRe = regexp.MustCompile(`(\w+-Cluster)\s+HW active residency:\s+(\d+\.\d+)%`)
frequencyRe = regexp.MustCompile(`(\w+-Cluster)\s+HW active frequency:\s+(\d+)\s+MHz`)
re = regexp.MustCompile(`GPU\s*(HW)?\s*active\s*(residency|frequency):\s+(\d+\.\d+)%?`)
freqRe = regexp.MustCompile(`(\d+)\s*MHz:\s*(\d+)%`)
)
func setupUI() {
appleSiliconModel := getSOCInfo()
modelText, helpText = w.NewParagraph(), w.NewParagraph()
modelText.Title = "Apple Silicon"
helpText.Title = "mactop help menu"
modelName, ok := appleSiliconModel["name"].(string)
if !ok {
modelName = "Unknown Model"
}
eCoreCount, ok := appleSiliconModel["e_core_count"].(int)
if !ok {
eCoreCount = 0 // Default or error value
}
pCoreCount, ok := appleSiliconModel["p_core_count"].(int)
if !ok {
pCoreCount = 0
}
gpuCoreCount, ok := appleSiliconModel["gpu_core_count"].(string) // Assuming this is stored as a string
if !ok {
gpuCoreCount = "?"
}
modelText.Text = fmt.Sprintf("%s\nTotal Cores: %d\nE-Cores: %d\nP-Cores: %d\nGPU Cores: %s",
modelName,
eCoreCount+pCoreCount,
eCoreCount,
pCoreCount,
gpuCoreCount,
)
helpText.Text = "mactop is open source monitoring tool for Apple Silicon authored by Carsen Klock in Go Lang!\n\nRepo: github.com/context-labs/mactop\n\nControls:\n- r: Refresh the UI data manually\n- l: Toggle the main display's layout\n- h or ?: Toggle this help menu\n- q or <C-c>: Quit the application\n\nStart Flags:\n--help, -h: Show this help menu\n--version, -v: Show the version of mactop\n--interval, -i: Set the powermetrics update interval in milliseconds. Default is 1000.\n--color, -c: Set the UI color. Default is none. Options are 'green', 'red', 'blue', 'cyan', 'magenta', 'yellow', and 'white'."
stderrLogger.Printf("Model: %s\nE-Core Count: %d\nP-Core Count: %d\nGPU Core Count: %s",
modelName,
eCoreCount,
pCoreCount,
gpuCoreCount,
)
gauges := []*w.Gauge{
w.NewGauge(), w.NewGauge(), w.NewGauge(), w.NewGauge(), w.NewGauge(),
}
titles := []string{"E-CPU Usage", "P-CPU Usage", "GPU Usage", "ANE", "Memory Usage"}
colors := []ui.Color{ui.ColorGreen, ui.ColorYellow, ui.ColorMagenta, ui.ColorBlue, ui.ColorCyan}
for i, gauge := range gauges {
gauge.Percent = 0
gauge.Title = titles[i]
gauge.BarColor = colors[i]
}
cpu1Gauge, cpu2Gauge, gpuGauge, aneGauge, memoryGauge = gauges[0], gauges[1], gauges[2], gauges[3], gauges[4]
PowerChart, NetworkInfo, ProcessInfo = w.NewParagraph(), w.NewParagraph(), w.NewParagraph()
PowerChart.Title, NetworkInfo.Title, ProcessInfo.Title = "Power Usage", "Network & Disk Info", "Process Info"
TotalPowerChart = w.NewBarChart()
TotalPowerChart.Title = "~ W Total Power"
TotalPowerChart.SetRect(50, 0, 75, 10)
TotalPowerChart.BarWidth = 5 // Adjust the bar width to fill the available space
TotalPowerChart.BarGap = 1 // Remove the gap between the bars
TotalPowerChart.PaddingBottom = 0
TotalPowerChart.PaddingTop = 1
TotalPowerChart.NumFormatter = func(num float64) string {
return ""
}
}
func setupGrid() {
grid = ui.NewGrid()
grid.Set(
ui.NewRow(1.0/2, // This row now takes half the height of the grid
ui.NewCol(1.0/2, ui.NewRow(1.0/2, cpu1Gauge), ui.NewCol(1.0, ui.NewRow(1.0, cpu2Gauge))),
ui.NewCol(1.0/2, ui.NewRow(1.0/2, gpuGauge), ui.NewCol(1.0, ui.NewRow(1.0, aneGauge))), // ui.NewCol(1.0/2, ui.NewRow(1.0, ProcessInfo)), // ProcessInfo spans this entire column
),
ui.NewRow(1.0/4,
ui.NewCol(1.0/6, modelText),
ui.NewCol(1.0/3, NetworkInfo),
ui.NewCol(1.0/4, PowerChart),
ui.NewCol(1.0/4, TotalPowerChart),
),
ui.NewRow(1.0/4,
ui.NewCol(1.0, memoryGauge),
),
)
}
func switchGridLayout() {
if currentGridLayout == "default" {
newGrid := ui.NewGrid()
newGrid.Set(
ui.NewRow(1.0/2, // This row now takes half the height of the grid
ui.NewCol(1.0/2, ui.NewRow(1.0, cpu1Gauge)), // ui.NewCol(1.0, ui.NewRow(1.0, cpu2Gauge))),
ui.NewCol(1.0/2, ui.NewRow(1.0, cpu2Gauge)), // ProcessInfo spans this entire column
),
ui.NewRow(1.0/4,
ui.NewCol(1.0/4, gpuGauge),
ui.NewCol(1.0/4, aneGauge),
ui.NewCol(1.0/4, PowerChart),
ui.NewCol(1.0/4, TotalPowerChart),
),
ui.NewRow(1.0/4,
ui.NewCol(3.0/6, memoryGauge),
ui.NewCol(1.0/6, modelText),
ui.NewCol(2.0/6, NetworkInfo),
),
)
termWidth, termHeight := ui.TerminalDimensions()
newGrid.SetRect(0, 0, termWidth, termHeight)
grid = newGrid
currentGridLayout = "alternative"
} else {
newGrid := ui.NewGrid()
newGrid.Set(
ui.NewRow(1.0/2,
ui.NewCol(1.0/2, ui.NewRow(1.0/2, cpu1Gauge), ui.NewCol(1.0, ui.NewRow(1.0, cpu2Gauge))),
ui.NewCol(1.0/2, ui.NewRow(1.0/2, gpuGauge), ui.NewCol(1.0, ui.NewRow(1.0, aneGauge))),
),
ui.NewRow(1.0/4,
ui.NewCol(1.0/4, modelText),
ui.NewCol(1.0/4, NetworkInfo),
ui.NewCol(1.0/4, PowerChart),
ui.NewCol(1.0/4, TotalPowerChart),
),
ui.NewRow(1.0/4,
ui.NewCol(1.0, memoryGauge),
),
)
termWidth, termHeight := ui.TerminalDimensions()
newGrid.SetRect(0, 0, termWidth, termHeight)
grid = newGrid
currentGridLayout = "default"
}
}
func toggleHelpMenu() {
showHelp = !showHelp
if showHelp {
newGrid := ui.NewGrid()
newGrid.Set(
ui.NewRow(1.0,
ui.NewCol(1.0, helpText),
),
)
termWidth, termHeight := ui.TerminalDimensions()
helpTextGridWidth := termWidth
helpTextGridHeight := termHeight
x := (termWidth - helpTextGridWidth) / 2
y := (termHeight - helpTextGridHeight) / 2
newGrid.SetRect(x, y, x+helpTextGridWidth, y+helpTextGridHeight)
grid = newGrid
} else {
currentGridLayout = map[bool]string{
true: "alternative",
false: "default",
}[currentGridLayout == "default"]
switchGridLayout()
}
ui.Clear()
ui.Render(grid)
}
func StderrToLogfile(logfile *os.File) {
syscall.Dup2(int(logfile.Fd()), 2)
}
func main() {
var (
colorName string
interval int
err error
setColor, setInterval bool
)
version := "v0.1.9"
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
case "--help", "-h":
fmt.Print("Usage: mactop [--help] [--version] [--interval] [--color]\n--help: Show this help message\n--version: Show the version of mactop\n--interval: Set the powermetrics update interval in milliseconds. Default is 1000.\n--color: Set the UI color. Default is none. Options are 'green', 'red', 'blue', 'cyan', 'magenta', 'yellow', and 'white'. (-c green)\n\nYou must use sudo to run mactop, as powermetrics requires root privileges.\n\nFor more information, see https://github.com/context-labs/mactop written by Carsen Klock.\n")
os.Exit(0)
case "--version", "-v":
fmt.Println("mactop version:", version)
os.Exit(0)
case "--test", "-t":
if i+1 < len(os.Args) {
testInput := os.Args[i+1]
fmt.Printf("Test input received: %s\n", testInput)
os.Exit(0)
}
case "--color", "-c":
if i+1 < len(os.Args) {
colorName = strings.ToLower(os.Args[i+1])
setColor = true
i++
} else {
fmt.Println("Error: --color flag requires a color value")
os.Exit(1)
}
case "--interval", "-i":
if i+1 < len(os.Args) {
interval, err = strconv.Atoi(os.Args[i+1])
if err != nil {
fmt.Println("Invalid interval:", err)
os.Exit(1)
}
setInterval = true
i++
} else {
fmt.Println("Error: --interval flag requires an interval value")
os.Exit(1)
}
}
}
if os.Geteuid() != 0 {
fmt.Println("Welcome to mactop! Please try again and run mactop with sudo privileges!")
fmt.Println("Usage: sudo mactop")
os.Exit(1)
}
logfile, err := setupLogfile()
if err != nil {
stderrLogger.Fatalf("failed to setup log file: %v", err)
}
defer logfile.Close()
if err := ui.Init(); err != nil {
stderrLogger.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
StderrToLogfile(logfile)
if setColor {
var color ui.Color
switch colorName {
case "green":
color = ui.ColorGreen
case "red":
color = ui.ColorRed
case "blue":
color = ui.ColorBlue
case "cyan":
color = ui.ColorCyan
case "magenta":
color = ui.ColorMagenta
case "yellow":
color = ui.ColorYellow
case "white":
color = ui.ColorWhite
default:
stderrLogger.Printf("Unsupported color: %s. Using default color.\n", colorName)
color = ui.ColorWhite
}
ui.Theme.Block.Title.Fg = color
ui.Theme.Block.Border.Fg = color
ui.Theme.Paragraph.Text.Fg = color
ui.Theme.BarChart.Bars = []ui.Color{color}
ui.Theme.Gauge.Label.Fg = color
ui.Theme.Gauge.Bar = color
setupUI()
cpu1Gauge.BarColor = color
cpu2Gauge.BarColor = color
aneGauge.BarColor = color
gpuGauge.BarColor = color
memoryGauge.BarColor = color
} else {
setupUI()
}
if setInterval {
updateInterval = interval
}
setupGrid()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
ui.Render(grid)
cpuMetricsChan := make(chan CPUMetrics)
gpuMetricsChan := make(chan GPUMetrics)
netdiskMetricsChan := make(chan NetDiskMetrics)
processMetricsChan := make(chan []ProcessMetrics)
done := make(chan struct{})
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
appleSiliconModel := getSOCInfo()
go collectMetrics(done, cpuMetricsChan, gpuMetricsChan, netdiskMetricsChan, processMetricsChan, appleSiliconModel["name"].(string))
lastUpdateTime = time.Now()
needRender := NewEventThrottler(time.Duration(updateInterval/2) * time.Millisecond)
go func() {
for {
select {
case cpuMetrics := <-cpuMetricsChan:
updateCPUUI(cpuMetrics)
updateTotalPowerChart(cpuMetrics.PackageW)
needRender.Notify()
case gpuMetrics := <-gpuMetricsChan:
updateGPUUI(gpuMetrics)
needRender.Notify()
case netdiskMetrics := <-netdiskMetricsChan:
updateNetDiskUI(netdiskMetrics)
needRender.Notify()
case processMetrics := <-processMetricsChan:
updateProcessUI(processMetrics)
needRender.Notify()
case <-needRender.C:
ui.Render(grid)
case <-quit:
close(done)
ui.Close()
os.Exit(0)
return
}
}
}()
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>": // "q" or Ctrl+C to quit
close(done)
ui.Close()
os.Exit(0)
return
case "<Resize>":
payload := e.Payload.(ui.Resize)
grid.SetRect(0, 0, payload.Width, payload.Height)
ui.Render(grid)
case "r":
// refresh ui data
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
ui.Clear()
ui.Render(grid)
case "l":
// Set the new grid's dimensions to match the terminal size
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
ui.Clear()
switchGridLayout()
ui.Render(grid)
case "h", "?": // "h" or "?" to open help menu
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
ui.Clear()
toggleHelpMenu()
ui.Render(grid)
}
case <-done:
ui.Close()
os.Exit(0)
return
}
}
}
func setupLogfile() (*os.File, error) {
if err := os.MkdirAll("/var/log", 0755); err != nil {
return nil, fmt.Errorf("failed to make the log directory: %v", err)
}
logfile, err := os.OpenFile("/var/log/mactop.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %v", err)
}
log.SetFlags(log.Ltime | log.Lshortfile)
log.SetOutput(logfile)
return logfile, nil
}
func collectMetrics(done chan struct{}, cpumetricsChan chan CPUMetrics, gpumetricsChan chan GPUMetrics, netdiskMetricsChan chan NetDiskMetrics, processMetricsChan chan []ProcessMetrics, modelName string) {
var cpuMetrics CPUMetrics
var gpuMetrics GPUMetrics
var netdiskMetrics NetDiskMetrics
var processMetrics []ProcessMetrics
cmd := exec.Command("powermetrics", "--samplers", "cpu_power,gpu_power,thermal,network,disk", "--show-process-gpu", "--show-process-energy", "--show-initial-usage", "--show-process-netstats", "-i", strconv.Itoa(updateInterval))
stdout, err := cmd.StdoutPipe()
if err != nil {
stderrLogger.Fatalf("failed to get stdout pipe: %v", err)
}
if err := cmd.Start(); err != nil {
stderrLogger.Fatalf("failed to start command: %v", err)
}
scanner := bufio.NewScanner(stdout)
go func() {
for {
select {
case <-done: // Check if we need to exit
cmd.Process.Kill() // Ensure subprocess is terminated
os.Exit(0)
return
default:
if scanner.Scan() {
line := scanner.Text()
cpuMetrics = parseCPUMetrics(line, cpuMetrics, modelName)
gpuMetrics = parseGPUMetrics(line, gpuMetrics)
netdiskMetrics = parseActivityMetrics(line, netdiskMetrics)
processMetrics = parseProcessMetrics(line, processMetrics)
cpumetricsChan <- cpuMetrics
gpumetricsChan <- gpuMetrics
netdiskMetricsChan <- netdiskMetrics
processMetricsChan <- processMetrics
} else {
if err := scanner.Err(); err != nil {
stderrLogger.Printf("error during scan: %v", err)
}
return // Exit loop if Scan() returns false
}
}
}
}()
if err := cmd.Wait(); err != nil {
stderrLogger.Fatalf("command failed: %v", err)
}
}
func updateTotalPowerChart(newPowerValue float64) {
currentTime := time.Now()
powerValues = append(powerValues, newPowerValue)
if currentTime.Sub(lastUpdateTime) >= 2*time.Second {
var sum float64
for _, value := range powerValues {
sum += value
}
averagePower := sum / float64(len(powerValues))
averagePower = math.Round(averagePower)
TotalPowerChart.Data = append([]float64{averagePower}, TotalPowerChart.Data...)
if len(TotalPowerChart.Data) > 25 {
TotalPowerChart.Data = TotalPowerChart.Data[:25]
}
powerValues = nil
lastUpdateTime = currentTime
}
}
func updateCPUUI(cpuMetrics CPUMetrics) {
cpu1Gauge.Title = fmt.Sprintf("E-CPU Usage: %d%% @ %d MHz", cpuMetrics.EClusterActive, cpuMetrics.EClusterFreqMHz)
cpu1Gauge.Percent = cpuMetrics.EClusterActive
cpu2Gauge.Title = fmt.Sprintf("P-CPU Usage: %d%% @ %d MHz", cpuMetrics.PClusterActive, cpuMetrics.PClusterFreqMHz)
cpu2Gauge.Percent = cpuMetrics.PClusterActive
aneUtil := int(cpuMetrics.ANEW * 100 / 8.0)
aneGauge.Title = fmt.Sprintf("ANE Usage: %d%% @ %.1f W", aneUtil, cpuMetrics.ANEW)
aneGauge.Percent = aneUtil
TotalPowerChart.Title = fmt.Sprintf("%.1f W Total Power", cpuMetrics.PackageW)
PowerChart.Title = fmt.Sprintf("%.1f W CPU - %.1f W GPU", cpuMetrics.CPUW, cpuMetrics.GPUW)
PowerChart.Text = fmt.Sprintf("CPU Power: %.1f W\nGPU Power: %.1f W\nANE Power: %.1f W\nTotal Power: %.1f W", cpuMetrics.CPUW, cpuMetrics.GPUW, cpuMetrics.ANEW, cpuMetrics.PackageW)
memoryMetrics := getMemoryMetrics()
memoryGauge.Title = fmt.Sprintf("Memory Usage: %.2f GB / %.2f GB (Swap: %.2f/%.2f GB)", float64(memoryMetrics.Used)/1024/1024/1024, float64(memoryMetrics.Total)/1024/1024/1024, float64(memoryMetrics.SwapUsed)/1024/1024/1024, float64(memoryMetrics.SwapTotal)/1024/1024/1024)
memoryGauge.Percent = int((float64(memoryMetrics.Used) / float64(memoryMetrics.Total)) * 100)
}
func updateGPUUI(gpuMetrics GPUMetrics) {
gpuGauge.Title = fmt.Sprintf("GPU Usage: %d%% @ %d MHz", int(gpuMetrics.Active), gpuMetrics.FreqMHz)
gpuGauge.Percent = int(gpuMetrics.Active)
}
func updateNetDiskUI(netdiskMetrics NetDiskMetrics) {
NetworkInfo.Text = fmt.Sprintf("Out: %.1f packets/s, %.1f bytes/s\nIn: %.1f packets/s, %.1f bytes/s\nRead: %.1f ops/s, %.1f KBytes/s\nWrite: %.1f ops/s, %.1f KBytes/s", netdiskMetrics.OutPacketsPerSec, netdiskMetrics.OutBytesPerSec, netdiskMetrics.InPacketsPerSec, netdiskMetrics.InBytesPerSec, netdiskMetrics.ReadOpsPerSec, netdiskMetrics.ReadKBytesPerSec, netdiskMetrics.WriteOpsPerSec, netdiskMetrics.WriteKBytesPerSec)
}
func updateProcessUI(processMetrics []ProcessMetrics) {
ProcessInfo.Text = ""
sort.Slice(processMetrics, func(i, j int) bool {
return processMetrics[i].CPUUsage > processMetrics[j].CPUUsage
})
maxEntries := 15
if len(processMetrics) > maxEntries {
processMetrics = processMetrics[:maxEntries]
}
for _, pm := range processMetrics {
ProcessInfo.Text += fmt.Sprintf("%d - %s: %.2f ms/s\n", pm.ID, pm.Name, pm.CPUUsage)
}
}
func parseProcessMetrics(powermetricsOutput string, processMetrics []ProcessMetrics) []ProcessMetrics {
lines := strings.Split(powermetricsOutput, "\n")
seen := make(map[int]bool) // Map to track seen process IDs
for _, line := range lines {
matches := dataRegex.FindStringSubmatch(line)
if len(matches) > 3 {
processName := matches[1]
if processName == "mactop" || processName == "main" || processName == "powermetrics" {
continue // Skip this process
}
id, _ := strconv.Atoi(matches[2])
if !seen[id] {
seen[id] = true
cpuMsPerS, _ := strconv.ParseFloat(matches[3], 64)
processMetrics = append(processMetrics, ProcessMetrics{
Name: matches[1],
ID: id,
CPUUsage: cpuMsPerS,
})
}
}
}
sort.Slice(processMetrics, func(i, j int) bool {
return processMetrics[i].CPUUsage > processMetrics[j].CPUUsage
})
return processMetrics
}
func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetrics) NetDiskMetrics {
outMatches := outRegex.FindStringSubmatch(powermetricsOutput)
inMatches := inRegex.FindStringSubmatch(powermetricsOutput)
if len(outMatches) == 3 {
netdiskMetrics.OutPacketsPerSec, _ = strconv.ParseFloat(outMatches[1], 64)
netdiskMetrics.OutBytesPerSec, _ = strconv.ParseFloat(outMatches[2], 64)
}
if len(inMatches) == 3 {
netdiskMetrics.InPacketsPerSec, _ = strconv.ParseFloat(inMatches[1], 64)
netdiskMetrics.InBytesPerSec, _ = strconv.ParseFloat(inMatches[2], 64)
}
readMatches := readRegex.FindStringSubmatch(powermetricsOutput)
writeMatches := writeRegex.FindStringSubmatch(powermetricsOutput)
if len(readMatches) == 3 {
netdiskMetrics.ReadOpsPerSec, _ = strconv.ParseFloat(readMatches[1], 64)
netdiskMetrics.ReadKBytesPerSec, _ = strconv.ParseFloat(readMatches[2], 64)
}
if len(writeMatches) == 3 {
netdiskMetrics.WriteOpsPerSec, _ = strconv.ParseFloat(writeMatches[1], 64)
netdiskMetrics.WriteKBytesPerSec, _ = strconv.ParseFloat(writeMatches[2], 64)
}
return netdiskMetrics
}
func parseCPUMetrics(powermetricsOutput string, cpuMetrics CPUMetrics, modelName string) CPUMetrics {
lines := strings.Split(powermetricsOutput, "\n")
eCores := []int{}
pCores := []int{}
var eClusterActiveSum, pClusterActiveSum, eClusterFreqSum, pClusterFreqSum float64
var eClusterCount, pClusterCount, eClusterActiveTotal, pClusterActiveTotal, eClusterFreqTotal, pClusterFreqTotal int
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)
for _, line := range lines {
maxCores := 15 // 16 Cores for M3 Max (4+12)
if modelName == "Apple M2 Max" {
maxCores = 11 // 12 Cores M2 Max (4+8)
}
for i := 0; i <= maxCores; i++ {
re := regexp.MustCompile(`CPU ` + strconv.Itoa(i) + ` active residency:\s+(\d+\.\d+)%`)
matches := re.FindStringSubmatch(powermetricsOutput)
if len(matches) > 1 {
activeResidency, _ := strconv.ParseFloat(matches[1], 64)
if i <= 3 {
eClusterActiveSum += activeResidency
eClusterCount++
} else {
pClusterActiveSum += activeResidency
pClusterCount++
}
}
}
for i := 0; i <= maxCores; i++ {
fre := regexp.MustCompile(`^CPU\s+` + strconv.Itoa(i) + `\s+frequency:\s+(\d+)\s+MHz$`)
matches := fre.FindStringSubmatch(powermetricsOutput)
if len(matches) > 1 {
activeFreq, _ := strconv.ParseFloat(matches[1], 64)
if i <= 3 {
eClusterFreqSum += activeFreq
eClusterCount++
} else {
pClusterFreqSum += activeFreq
pClusterCount++
}
}
}
if eClusterCount > 0 && eClusterActiveSum > 0.0 && eClusterActiveSum < 100.0 && eClusterActiveSum != 0 {
cpuMetrics.EClusterActive = int(eClusterActiveSum / float64(eClusterCount))
}
if pClusterCount > 0 && pClusterActiveSum > 0.0 && pClusterActiveSum < 100.0 && pClusterActiveSum != 0 {
cpuMetrics.PClusterActive = int(pClusterActiveSum / float64(pClusterCount))
}
if eClusterCount > 0 && eClusterFreqSum > 0.0 && eClusterFreqSum != 0 {
cpuMetrics.EClusterFreqMHz = int(eClusterFreqSum / float64(eClusterCount))
}
if pClusterCount > 0 && pClusterFreqSum > 0.0 && pClusterFreqSum != 0 {
cpuMetrics.PClusterFreqMHz = int(pClusterFreqSum / float64(pClusterCount))
}
if strings.Contains(line, "CPU ") && strings.Contains(line, "frequency") {
fields := strings.Fields(line)
if len(fields) >= 3 {
core, _ := strconv.Atoi(strings.TrimPrefix(fields[1], "CPU"))
if strings.Contains(line, "E-Cluster") {
eCores = append(eCores, core)
} else if strings.Contains(line, "P-Cluster") {
pCores = append(pCores, core)
}
}
} else if strings.Contains(line, "ANE Power") {
fields := strings.Fields(line)
if len(fields) >= 3 {
cpuMetrics.ANEW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[2], "mW"), 64)
cpuMetrics.ANEW /= 1000 // Convert mW to W
}
} else if strings.Contains(line, "CPU Power") {
fields := strings.Fields(line)
if len(fields) >= 3 {
cpuMetrics.CPUW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[2], "mW"), 64)
cpuMetrics.CPUW /= 1000 // Convert mW to W
}
} else if strings.Contains(line, "GPU Power") {
fields := strings.Fields(line)
if len(fields) >= 3 {
cpuMetrics.GPUW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[2], "mW"), 64)
cpuMetrics.GPUW /= 1000 // Convert mW to W
}
} else if strings.Contains(line, "Combined Power (CPU + GPU + ANE)") {
fields := strings.Fields(line)
if len(fields) >= 8 {
cpuMetrics.PackageW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[7], "mW"), 64)
cpuMetrics.PackageW /= 1000 // Convert mW to W
}
}
}
cpuMetrics.ECores = eCores
cpuMetrics.PCores = pCores
} else {
for _, line := range lines {
residencyMatches := residencyRe.FindStringSubmatch(line)
frequencyMatches := frequencyRe.FindStringSubmatch(line)
if residencyMatches != nil {
cluster := residencyMatches[1]
percent, _ := strconv.ParseFloat(residencyMatches[2], 64)
switch cluster {
case "E0-Cluster":
cpuMetrics.E0ClusterActive = int(percent)
case "E1-Cluster":
cpuMetrics.E1ClusterActive = int(percent)
case "P0-Cluster":
cpuMetrics.P0ClusterActive = int(percent)
case "P1-Cluster":
cpuMetrics.P1ClusterActive = int(percent)
case "P2-Cluster":
cpuMetrics.P2ClusterActive = int(percent)
case "P3-Cluster":
cpuMetrics.P3ClusterActive = int(percent)
}
if strings.HasPrefix(cluster, "E") {
eClusterActiveTotal += int(percent)
eClusterCount++
} else if strings.HasPrefix(cluster, "P") {
pClusterActiveTotal += int(percent)
pClusterCount++
cpuMetrics.PClusterActive = pClusterActiveTotal / pClusterCount
}
}
if frequencyMatches != nil {
cluster := frequencyMatches[1]
freqMHz, _ := strconv.Atoi(frequencyMatches[2])
switch cluster {
case "E0-Cluster":
cpuMetrics.E0ClusterFreqMHz = freqMHz
case "E1-Cluster":
cpuMetrics.E1ClusterFreqMHz = freqMHz
case "P0-Cluster":
cpuMetrics.P0ClusterFreqMHz = freqMHz
case "P1-Cluster":
cpuMetrics.P1ClusterFreqMHz = freqMHz
case "P2-Cluster":
cpuMetrics.P2ClusterFreqMHz = freqMHz
case "P3-Cluster":
cpuMetrics.P3ClusterFreqMHz = freqMHz
}
if strings.HasPrefix(cluster, "E") {
eClusterFreqTotal += freqMHz
cpuMetrics.EClusterFreqMHz = eClusterFreqTotal
} else if strings.HasPrefix(cluster, "P") {
pClusterFreqTotal += freqMHz
cpuMetrics.PClusterFreqMHz = pClusterFreqTotal
}
}
if strings.Contains(line, "CPU ") && strings.Contains(line, "frequency") {
fields := strings.Fields(line)
if len(fields) >= 3 {
core, _ := strconv.Atoi(strings.TrimPrefix(fields[1], "CPU"))
if strings.Contains(line, "E-Cluster") {
eCores = append(eCores, core)
} else if strings.Contains(line, "P-Cluster") {
pCores = append(pCores, core)
}
}
} else if strings.Contains(line, "ANE Power") {
fields := strings.Fields(line)
if len(fields) >= 3 {
cpuMetrics.ANEW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[2], "mW"), 64)
cpuMetrics.ANEW /= 1000 // Convert mW to W
}
} else if strings.Contains(line, "CPU Power") {
fields := strings.Fields(line)
if len(fields) >= 3 {
cpuMetrics.CPUW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[2], "mW"), 64)
cpuMetrics.CPUW /= 1000 // Convert mW to W
}
} else if strings.Contains(line, "GPU Power") {
fields := strings.Fields(line)
if len(fields) >= 3 {
cpuMetrics.GPUW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[2], "mW"), 64)
cpuMetrics.GPUW /= 1000 // Convert mW to W
}
} else if strings.Contains(line, "Combined Power (CPU + GPU + ANE)") {
fields := strings.Fields(line)
if len(fields) >= 8 {
cpuMetrics.PackageW, _ = strconv.ParseFloat(strings.TrimSuffix(fields[7], "mW"), 64)
cpuMetrics.PackageW /= 1000 // Convert mW to W
}
}
}
cpuMetrics.ECores = eCores
cpuMetrics.PCores = pCores
multra, mmax := false, false
if cpuMetrics.E1ClusterActive != 0 { // M1 Ultra
cpuMetrics.EClusterActive = (cpuMetrics.E0ClusterActive + cpuMetrics.E1ClusterActive) / 2
cpuMetrics.EClusterFreqMHz = max(cpuMetrics.E0ClusterFreqMHz, cpuMetrics.E1ClusterFreqMHz)
multra = true
}
if cpuMetrics.P3ClusterActive != 0 { // M1 Ultra
cpuMetrics.PClusterActive = (cpuMetrics.P0ClusterActive + cpuMetrics.P1ClusterActive + cpuMetrics.P2ClusterActive + cpuMetrics.P3ClusterActive) / 4
cpuMetrics.PClusterFreqMHz = max(cpuMetrics.P0ClusterFreqMHz, cpuMetrics.P1ClusterFreqMHz, cpuMetrics.P2ClusterFreqMHz, cpuMetrics.P3ClusterFreqMHz)
multra = true
} else if cpuMetrics.P1ClusterActive != 0 && !multra { // M1/M2/M3 Max/Pro
cpuMetrics.PClusterActive = (cpuMetrics.P0ClusterActive + cpuMetrics.P1ClusterActive) / 2
cpuMetrics.PClusterFreqMHz = max(cpuMetrics.P0ClusterFreqMHz, cpuMetrics.P1ClusterFreqMHz)
mmax = true
} else if !multra && !mmax { // M1
cpuMetrics.PClusterActive = cpuMetrics.PClusterActive + cpuMetrics.P0ClusterActive
}
if eClusterCount > 0 && !multra && !mmax { // Calculate average active residency and frequency for E and P clusters
cpuMetrics.EClusterActive = eClusterActiveTotal / eClusterCount
}
}
return cpuMetrics
}
func max(nums ...int) int {
maxVal := nums[0]
for _, num := range nums[1:] {
if num > maxVal {
maxVal = num
}
}
return maxVal
}
func parseGPUMetrics(powermetricsOutput string, gpuMetrics GPUMetrics) GPUMetrics {
lines := strings.Split(powermetricsOutput, "\n")
for _, line := range lines {
if strings.Contains(line, "GPU active") || strings.Contains(line, "GPU HW active") {
matches := re.FindStringSubmatch(line)
if len(matches) > 3 {
if strings.Contains(matches[2], "residency") {
gpuMetrics.Active, _ = strconv.ParseFloat(matches[3], 64)
} else if strings.Contains(matches[2], "frequency") {
gpuMetrics.FreqMHz, _ = strconv.Atoi(strings.TrimSuffix(matches[3], "MHz"))
}
}
freqMatches := freqRe.FindAllStringSubmatch(line, -1)
for _, match := range freqMatches {
if len(match) == 3 {
freq, _ := strconv.Atoi(match[1])
residency, _ := strconv.ParseFloat(match[2], 64)
if residency > 0 {
gpuMetrics.FreqMHz = freq
break
}
}
}
}
}
return gpuMetrics
}
func getSOCInfo() map[string]interface{} {
cpuInfoDict := getCPUInfo()
coreCountsDict := getCoreCounts()
var eCoreCounts, pCoreCounts int
if val, ok := coreCountsDict["hw.perflevel1.logicalcpu"]; ok {
eCoreCounts = val
}
if val, ok := coreCountsDict["hw.perflevel0.logicalcpu"]; ok {
pCoreCounts = val
}
socInfo := map[string]interface{}{
"name": cpuInfoDict["machdep.cpu.brand_string"],
"core_count": cpuInfoDict["machdep.cpu.core_count"],
"cpu_max_power": nil,
"gpu_max_power": nil,
"cpu_max_bw": nil,
"gpu_max_bw": nil,
"e_core_count": eCoreCounts,
"p_core_count": pCoreCounts,
"gpu_core_count": getGPUCores(),
}
return socInfo
}
func getMemoryMetrics() MemoryMetrics {
v, _ := mem.VirtualMemory()
s, _ := mem.SwapMemory()
totalMemory := v.Total
usedMemory := v.Used
availableMemory := v.Available
swapTotal := s.Total
swapUsed := s.Used
return MemoryMetrics{
Total: totalMemory,
Used: usedMemory,
Available: availableMemory,
SwapTotal: swapTotal,
SwapUsed: swapUsed,
}
}
func getCPUInfo() map[string]string {
out, err := exec.Command("sysctl", "machdep.cpu").Output()
if err != nil {
stderrLogger.Fatalf("failed to execute getCPUInfo() sysctl command: %v", err)
}
cpuInfo := string(out)
cpuInfoLines := strings.Split(cpuInfo, "\n")
dataFields := []string{"machdep.cpu.brand_string", "machdep.cpu.core_count"}
cpuInfoDict := make(map[string]string)
for _, line := range cpuInfoLines {
for _, field := range dataFields {
if strings.Contains(line, field) {
value := strings.TrimSpace(strings.Split(line, ":")[1])
cpuInfoDict[field] = value
}
}
}
return cpuInfoDict
}
func getCoreCounts() map[string]int {
out, err := exec.Command("sysctl", "hw.perflevel0.logicalcpu", "hw.perflevel1.logicalcpu").Output()
if err != nil {
stderrLogger.Fatalf("failed to execute getCoreCounts() sysctl command: %v", err)
}
coresInfo := string(out)
coresInfoLines := strings.Split(coresInfo, "\n")
dataFields := []string{"hw.perflevel0.logicalcpu", "hw.perflevel1.logicalcpu"}
coresInfoDict := make(map[string]int)
for _, line := range coresInfoLines {
for _, field := range dataFields {
if strings.Contains(line, field) {
value, _ := strconv.Atoi(strings.TrimSpace(strings.Split(line, ":")[1]))
coresInfoDict[field] = value
}
}
}
return coresInfoDict
}
func getGPUCores() string {
cmd, err := exec.Command("system_profiler", "-detailLevel", "basic", "SPDisplaysDataType").Output()
if err != nil {
stderrLogger.Fatalf("failed to execute system_profiler command: %v", err)
}
output := string(cmd)
stderrLogger.Printf("Output: %s\n", output)
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.Contains(line, "Total Number of Cores") {
parts := strings.Split(line, ": ")
if len(parts) > 1 {
cores := strings.TrimSpace(parts[1])
return cores
}
break
}
}
return "?"
}