-
Notifications
You must be signed in to change notification settings - Fork 2
/
zfs_frag.py
67 lines (58 loc) · 2.9 KB
/
zfs_frag.py
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
#!/usr/bin/env python3
import sys,re
from collections import defaultdict
import operator
file_total_blocks={};
file_fragmented_blocks={};
file_not_fragmented_blocks={};
file_total_blocks = defaultdict(lambda:0,file_total_blocks)
file_fragmented_blocks=defaultdict(lambda:0,file_fragmented_blocks)
file_not_fragmented_blocks=defaultdict(lambda:0,file_not_fragmented_blocks)
file_number=0
next_block=0
filename=''
for line in open(sys.argv[1]):
#print line
matchObj = re.match( r'\tpath\t(.*)', line)
if matchObj:
filename= matchObj.group(1)
file_total_blocks[filename]=0;
file_fragmented_blocks[filename]=0;
file_not_fragmented_blocks[filename]=0;
#print filename
matchObj = re.match( r'Indirect blocks', line)
if matchObj:
file_number+=1
next_block = 0
matchObj = re.match( r'.*L0\s+.*:(.*):(.*?)\s+.+',line)
if matchObj:
#print("@",matchObj.group(1),"@",matchObj.group(2),"@",line)
this_block=int(matchObj.group(1),16)
this_block_size=int(matchObj.group(2),16)
file_total_blocks[filename]+=1
if next_block != 0:
#print(next_block,this_block)
if next_block == this_block:
file_not_fragmented_blocks[filename]+=1
#print('not fragmented')
else:
file_fragmented_blocks[filename]+=1
#print('fragmented')
next_block=this_block+this_block_size
print('There are {} files.'.format( file_number ))
fragmented=sum(file_fragmented_blocks.values())
not_fragmented=sum(file_not_fragmented_blocks.values())
total_fragment_blocks = fragmented + not_fragmented
print("There are {} blocks and {} fragment blocks.".format(sum(file_total_blocks.values()), total_fragment_blocks ))
print("There are {} fragmented blocks {:.2%}.".format(fragmented, float(fragmented)/total_fragment_blocks ))
print("There are {} contiguous blocks {:.2%}.".format(not_fragmented, float(not_fragmented)/total_fragment_blocks ))
fragmented_files={}
for filename in file_total_blocks:
if file_total_blocks[filename]>1 and file_fragmented_blocks[filename]>0:
#print(filename,":",file_fragmented_blocks[filename]+file_not_fragmented_blocks[filename],":",file_fragmented_blocks[filename])
fragmented_files[filename]=(file_fragmented_blocks[filename]+file_not_fragmented_blocks[filename],file_not_fragmented_blocks[filename])
sorted_fragmented_files = sorted(fragmented_files.items(), key=operator.itemgetter(1))
for item in sorted_fragmented_files:
if item[0]=='':
continue
print("Name: {} Blocks: {} Fragmentation {:.2%}".format(item[0],item[1][0],float(item[1][0]-item[1][1])/item[1][0]))