Pyqt 时时CPU使用情况

借鉴代码来自:https://github.com/hgoldfish/quickpanel

实现代码:

  1 # -*- coding:utf-8 -*-
  2 from __future__ import print_function
  3 from __future__ import unicode_literals
  4 from __future__ import division
  5 from __future__ import absolute_import
  6
  7 import ctypes, sys
  8 import ctypes.wintypes
  9 from PyQt4.QtCore import QPoint, QRect, QTimer, Qt
 10 from PyQt4.QtGui import QPainter, QPen, QPolygon, QApplication, QWidget
 11
 12 GetSystemTimes = ctypes.windll.kernel32.GetSystemTimes
 13 class FILETIME(ctypes.Structure):
 14     _fields_ = [("dwLowDateTime", ctypes.wintypes.DWORD), ("dwHighDateTime", ctypes.wintypes.DWORD)]
 15
 16     def __int__(self):
 17         # print(self.dwHighDateTime)
 18         return self.dwHighDateTime * 0x100000000 + self.dwLowDateTime
 19 class MachineLoad:
 20     _instance = None
 21
 22     @staticmethod
 23     def getInstance():
 24         if MachineLoad._instance is None:
 25             MachineLoad._instance = MachineLoad()
 26         return MachineLoad._instance
 27
 28     def __init__(self):
 29         idle, kernel, user = FILETIME(), FILETIME(), FILETIME()
 30         GetSystemTimes(ctypes.byref(idle), ctypes.byref(kernel), ctypes.byref(user))
 31         self.idle0, self.kernel0, self.user0 = int(idle), int(kernel), int(user)
 32
 33     def getLoad(self):
 34         idle, kernel, user = FILETIME(), FILETIME(), FILETIME()
 35         GetSystemTimes(ctypes.byref(idle), ctypes.byref(kernel), ctypes.byref(user))
 36         idle1, kernel1, user1 = int(idle), int(kernel), int(user)
 37         a, b, c = idle1 - self.idle0, kernel1 - self.kernel0, user1 - self.user0
 38         self.idle0, self.kernel0, self.user0 = idle1, kernel1, user1
 39         if (b + c) == 0:
 40             return 1
 41         return (b + c - a) / (b + c)
 42
 43 class MachineLoadWidget(QWidget):
 44     def __init__(self, parent):
 45         QWidget.__init__(self, parent)
 46         self.timer = QTimer()
 47         self.timer.timeout.connect(self.collectMachineLoad)
 48         self.loads = []
 49         self.maxLength = 400
 50         self.pointDistance = 5 #每点之间的间隔
 51         self.updateInterval = 500 #更新的时间间隔
 52         self.timer.setInterval(self.updateInterval)
 53         self.timer.start()
 54         self.machineLoad = MachineLoad.getInstance()
 55         self.boxWidth = 60
 56
 57     def collectMachineLoad(self):
 58         rate = self.machineLoad.getLoad()
 59         self.loads.insert(0, rate)
 60         if len(self.loads) > self.maxLength:
 61             self.loads.pop(- 1)
 62         if self.isVisible():
 63             self.update()
 64
 65     def paintEvent(self, event):
 66         QWidget.paintEvent(self, event)
 67         width, height = self.width(), self.height()
 68         polygon = QPolygon()
 69         for i, rate in enumerate(self.loads):
 70             x = width - i * self.pointDistance
 71             y = height - rate * height
 72             if x < self.boxWidth:
 73                 break
 74             polygon.append(QPoint(x, y))
 75         painter = QPainter(self)
 76         pen = QPen()
 77         pen.setColor(Qt.darkGreen)
 78         painter.setPen(pen)
 79         painter.setRenderHint(QPainter.Antialiasing, True)
 80         #画网格
 81         painter.setOpacity(0.5)
 82         gridSize = self.pointDistance * 4
 83         deltaX = (width - self.boxWidth) % gridSize + self.boxWidth
 84         deltaY = height % gridSize
 85         for i in range(int(width / gridSize)):
 86             x = deltaX + gridSize * i
 87             painter.drawLine(x, 0, x, height)
 88         for j in range(int(height / gridSize)):
 89             y = j * gridSize + deltaY
 90             painter.drawLine(self.boxWidth, y, width, y)
 91         #画折线
 92         pen.setColor(Qt.darkCyan)
 93         pen.setWidth(2)
 94         painter.setPen(pen)
 95         painter.setOpacity(1)
 96         painter.drawPolyline(polygon)
 97         #画展示框
 98         if len(self.loads) > 0:
 99             rate = self.loads[0]
