本文整理汇总了Python中psutil.cpu_times_percent方法的典型用法代码示例。如果您正苦于以下问题:Python psutil.cpu_times_percent方法的具体用法?Python psutil.cpu_times_percent怎么用?Python psutil.cpu_times_percent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil
的用法示例。
在下文中一共展示了psutil.cpu_times_percent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cpu_times_percent
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def cpu_times_percent():
c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.cpu')
while True:
value = psutil.cpu_percent(interval=1)
c.gauge('system_wide.percent', value)
cpu_t_percent = psutil.cpu_times_percent(interval=1)
c.gauge('system_wide.times_percent.user', cpu_t_percent.user)
c.gauge('system_wide.times_percent.nice', cpu_t_percent.nice)
c.gauge('system_wide.times_percent.system', cpu_t_percent.system)
c.gauge('system_wide.times_percent.idle', cpu_t_percent.idle)
c.gauge('system_wide.times_percent.iowait', cpu_t_percent.iowait)
c.gauge('system_wide.times_percent.irq', cpu_t_percent.irq)
c.gauge('system_wide.times_percent.softirq', cpu_t_percent.softirq)
c.gauge('system_wide.times_percent.steal', cpu_t_percent.steal)
c.gauge('system_wide.times_percent.guest', cpu_t_percent.guest)
c.gauge('system_wide.times_percent.guest_nice', cpu_t_percent.guest_nice)
time.sleep(GRANULARITY)
示例2: OSinfo
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def OSinfo():
'''操作系统基本信息查看'''
core_number = psutil.cpu_count()
cpu_number = psutil.cpu_count(logical=True)
cpu_usage_precent = psutil.cpu_times_percent()
mem_info = psutil.virtual_memory()
result = {
"memtotal": mem_info[0],
"memavail": mem_info[1],
"memprecn": mem_info[2],
"memusage": mem_info[3],
"memfreed": mem_info[4],
}
print '''
内核版本 : %s
CORE数量 : %s
CPU数量 : %s
CPU使用率 : %s
内存总量 : %s
内存使用率 : %s
'''%(str(platform.platform()),str(core_number),str(cpu_number),str(cpu_usage_precent),str(mem_info[0]),str(mem_info[2]))
示例3: crawl
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def crawl(self, **kwargs):
logger.debug('Crawling %s' % (self.get_feature()))
for (idx, cpu) in enumerate(psutil.cpu_times_percent(percpu=True)):
feature_attributes = CpuFeature(
cpu.idle,
cpu.nice,
cpu.user,
cpu.iowait,
cpu.system,
cpu.irq,
cpu.steal,
100 - int(cpu.idle),
)
feature_key = '{0}-{1}'.format('cpu', idx)
yield (feature_key, feature_attributes, 'cpu')
示例4: test_serialization
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def test_serialization(self):
def check(ret):
if json is not None:
json.loads(json.dumps(ret))
a = pickle.dumps(ret)
b = pickle.loads(a)
self.assertEqual(ret, b)
check(psutil.Process().as_dict())
check(psutil.virtual_memory())
check(psutil.swap_memory())
check(psutil.cpu_times())
check(psutil.cpu_times_percent(interval=0))
check(psutil.net_io_counters())
if LINUX and not os.path.exists('/proc/diskstats'):
pass
else:
if not APPVEYOR:
check(psutil.disk_io_counters())
check(psutil.disk_partitions())
check(psutil.disk_usage(os.getcwd()))
check(psutil.users())
示例5: __call__
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def __call__(self):
""" Returns a derived gauge for the processor time.
Processor time is defined as a float representing the current system
wide CPU utilization minus idle CPU time as a percentage. Idle CPU
time is defined as the time spent doing nothing. Return values range
from 0.0 to 100.0 inclusive.
:rtype: :class:`opencensus.metrics.export.gauge.DerivedDoubleGauge`
:return: The gauge representing the processor time metric
"""
gauge = DerivedDoubleGauge(
ProcessorTimeMetric.NAME,
'Processor time as a percentage',
'percentage',
[])
gauge.create_default_time_series(ProcessorTimeMetric.get_value)
# From the psutil docs: the first time this method is called with
# interval = None it will return a meaningless 0.0 value which you are
# supposed to ignore. Call cpu_percent() once so that the subsequent
# calls from the gauge will be meaningful.
psutil.cpu_times_percent()
return gauge
示例6: iowait
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def iowait():
cpu_percent = psutil.cpu_times_percent()
try:
return cpu_percent.iowait
except AttributeError:
return None
示例7: test_cpu_times_percent
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def test_cpu_times_percent(self):
last = psutil.cpu_times_percent(interval=0.001)
for x in range(100):
new = psutil.cpu_times_percent(interval=None)
for percent in new:
self._test_cpu_percent(percent, last, new)
self._test_cpu_percent(sum(new), last, new)
last = new
示例8: test_per_cpu_times_percent
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def test_per_cpu_times_percent(self):
last = psutil.cpu_times_percent(interval=0.001, percpu=True)
self.assertEqual(len(last), psutil.cpu_count())
for x in range(100):
new = psutil.cpu_times_percent(interval=None, percpu=True)
for cpu in new:
for percent in cpu:
self._test_cpu_percent(percent, last, new)
self._test_cpu_percent(sum(cpu), last, new)
last = new
示例9: test_per_cpu_times_percent_negative
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def test_per_cpu_times_percent_negative(self):
# see: https://github.com/giampaolo/psutil/issues/645
psutil.cpu_times_percent(percpu=True)
zero_times = [x._make([0 for x in range(len(x._fields))])
for x in psutil.cpu_times(percpu=True)]
with mock.patch('psutil.cpu_times', return_value=zero_times):
for cpu in psutil.cpu_times_percent(percpu=True):
for percent in cpu:
self._test_cpu_percent(percent, None, None)
示例10: test_cpu_times_percent
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def test_cpu_times_percent(self):
self.assert_ntuple_of_nums(psutil.cpu_times_percent(interval=None))
self.assert_ntuple_of_nums(psutil.cpu_times_percent(interval=0.0001))
示例11: test_cpu_steal_decrease
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def test_cpu_steal_decrease(self):
# Test cumulative cpu stats decrease. We should ignore this.
# See issue #1210.
with mock_open_content(
"/proc/stat",
textwrap.dedent("""\
cpu 0 0 0 0 0 0 0 1 0 0
cpu0 0 0 0 0 0 0 0 1 0 0
cpu1 0 0 0 0 0 0 0 1 0 0
""").encode()) as m:
# first call to "percent" functions should read the new stat file
# and compare to the "real" file read at import time - so the
# values are meaningless
psutil.cpu_percent()
assert m.called
psutil.cpu_percent(percpu=True)
psutil.cpu_times_percent()
psutil.cpu_times_percent(percpu=True)
with mock_open_content(
"/proc/stat",
textwrap.dedent("""\
cpu 1 0 0 0 0 0 0 0 0 0
cpu0 1 0 0 0 0 0 0 0 0 0
cpu1 1 0 0 0 0 0 0 0 0 0
""").encode()) as m:
# Increase "user" while steal goes "backwards" to zero.
cpu_percent = psutil.cpu_percent()
assert m.called
cpu_percent_percpu = psutil.cpu_percent(percpu=True)
cpu_times_percent = psutil.cpu_times_percent()
cpu_times_percent_percpu = psutil.cpu_times_percent(percpu=True)
self.assertNotEqual(cpu_percent, 0)
self.assertNotEqual(sum(cpu_percent_percpu), 0)
self.assertNotEqual(sum(cpu_times_percent), 0)
self.assertNotEqual(sum(cpu_times_percent), 100.0)
self.assertNotEqual(sum(map(sum, cpu_times_percent_percpu)), 0)
self.assertNotEqual(sum(map(sum, cpu_times_percent_percpu)), 100.0)
self.assertEqual(cpu_times_percent.steal, 0)
self.assertNotEqual(cpu_times_percent.user, 0)
示例12: init
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def init(self):
t = tp_timestamp_ms() - 10 * 60 * 1000
cnt = int((10 * 60 + self._INTERVAL - 1) / self._INTERVAL)
for i in range(cnt):
val = {
't': t,
'cpu': {'u': 0, 's': 0},
'mem': {'u': 0, 't': 100},
'disk': {'r': 0, 'w': 0},
'net': {'r': 0, 's': 0}
}
self._sys_stats.append(val)
t += self._INTERVAL * 1000
psutil.cpu_times_percent()
net = psutil.net_io_counters(pernic=False)
self._net_recv = net.bytes_recv
self._net_sent = net.bytes_sent
disk = psutil.disk_io_counters(perdisk=False)
self._disk_read = disk.read_bytes
self._disk_write = disk.write_bytes
err, c = stats.get_basic_stats()
if TPE_OK == err:
self._counter_stats = c
# 每 5秒 采集一次系统状态统计数据
tp_cron().add_job('sys_status', self._check_sys_stats, first_interval_seconds=self._INTERVAL, interval_seconds=self._INTERVAL)
# 每 1小时 重新查询一次数据库,得到用户数/主机数/账号数/连接数,避免统计数量出现偏差
tp_cron().add_job('query_counter', self._query_counter, first_interval_seconds=60 * 60, interval_seconds=60 * 60)
# 每 1分钟 检查一下临时锁定用户是否可以自动解锁了
tp_cron().add_job('check_temp_locked_user', self._check_temp_locked_user, interval_seconds=60)
tp_wss().register_get_sys_status_callback(self.get_sys_stats)
tp_wss().register_get_stat_counter_callback(self.get_counter_stats)
return True
示例13: _check_sys_stats
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def _check_sys_stats(self):
val = {'t': tp_timestamp_ms()}
cpu = psutil.cpu_times_percent()
val['cpu'] = {'u': cpu.user, 's': cpu.system}
mem = psutil.virtual_memory()
val['mem'] = {'u': mem.used, 't': mem.total}
disk = psutil.disk_io_counters(perdisk=False)
_read = disk.read_bytes - self._disk_read
_write = disk.write_bytes - self._disk_write
self._disk_read = disk.read_bytes
self._disk_write = disk.write_bytes
if _read < 0:
_read = 0
if _write < 0:
_write = 0
val['disk'] = {'r': int(_read / self._INTERVAL), 'w': int(_write / self._INTERVAL)}
net = psutil.net_io_counters(pernic=False)
_recv = net.bytes_recv - self._net_recv
_sent = net.bytes_sent - self._net_sent
self._net_recv = net.bytes_recv
self._net_sent = net.bytes_sent
# On some systems such as Linux, on a very busy or long-lived system, the numbers
# returned by the kernel may overflow and wrap (restart from zero)
if _recv < 0:
_recv = 0
if _sent < 0:
_sent = 0
val['net'] = {'r': int(_recv / self._INTERVAL), 's': int(_sent / self._INTERVAL)}
self._sys_stats.pop(0)
self._sys_stats.append(val)
tp_wss().send_message('sys_status', val)
示例14: get_system_load
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def get_system_load(self):
return {
MEM_AVAILABLE: psutil.virtual_memory().available,
CPU_IDLE_PERCENT: psutil.cpu_times_percent(
self.cpu_sample_interval).idle,
CLIENT_NUMBER: len(self.protocol.server.clients),
}
示例15: __get_cpu_usage
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times_percent [as 别名]
def __get_cpu_usage(self):
return (100.0 - psutil.cpu_times_percent(interval=0.1).idle) / 100.0