@@ -4,6 +4,7 @@ using Dates: Dates, Day, DateTime, Second
4
4
using UnicodePlots
5
5
import Term # this is required by UnicodePlots.panel
6
6
using Term: Consoles
7
+ using Statistics: mean
7
8
using MLDataDevices: MLDataDevices, CUDADevice, CPUDevice, MetalDevice
8
9
9
10
export monitor # entrypoint from REPL
@@ -83,6 +84,15 @@ function plot_cpu_utilization_rates(::Type{CPUDevice})
83
84
ys = get_cpu_percent ()
84
85
npad = 1 + floor (Int, log10 (length (ys)))
85
86
xs = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (ys)]
87
+ function plot_cpu_utilization_rates (:: Type{CPUDevice} , statfn= identity)
88
+ ys = statfn (get_cpu_percent ())
89
+ if ! (ys isa AbstractVector)
90
+ xs = [" CPU: " * string (statfn)]
91
+ ys = [ys]
92
+ else
93
+ npad = 1 + floor (Int, log10 (length (ys)))
94
+ xs = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (ys)]
95
+ end
86
96
87
97
ncpus = length (ys)
88
98
ys = round .(ys, digits = 1 )
@@ -136,13 +146,14 @@ function main(dummyargs...)
136
146
# Control cursor hiding and showing with Term.Consoles
137
147
Consoles. hide_cursor ()
138
148
149
+ statfn = identity
139
150
while true
140
151
try
141
- _ , cols = displaysize (stdout )
152
+ rows , cols = displaysize (stdout )
142
153
t1 = @async begin
143
154
try
144
155
plts = []
145
- append! (plts, plot_cpu_utilization_rates (CPUDevice))
156
+ append! (plts, plot_cpu_utilization_rates (CPUDevice, statfn ))
146
157
n = max (1 , cols ÷ 25 )
147
158
chunks = collect (Iterators. partition (plts, n))
148
159
f = foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), chunks))
@@ -226,6 +237,16 @@ function main(dummyargs...)
226
237
227
238
Consoles. move_to_line (stdout , 1 )
228
239
Consoles. cleartoend (stdout )
240
+ # If user's machine has lots of CPU cores, we can't fit all the plots in one screen.
241
+ # So we need to use `mean` function to reduce the number of plots.
242
+ if length (split (string (f), " \n " )) > rows
243
+ statfn = mean
244
+ end
245
+
246
+ # If terminal has enough space, we can use `identity` function again to show all the plots.
247
+ if rows > 2 length (split (string (f), " \n " ))
248
+ statfn = identity
249
+ end
229
250
display (f)
230
251
catch e
231
252
Consoles. show_cursor ()
0 commit comments