-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfree.pl
executable file
·293 lines (249 loc) · 8.12 KB
/
free.pl
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
#!/usr/bin/perl -w
#
# free.pl -- display the infomation of memory. {{{1
#
# Author: soarpenguin <[email protected]>
# First release Nov.14 2012
# 1}}}
# total used free shared buffers cached
#Mem: 1024800 897528 127272 0 47684 292140
#-/+ buffers/cache: 557704 467096
#Swap: 1046524 7560 1038964
#-----run dprofpp to analyze the profile.-----
# $perl -d:DProf free.pl
# $dprofpp -u
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
#use Smart::Comments;
use Term::ANSIColor;
#print color("red"), "Stop!\n", color("reset");
#print color("green"), "Go!\n", color("reset");
my ($version, $help);
my $myvesion = "0.1.0";
my $script = basename $0;
my $meminfo = '/proc/meminfo';
my ($fd, @lines);
my ($byte, $kb, $mb, $gb, $changed);
my ($oldfmt, $count, $countflag, $sleep, $total);
my $byteshift = 10;
my ($memtotal, $memused, $memfree, $memshared, $membuf, $memcached);
my ($minus, $plus); #for (-/+ buffers/cached)
my ($lowtotal, $lowfree);
my ($hightotal, $highfree);
my $lhdetail; # display low/high memory detail info or not.
my ($swaptotal, $swapused, $swapfree);
my $usage = "
Usage: $script [-b|-k|-m|-g] [-c count] [-l] [-o] [-t] [-s delay] [-V]
-b Display the amount of memory in bytes.
-c count
Display the result count times. Requires the -s option.
-g Display the amount of memory in gigabytes.
-k Display the amount of memory in kilobytes. This is the default.
-l Show detailed low and high memory statistics.
-m Display the amount of memory in megabytes.
-o Display the output in old format, the only difference being
this option will disable the display of the \"buffer adjusted\" line.
-s Continuously display the result delay seconds apart.
You may actually specify any floating point number for
delay, usleep(3) is used for microsecond resolution delay times.
-t Display a line showing the column totals.
-h, --help
Display this help and exit
-V Display version information.
";
my $ret = GetOptions(
'byte|b' => \$byte,
'k|KB' => \$kb,
'm|MB' => \$mb,
'g|GB' => \$gb,
'c=i' => \$count,
's=f' => \$sleep,
't' => \$total,
'o' => \$oldfmt,
'l' => \$lhdetail,
'help|h|?' => \&usage, #point to the usage();
'version|V' => \&version
);
if(! $ret) {
&usage();
}
#if($help or $version) {
# &usage();
#}
if(! -e $meminfo) {
print "Need the system file of $meminfo. Try mount /proc\n";
die;
}
# function for signal action
sub catch_int {
my $signame = shift;
print color("red"), "Stoped by SIG$signame\n", color("reset");
exit;
}
$SIG{INT} = __PACKAGE__ . "::catch_int";
$SIG{INT} = \&catch_int; # best strategy
if($byte) {
$byteshift = 0;
$changed = 1;
} elsif ($mb) {
$byteshift = 20;
$changed = 1;
} elsif ($gb) {
$byteshift = 30;
$changed = 1;
}
### $changed
### $byteshift
if($count and $sleep) {
$countflag = 1;
if($count < 0) {
$count = -$count;
}
} elsif ($sleep) {
$countflag = 0;
$count = 0;
} else {
$countflag = 0;
$count = 0;
$sleep = 0;
}
### $sleep
### $count;
### $countflag;
$| = 1;
do {
open($fd, "<", $meminfo);
die "Failed to open the file $meminfo" unless $fd;
@lines = <$fd>;
close $fd;
#print @lines;
foreach my $line (@lines) {
if($line =~ /\bMemTotal:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$memtotal = $line;
} elsif ($line =~ /\bMemFree:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$memfree = $line;
} elsif ($line =~ /\bBuffers:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$membuf = $line;
} elsif ($line =~ /\bCached:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$memcached = $line;
} elsif ($line =~ /\bHighTotal:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$hightotal = $line;
} elsif ($line =~ /\bHighFree:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$highfree = $line;
} elsif ($line =~ /\bLowTotal:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$lowtotal = $line;
} elsif ($line =~ /\bLowFree:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$lowfree = $line;
} elsif ($line =~ /\bSwapTotal:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$swaptotal = $line;
} elsif ($line =~ /\bSwapFree:(\s+)(\d+)/i) {
$line =~ s/[^0-9]//g;
$swapfree = $line;
last;
}
}
# I'm not sure the $2 will influence the profile. so not use it.
#foreach my $line (@lines) {
# if($line =~ /\bMemTotal:(\s+)(\d+)/) {
# $memtotal = $2;
# } elsif ($line =~ /\bMemFree:(\s+)(\d+)/) {
# $memfree = $2;
# } elsif ($line =~ /\bBuffers:(\s+)(\d+)/) {
# $membuf = $2;
# } elsif ($line =~ /\bCached:(\s+)(\d+)/) {
# $memcached = $2;
# } elsif ($line =~ /\bHighTotal:(\s+)(\d+)/) {
# $hightotal = $2;
# } elsif ($line =~ /\bHighFree:(\s+)(\d+)/) {
# $highfree = $2;
# } elsif ($line =~ /\bLowTotal:(\s+)(\d+)/) {
# $lowtotal = $2;
# } elsif ($line =~ /\bLowFree:(\s+)(\d+)/) {
# $lowfree = $2;
# } elsif ($line =~ /\bSwapTotal:(\s+)(\d+)/) {
# $swaptotal = $2;
# } elsif ($line =~ /\bSwapFree:(\s+)(\d+)/) {
# $swapfree = $2;
# last;
# }
#}
$memshared = 0;
$memused = $memtotal - $memfree;
$swapused = $swaptotal - $swapfree;
$minus = $memused - $membuf - $memcached;
$plus = $memfree + $membuf + $memcached;
if($changed) {
$memtotal = &sizeshift($memtotal, $byteshift); # mem total/used/free/buffer/cached
$memused = &sizeshift($memused, $byteshift);
$memfree = &sizeshift($memfree, $byteshift);
$membuf = &sizeshift($membuf, $byteshift);
$memcached = &sizeshift($memcached, $byteshift);
$lowtotal = &sizeshift($lowtotal, $byteshift); # low total/free
$lowfree = &sizeshift($lowfree, $byteshift);
$hightotal = &sizeshift($hightotal, $byteshift); #high total/free
$highfree = &sizeshift($highfree, $byteshift);
$minus = &sizeshift($minus, $byteshift); # minus/plus buffer/cached
$plus = &sizeshift($plus, $byteshift);
$swaptotal = &sizeshift($swaptotal, $byteshift); # swap total/used/free
$swapused = &sizeshift($swapused, $byteshift);
$swapfree = &sizeshift($swapfree, $byteshift);
}
print color("blue"); # use blue color for infomation head.
printf("%18s %10s %10s %10s %10s %10s\n", "total", "used",
"free", "shared", "buffers", "cached");
print color("reset"); # reset the default color.
printf("%-6s %11d %10d %10d %10d %10d %10d\n", "Mem:", $memtotal, $memused,
$memfree, $memshared, $membuf, $memcached);
# diplay the detail of low/high memory infomation
if($lhdetail) {
printf("%-6s %11d %10d %10d\n", "Low:", $lowtotal, $lowtotal-$lowfree, $lowfree);
printf("%-6s %11d %10d %10d\n", "High:", $hightotal, $hightotal-$highfree, $highfree);
}
if(! $oldfmt) {
printf("%18s %10d %10d\n", "-/+ buffers/cache:", $minus, $plus);
}
printf("%-6s %11d %10d %10d\n", "Swap:", $swaptotal, $swapused, $swapfree);
if($total) {
printf("%-6s %11d %10d %10d\n", "Total:", $memtotal + $swaptotal,
$memused+$swapused, $memfree+$swapfree);
}
if($countflag) {
$count -= 1;
if($count <= 0) {
$sleep = 0;
}
}
if($sleep) {
select(undef, undef, undef, $sleep); # use "select" for "usleep".
print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
}
} while ($sleep);
sub usage {
print "$script version $myvesion\n";
print $usage;
exit;
}
sub version {
print "$script version $myvesion\n";
exit;
}
sub sizeshift {
my ($size, $shift) = @_;
return (($size << 10) >> $shift);
}
### $byteshift
### $memtotal
### $memfree
### $membuf
### $memcached