@@ -11,13 +11,13 @@ busy_time(info::Sys.CPUinfo) = Int64(
11
11
)
12
12
13
13
"""
14
- cpu_percent (period)
14
+ get_cpu_percent (period)
15
15
16
16
CPU usage between 0.0 and 100 [percent]
17
17
The idea is borrowed from https://discourse.julialang.org/t/get-cpu-usage/24468/7
18
18
Thank you @fonsp.
19
19
"""
20
- function cpu_percent (period:: Real = 1.0 )
20
+ function get_cpu_percent (period:: Real = 1.0 )
21
21
22
22
info = Sys. cpu_info ()
23
23
busies = busy_time .(info)
@@ -59,10 +59,22 @@ function unhidecursor()
59
59
print (" \u 001B[?25h" ) # unhide cursor
60
60
end
61
61
62
- function layout (x, y)
62
+ function extract_number_and_unit (str:: AbstractString )
63
+ m = match (r" (\d +\.\d +)\s *(\w +)" , str)
64
+ if ! isnothing (m)
65
+ return parse (Float64, m. captures[1 ]), m. captures[2 ]
66
+ else
67
+ return nothing , nothing
68
+ end
69
+ end
70
+
71
+ function plot_cpu_utilization_rates ()
72
+ y = get_cpu_percent ()
73
+ npad = 1 + floor (Int, log10 (length (y)))
74
+ x = [" id: $(lpad (i- 1 , npad)) " for (i, _) in enumerate (y)]
75
+
63
76
ncpus = length (y)
64
77
y = round .(y, digits = 1 )
65
- _, cols = displaysize (stdout )
66
78
67
79
plts = []
68
80
@@ -73,16 +85,19 @@ function layout(x, y)
73
85
barplot (x[c], y[c], maximum = 100 , width = max (5 , 15 ), height = length (c)),
74
86
)
75
87
end
76
- memoryusageGB = round ((Sys. total_memory () - Sys. free_memory ()) / 2 ^ 30 , digits = 1 )
77
- memorytotGB = Sys. total_memory () / 2 ^ 30
78
- (memorytot, memoryusage, memoryunit) = if memorytotGB ≤ 1.0
79
- round (1024 memorytotGB), 1024 memoryusageGB, " MB"
80
- else
81
- round (memorytotGB), memoryusageGB, " GB"
82
- end
88
+ return plts
89
+ end
90
+
91
+ function plot_cpu_memory_utilization ()
92
+ memorytot, memoryunit = Sys. total_memory () |> Base. format_bytes |> extract_number_and_unit
93
+ memoryfree, _ = Sys. free_memory () |> Base. format_bytes |> extract_number_and_unit
94
+ memoryusage = memorytot - memoryfree
95
+ memorytot = round (memorytot)
83
96
84
97
seconds = floor (Int, Sys. uptime ())
85
98
datetime = DateTime (1970 ) + Second (seconds)
99
+
100
+ plts = []
86
101
push! (
87
102
plts,
88
103
barplot (
@@ -103,26 +118,24 @@ function layout(x, y)
103
118
),
104
119
)
105
120
106
- n = max (1 , cols ÷ 25 )
107
- chunks = collect (Iterators. partition (plts, n))
108
-
109
-
110
- return foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), chunks))
111
121
end
112
122
113
123
function main (dummyargs... )
114
124
hidecursor ()
115
125
116
126
while true
117
127
try
118
- y = cpu_percent ()
119
- npad = 1 + floor (Int, log10 ( length (y) ))
120
- x = [ " id: $( lpad (i - 1 , npad)) " for (i, _) in enumerate (y)]
128
+ plts = []
129
+ append! (plts, plot_cpu_utilization_rates ( ))
130
+ append! (plts, plot_cpu_memory_utilization ())
121
131
122
- f = layout (x, y)
132
+ # adjust layout
133
+ _, cols = displaysize (stdout )
134
+ n = max (1 , cols ÷ 25 )
135
+ chunks = collect (Iterators. partition (plts, n))
136
+ f = foldl (/ , map (c -> prod (UnicodePlots. panel .(c)), chunks))
123
137
124
138
clearlinesall ()
125
-
126
139
display (f)
127
140
catch e
128
141
unhidecursor () # unhide cursor
0 commit comments