Mã này tính toán mức độ sử dụng của người dùng trên tất cả các lõi.
import os
import time
import multiprocessing
def main():
jiffy = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
num_cpu = multiprocessing.cpu_count()
stat_fd = open('/proc/stat')
stat_buf = stat_fd.readlines()[0].split()
user, nice, sys, idle, iowait, irq, sirq = (float(stat_buf[1]), float(stat_buf[2]),
float(stat_buf[3]), float(stat_buf[4]),
float(stat_buf[5]), float(stat_buf[6]),
float(stat_buf[7]))
stat_fd.close()
time.sleep(1)
stat_fd = open('/proc/stat')
stat_buf = stat_fd.readlines()[0].split()
user_n, nice_n, sys_n, idle_n, iowait_n, irq_n, sirq_n = (float(stat_buf[1]), float(stat_buf[2]),.
float(stat_buf[3]), float(stat_buf[4]),
float(stat_buf[5]), float(stat_buf[6]),
float(stat_buf[7]))
stat_fd.close()
print ((user_n - user) * 100/jiffy)/num_cpu
if __name__ == '__main__':
main()
Tôi sẽ thử điều này và quay lại. – jgr
Điều này sẽ cho tôi tổng số tiền được chi tiêu trong người dùng, v.v., đúng không? Tôi bị can thiệp vào số tiền hiện tại, tức là tại thời điểm hiện tại. – jgr
Bạn cần phải tự mình theo dõi bằng cách đọc tệp đó ví dụ như mỗi giây và "khác" để nhập các giá trị. – Mat