Created
July 23, 2010 00:46
-
-
Save cdleary/486853 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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