100         else:
101             rate = 1.0
102         rect1 = QRect(4, height * 0.05, self.boxWidth - 9, height * 0.7)
103         rect2 = QRect(4, height * 0.8, self.boxWidth - 9, height * 0.2)
104         centerX = int(rect1.width() / 2) + 1
105         pen.setWidth(1)
106         for i in range(rect1.height()):
107             if i % 4 == 0:
108                 continue
109             if (rect1.height() - i) / rect1.height() > rate:
110                 pen.setColor(Qt.darkGreen)
111             else:
112                 pen.setColor(Qt.green)
113             painter.setPen(pen)
114             for j in range(rect1.width()):
115                 if centerX - 1 <= j <= centerX + 1:
116                     continue
117                 painter.drawPoint(rect1.x() + j, rect1.y() + i)
118         pen.setColor(Qt.black)
119         painter.setPen(pen)
120         painter.drawText(rect2, Qt.AlignHCenter | Qt.AlignVCenter, str(int(rate * 100)) + "%")
121
122
123
124 class CPUstatus(QWidget):
125     def __init__(self):
126         super(CPUstatus, self).__init__()
127         self.resize(200,200)
128         self.factory = MachineLoadWidget(self)
129         self.factory.resize(200, 200)
130
131
132 if __name__ == "__main__":
133     app = QApplication(sys.argv)
134     platform = CPUstatus()
135     platform.show()
136     sys.exit(app.exec_())

效果:

    

时间: 2024-08-08 11:46:29

Pyqt 时时CPU使用情况的相关文章

使用gawk记录一段时间内,某个进程占用内存和CPU的情况

很多时候,我们在后台测试程序的时候,都需要隔一段时间监控程序的内存和CPU占用情况,但是又不能经常盯着top命令的输出. 这时候就需要使用脚本来帮我们记录这些信息,方便我们监控了. 废话不多说,直接上代码: ```shell #!/bin/bash FileName="res.txt" echo "%CPU\t%MEM" > $FileName for (( i = 0; i < 10; i++ )) do output=`top -n 1 -p 1 &

[网站日志]今天早上遭遇的CPU 100%情况

今天早上9:06左右,Windows性能监视器监测到主站的Web服务器出现了CPU 100%的情况,伴随着Requests/Sec的上升,详见下图. 上图中红色线条表示的是%Processor Time.QPS最高冲到了601. IIS的Current Connections也随之上升,见下图: 带来的影响就是请求执行时间(Request Execution Time)变长,见下图: [网站日志]今天早上遭遇的CPU 100%情况,布布扣,bubuko.com [网站日志]今天早上遭遇的CPU

cgroup对程序的cpu使用情况限制的方法

cgroup用来管理linux下的cpu资源,使用cgroup可以很好的限制程序的资源使用情况,下面是对cgroup限制程序cpu使用情况的一些介绍: 1.首先,构造一个占用cpu资源的程序 echo 'while True:pass'|python & 使用top命令可以看到该进程CPU使用达到90%以上 2.进入到/sys/fs/cgroup/cpu目录下,创建一个文件夹,例如test 3.进入到test文件夹下,会看到已自动创建了一些文件,此时,输入 echo 50000 > cpu.

根据dba_hist_osstat统计CPU占用情况

在11g里面,视图dba_hist_osstat用来记录OS级别的time时间指标.视图dba_hist_osstat_name显示了相关的指标名称. [email protected]134.32.114.1:1521/dzgddb> select * from DBA_HIST_OSSTAT_NAME; DBID STAT_ID STAT_NAME --------------- --------------- ----------------------------------------

Oracle CPU使用情况查询

--发现那些SQL运行了大量的PARSE select sql_text, parse_calls, executions from v$sqlarea order by parse_calls desc; --SYS的总的PARSE情况 select name, value from v$sysstat where name like 'parse count%'; --CPU空间及繁忙情况 select * from v$osstat; --查看每个Session的CPU利用情况: sele

查询目前运行状态-CPU等情况

对目前的数据库的运行状况有一个基本的了解 SELECT TOP ( 10 ) DB_NAME(a.dbid) AS dbname , loginame , spid , cpu , b.text , lastwaittype , waitresource , a.[status] , hostname AS WebServer , [program_name] AS AppName , [cmd] , 'cpu' AS Type --into #cpu FROM master..sysproce

Python脚本分析CPU使用情况

在这篇文章中,我将讨论一个工具,用以分析Python中CPU使用情况.CPU分析是通过分析CPU执行代码的方式来测量代码的性能,以此找到代码中的不妥之处,然后处理它们. 接下来我们将看看如何跟踪Python脚本使用时CPU使用情况,重点关注以下几个方面: 1.cProfile 2.line_profiler 3.pprofile 4.vprof 测量CPU使用率 对于这篇文章,我将主要使用与内存分析中使用脚本相同的脚本,具体如下: 另外,请记住,在PyPy2中,您需要使用与之配合的pip版本:

ubuntu下查看服务器的CPU详细情况

https://www.cnblogs.com/liuq/p/5623565.html 全面了解 Linux 服务器 - 1. 查看 Linux 服务器的 CPU 详细情况 ubuntu下查看服务器的CPU详细情况 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/11 14:08 版本:V1.0 Posted on 2016-06-28 15:16 刘[小]倩 阅读(681) 评论(0) 编辑 收藏 1. 查看 Linux 服务器的 C

adb命令检测apk启动时间、内存、CPU使用情况、流量、电池电量等——常用的adb命令

ADB:Android Debug Bridge,是Android SDK里一个可以直接操作安卓模拟器或真实设备的工具,颇为强大. 检测APP: adb shell am start -W $package/.MainActivity               //启动时间 adb shell dumpsys meminfo $pid                          // 指定程序内存使用情况 adb shell dumpsys meminfo $package