Skip to content

Commit a14d9e6

Browse files
Merge pull request #13 from AtelierArith/terasaki/issue-9
Resolve Issue 9
2 parents eba75a2 + f01f397 commit a14d9e6

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.1.5"
66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
88
MLDataDevices = "7e8f7934-dd98-4c1a-8fe8-92b47a384d40"
9+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
910
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
1011
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
1112

@@ -25,6 +26,7 @@ JET = "0.9.12"
2526
MLDataDevices = "1.5.3"
2627
MacOSIOReport = "0.1.0"
2728
ReTestItems = "1.29.0"
29+
Statistics = "1"
2830
Term = "2.0.6"
2931
Test = "1"
3032
UnicodePlots = "3.7.0"

src/TerminalSystemMonitor.jl

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using Dates: Dates, Day, DateTime, Second
44
using UnicodePlots
55
import Term # this is required by UnicodePlots.panel
66
using Term: Consoles
7+
using Statistics: mean
78
using MLDataDevices: MLDataDevices, CUDADevice, CPUDevice, MetalDevice
89

910
export monitor # entrypoint from REPL
@@ -79,13 +80,22 @@ function extract_number_and_unit(str::AbstractString)
7980
end
8081
end
8182

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
8694

8795
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)
8999

90100
plts = []
91101

@@ -136,13 +146,14 @@ function main(dummyargs...)
136146
# Control cursor hiding and showing with Term.Consoles
137147
Consoles.hide_cursor()
138148

149+
statfn = identity
139150
while true
140151
try
141-
_, cols = displaysize(stdout)
152+
rows, cols = displaysize(stdout)
142153
t1 = @async begin
143154
try
144155
plts = []
145-
append!(plts, plot_cpu_utilization_rates(CPUDevice))
156+
append!(plts, plot_cpu_utilization_rates(CPUDevice, statfn))
146157
n = max(1, cols ÷ 25)
147158
chunks = collect(Iterators.partition(plts, n))
148159
f = foldl(/, map(c -> prod(UnicodePlots.panel.(c)), chunks))
@@ -226,6 +237,16 @@ function main(dummyargs...)
226237

227238
Consoles.move_to_line(stdout, 1)
228239
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")) > 2rows
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 > 2length(split(string(f), "\n"))
248+
statfn = identity
249+
end
229250
display(f)
230251
catch e
231252
Consoles.show_cursor()

0 commit comments

Comments
 (0)