def swap_memory(): sin, sout = cext.swap_mem() # XXX # we are supposed to get total/free by doing so: # http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/ # usr/src/cmd/swap/swap.c # ...nevertheless I can't manage to obtain the same numbers as 'swap' # cmdline utility, so let's parse its output (sigh!) p = subprocess.Popen(['swap', '-l', '-k'], stdout=subprocess.PIPE) stdout, stderr = p.communicate() if PY3: stdout = stdout.decode(sys.stdout.encoding) if p.returncode != 0: raise RuntimeError("'swap -l -k' failed (retcode=%s)" % p.returncode) lines = stdout.strip().split('\n')[1:] if not lines: raise RuntimeError('no swap device(s) configured') total = free = 0 for line in lines: line = line.split() t, f = line[-2:] t = t.replace('K', '') f = f.replace('K', '') total += int(int(t) * 1024) free += int(int(f) * 1024) used = total - free percent = usage_percent(used, total, _round=1) return _common.sswap(total, used, free, percent, sin * PAGE_SIZE, sout * PAGE_SIZE)
def swap_memory(): _, _, _, _, total, free = cext.linux_sysinfo() used = total - free percent = usage_percent(used, total, _round=1) # get pgin/pgouts f = open("/proc/vmstat", "rb") SIN, SOUT = b('pswpin'), b('pswpout') sin = sout = None try: for line in f: # values are expressed in 4 kilo bytes, we want bytes instead if line.startswith(SIN): sin = int(line.split(b(' '))[1]) * 4 * 1024 elif line.startswith(SOUT): sout = int(line.split(b(' '))[1]) * 4 * 1024 if sin is not None and sout is not None: break else: # we might get here when dealing with exotic Linux flavors, see: # http://code.google.com/p/psutil/issues/detail?id=313 msg = "'sin' and 'sout' swap memory stats couldn't " \ "be determined and were set to 0" warnings.warn(msg, RuntimeWarning) sin = sout = 0 finally: f.close() return _common.sswap(total, used, free, percent, sin, sout)
def swap_memory(): _, _, _, _, total, free = cext.linux_sysinfo() used = total - free percent = usage_percent(used, total, _round=1) # get pgin/pgouts f = open("/proc/vmstat", "rb") SIN, SOUT = b('pswpin'), b('pswpout') sin = sout = None try: for line in f: # values are expressed in 4 kilo bytes, we want bytes instead if line.startswith(SIN): sin = int(line.split(b(' '))[1]) * 4 * 1024 elif line.startswith(SOUT): sout = int(line.split(b(' '))[1]) * 4 * 1024 if sin is not None and sout is not None: break else: # we might get here when dealing with exotic Linux flavors, see: # https://github.com/giampaolo/psutil/issues/313 msg = "'sin' and 'sout' swap memory stats couldn't " \ "be determined and were set to 0" warnings.warn(msg, RuntimeWarning) sin = sout = 0 finally: f.close() return _common.sswap(total, used, free, percent, sin, sout)
def swap_memory(): """Swap system memory as a (total, used, free, sin, sout) tuple.""" mem = cext.virtual_mem() total = mem[2] free = mem[3] used = total - free percent = usage_percent(used, total, _round=1) return _common.sswap(total, used, free, percent, 0, 0)
def swap_memory(): _, _, _, _, total, free = cext.linux_sysinfo() used = total - free percent = usage_percent(used, total, _round=1) # get pgin/pgouts with open("/proc/vmstat", "rb") as f: sin = sout = None for line in f: # values are expressed in 4 kilo bytes, we want bytes instead if line.startswith(b'pswpin'): sin = int(line.split(b' ')[1]) * 4 * 1024 elif line.startswith(b'pswpout'): sout = int(line.split(b' ')[1]) * 4 * 1024 if sin is not None and sout is not None: break else: # we might get here when dealing with exotic Linux flavors, see: # https://github.com/giampaolo/psutil/issues/313 msg = "'sin' and 'sout' swap memory stats couldn't " \ "be determined and were set to 0" warnings.warn(msg, RuntimeWarning) sin = sout = 0 return _common.sswap(total, used, free, percent, sin, sout)
def swap_memory(): """Swap system memory as a (total, used, free, sin, sout) tuple.""" total, used, free, sin, sout = cext.swap_mem() percent = usage_percent(used, total, _round=1) return _common.sswap(total, used, free, percent, sin, sout)
def swap_memory(): """System swap memory as (total, used, free, sin, sout) namedtuple.""" total, used, free, sin, sout = [x * PAGESIZE for x in cext.swap_mem()] percent = usage_percent(used, total, _round=1) return _common.sswap(total, used, free, percent, sin, sout)
class TestSwapDrivers(BaseTestCase): fake_swap = sswap(total=17099124736, used=10, free=17099124726, percent=0.0, sin=0, sout=0) scenarios = ( ("TotalSwapPuller", { "puller_factory": swap.TotalSwapPuller, "swap_patch": patch.object(psutil, "swap_memory", Mock(return_value=fake_swap)), "expected_data": Measurement(name="compute.node.swap.total", timestamp="2015-08-04T15:15:45.703542+00:00", unit="bytes", type_="gauge", value=17099124736, resource_id="test_node", host="test_node", resource_metadata={ "host": "test_node", "title": "swap_total" }) }), ("FreeSwapPuller", { "puller_factory": swap.FreeSwapPuller, "swap_patch": patch.object(psutil, "swap_memory", Mock(return_value=fake_swap)), "expected_data": Measurement(name="compute.node.swap.free", timestamp="2015-08-04T15:15:45.703542+00:00", unit="bytes", type_="gauge", value=17099124726, resource_id="test_node", host="test_node", resource_metadata={ "host": "test_node", "title": "swap_free" }) }), ("UsedSwapPuller", { "puller_factory": swap.UsedSwapPuller, "swap_patch": patch.object(psutil, "swap_memory", Mock(return_value=fake_swap)), "expected_data": Measurement(name="compute.node.swap.used", timestamp="2015-08-04T15:15:45.703542+00:00", unit="bytes", type_="gauge", value=10, resource_id="test_node", host="test_node", resource_metadata={ "host": "test_node", "title": "swap_used" }) }), ) @freeze_time("2015-08-04T15:15:45.703542+00:00") @patch.object(platform, "node", Mock(return_value="test_node")) def test_swap(self): data_puller = self.puller_factory( self.puller_factory.get_name(), self.puller_factory.get_default_probe_id(), self.puller_factory.get_default_interval(), ) with self.swap_patch: pulled_data = data_puller.do_pull() self.assertEqual( [measurement.as_dict() for measurement in pulled_data], [self.expected_data.as_dict()])