Last active
June 21, 2024 12:18
-
-
Save terasakisatoshi/a990726676c530002f6d6a10c083d1a4 to your computer and use it in GitHub Desktop.
barplot_cpu_usage.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#= | |
CPU Usage | |
┌ ┐ | |
id: 0 ┤■■■■■■■■■■ 23 | |
id: 1 ┤ 0 | |
id: 2 ┤■■■■■ 11.1111 | |
id: 3 ┤ 0 | |
id: 4 ┤■■ 5.05051 | |
id: 5 ┤ 0 | |
id: 6 ┤■ 3 | |
id: 7 ┤ 0 | |
id: 8 ┤ 1 | |
id: 9 ┤ 0 | |
id: 10 ┤ 0 | |
id: 11 ┤ 0 | |
id: 12 ┤ 0 | |
id: 13 ┤ 0 | |
id: 14 ┤ 0 | |
id: 15 ┤ 0 | |
└ ┘ | |
=# | |
using UnicodePlots | |
idle_time(info::Sys.CPUinfo) = Int64(info.cpu_times!idle) | |
busy_time(info::Sys.CPUinfo) = Int64( | |
info.cpu_times!user + info.cpu_times!nice + info.cpu_times!sys + info.cpu_times!irq, | |
) | |
""" | |
cpu_percent(period) | |
CPU usage between 0.0 and 100 [percent] | |
The idea is borrowed from https://discourse.julialang.org/t/get-cpu-usage/24468/7 | |
Thank you @fonsp. | |
""" | |
function cpu_percent(period::Real = 1.0) | |
info = Sys.cpu_info() | |
busies = busy_time.(info) | |
idles = idle_time.(info) | |
sleep(period) | |
info = Sys.cpu_info() | |
busies = busy_time.(info) .- busies | |
idles = idle_time.(info) .- idles | |
100 * busies ./ (idles .+ busies) | |
end | |
function clearline(; move_up::Bool = false) | |
buf = IOBuffer() | |
print(buf, "\x1b[2K") # clear line | |
print(buf, "\x1b[999D") # rollback the cursor | |
move_up && print(buf, "\x1b[1A") # move up | |
print(buf |> take! |> String) | |
end | |
function clearlines(H::Integer) | |
for i = 1:H | |
clearline(move_up = true) | |
end | |
end | |
function hidecursor() | |
print("\x1b[?25l") # hidecursor | |
end | |
function unhidecursor() | |
print("\u001B[?25h") # unhide cursor | |
end | |
function main() | |
hidecursor() | |
while true | |
try | |
y = cpu_percent() | |
x = ["id: $(i-1)" for (i,_) in enumerate(y)] | |
lines = parse(Int, chop(read(`tput lines`, String))) | |
cols = parse(Int, chop(read(`tput cols`, String))) | |
f = barplot(x, y, title="CPU Usage", maximum=100, width=max(10, cols-15), height=length(y)) | |
str = string(f) | |
clearlines(1+length(collect(eachmatch(r"\n", str)))) | |
display(f) | |
catch e | |
unhidecursor() # unhide cursor | |
if e isa InterruptException | |
@info "Intrrupted" | |
break | |
else | |
@warn "Got Exception" | |
rethrow(e) # so we don't swallow true exceptions | |
end | |
end | |
end | |
@info "Unhide cursor" | |
unhidecursor() # unhide cursor | |
end | |
if abspath(PROGRAM_FILE) == @__FILE__ | |
Base.exit_on_sigint(false) | |
main() | |
end |
Author
terasakisatoshi
commented
Jun 16, 2024
![image](https://private-user-images.githubusercontent.com/16760547/340062290-6de97d1d-6613-4e1d-8adf-6465f340dbeb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE3MDI4NjIsIm5iZiI6MTcyMTcwMjU2MiwicGF0aCI6Ii8xNjc2MDU0Ny8zNDAwNjIyOTAtNmRlOTdkMWQtNjYxMy00ZTFkLThhZGYtNjQ2NWYzNDBkYmViLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIzVDAyNDI0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTRjMDMzNjZiYzg0NTE1MDI3MjVkMGI0YmZiYzBlMWRhMDhiM2M3NWQwMmVjZGE5NGEzZDQ1YmYyNmQ1Njg2NjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.B6UhgoiIkxRFuVp6utDwIBpcjDomjwogw5XmMsSFBI8)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment