@@ -48,6 +48,35 @@ type MemoryMetrics struct {
48
48
Total , Used , Available , SwapTotal , SwapUsed uint64
49
49
}
50
50
51
+ type EventThrottler struct {
52
+ timer * time.Timer
53
+ gracePeriod time.Duration
54
+
55
+ C chan struct {}
56
+ }
57
+
58
+ func NewEventThrottler (gracePeriod time.Duration ) * EventThrottler {
59
+ return & EventThrottler {
60
+ timer : nil ,
61
+ gracePeriod : gracePeriod ,
62
+ C : make (chan struct {}, 1 ),
63
+ }
64
+ }
65
+
66
+ func (e * EventThrottler ) Notify () {
67
+ if e .timer != nil {
68
+ return
69
+ }
70
+
71
+ e .timer = time .AfterFunc (e .gracePeriod , func () {
72
+ e .timer = nil
73
+ select {
74
+ case e .C <- struct {}{}:
75
+ default :
76
+ }
77
+ })
78
+ }
79
+
51
80
var (
52
81
cpu1Gauge , cpu2Gauge , gpuGauge , aneGauge * w.Gauge
53
82
TotalPowerChart * w.BarChart
@@ -173,7 +202,6 @@ func setupGrid() {
173
202
174
203
func switchGridLayout () {
175
204
if currentGridLayout == "default" {
176
- ui .Clear ()
177
205
newGrid := ui .NewGrid ()
178
206
newGrid .Set (
179
207
ui .NewRow (1.0 / 2 , // This row now takes half the height of the grid
@@ -196,9 +224,7 @@ func switchGridLayout() {
196
224
newGrid .SetRect (0 , 0 , termWidth , termHeight )
197
225
grid = newGrid
198
226
currentGridLayout = "alternative"
199
- ui .Render (grid )
200
227
} else {
201
- ui .Clear ()
202
228
newGrid := ui .NewGrid ()
203
229
newGrid .Set (
204
230
ui .NewRow (1.0 / 2 ,
@@ -219,7 +245,6 @@ func switchGridLayout() {
219
245
newGrid .SetRect (0 , 0 , termWidth , termHeight )
220
246
grid = newGrid
221
247
currentGridLayout = "default"
222
- ui .Render (grid )
223
248
}
224
249
}
225
250
@@ -234,7 +259,7 @@ func main() {
234
259
err error
235
260
setColor , setInterval bool
236
261
)
237
- version := "v0.1.7 "
262
+ version := "v0.1.8 "
238
263
for i := 1 ; i < len (os .Args ); i ++ {
239
264
switch os .Args [i ] {
240
265
case "--help" , "-h" :
@@ -352,21 +377,24 @@ func main() {
352
377
appleSiliconModel := getSOCInfo ()
353
378
go collectMetrics (done , cpuMetricsChan , gpuMetricsChan , netdiskMetricsChan , processMetricsChan , appleSiliconModel ["name" ].(string ))
354
379
lastUpdateTime = time .Now ()
380
+ needRender := NewEventThrottler (time .Duration (updateInterval / 2 ) * time .Millisecond )
355
381
go func () {
356
382
for {
357
383
select {
358
384
case cpuMetrics := <- cpuMetricsChan :
359
385
updateCPUUI (cpuMetrics )
360
386
updateTotalPowerChart (cpuMetrics .PackageW )
361
- ui . Render ( grid )
387
+ needRender . Notify ( )
362
388
case gpuMetrics := <- gpuMetricsChan :
363
389
updateGPUUI (gpuMetrics )
364
- ui . Render ( grid )
390
+ needRender . Notify ( )
365
391
case netdiskMetrics := <- netdiskMetricsChan :
366
392
updateNetDiskUI (netdiskMetrics )
367
- ui . Render ( grid )
393
+ needRender . Notify ( )
368
394
case processMetrics := <- processMetricsChan :
369
395
updateProcessUI (processMetrics )
396
+ needRender .Notify ()
397
+ case <- needRender .C :
370
398
ui .Render (grid )
371
399
case <- quit :
372
400
close (done )
@@ -489,7 +517,6 @@ func updateTotalPowerChart(newPowerValue float64) {
489
517
}
490
518
powerValues = nil
491
519
lastUpdateTime = currentTime
492
- ui .Render (TotalPowerChart )
493
520
}
494
521
}
495
522
@@ -507,8 +534,6 @@ func updateCPUUI(cpuMetrics CPUMetrics) {
507
534
memoryMetrics := getMemoryMetrics ()
508
535
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 )
509
536
memoryGauge .Percent = int ((float64 (memoryMetrics .Used ) / float64 (memoryMetrics .Total )) * 100 )
510
- ui .Render (grid )
511
- ui .Render (cpu1Gauge , cpu2Gauge , gpuGauge , aneGauge , memoryGauge , modelText , PowerChart )
512
537
}
513
538
514
539
func updateGPUUI (gpuMetrics GPUMetrics ) {
@@ -532,7 +557,6 @@ func updateProcessUI(processMetrics []ProcessMetrics) {
532
557
for _ , pm := range processMetrics {
533
558
ProcessInfo .Text += fmt .Sprintf ("%d - %s: %.2f ms/s\n " , pm .ID , pm .Name , pm .CPUUsage )
534
559
}
535
- ui .Render (ProcessInfo )
536
560
}
537
561
538
562
func parseProcessMetrics (powermetricsOutput string , processMetrics []ProcessMetrics ) []ProcessMetrics {
@@ -870,48 +894,6 @@ func getSOCInfo() map[string]interface{} {
870
894
"gpu_core_count" : getGPUCores (),
871
895
}
872
896
873
- switch socInfo ["name" ] {
874
- case "Apple M1 Max" :
875
- socInfo ["cpu_max_power" ] = 30
876
- socInfo ["gpu_max_power" ] = 60
877
- case "Apple M1 Pro" :
878
- socInfo ["cpu_max_power" ] = 30
879
- socInfo ["gpu_max_power" ] = 30
880
- case "Apple M1" :
881
- socInfo ["cpu_max_power" ] = 20
882
- socInfo ["gpu_max_power" ] = 20
883
- case "Apple M1 Ultra" :
884
- socInfo ["cpu_max_power" ] = 60
885
- socInfo ["gpu_max_power" ] = 120
886
- case "Apple M2" :
887
- socInfo ["cpu_max_power" ] = 25
888
- socInfo ["gpu_max_power" ] = 15
889
- default :
890
- socInfo ["cpu_max_power" ] = 20
891
- socInfo ["gpu_max_power" ] = 20
892
- }
893
-
894
- switch socInfo ["name" ] {
895
- case "Apple M1 Max" :
896
- socInfo ["cpu_max_bw" ] = 250
897
- socInfo ["gpu_max_bw" ] = 400
898
- case "Apple M1 Pro" :
899
- socInfo ["cpu_max_bw" ] = 200
900
- socInfo ["gpu_max_bw" ] = 200
901
- case "Apple M1" :
902
- socInfo ["cpu_max_bw" ] = 70
903
- socInfo ["gpu_max_bw" ] = 70
904
- case "Apple M1 Ultra" :
905
- socInfo ["cpu_max_bw" ] = 500
906
- socInfo ["gpu_max_bw" ] = 800
907
- case "Apple M2" :
908
- socInfo ["cpu_max_bw" ] = 100
909
- socInfo ["gpu_max_bw" ] = 100
910
- default :
911
- socInfo ["cpu_max_bw" ] = 70
912
- socInfo ["gpu_max_bw" ] = 70
913
- }
914
-
915
897
return socInfo
916
898
}
917
899
0 commit comments