Skip to content

Commit a6cf8a1

Browse files
Init project
1 parent feccee3 commit a6cf8a1

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

Project.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name = "TerminalSystemMonitor"
2+
uuid = "bd0a793d-05ca-4aa9-be12-7c4786d45f63"
3+
authors = ["Satoshi Terasaki <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
8+
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

main.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using TerminalSystemMonitor: main
2+
3+
if abspath(PROGRAM_FILE) == @__FILE__
4+
Base.exit_on_sigint(false)
5+
main()
6+
end

src/TerminalSystemMonitor.jl

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
module TerminalSystemMonitor
2+
3+
#=
4+
CPU Usage
5+
┌ ┐
6+
id: 0 ┤■■■■■■■■■■ 23
7+
id: 1 ┤ 0
8+
id: 2 ┤■■■■■ 11.1111
9+
id: 3 ┤ 0
10+
id: 4 ┤■■ 5.05051
11+
id: 5 ┤ 0
12+
id: 6 ┤■ 3
13+
id: 7 ┤ 0
14+
id: 8 ┤ 1
15+
id: 9 ┤ 0
16+
id: 10 ┤ 0
17+
id: 11 ┤ 0
18+
id: 12 ┤ 0
19+
id: 13 ┤ 0
20+
id: 14 ┤ 0
21+
id: 15 ┤ 0
22+
└ ┘
23+
=#
24+
25+
using UnicodePlots
26+
using Term
27+
28+
idle_time(info::Sys.CPUinfo) = Int64(info.cpu_times!idle)
29+
30+
busy_time(info::Sys.CPUinfo) = Int64(
31+
info.cpu_times!user + info.cpu_times!nice + info.cpu_times!sys + info.cpu_times!irq,
32+
)
33+
34+
"""
35+
cpu_percent(period)
36+
37+
CPU usage between 0.0 and 100 [percent]
38+
The idea is borrowed from https://discourse.julialang.org/t/get-cpu-usage/24468/7
39+
Thank you @fonsp.
40+
"""
41+
function cpu_percent(period::Real=1.0)
42+
43+
info = Sys.cpu_info()
44+
busies = busy_time.(info)
45+
idles = idle_time.(info)
46+
47+
sleep(period)
48+
49+
info = Sys.cpu_info()
50+
busies = busy_time.(info) .- busies
51+
idles = idle_time.(info) .- idles
52+
53+
100 * busies ./ (idles .+ busies)
54+
end
55+
56+
function clearline(; move_up::Bool=false)
57+
buf = IOBuffer()
58+
print(buf, "\x1b[2K") # clear line
59+
print(buf, "\x1b[999D") # rollback the cursor
60+
move_up && print(buf, "\x1b[1A") # move up
61+
print(buf |> take! |> String)
62+
end
63+
64+
function clearlines(H::Integer)
65+
for i = 1:H
66+
clearline(move_up=true)
67+
end
68+
end
69+
70+
function hidecursor()
71+
print("\x1b[?25l") # hidecursor
72+
end
73+
74+
function unhidecursor()
75+
print("\u001B[?25h") # unhide cursor
76+
end
77+
78+
function layout(x, y)
79+
ncpus = length(y)
80+
y = round.(y, digits=1)
81+
_, cols = displaysize(stdout)
82+
83+
plts = []
84+
85+
chunks = collect.(collect(Iterators.partition((1:ncpus), 4)))
86+
for c in chunks
87+
push!(
88+
plts,
89+
barplot(
90+
x[c], y[c],
91+
# title="CPU Usage",
92+
maximum=100, width=max(5, 15), height=length(c),
93+
)
94+
)
95+
end
96+
97+
n = max(1, cols ÷ 25)
98+
chunks = collect(Iterators.partition(plts, n))
99+
100+
foldl(/, map(c->prod(UnicodePlots.panel.(c)), chunks))
101+
102+
chunks = collect(Iterators.partition(plts, n))
103+
104+
canvas = foldl(/, map(c->prod(UnicodePlots.panel.(c)), chunks))
105+
106+
memoryusage = round((Sys.total_memory() - Sys.free_memory()) / 2 ^ 20 / 1000, digits=1)
107+
108+
canvas / UnicodePlots.panel(barplot(
109+
["Mem: "],
110+
[memoryusage],
111+
title="Memmory $(memoryusage)/$(floor(Sys.total_memory()/2^20 / 1000)) GB",
112+
maximum=Sys.total_memory()/2^20 / 1000, width=max(10, 40)
113+
))
114+
end
115+
116+
117+
function main()
118+
hidecursor()
119+
while true
120+
try
121+
y = cpu_percent()
122+
x = ["id: $(i-1)" for (i, _) in enumerate(y)]
123+
(_, cols) = displaysize(stdout)
124+
125+
# f = barplot(x, y, title="CPU Usage", maximum=100, width=max(10, cols - 15), height=length(y))
126+
f = layout(x, y)
127+
str = string(f)
128+
clearlines(2 + length(collect(eachmatch(r"\n", str))))
129+
display(f)
130+
catch e
131+
unhidecursor() # unhide cursor
132+
if e isa InterruptException
133+
@info "Intrrupted"
134+
break
135+
else
136+
@warn "Got Exception"
137+
rethrow(e) # so we don't swallow true exceptions
138+
end
139+
end
140+
end
141+
@info "Unhide cursor"
142+
unhidecursor() # unhide cursor
143+
end
144+
145+
end # module TerminalSystemMonitor

0 commit comments

Comments
 (0)