forked from JeremyGrosser/fincore
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpython_fincore
More file actions
executable file
·134 lines (109 loc) · 4.36 KB
/
python_fincore
File metadata and controls
executable file
·134 lines (109 loc) · 4.36 KB
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
#!/usr/bin/env python
import sys
import ftools
import resource
import optparse
import os
import stat
def convert_bytes(bytes):
bytes = float(bytes)
if bytes >= 1099511627776:
terabytes = bytes / 1099511627776
size = '%.2fT' % terabytes
elif bytes >= 1073741824:
gigabytes = bytes / 1073741824
size = '%.2fG' % gigabytes
elif bytes >= 1048576:
megabytes = bytes / 1048576
size = '%.2fM' % megabytes
elif bytes >= 1024:
kilobytes = bytes / 1024
size = '%.2fK' % kilobytes
else:
size = '%.2fb' % bytes
return size
def matrix_to_string(matrix, header):
"""
Note: this function is from: http://mybravenewworld.wordpress.com/2010/09/19/print-tabular-data-nicely-using-python/
i modified it a bit. ;-)
Return a pretty, aligned string representation
of a nxm matrix.
This representation can be used to print any
tabular data, such as database results. It
works by scanning the lengths of each element
in each column, and determining the format
string dynamically.
@param matrix: Matrix representation (list with n rows
of m elements).
@param header: Optional tuple with header elements to be
displayed.
"""
lengths = []
matrix = [header] + matrix
for row in matrix:
for column in row:
i = row.index(column)
cl = len(str(column))
try:
ml = lengths[i]
if cl > ml:
lengths[i] = cl
except IndexError:
lengths.append(cl)
lengths = tuple(lengths)
format_string = ""
for length in lengths:
format_string += "%-" + str(length) + "s "
format_string += "\n"
matrix_str = ""
#matrix_str += format_string % header
for row in matrix:
matrix_str += format_string % tuple(row)
return matrix_str
def main():
parser = optparse.OptionParser(description="Determine how much of a file is in filesystem page-cache.")
parser.add_option('-d', '--directory', dest='directory', default=None,
help="Recursively descend into a directory")
parser.add_option('-s', '--summary', dest='summary', default=False, action='store_true',
help="Summarize report")
options, args = parser.parse_args()
if len(args) == 0 and options.directory == None:
parser.print_help()
return 1
page_size = resource.getpagesize()
header = ['filename', 'file size', 'total pages', 'pages cached', 'cached size', 'percentage cached']
rows = []
if options.directory:
for (path, dirs, files) in os.walk(options.directory):
for myfile in files:
f = os.path.join(path,myfile)
if os.path.isfile(f):
fd = file(f,'r')
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
if file_size == 0:
fd.close()
continue
pages_cached, pages_total = ftools.fincore_ratio(fd.fileno())
fd.close()
rows.append([f, file_size, pages_total, pages_cached, (pages_cached * page_size), (float(pages_cached) / float(pages_total)) * 100.0])
for f in args:
fd = file(f, 'r')
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
if file_size == 0:
fd.close()
continue
pages_cached, pages_total = ftools.fincore_ratio(fd.fileno())
fd.close()
rows.append([f, file_size, pages_total, pages_cached, (pages_cached * page_size), (float(pages_cached) / float(pages_total)) * 100.0])
rows = sorted(rows, key=lambda t:t[5], reverse=True)
result = matrix_to_string(rows, header)
print result
if options.summary:
file_size_total, pages_total, pages_cached_total, pages_cached_size_total = map(sum, zip(*rows)[1:-1])
sum_header = ['total file sizes','total file pages', 'total pages cached', 'total cached pages size', 'total percentage cached']
total_percentage_cached = (float(pages_cached_total) / float(pages_total)) * 100.0
summary_row = [convert_bytes(file_size_total), pages_total, pages_cached_total, convert_bytes(pages_cached_size_total), total_percentage_cached]
summary_line = matrix_to_string([summary_row], sum_header)
print summary_line
if __name__ == '__main__':
main()