Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ploop_fragmentation_checker.py
#!/usr/bin/python
###
### Author pavel.odintsov at gmail.com
###
### This script can find non initilized blocks in ploop and detect wasted space due to cluster size.
### http://en.wikipedia.org/wiki/Fragmentation_%28computing%29
###
# TODO count only continius zero blocks as "wasted space"
import sys
zero_bytes = 0
non_zero_bytes = 0
CHUNKSIZE = 1024 * 1024
def main(ploop_file_path):
file = open(ploop_file_path, "rb")
try:
bytes_read = file.read(CHUNKSIZE)
while bytes_read:
process_chunk(bytes_read)
bytes_read = file.read(CHUNKSIZE)
finally:
file.close()
print "We count %d bytes" % (zero_bytes + non_zero_bytes)
print "We count %d zero bytes" % zero_bytes
print "We count %d non zero bytes" % non_zero_bytes
print "We have %f %% of space lost due to ploop fragmentation" % (float(zero_bytes) / (float(zero_bytes) + float(non_zero_bytes)) * 100)
def process_chunk(bytes_array):
global zero_bytes
global non_zero_bytes
global CHUNKSIZE
zero_bytes_in_chank = 0
non_zero_bytes_in_chank = 0
for byte in bytes_array:
if byte == '\x00':
zero_bytes_in_chank += 1
else:
non_zero_bytes_in_chank += 1
#zero_percent = float(zero_bytes_in_chank) / float(CHUNKSIZE) * 100
#print "We have %d zero and %d non zero bytes in this chank from %d bytes" % (zero_bytes_in_chank, non_zero_bytes_in_chank, CHUNKSIZE)
#print "Fragmentation level for this chank is: %.1f" % zero_percent
zero_bytes += zero_bytes_in_chank
non_zero_bytes += non_zero_bytes_in_chank
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Please specify path to ploop file"
sys.exit(1)
ploop_file_path = sys.argv[1]
main(ploop_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.