Skip to content

Instantly share code, notes, and snippets.

@cdleary
Created July 23, 2010 00:46
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 cdleary/486853 to your computer and use it in GitHub Desktop.
Save cdleary/486853 to your computer and use it in GitHub Desktop.
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment