|
| 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