-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem
490 lines (368 loc) · 12.4 KB
/
mem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/bin/bash
# Licence: GNU GPL v3 https://www.gnu.org/licenses/gpl-3.0.html
VERSION="2"
SOURCE="https://github.com/tele1/LinuxScripts"
# Destiny: Attempted to summarize the value of RAM used
# Script use: bash mem --help
###########################}
# Check Dependecies - List created automatically by find.bash.dep.sh version=8
# source=https://github.com/tele1/LinuxScripts
[[ -z $(type -P awk) ]] && DEP="$DEP"$'\n'"awk"
[[ -z $(type -P cut) ]] && DEP="$DEP"$'\n'"cut"
[[ -z $(type -P echo) ]] && DEP="$DEP"$'\n'"echo"
[[ -z $(type -P ps) ]] && DEP="$DEP"$'\n'"ps"
[[ -z $(type -P top) ]] && DEP="$DEP"$'\n'"top"
[[ -z $(type -P tr) ]] && DEP="$DEP"$'\n'"tr"
# End script if exist any error
[ -z "$DEP" ] || { echo " Error: Missing dependencies, before run script please install: $DEP" ; exit 1 ;}
###########################}
## Dictionary:
## F_NAME - function name
## F_NAME_L_NAME - function name from library / file name
## $[..] is replaced via $((..))
LC_ALL=C
# For DEBUG available options: ON , OFF
DEBUG=OFF
NC='\e[0m' # Reset Color
BL='\e[0;36m' # Cyan ECHO
GN='\e[0;32m' # Green ECHO
YW='\e[0;33m' # Yellow ECHO
RD='\e[0;31m' # Red ECHO
#----------------{
F_MESSAGE_ERROR() {
# print text in red and redirect to error
echo -e "${RD} ERROR: $@ ${NC}" 1>&2
#; exit 1
}
#----------------}
#----------------{
F_MESSAGE_DEBUG() {
if [[ "$DEBUG" == "ON" ]] ; then
echo -e "${BL} DEBUG: $1 ${NC}"
fi
}
#----------------}
#----------------{
F_MESSAGE_DEBUG2() {
if [[ "$DEBUG" == "ON" ]] ; then
echo -e "${GN} DEBUG: $1 ${NC}"
fi
}
#----------------}
#====================================================================================================
#================================{
F_FOREST() {
ps -xo pid,size,command --forest
}
#================================}
#================={
F_COUNT_CHARACTERS() {
# break one line into characters
# I want to calculate how far the command is. From the beginning of the line to the command
COUNT_CHAR=0
while IFS= read -r -d "" -n 1 CHAR; do
#echo "$CHAR"
## if this is part of a command then end this loop
case "$CHAR" in
"/") break ;;
[a-zA-Z]) break ;; esac
SAVE_COUNT_CHAR="$COUNT_CHAR"
COUNT_CHAR=$(($COUNT_CHAR+1))
done <<< $1
}
#=================}
#================={
F_TOTAL_USED() {
## I used "free" command because probably the full data the command "ps" will show with root privileges.
NUM=0
while read A B C D; do
while read LINE; do
NUM=$(($NUM+1))
F_MESSAGE_DEBUG "NUM = $NUM"
F_MESSAGE_DEBUG "LINE = $LINE"
[ $NUM -eq 2 ] && TOTAL_USED="$LINE"
done <<< "$C"
done <<< "$(free -m)"
}
#=================}
#============================================{
F_PS_GROUP() {
F_MESSAGE_DEBUG2 "PID: $1"
## Tree of all processes
TREE_1=$(ps -xo pid,rss,command --forest --no-headers)
## Example about RSS https://askingthelot.com/what-is-rss-in-ps-output/
## A loop which will find the specified process + find its process tree
## and it will return a list of these processes together.
IFS=" " ; while read A B C ; do
# Find line containing specific PID
if [[ "${A}" == "${1}" ]] ; then
DATA="$A $B $C"
# START list of processes from this line
START_P1=START
## Command distance from F_COUNT_CHARACTERS
F_COUNT_CHARACTERS "$C"
COUNT_FIRST_LINE="$SAVE_COUNT_CHAR"
F_MESSAGE_DEBUG2 "$A $B $C"
F_MESSAGE_DEBUG "COUNT_FIRST_LINE = $COUNT_FIRST_LINE"
RSS="$B"
COMMAND=$(cut -c $(($COUNT_FIRST_LINE+1))- <<< $C)
PROCESS_COUNTER=1
continue
fi
if [[ "${START_P1}" == "START" ]] ; then
## Command distance from F_COUNT_CHARACTERS
F_COUNT_CHARACTERS "$C"
COUNT_NEXT_LINE="$SAVE_COUNT_CHAR"
F_MESSAGE_DEBUG "COUNT_NEXT_LINE = $COUNT_NEXT_LINE"
# STOP loop and also list
[ "$COUNT_NEXT_LINE" -le "$COUNT_FIRST_LINE" ] && break
# The sum of all processes (RSS)
# If "SUM" is empty then put the first number from first line.
[ -z "$SUM" ] && SUM="$RSS"
SUM=$(($B+$SUM))
F_MESSAGE_DEBUG "B = $B"
F_MESSAGE_DEBUG "SUM = $SUM"
PROCESS_COUNTER=$((PROCESS_COUNTER+1))
fi
## Count only to 10 . THIS IS ONLY temporarily for test / debug "break" commmand.
## [ -z "$N" ] && N=1 ; [ $N -eq 10 ] && break ; N=$(($N+1))
done <<< "$TREE_1"
[ -z "$DATA" ] && { F_MESSAGE_ERROR "not found PID: $1 " ; exit 1;}
echo " Command = $COMMAND"
echo " RSS = $(("$SUM"/1024)) MiB (Number of processes: $PROCESS_COUNTER)"
# Total used
F_TOTAL_USED ; echo " TOTAL_USED = $TOTAL_USED MiB (from free command)"
}
#============================================}
#========={
F_PS_SINGLE() {
LINE=$(ps -o rss,command -p "$1" --no-headers)
[ -z "$LINE" ] && { F_MESSAGE_ERROR "not found PID: $1 " ; exit 1;}
while read RSS COMMAND ; do
echo " Command = $COMMAND"
echo " RSS = $(("$RSS"/1024)) MiB"
done <<< "$LINE"
# Total used
F_TOTAL_USED ; echo " TOTAL_USED = $TOTAL_USED MiB (from free command)"
}
#=========}
#========={
F_TOP_SINGLE() {
TOP_WINDOW=$(top -wbn 1)
COUNT=0 ; IFS=" " ; while read ONE TWO TREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE ; do
COUNT=$(($COUNT+1))
# to get total used memory
# if there is a fourth row
if [ "${COUNT}" -eq "4" ] ; then
TOTAL_USED="$EIGHT MiB"
fi
# to get list of PID,RES,COMMAND
if [ "${COUNT}" -gt "7" ] ; then
# Find line containing specific PID
if [[ "${ONE}" == "${1}" ]] ; then
echo " COMMAND = $TWELVE"
echo " REZ = ${SIX}"
fi
fi
done <<< "$TOP_WINDOW"
echo " TOTAL_USED = $TOTAL_USED (from top command)"
}
#=========}
#============================================{
F_TOP_GROUP() {
TOP_WINDOW=$(top -wbn 1)
# get a list of PID-s of processes from the process tree
# I will usee "ps" command for this job. Because I don't see the possibility of using "top" command.
TREE_1=$(ps -xo pid,command --forest --no-headers)
# parse "ps" output
IFS=" " ; while read PID COMMAND ; do
# Find line containing specific PID
if [[ "${PID}" == "${1}" ]] ; then
# Start list of processes from this line (the list will only contain PID numbers)
START_P1=START
## Command distance from F_COUNT_CHARACTERS
F_COUNT_CHARACTERS "$COMMAND"
COUNT_FIRST_LINE="$SAVE_COUNT_CHAR"
F_MESSAGE_DEBUG "COUNT_FIRST_LINE = $COUNT_FIRST_LINE"
FIRST_PID="${PID}"
PROCESS_COUNTER=1
continue
fi
if [[ "${START_P1}" == "START" ]] ; then
## Command distance from F_COUNT_CHARACTERS
F_COUNT_CHARACTERS "$COMMAND"
COUNT_NEXT_LINE="$SAVE_COUNT_CHAR"
F_MESSAGE_DEBUG "COUNT_NEXT_LINE = $COUNT_NEXT_LINE"
# STOP loop and also list
[ "$COUNT_NEXT_LINE" -le "$COUNT_FIRST_LINE" ] && break
# List of PID-s
[ -z "$LIST_PIDS" ] && LIST_PIDS="$FIRST_PID"
LIST_PIDS="$(printf "%s\n%s" "${LIST_PIDS}" "${PID}")"
PROCESS_COUNTER=$((PROCESS_COUNTER+1))
fi
done <<< "$TREE_1"
F_MESSAGE_DEBUG "PROCESS_COUNTER = $PROCESS_COUNTER"
F_MESSAGE_DEBUG "LIST_PIDS = $LIST_PIDS"
COUNT=0 ; IFS=" " ; while read ONE TWO TREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE ; do
COUNT=$(($COUNT+1))
# to get total used memory
# if there is a fourth row
if [ "${COUNT}" -eq "4" ] ; then
TOTAL_USED="$EIGHT MiB"
fi
# to get list of PID,RES,COMMAND
if [ "${COUNT}" -gt "7" ] ; then
# Find line containing specific PID from $LIST_PIDS
while read LINE ; do
if [[ "${LINE}" == "${ONE}" ]] ; then
REZ="$SIX"
F_MESSAGE_DEBUG "----"
F_MESSAGE_DEBUG "PID = $ONE"
F_MESSAGE_DEBUG "REZ = $REZ"
F_MESSAGE_DEBUG "COMMAND = $TWELVE"
#========={
# The REZ may contain numbers rounded to GiB :(
# break one line into characters
VALUE=0 ; while IFS= read -r -d "" -n 1 CHAR; do
case "$CHAR" in
",") REZ=$(tr ',' '.' <<< "$REZ") ;;
"g") REZ=$(tr -d 'g' <<< "$REZ") ; REZ=$(awk '{N=$1*1024*1024 ; printf("%.f" , N)}' <<< "$REZ") ; break ;;
"m") REZ=$(tr -d 'm' <<< "$REZ") ; REZ=$(awk '{N=$1*1024 ; printf("%.f" , N)}' <<< "$REZ") ; break ;;
[a-zA-Z]) VALUE=1 ; break ;; esac
done <<< "$REZ"
if [[ "${VALUE}" == "1" ]] ; then
LIST_ALPHA_REZ="$( printf "%s\n%s" "${LIST_ALPHA_REZ}" "${REZ}")"
else
#LIST_NUMBER_REZ="$(printf "%s\n%s" "${LIST_NUMBER_REZ}" "${REZ}")"
[ -z "$SUM" ] && SUM="$RSS"
SUM_NUMBER_REZ=$(($SUM_NUMBER_REZ+$REZ))
fi
#=========}
fi
done <<< "$LIST_PIDS"
if [[ "${ONE}" == "${1}" ]] ; then
COMMAND="$TWELVE"
fi
fi
done <<< "$TOP_WINDOW"
F_MESSAGE_DEBUG "LIST_ALPHA_REZ = $LIST_ALPHA_REZ"
F_MESSAGE_DEBUG "SUM_NUMBER_REZ = $SUM_NUMBER_REZ"
echo " Command = $COMMAND"
echo " REZ = $((SUM_NUMBER_REZ/1024)) MiB"
if [ ! -z "$LIST_ALPHA_REZ" ] ; then
echo " "
echo " Info: The value may contain additional characters, "
echo " therefore additional values may be below. "
echo " Try fix script."
echo " "
echo " contains characters a-z = $LIST_ALPHA_REZ"
echo " "
fi
echo " TOTAL_USED = $TOTAL_USED (from top command)"
}
#============================================}
F_PS_MEM() {
[[ ! -f "ps_mem.py" ]] && echo "Script not found in this directory: ps_mem.py"
[[ -z $(type -P python) ]] && DEP="$DEP"$'\n'"python"
[[ -z $(type -P sudo) ]] && DEP="$DEP"$'\n'"sudo"
# End script if exist any error
[ -z "$DEP" ] || { echo " Error: Missing dependencies, before run script please install: $DEP" ; exit 1 ;}
OUTPUT=$(sudo python ps_mem.py)
while read ONE TWO TREE FOUR FIVE SIX ; do
if [ -z "$ONE" ] ; then
# Start job from this line.
START_JOB=1
continue
fi
if [[ "$START_JOB" == "1" ]] ; then
# Stop job if "-" char is detected
while IFS= read -r -d "" -n 1 CHAR; do
case "$CHAR" in
"-") START_JOB=0 ; break ;; esac
done <<< "$ONE"
if [[ "$START_JOB" == "1" ]] ; then
# Change KiB,MiB,GiB to KiB and round to integers for "Private" column
while IFS= read -r -d "" -n 1 CHAR; do
case "$CHAR" in
"K") ONE=$(awk '{N=$1 ; printf("%.f" , N)}' <<< "$ONE") ; break ;;
"M") ONE=$(awk '{N=$1*1024 ; printf("%.f" , N)}' <<< "$ONE") ; break ;;
"G") ONE=$(awk '{N=$1*1024*1024 ; printf("%.f" , N)}' <<< "$ONE") ; break ;;
"*") F_MESSAGE_ERROR "Unknown CHAR variable, try fix script." ; exit 1 ;; esac
done <<< "$TWO"
# F_MESSAGE_DEBUG "Private = $ONE"
[ -z "$SUM_ONE" ] && SUM_ONE=0
SUM_ONE="$((${SUM_ONE}+${ONE}))"
# F_MESSAGE_DEBUG "Sum = ${SUM_ONE}"
# Change KiB,MiB,GiB to KiB and round to integers for "Shared" column
while IFS= read -r -d "" -n 1 CHAR; do
case "$CHAR" in
"K") FOUR=$(awk '{N=$1 ; printf("%.f" , N)}' <<< "$FOUR") ; break ;;
"M") FOUR=$(awk '{N=$1*1024 ; printf("%.f" , N)}' <<< "$FOUR") ; break ;;
"G") FOUR=$(awk '{N=$1*1024*1024 ; printf("%.f" , N)}' <<< "$FOUR") ; break ;;
"*") F_MESSAGE_ERROR "Unknown CHAR variable, try fix script." ; exit 1 ;; esac
done <<< "$FIVE"
[ -z "$SUM_FOUR" ] && SUM_FOUR=0
SUM_FOUR="$((${SUM_FOUR}+${FOUR}))"
fi
fi
done <<< "$OUTPUT"
echo "$OUTPUT"
echo " Total Private = $((${SUM_ONE}/1024)) MiB"
echo " Total Shared = $((${SUM_FOUR}/1024)) MiB"
echo " Together = $(($((${SUM_ONE}/1024))+$((${SUM_FOUR}/1024)))) MiB"
F_TOTAL_USED ; echo " Total used (from free command) = $TOTAL_USED MiB"
}
#===================
#========================={
case $1 in
"--help"|"-h")
echo " Help help. "
echo " Is there someone there? :)"
echo " "
echo " Tool to summarize used memory"
echo " "
echo " Options:"
echo " "
echo " --forest The process tree. Useful to find the PID number."
echo " "
echo " --ps-group PID_number summary for the group of processes "
echo " (used ps command)"
echo " "
echo " --ps-single PID_number summary for one process "
echo " (used ps command)"
echo " "
echo " --top-group PID_number summary for the group of processes "
echo " (used top command)"
echo " "
echo " --top-single PID_number summary for one process "
echo " (used top command)"
echo " "
echo " --ps-mem.py summary from ps_mem.py script from place where is this script."
echo " "
echo " "
;;
"--ps-group")
F_PS_GROUP $2
;;
"--ps-single")
F_PS_SINGLE $2
;;
"--top-group")
F_TOP_GROUP $2
;;
"--top-single")
F_TOP_SINGLE $2
;;
"--ps-mem.py")
## ! It requires having a script "ps_mem.py" in this directory where the script will run. ! Date: 23.02.2022 y.
F_PS_MEM
;;
"--forest")
F_FOREST
;;
*)
echo "Unknown option: $1"
echo " Try use: $0 --help"
;;
esac
#=========================}