Skip to content

@cdleary /firefrag.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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
Something went wrong with that request. Please try again.