| Index: third_party/psutil/psutil/_pslinux.py
|
| diff --git a/third_party/psutil/psutil/_pslinux.py b/third_party/psutil/psutil/_pslinux.py
|
| old mode 100644
|
| new mode 100755
|
| index 443b6ade9d2a9744778cc30e9ac55cc2d5da6658..ffe043d617afab6adfc5f4213caea7a997df9534
|
| --- a/third_party/psutil/psutil/_pslinux.py
|
| +++ b/third_party/psutil/psutil/_pslinux.py
|
| @@ -1,6 +1,6 @@
|
| #!/usr/bin/env python
|
| #
|
| -# $Id: _pslinux.py 1142 2011-10-05 18:45:49Z g.rodola $
|
| +# $Id: _pslinux.py 1173 2011-10-19 19:29:46Z g.rodola $
|
| #
|
| # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
|
| # Use of this source code is governed by a BSD-style license that can be
|
| @@ -41,18 +41,26 @@ def _get_boot_time():
|
|
|
| def _get_num_cpus():
|
| """Return the number of CPUs on the system"""
|
| - num = 0
|
| - f = open('/proc/cpuinfo', 'r')
|
| + # we try to determine num CPUs by using different approaches.
|
| + # SC_NPROCESSORS_ONLN seems to be the safer and it is also
|
| + # used by multiprocessing module
|
| try:
|
| - lines = f.readlines()
|
| - finally:
|
| - f.close()
|
| - for line in lines:
|
| - if line.lower().startswith('processor'):
|
| - num += 1
|
| + return os.sysconf("SC_NPROCESSORS_ONLN")
|
| + except ValueError:
|
| + # as a second fallback we try to parse /proc/cpuinfo
|
| + num = 0
|
| + f = open('/proc/cpuinfo', 'r')
|
| + try:
|
| + lines = f.readlines()
|
| + finally:
|
| + f.close()
|
| + for line in lines:
|
| + if line.lower().startswith('processor'):
|
| + num += 1
|
|
|
| # unknown format (e.g. amrel/sparc architectures), see:
|
| # http://code.google.com/p/psutil/issues/detail?id=200
|
| + # try to parse /proc/stat as a last resort
|
| if num == 0:
|
| f = open('/proc/stat', 'r')
|
| try:
|
| @@ -115,37 +123,20 @@ def phymem_buffers():
|
| kernel in bytes.
|
| This reflects the "buffers" column of free command line utility.
|
| """
|
| - f = open('/proc/meminfo', 'r')
|
| - try:
|
| - for line in f:
|
| - if line.startswith('Buffers:'):
|
| - return int(line.split()[1]) * 1024
|
| - raise RuntimeError("line not found")
|
| - finally:
|
| - f.close()
|
| + total, free, buffers = _psutil_linux.get_physmem()
|
| + return buffers
|
|
|
| def phymem_usage():
|
| + """Return physical memory usage statistics as a namedutple including
|
| + tota, used, free and percent usage.
|
| + """
|
| # total, used and free values are matched against free cmdline utility
|
| # the percentage matches top/htop and gnome-system-monitor
|
| - f = open('/proc/meminfo', 'r')
|
| - try:
|
| - total = free = buffers = cached = None
|
| - for line in f:
|
| - if line.startswith('MemTotal:'):
|
| - total = int(line.split()[1]) * 1024
|
| - elif line.startswith('MemFree:'):
|
| - free = int(line.split()[1]) * 1024
|
| - elif line.startswith('Buffers:'):
|
| - buffers = int(line.split()[1]) * 1024
|
| - elif line.startswith('Cached:'):
|
| - cached = int(line.split()[1]) * 1024
|
| - break
|
| - used = total - free
|
| - percent = usage_percent(total - (free + buffers + cached), total,
|
| - _round=1)
|
| - return ntuple_sysmeminfo(total, used, free, percent)
|
| - finally:
|
| - f.close()
|
| + total, free, buffers = _psutil_linux.get_physmem()
|
| + cached = cached_phymem()
|
| + used = total - free
|
| + percent = usage_percent(total - (free + buffers + cached), total, _round=1)
|
| + return ntuple_sysmeminfo(total, used, free, percent)
|
|
|
|
|
| def virtmem_usage():
|
| @@ -607,7 +598,23 @@ class Process(object):
|
| return retlist
|
|
|
| @wrap_exceptions
|
| - def get_connections(self):
|
| + def get_connections(self, kind='inet'):
|
| + """Return connections opened by process as a list of namedtuples.
|
| + The kind parameter filters for connections that fit the following
|
| + criteria:
|
| +
|
| + Kind Value Number of connections using
|
| + inet IPv4 and IPv6
|
| + inet4 IPv4
|
| + inet6 IPv6
|
| + tcp TCP
|
| + tcp4 TCP over IPv4
|
| + tcp6 TCP over IPv6
|
| + udp UDP
|
| + udp4 UDP over IPv4
|
| + udp6 UDP over IPv6
|
| + all the sum of all the possible families and protocols
|
| + """
|
| inodes = {}
|
| # os.listdir() is gonna raise a lot of access denied
|
| # exceptions in case of unprivileged user; that's fine:
|
| @@ -626,41 +633,65 @@ class Process(object):
|
| # no connections for this process
|
| return []
|
|
|
| - def process(file, family, _type):
|
| + def process(file, family, type_):
|
| retlist = []
|
| - f = open(file)
|
| + try:
|
| + f = open(file, 'r')
|
| + except IOError, err:
|
| + # IPv6 not supported on this platform
|
| + if err.errno == errno.ENOENT and file.endswith('6'):
|
| + return []
|
| + else:
|
| + raise
|
| try:
|
| f.readline() # skip the first line
|
| for line in f:
|
| - _, laddr, raddr, status, _, _, _, _, _, inode = \
|
| - line.split()[:10]
|
| - if inode in inodes:
|
| - laddr = self._decode_address(laddr, family)
|
| - raddr = self._decode_address(raddr, family)
|
| - if _type == socket.SOCK_STREAM:
|
| - status = _TCP_STATES_TABLE[status]
|
| - else:
|
| - status = ""
|
| - fd = int(inodes[inode])
|
| - conn = ntuple_connection(fd, family, _type, laddr,
|
| - raddr, status)
|
| - retlist.append(conn)
|
| + # IPv4 / IPv6
|
| + if family in (socket.AF_INET, socket.AF_INET6):
|
| + _, laddr, raddr, status, _, _, _, _, _, inode = \
|
| + line.split()[:10]
|
| + if inode in inodes:
|
| + laddr = self._decode_address(laddr, family)
|
| + raddr = self._decode_address(raddr, family)
|
| + if type_ == socket.SOCK_STREAM:
|
| + status = _TCP_STATES_TABLE[status]
|
| + else:
|
| + status = ""
|
| + fd = int(inodes[inode])
|
| + conn = ntuple_connection(fd, family, type_, laddr,
|
| + raddr, status)
|
| + retlist.append(conn)
|
| + else:
|
| + raise ValueError(family)
|
| return retlist
|
| finally:
|
| f.close()
|
|
|
| - tcp4 = process("/proc/net/tcp", socket.AF_INET, socket.SOCK_STREAM)
|
| - udp4 = process("/proc/net/udp", socket.AF_INET, socket.SOCK_DGRAM)
|
| - try:
|
| - tcp6 = process("/proc/net/tcp6", socket.AF_INET6, socket.SOCK_STREAM)
|
| - udp6 = process("/proc/net/udp6", socket.AF_INET6, socket.SOCK_DGRAM)
|
| - except IOError, err:
|
| - if err.errno == errno.ENOENT:
|
| - # IPv6 is not supported on this platform
|
| - tcp6 = udp6 = []
|
| - else:
|
| - raise
|
| - return tcp4 + tcp6 + udp4 + udp6
|
| + tcp4 = ("tcp" , socket.AF_INET , socket.SOCK_STREAM)
|
| + tcp6 = ("tcp6", socket.AF_INET6, socket.SOCK_STREAM)
|
| + udp4 = ("udp" , socket.AF_INET , socket.SOCK_DGRAM)
|
| + udp6 = ("udp6", socket.AF_INET6, socket.SOCK_DGRAM)
|
| +
|
| + tmap = {
|
| + "all" : (tcp4, tcp6, udp4, udp6),
|
| + "tcp" : (tcp4, tcp6),
|
| + "tcp4" : (tcp4,),
|
| + "tcp6" : (tcp6,),
|
| + "udp" : (udp4, udp6),
|
| + "udp4" : (udp4,),
|
| + "udp6" : (udp6,),
|
| + "inet" : (tcp4, tcp6, udp4, udp6),
|
| + "inet4": (tcp4, udp4),
|
| + "inet6": (tcp6, udp6),
|
| + }
|
| + if kind not in tmap:
|
| + raise ValueError("invalid %r kind argument; choose between %s"
|
| + % (kind, ', '.join([repr(x) for x in tmap])))
|
| + ret = []
|
| + for f, family, type_ in tmap[kind]:
|
| + ret += process("/proc/net/%s" % f, family, type_)
|
| + return ret
|
| +
|
|
|
| # --- lsof implementation
|
| #
|
|
|