Skip to content

Commit a129c0f

Browse files
Use async macro
1 parent bda08c9 commit a129c0f

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

ext/TerminalSystemMonitorMetalExt/TerminalSystemMonitorMetalExt.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module TerminalSystemMonitorMetalExt
22

3-
using Metal
43
using MLDataDevices: MetalDevice
54
using UnicodePlots: barplot
65
import TerminalSystemMonitor
6+
using Metal: MTLDevice
77
using MacOSIOReport: Sampler, get_metrics
88
using TerminalSystemMonitor: extract_number_and_unit
99

@@ -15,7 +15,7 @@ end
1515

1616
function TerminalSystemMonitor.plot_cpu_utilization_rates(::Type{MetalDevice})
1717
sampler = Sampler()
18-
msec = UInt(1000)
18+
msec = UInt(500)
1919
m = get_metrics(sampler, msec)
2020
cpu_ids = ["E-CPU: ", "P-CPU: "]
2121
usages = [
@@ -27,15 +27,16 @@ end
2727

2828
function TerminalSystemMonitor.plot_gpu_utilization_rates(::Type{MetalDevice})
2929
sampler = Sampler()
30-
msec = UInt(1000)
30+
msec = UInt(500)
3131
m = get_metrics(sampler, msec)
3232
gpu_usages = [
3333
("GPU: ", round(100 * m.gpu_usage[2], digits = 1)),
3434
]
3535
plts = []
36-
for (id, usage) in gpu_usages
37-
push!(plts, _plot_cpu_utilization_rates(id, usage))
38-
end
36+
chip_name = String(MTLDevice(1).name)
37+
push!(
38+
plts, barplot(["GPU: "], [round(100 * m.gpu_usage[2], digits = 1)], xlabel=chip_name, maximum=100, width=15)
39+
)
3940
return plts
4041
end
4142

src/TerminalSystemMonitor.jl

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,22 @@ function main(dummyargs...)
141141

142142
while true
143143
try
144-
plts = []
145-
append!(plts, plot_cpu_utilization_rates(CPUDevice))
146144
_, cols = displaysize(stdout)
147-
n = max(1, cols ÷ 25)
148-
chunks = collect(Iterators.partition(plts, n))
149-
f = foldl(/, map(c -> prod(UnicodePlots.panel.(c)), chunks))
150-
151-
f /= prod(UnicodePlots.panel.(plot_cpu_memory_utilization(CPUDevice)))
145+
t1 = @async begin
146+
plts = []
147+
append!(plts, plot_cpu_utilization_rates(CPUDevice))
148+
n = max(1, cols ÷ 25)
149+
chunks = collect(Iterators.partition(plts, n))
150+
f = foldl(/, map(c -> prod(UnicodePlots.panel.(c)), chunks))
151+
152+
f /= prod(UnicodePlots.panel.(plot_cpu_memory_utilization(CPUDevice)))
153+
f
154+
end
152155

153156
if isdefined(Main, :CUDA) &&
154157
getproperty(getproperty(Main, :CUDA), :functional)()
158+
wait(t1)
159+
f = fetch(t1)
155160
cudaplts = []
156161
n = max(1, cols ÷ 50)
157162
plts1 = plot_gpu_utilization_rates(CUDADevice)::Vector{Any}
@@ -162,13 +167,16 @@ function main(dummyargs...)
162167
end
163168
gpuchunks = collect(Iterators.partition(cudaplts, n))
164169
f /= foldl(/, map(c -> prod(UnicodePlots.panel.(c)), gpuchunks))
165-
end
166-
167-
if isdefined(Main, :Metal) && Sys.ARCH == :aarch64
170+
elseif isdefined(Main, :Metal) && Sys.isapple() && Sys.ARCH == :aarch64
168171
metalplts = []
169172
n = max(1, cols ÷ 50)
170-
plts1 = plot_cpu_utilization_rates(MetalDevice)::Vector{Any}
171-
plts2 = plot_gpu_utilization_rates(MetalDevice)::Vector{Any}
173+
t2 = @async plot_cpu_utilization_rates(MetalDevice)
174+
t3 = @async plot_gpu_utilization_rates(MetalDevice)
175+
wait(t1)
176+
wait(t2)
177+
wait(t3)
178+
plts1 = fetch(t2)
179+
plts2 = fetch(t3)
172180
for i in eachindex(plts1)
173181
push!(metalplts, plts1[i])
174182
end
@@ -177,12 +185,15 @@ function main(dummyargs...)
177185
end
178186
metalchunks = collect(Iterators.partition(metalplts, n))
179187
f /= foldl(/, map(c -> prod(UnicodePlots.panel.(c)), metalchunks))
188+
else
189+
wait(t1)
190+
f = fetch(t1)
180191
end
181192
clearlinesall()
182193
display(f)
183194
catch e
184195
unhidecursor() # unhide cursor
185-
if e isa InterruptException
196+
if e isa InterruptException || e isa TaskFailedException
186197
@info "Intrrupted"
187198
break
188199
else

0 commit comments

Comments
 (0)