public
Created

  • Download Gist
firefrag.py
Python
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
#!/usr/bin/env python
 
import collections
import os
import re
from subprocess import Popen, PIPE
 
 
LSOFItem = collections.namedtuple('LSOFItem', 'command pid user fd type device size node name'.split())
extent_regex = re.compile(r'(\d+) extents? found')
 
data = [] # (extents, name)
 
print 'Running lsof...'
lsof = Popen(['lsof'], stdout=PIPE)
lsof_data, _ = lsof.communicate()
print 'Done running lsof...'
for line in lsof_data.splitlines():
if not line.startswith('firefox'):
continue
try:
item = LSOFItem(*line.strip().split())
except TypeError:
print 'Bad line', line.strip()
continue
if item.type != 'REG':
continue
if not os.path.isfile(item.name):
continue
filefrag = Popen(['filefrag', item.name], stdout=PIPE)
stdout, _ = filefrag.communicate()
match = extent_regex.search(stdout)
if not match:
print 'Bad filefrag result:', stdout.strip()
continue
data.append((int(match.group(1)), item.name))
 
data.sort(reverse=True)
for extents, name in data:
print '%120s: %d' % (name, extents)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.