@@ -3,11 +3,12 @@ module TerminalSystemMonitor
3
3
using Dates: Dates, Day, DateTime, Second
4
4
using UnicodePlots
5
5
import Term # this is required by UnicodePlots.panel
6
- using MLDataDevices: MLDataDevices, CUDADevice
6
+ using MLDataDevices: MLDataDevices, CUDADevice, CPUDevice, MetalDevice
7
7
8
8
export monitor # entrypoint from REPL
9
9
10
10
# These function will be defined in Package extensions
11
+ function plot_cpu_utilization_rates end
11
12
function plot_gpu_utilization_rates end
12
13
function plot_gpu_memory_utilization end
13
14
@@ -77,7 +78,7 @@ function extract_number_and_unit(str::AbstractString)
77
78
end
78
79
end
79
80
80
- function plot_cpu_utilization_rates ()
81
+ function plot_cpu_utilization_rates (:: Type{CPUDevice} )
81
82
ys = get_cpu_percent ()
82
83
npad = 1 + floor (Int, log10 (length (ys)))
83
84
xs = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (ys)]
@@ -94,7 +95,7 @@ function plot_cpu_utilization_rates()
94
95
return plts
95
96
end
96
97
97
- function plot_cpu_memory_utilization ()
98
+ function plot_cpu_memory_utilization (:: Type{CPUDevice} )
98
99
memorytotal, memorytotal_unit =
99
100
Sys. total_memory () |> Base. format_bytes |> extract_number_and_unit
100
101
memoryfree, _ = Sys. free_memory () |> Base. format_bytes |> extract_number_and_unit
@@ -140,42 +141,99 @@ function main(dummyargs...)
140
141
141
142
while true
142
143
try
143
- plts = []
144
- append! (plts, plot_cpu_utilization_rates ())
145
144
_, 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
151
163
152
164
if isdefined (Main, :CUDA ) &&
153
165
getproperty (getproperty (Main, :CUDA ), :functional )()
166
+ wait (t1)
167
+ f = fetch (t1)
168
+ if isnothing (f)
169
+ break
170
+ end
154
171
cudaplts = []
155
172
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}
158
175
for i in eachindex (plts1, plts2)
159
176
push! (cudaplts, plts1[i])
160
177
push! (cudaplts, plts2[i])
161
178
end
162
179
gpuchunks = collect (Iterators. partition (cudaplts, n))
163
180
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
164
228
end
165
229
clearlinesall ()
166
230
display (f)
167
231
catch e
168
232
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
176
235
end
177
236
end
178
- @info " Unhide cursor"
179
237
unhidecursor () # unhide cursor
180
238
end
181
239
0 commit comments