@@ -3,11 +3,12 @@ module TerminalSystemMonitor
33using Dates: Dates, Day, DateTime, Second
44using UnicodePlots
55import Term # this is required by UnicodePlots.panel
6- using MLDataDevices: MLDataDevices, CUDADevice
6+ using MLDataDevices: MLDataDevices, CUDADevice, CPUDevice, MetalDevice
77
88export monitor # entrypoint from REPL
99
1010# These function will be defined in Package extensions
11+ function plot_cpu_utilization_rates end
1112function plot_gpu_utilization_rates end
1213function plot_gpu_memory_utilization end
1314
@@ -77,7 +78,7 @@ function extract_number_and_unit(str::AbstractString)
7778 end
7879end
7980
80- function plot_cpu_utilization_rates ()
81+ function plot_cpu_utilization_rates (:: Type{CPUDevice} )
8182 ys = get_cpu_percent ()
8283 npad = 1 + floor (Int, log10 (length (ys)))
8384 xs = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (ys)]
@@ -94,7 +95,7 @@ function plot_cpu_utilization_rates()
9495 return plts
9596end
9697
97- function plot_cpu_memory_utilization ()
98+ function plot_cpu_memory_utilization (:: Type{CPUDevice} )
9899 memorytotal, memorytotal_unit =
99100 Sys. total_memory () |> Base. format_bytes |> extract_number_and_unit
100101 memoryfree, _ = Sys. free_memory () |> Base. format_bytes |> extract_number_and_unit
@@ -140,42 +141,99 @@ function main(dummyargs...)
140141
141142 while true
142143 try
143- plts = []
144- append! (plts, plot_cpu_utilization_rates ())
145144 _, cols = displaysize (stdout )
146- n = max (1 , cols ÷ 25 )
147- chunks = collect (Iterators. partition (plts, n))
148- f = foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), chunks))
149-
150- f /= prod (UnicodePlots. panel .(plot_cpu_memory_utilization ()))
145+ t1 = @async begin
146+ try
147+ plts = []
148+ append! (plts, plot_cpu_utilization_rates (CPUDevice))
149+ n = max (1 , cols ÷ 25 )
150+ chunks = collect (Iterators. partition (plts, n))
151+ f = foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), chunks))
152+
153+ f /= prod (UnicodePlots. panel .(plot_cpu_memory_utilization (CPUDevice)))
154+ return f
155+ catch e
156+ if e isa InterruptException
157+ return nothing
158+ else
159+ rethrow (e)
160+ end
161+ end
162+ end
151163
152164 if isdefined (Main, :CUDA ) &&
153165 getproperty (getproperty (Main, :CUDA ), :functional )()
166+ wait (t1)
167+ f = fetch (t1)
168+ if isnothing (f)
169+ break
170+ end
154171 cudaplts = []
155172 n = max (1 , cols ÷ 50 )
156- plts1 = plot_gpu_utilization_rates (MLDataDevices . CUDADevice):: Vector{Any}
157- plts2 = plot_gpu_memory_utilization (MLDataDevices . CUDADevice):: Vector{Any}
173+ plts1 = plot_gpu_utilization_rates (CUDADevice):: Vector{Any}
174+ plts2 = plot_gpu_memory_utilization (CUDADevice):: Vector{Any}
158175 for i in eachindex (plts1, plts2)
159176 push! (cudaplts, plts1[i])
160177 push! (cudaplts, plts2[i])
161178 end
162179 gpuchunks = collect (Iterators. partition (cudaplts, n))
163180 f /= foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), gpuchunks))
181+ elseif isdefined (Main, :MacOSIOReport ) && Sys. isapple () && Sys. ARCH == :aarch64
182+ metalplts = []
183+ n = max (1 , cols ÷ 50 )
184+ t2 = @async begin
185+ try
186+ return plot_cpu_utilization_rates (MetalDevice)
187+ catch e
188+ if e isa InterruptException
189+ return nothing
190+ else
191+ rethrow (e)
192+ end
193+ end
194+ end
195+ t3 = @async begin
196+ try
197+ return plot_gpu_utilization_rates (MetalDevice)
198+ catch e
199+ if e isa InterruptException
200+ return nothing
201+ else
202+ rethrow (e)
203+ end
204+ end
205+ end
206+ wait (t1)
207+ wait (t2)
208+ wait (t3)
209+ plts1 = fetch (t2)
210+ plts2 = fetch (t3)
211+ if isnothing (plts1) || isnothing (plts2)
212+ break
213+ end
214+ for i in eachindex (plts1)
215+ push! (metalplts, plts1[i])
216+ end
217+ for i in eachindex (plts2)
218+ push! (metalplts, plts2[i])
219+ end
220+ metalchunks = collect (Iterators. partition (metalplts, n))
221+ f /= foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), metalchunks))
222+ else
223+ wait (t1)
224+ f = fetch (t1)
225+ if isnothing (f)
226+ break
227+ end
164228 end
165229 clearlinesall ()
166230 display (f)
167231 catch e
168232 unhidecursor () # unhide cursor
169- if e isa InterruptException
170- @info " Intrrupted"
171- break
172- else
173- @warn " Got Exception"
174- rethrow (e) # so we don't swallow true exceptions
175- end
233+ @warn " Got Exception"
234+ rethrow (e) # so we don't swallow true exceptions
176235 end
177236 end
178- @info " Unhide cursor"
179237 unhidecursor () # unhide cursor
180238end
181239
0 commit comments