def _resources_via_proc(pid): """ Fetches resource usage information about a given process via proc. This returns a tuple of the form... (total_cpu_time, uptime, memory_in_bytes, memory_in_percent) :param int pid: process to be queried :returns: **tuple** with the resource usage information :raises: **IOError** if unsuccessful """ utime, stime, start_time = proc.stats( pid, proc.Stat.CPU_UTIME, proc.Stat.CPU_STIME, proc.Stat.START_TIME, ) total_cpu_time = float(utime) + float(stime) memory_in_bytes = proc.memory_usage(pid)[0] total_memory = proc.physical_memory() uptime = time.time() - float(start_time) memory_in_percent = float(memory_in_bytes) / total_memory return (total_cpu_time, uptime, memory_in_bytes, memory_in_percent)
def test_physical_memory(self, get_line_mock): """ Tests the physical_memory function. """ get_line_mock.side_effect = lambda *params: { ('/proc/meminfo', 'MemTotal:', 'system physical memory'): 'MemTotal: 12345 kB', }[params] self.assertEqual((12345 * 1024), proc.physical_memory())