我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用psutil.cpu_stats()。
def getLoadAverage(): if linux: import multiprocessing k = 1.0 k /= multiprocessing.cpu_count() if os.path.exists('/proc/loadavg'): return [float(open('/proc/loadavg').read().split()[x]) * k for x in range(3)] else: tokens = subprocess.check_output(['uptime']).split() return [float(x.strip(',')) * k for x in tokens[-3:]] if mswindows: # TODO(Guodong Ding) get this field data like on Linux for Windows # print psutil.cpu_percent() # print psutil.cpu_times_percent() # print psutil.cpu_times() # print psutil.cpu_stats() return "%.2f%%" % psutil.cpu_percent()
def test_cpu_stats(self): self.execute(psutil.cpu_stats) # --- mem
def test_ctx_switches(self): vmstat_value = vmstat("context switches") psutil_value = psutil.cpu_stats().ctx_switches self.assertAlmostEqual(vmstat_value, psutil_value, delta=500)
def test_interrupts(self): vmstat_value = vmstat("interrupts") psutil_value = psutil.cpu_stats().interrupts self.assertAlmostEqual(vmstat_value, psutil_value, delta=500) # ===================================================================== # system network # =====================================================================
def test_cpu_stats(self): # Tested more extensively in per-platform test modules. infos = psutil.cpu_stats() for name in infos._fields: value = getattr(infos, name) self.assertGreaterEqual(value, 0) if name in ('ctx_switches', 'interrupts'): self.assertGreater(value, 0)
def test_cpu_stats_ctx_switches(self): self.assertAlmostEqual(psutil.cpu_stats().ctx_switches, sysctl('vm.stats.sys.v_swtch'), delta=1000)
def test_cpu_stats_soft_interrupts(self): self.assertAlmostEqual(psutil.cpu_stats().soft_interrupts, sysctl('vm.stats.sys.v_soft'), delta=1000)
def test_cpu_stats_syscalls(self): self.assertAlmostEqual(psutil.cpu_stats().syscalls, sysctl('vm.stats.sys.v_syscall'), delta=1000) # def test_cpu_stats_traps(self): # self.assertAlmostEqual(psutil.cpu_stats().traps, # sysctl('vm.stats.sys.v_trap'), delta=1000) # --- others
def test_cpu_stats_interrupts(self): with open('/proc/stat', 'rb') as f: for line in f: if line.startswith(b'intr'): interrupts = int(line.split()[1]) break else: raise ValueError("couldn't find line") self.assertAlmostEqual( psutil.cpu_stats().interrupts, interrupts, delta=1000)
def test_cpu_stats_ctx_switches(self): with open('/proc/stat', 'rb') as f: for line in f: if line.startswith(b'ctxt'): ctx_switches = int(line.split()[1]) break else: raise ValueError("couldn't find line") self.assertAlmostEqual( psutil.cpu_stats().ctx_switches, ctx_switches, delta=1000)
def stats(): return psutil.cpu_stats()
def test_cpu_stats(self): self.execute(psutil.cpu_stats)
def test_interrupts(self): vmstat_value = vmstat("interrupts") psutil_value = psutil.cpu_stats().interrupts self.assertAlmostEqual(vmstat_value, psutil_value, delta=500) # ===================================================================== # --- system network # =====================================================================
def f_print_linux_status(save_as): ###????################################################################### #scputimes(user=, nice, system, idle, iowait, irq, softirq,steal, guest, guest_nice) cpu_times = psutil.cpu_times() #scpustats(ctx_switches, interrupts, soft_interrupts, syscalls) #cpu_stats = psutil.cpu_stats() # svmem(total , available, percent, used , free, active, inactive, buffers, cached, shared) mem = psutil.virtual_memory() # sswap(total, used, free, percent, sin, sout) swap = psutil.swap_memory() #sdiskusage(total, used, free, percent) #disk_usage = psutil.disk_usage('/') #sdiskio(read_count, write_count, read_bytes, write_bytes, read_time, write_time) #disk_io_counters = psutil.disk_io_counters() #snetio(bytes_sent, bytes_recv, packets_sent, packets_recv, errin, errout, dropin, dropout) #net = psutil.net_io_counters() #load try: load = os.getloadavg() except (OSError, AttributeError): stats = {} else: stats = {'min1': load[0], 'min5': load[1], 'min15': load[2]} #Uptime = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") ###????################################################################### style1 = {1: ' ,6,l', 2: ' ,10,r',3: ' ,6,l', 4: ' ,10,r',5: ' ,6,l', 6: ' ,6,r',7: ' ,8,l',8: ' ,6,r',9: ' ,6,l', 10: ' ,6,r',11: ' ,6,l', 12: ' ,5,r',} style = {1: ' ,l', 2: ' ,r',3: ' ,l', 4: ' ,r',5: ' ,l', 6: ' ,r',7: ' ,l',8: ' ,r',9: ' ,l', 10: ' ,r',11: ' ,l', 12: ' ,r',} rows=[ ["CPU", str(psutil.cpu_percent(interval=1))+'%',"nice", cpu_times.nice,"MEM", str(mem.percent) + '%',"active", str(mem.active/1024/1024) + 'M',"SWAP", str(swap.percent)+'%',"LOAD", str(psutil.cpu_count())+'core'], ["user", cpu_times.user,"irq", cpu_times.irq,"total", str(mem.total/1024/1024)+'M',"inactive", str(mem.inactive/1024/1024) + 'M',"total", str(swap.total/1024/1024) + 'M',"1 min", stats["min1"]], ["system", cpu_times.system,"iowait", cpu_times.iowait,"used", str(mem.used/1024/1024)+'M',"buffers", str(mem.buffers/1024/1024) + 'M',"used", str(swap.used/1024/1024) + 'M',"5 min", stats["min5"]], ["idle", cpu_times.idle,"steal", cpu_times.steal,"free", str(mem.free/1024/1024) + 'M',"cached", str(mem.cached/1024/1024) + 'M',"free", str(swap.free/1024/1024) + 'M',"15 min", stats["min15"]] ] title = "Linux Overview" if save_as == "txt": f_print_title(title) f_print_table_body(rows, style1,' ') elif save_as == "html": f_print_table_html(rows, title, style)