Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b92321

Browse files
authored
major rework introducing snapshot class
Introduce snapshot class to facilitate calculations. Calculate also the size of file extents that where added compared to the previous snapshot.
1 parent 91adbda commit 4b92321

File tree

1 file changed

+85
-53
lines changed

1 file changed

+85
-53
lines changed

‎subvolume.py‎

Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,51 @@
1919
import btrfs
2020
import os
2121
import sys
22-
from collections import OrderedDict
22+
23+
def sizeof_fmt(num, suffix='B'):
24+
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
25+
if abs(num) < 1024.0:
26+
return "%3.1f%s%s" % (num, unit, suffix)
27+
num /= 1024.0
28+
return "%.1f%s%s" % (num, 'Yi', suffix)
29+
30+
31+
class Snapshot:
32+
def __init__(self, objectid):
33+
self._objectid = objectid
34+
self._blocks=set()
35+
36+
@property
37+
def objectid(self):
38+
return self._objectid
39+
40+
@property
41+
def blocks(self):
42+
return self._blocks
43+
44+
def add(self,objectid,offset,size):
45+
pair=(objectid,offset,size)
46+
self._blocks.add(pair)
47+
48+
def _setblocks(self,data):
49+
self._blocks=data
50+
51+
def __sub__(self,other):
52+
leftover_blocks=self.blocks-other.blocks
53+
newsnapshot=Snapshot(self.objectid)
54+
newsnapshot._setblocks(leftover_blocks)
55+
return newsnapshot
56+
57+
@property
58+
def size(self):
59+
sum=0
60+
for extent in self.blocks:
61+
sum+=extent[2]
62+
return sum
63+
64+
def __str__(self):
65+
return '{:>10} {:>8}'.format(self.objectid,sizeof_fmt(self.size))
66+
2367

2468
#path of btrfs filesystem
2569
path = sys.argv[1]
@@ -29,66 +73,54 @@
2973
ignored_trees=set()
3074

3175
for item in sys.argv[2:]:
32-
ignored_trees.add(int(item))
76+
ignored_trees.add(int(item))
3377

3478
fs = btrfs.FileSystem(path)
35-
subvolume_extent_dictionary=OrderedDict()
79+
subvolume_list=[]
3680

3781
#iterate all subvolumes
3882

3983
for subvol in fs.subvolumes():
40-
tree = subvol.key.objectid
41-
if tree in ignored_trees:
42-
continue
43-
#print(subvol)
44-
#print(tree)
45-
extent_set=set()
46-
#search in this subvolume all file extents
47-
for header, data in btrfs.ioctl.search_v2(fs.fd, tree):
48-
if header.type == btrfs.ctree.EXTENT_DATA_KEY:
49-
datum=btrfs.ctree.FileExtentItem(header,data)
50-
#print(datum)
51-
#ignore inline file extents, they are small
52-
if datum.type != btrfs.ctree.FILE_EXTENT_INLINE:
53-
pair=(header.objectid,datum.logical_offset,datum.disk_num_bytes)
54-
#print(pair)
55-
#print(datum)
56-
extent_set.add(pair)
57-
#print(extent_set)
58-
subvolume_extent_dictionary[tree]=extent_set
59-
60-
#print(subvolume_extent_dictionary)
84+
tree = subvol.key.objectid
85+
if tree in ignored_trees:
86+
continue
87+
snapshot=Snapshot(tree)
88+
#search in this subvolume all file extents
89+
for header, data in btrfs.ioctl.search_v2(fs.fd, tree):
90+
if header.type == btrfs.ctree.EXTENT_DATA_KEY:
91+
datum=btrfs.ctree.FileExtentItem(header,data)
92+
#print(datum)
93+
#ignore inline file extents, they are small
94+
if datum.type != btrfs.ctree.FILE_EXTENT_INLINE:
95+
snapshot.add(header.objectid,datum.logical_offset,datum.disk_num_bytes)
96+
subvolume_list.append(snapshot)
6197

6298
#make sure that subvolume order is from newest (current) to oldest
6399

64-
trees=list(subvolume_extent_dictionary.keys())
65-
current_tree=trees[0]
66-
del trees[0]
67-
trees.append(current_tree)
68-
trees.reverse()
69-
#print(trees)
70-
#sys.exit(0)
100+
current_subvolume=subvolume_list[0]
101+
del subvolume_list[0]
102+
subvolume_list.append(current_subvolume)
103+
subvolume_list.reverse()
71104

72105
#parse list of subvolumes and calculate the number of the unique file extents in this subvolume
73-
subvolume_extent_dictionary_sizes=OrderedDict()
74-
for index,snapshot in enumerate(trees):
75-
previous_snapshot = trees[index-1]
76-
if index==0:
77-
previous_snapshot = None
78-
try:
79-
next_snapshot = trees[index+1]
80-
except:
81-
next_snapshot = None
82-
#print(index,snapshot,previous_snapshot,next_snapshot)
83-
if previous_snapshot != None and next_snapshot != None :
84-
subvolume_extent_dictionary_sizes[snapshot]= subvolume_extent_dictionary[snapshot] - subvolume_extent_dictionary[previous_snapshot] - subvolume_extent_dictionary[next_snapshot]
85-
#subvolume_extent_dictionary_sizes[snapshot].union(subvolume_extent_dictionary[snapshot] - subvolume_extent_dictionary[next_snapshot])
86-
elif previous_snapshot == None:
87-
subvolume_extent_dictionary_sizes[snapshot]=subvolume_extent_dictionary[snapshot] - subvolume_extent_dictionary[next_snapshot]
88-
else:
89-
subvolume_extent_dictionary_sizes[snapshot]= subvolume_extent_dictionary[snapshot] - subvolume_extent_dictionary[previous_snapshot]
90-
#calculate the sum of unique file extents for each subvolume
91-
sum=0
92-
for extent in subvolume_extent_dictionary_sizes[snapshot]:
93-
sum+=extent[2]
94-
print(snapshot,sum,len(subvolume_extent_dictionary_sizes[snapshot]))
106+
#calculate also how much data were changed, compared to the older subvolume
107+
108+
#next_snapshot is actually the older snapshot
109+
for index,snapshot in enumerate(subvolume_list):
110+
previous_snapshot = subvolume_list[index-1]
111+
if index==0:
112+
previous_snapshot = None
113+
try:
114+
next_snapshot = subvolume_list[index+1]
115+
except:
116+
next_snapshot = None
117+
#print(index,snapshot,previous_snapshot,next_snapshot)
118+
if previous_snapshot != None and next_snapshot != None :
119+
diff_snapshot=snapshot - next_snapshot
120+
unique_snapshot=diff_snapshot-previous_snapshot
121+
elif previous_snapshot == None:
122+
diff_snapshot = unique_snapshot = snapshot - next_snapshot
123+
else:
124+
diff_snapshot=snapshot
125+
unique_snapshot=diff_snapshot-previous_snapshot
126+
print(unique_snapshot,diff_snapshot)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /