Skip to content

Instantly share code, notes, and snippets.

@robsonpeixoto
Forked from gbin/ext4-inlinedata-calculator.py
Last active December 20, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsonpeixoto/6215363 to your computer and use it in GitHub Desktop.
Save robsonpeixoto/6215363 to your computer and use it in GitHub Desktop.
python 2.6 support
#
# This script is a tool that helps you calculate the benefits in occupied size on disk
# of the ext4 inlinedata new feature of the Linux kernel 3.8.0 see http://www.h-online.com/open/features/What-s-new-in-Linux-3-8-1804240.html
#
# Just run it on your ext4 mountpoints and it will tell give you the trade off for all your files depending on the inode size you # choose.
#
# To get you current inode size you can do :
# $ tune2fs -l /dev/mapper/isw_ddbeejgcgd_Volume03 | grep Inode
# Inode count: 15040512
# Inodes per group: 8192
# Inode blocks per group: 512
# Inode size: 256
#
# You can set the inode size at creation time with :
# mkfs.ext4 -I inode-size /dev/...
#
#!/usr/bin/env python
import os
from sys import argv
import string
def find_mount_point(path):
path = os.path.abspath(path)
while not os.path.ismount(path):
path = os.path.dirname(path)
return path
ext4mountedpaths = []
with open('/etc/mtab') as f:
for line in f.readlines():
splitted = string.split(line)
if splitted[2] == 'ext4':
ext4mountedpaths.append(splitted[1])
TYPICAL_OCCUPIED_SPACE_FOR_INODE = 124
EXPLORE_INODES_SIZES = (256, 512, 1024, 2048, 4096)
if len(argv) != 2:
print("Syntax : ext4-inlinedata-calculator.py mountpoint")
exit(-1)
print('Exploring {0} ...'.format(argv[1]))
allfiles = {}
for root, subFolders, files in os.walk(argv[1]):
for entry in files:
try:
filename = os.path.join(root, entry)
if find_mount_point(filename) not in ext4mountedpaths:
continue # only consider files from a mounted ext4 filesystems
allfiles[filename] = os.path.getsize(filename) if os.path.isfile(filename) else 0 # count as an empty file everything entry not beeing a real file
if not (len(allfiles) % 100000):
print('Read {0} files ...'.format(len(allfiles)))
except OSError:
continue # probably a systemfile
results = {}
for inode_size in EXPLORE_INODES_SIZES:
results[inode_size] = [0, 0]
total_occupied_space = 0
for _, filesize in allfiles.items():
total_occupied_space += filesize
for size in EXPLORE_INODES_SIZES:
if filesize <= size - TYPICAL_OCCUPIED_SPACE_FOR_INODE:
results[size][0] += 1
results[size][1] += size - TYPICAL_OCCUPIED_SPACE_FOR_INODE - filesize
else:
results[size][1] += TYPICAL_OCCUPIED_SPACE_FOR_INODE # the full empty space is wasted
if total_occupied_space == 0:
print("No relevant file found")
exit(-2)
print("""
== Results ==
Total file size {0:}
Inode size\t\tfiles fit in\t\twasted inode space""".format(total_occupied_space))
total_nb = len(allfiles)
for size in EXPLORE_INODES_SIZES:
nb, wasted = results[size]
print('{0}\t\t\t{1:>7.2%}\t\t\t{2:>15.8%}'.format(size, nb / float(total_nb), float(wasted) / float(total_occupied_space)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment