@@ -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
@@ -79,13 +80,22 @@ function extract_number_and_unit(str::AbstractString)
79
80
end
80
81
end
81
82
82
- function plot_cpu_utilization_rates (:: Type{CPUDevice} )
83
- ys = get_cpu_percent ()
84
- npad = 1 + floor (Int, log10 (length (ys)))
85
- xs = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (ys)]
83
+ _relu (x) = max (x, zero (x))
84
+
85
+ function plot_cpu_utilization_rates (:: Type{CPUDevice} , statfn= identity)
86
+ ys = statfn (get_cpu_percent ())
87
+ if ! (ys isa AbstractVector)
88
+ xs = [" CPU: " * string (statfn)]
89
+ ys = [ys]
90
+ else
91
+ npad = 1 + floor (Int, log10 (length (ys)))
92
+ xs = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (ys)]
93
+ end
86
94
87
95
ncpus = length (ys)
88
- ys = round .(ys, digits = 1 )
96
+ # Sometimes `ys`` can be negative, so we need to use relu function so that
97
+ # it ensures elements in `ys`` are positive.
98
+ ys = round .(_relu .(ys), digits = 1 )
89
99
90
100
plts = []
91
101
@@ -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 " )) > 2 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