Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Unified Diff: tools/linux/procfs.py

Issue 108073002: Error handling and refactoring in tools/linux/procfs.py, and rename multi-process-rss.py. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/multi-process-rss.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/linux/procfs.py
diff --git a/tools/linux/procfs.py b/tools/linux/procfs.py
index 6308fdd93da7a14ad279edd4be38630cd81b6d1c..8b0574411d9fbcfa4104dd2e951d6ded7e1839cd 100755
--- a/tools/linux/procfs.py
+++ b/tools/linux/procfs.py
@@ -92,8 +92,11 @@ class ProcStat(object):
@staticmethod
def load(pid):
- with open(os.path.join('/proc', str(pid), 'stat'), 'r') as stat_f:
- return ProcStat.load_file(stat_f)
+ try:
+ with open(os.path.join('/proc', str(pid), 'stat'), 'r') as stat_f:
+ return ProcStat.load_file(stat_f)
+ except IOError:
+ return None
@property
def raw(self):
@@ -135,7 +138,10 @@ class ProcStatm(object):
@staticmethod
def load_file(statm_f):
- raw = statm_f.readlines()
+ try:
+ raw = statm_f.readlines()
+ except (IOError, OSError):
peria 2013/12/09 05:08:13 You use two styles to catch multiple-type exceptio
+ return None
statm = ProcStatm._PATTERN.match(raw[0])
return ProcStatm(raw,
statm.groupdict().get('SIZE'),
@@ -148,8 +154,11 @@ class ProcStatm(object):
@staticmethod
def load(pid):
- with open(os.path.join('/proc', str(pid), 'statm'), 'r') as statm_f:
- return ProcStatm.load_file(statm_f)
+ try:
+ with open(os.path.join('/proc', str(pid), 'statm'), 'r') as statm_f:
+ return ProcStatm.load_file(statm_f)
+ except (IOError, OSError):
+ return None
@property
def raw(self):
@@ -330,8 +339,13 @@ class ProcMaps(object):
@staticmethod
def load(pid):
- with open(os.path.join('/proc', str(pid), 'maps'), 'r') as maps_f:
- return ProcMaps.load_file(maps_f)
+ try:
+ with open(os.path.join('/proc', str(pid), 'maps'), 'r') as maps_f:
+ return ProcMaps.load_file(maps_f)
+ except IOError:
+ return None
+ except OSError:
+ return None
def append_line(self, line):
entry = self.parse_line(line)
@@ -555,8 +569,13 @@ class ProcPagemap(object):
vma_internals = collections.OrderedDict()
process_pageframe_set = set()
- pagemap_fd = os.open(
- os.path.join('/proc', str(pid), 'pagemap'), os.O_RDONLY)
+ try:
+ pagemap_fd = os.open(
+ os.path.join('/proc', str(pid), 'pagemap'), os.O_RDONLY)
+ except IOError:
+ return None
+ except OSError:
+ return None
for vma in maps:
present = 0
swapped = 0
@@ -564,8 +583,13 @@ class ProcPagemap(object):
pageframes = collections.defaultdict(int)
begin_offset = ProcPagemap._offset(vma.begin)
chunk_size = ProcPagemap._offset(vma.end) - begin_offset
- os.lseek(pagemap_fd, begin_offset, os.SEEK_SET)
- buf = os.read(pagemap_fd, chunk_size)
+ try:
+ os.lseek(pagemap_fd, begin_offset, os.SEEK_SET)
+ buf = os.read(pagemap_fd, chunk_size)
+ except IOError:
+ return None
+ except OSError:
+ return None
if len(buf) < chunk_size:
_LOGGER.warn('Failed to read pagemap at 0x%x in %d.' % (vma.begin, pid))
pagemap_values = struct.unpack(
@@ -586,7 +610,10 @@ class ProcPagemap(object):
total_present += present
total_swapped += swapped
total_vsize += vsize
- os.close(pagemap_fd)
+ try:
+ os.close(pagemap_fd)
+ except OSError:
+ return None
return ProcPagemap(total_vsize, total_present, total_swapped,
vma_internals, in_process_dup)
« no previous file with comments | « no previous file | tools/multi-process-rss.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698