Skip to content

Commit 2ffb3ec

Browse files
authored
Add SniperMonitor (#29)
* add SniperMonitor with GUI based on PySide6
1 parent 68b3ab1 commit 2ffb3ec

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

SniperUtil/SniperProfiling/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ install(DIRECTORY SniperProfiling DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1616
if(USE_PYTHON)
1717
# install
1818
install(DIRECTORY python/SniperProfiling DESTINATION ${CMAKE_INSTALL_PYTHONDIR})
19+
install(FILES python/MonitorRunner.py DESTINATION ${CMAKE_INSTALL_BINDIR})
20+
install(DIRECTORY python/SniperMonitor DESTINATION ${CMAKE_INSTALL_PYTHONDIR})
1921
endif(USE_PYTHON)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
sys.path.append('../python/')
3+
4+
from PySide6.QtWidgets import QApplication
5+
from SniperMonitor.MainMonitor import MainMonitor
6+
#from SniperMonitor.MainMonitor import MainMonitor
7+
8+
if __name__ == "__main__":
9+
app = QApplication(sys.argv)
10+
sniper_monitor = MainMonitor()
11+
sniper_monitor.resize(1000, 500)
12+
sniper_monitor.show()
13+
sniper_monitor.AddInfo()
14+
15+
sys.exit(app.exec())
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import psutil
2+
from PySide6.QtCharts import QChart, QLineSeries, QValueAxis
3+
from PySide6.QtCore import Qt, QTimer
4+
from PySide6.QtGui import QPen
5+
6+
class CPUMonitor(QChart):
7+
def __init__(self, parent=None):
8+
super().__init__(QChart.ChartTypeCartesian, parent, Qt.WindowFlags())
9+
10+
# init essential elements
11+
self.series = QLineSeries(self)
12+
self.axisX = QValueAxis()
13+
self.axisYleft = QValueAxis()
14+
self.axisYright = QValueAxis()
15+
16+
# important values
17+
self.step = 1
18+
self.xRange = 20
19+
self.x = 0
20+
# get the cpu utilization
21+
self.y = psutil.cpu_percent()
22+
23+
# set timer properties
24+
self.timer = QTimer()
25+
self.timer.timeout.connect(self.handleTimeout)
26+
self.timer.setInterval(1000)
27+
28+
# set texts of legend and title
29+
self.legend().setVisible(True)
30+
self.legend().setAlignment(Qt.AlignBottom)
31+
self.series.setName("CPU: %.2f%%" %self.y)
32+
self.axisX.setLabelFormat("%d")
33+
self.axisX.setTitleText("Time(s)")
34+
self.axisYleft.setTitleText("Percent(%)")
35+
self.axisYright.setTitleText("Percent(%)")
36+
37+
# set series properties
38+
redColor = QPen(Qt.red)
39+
redColor.setWidth(3)
40+
self.series.setPen(redColor)
41+
self.series.append(self.x, self.y)
42+
43+
# set axes properties
44+
self.addAxis(self.axisX, Qt.AlignBottom)
45+
self.addAxis(self.axisYleft, Qt.AlignLeft)
46+
self.addAxis(self.axisYright, Qt.AlignRight)
47+
48+
self.axisX.setTickCount(self.xRange + 1)
49+
self.axisYleft.setTickCount(11)
50+
self.axisYright.setTickCount(11)
51+
52+
self.axisX.setRange(-self.xRange, 0)
53+
self.axisYleft.setRange(0, 100)
54+
self.axisYright.setRange(0, 100)
55+
56+
# attach axes to the graph
57+
self.addSeries(self.series)
58+
self.series.attachAxis(self.axisX)
59+
self.series.attachAxis(self.axisYleft)
60+
self.series.attachAxis(self.axisYright)
61+
62+
# start timing
63+
self.timer.start()
64+
65+
def handleTimeout(self):
66+
# one step forward
67+
self.x += self.step
68+
self.y = psutil.cpu_percent()
69+
self.series.setName("CPU: %.2f%%" %self.y)
70+
self.series.append(self.x, self.y)
71+
# move the line to the left for one step
72+
self.scroll(self.plotArea().width() / (self.axisX.tickCount() - 1), 0)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from PySide6.QtGui import QPainter
2+
from PySide6.QtCharts import QChart, QChartView
3+
from PySide6.QtWidgets import QMainWindow, QTabWidget
4+
from SniperMonitor.CPUMonitor import CPUMonitor
5+
from SniperMonitor.MemMonitor import MemMonitor
6+
7+
class MainMonitor(QMainWindow):
8+
def __init__(self, parent=None):
9+
super().__init__(parent)
10+
11+
self.tab_widget = QTabWidget()
12+
13+
self.setCentralWidget(self.tab_widget)
14+
self.setWindowTitle("SniperMonitor")
15+
self.AddInfo
16+
17+
def AddInfo(self):
18+
cpu_monitor = CPUMonitor()
19+
self.AddNewTab(cpu_monitor, "CPU")
20+
21+
mem_monitor = MemMonitor()
22+
self.AddNewTab(mem_monitor, "Memory")
23+
24+
def AddNewTab(self, obj, name):
25+
obj.setAnimationOptions(QChart.SeriesAnimations)
26+
char_view = QChartView(obj)
27+
char_view.setRenderHint(QPainter.Antialiasing)
28+
self.tab_widget.addTab(char_view, name)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from email.charset import QP
2+
import psutil
3+
from PySide6.QtCharts import QChart, QLineSeries, QValueAxis
4+
from PySide6.QtCore import Qt, QTimer
5+
from PySide6.QtGui import QPen
6+
from PySide6.QtWidgets import QLabel
7+
8+
class MemMonitor(QChart):
9+
def __init__(self, parent=None):
10+
super().__init__(QChart.ChartTypeCartesian, parent, Qt.WindowFlags())
11+
12+
# init essential elements
13+
self.seriesMem = QLineSeries(self)
14+
self.seriesSwp = QLineSeries(self)
15+
self.axisX = QValueAxis()
16+
self.axisYleft = QValueAxis()
17+
self.axisYright = QValueAxis()
18+
19+
# important values
20+
self.step = 1
21+
self.xRange = 20
22+
self.x = 0
23+
# total memory space
24+
self.memoryData = psutil.virtual_memory()
25+
self.swapData = psutil.swap_memory()
26+
self.GBCONV = 1073741824
27+
self.memoryTotal = psutil.virtual_memory().total / self.GBCONV
28+
self.swapTotal = psutil.swap_memory().total / self.GBCONV
29+
# get the memory utilization
30+
self.yMem = psutil.virtual_memory()
31+
self.ySwp = psutil.swap_memory()
32+
33+
# set timer properties
34+
self.timer = QTimer()
35+
self.timer.timeout.connect(self.handleTimeout)
36+
self.timer.setInterval(1000)
37+
38+
# set texts of legend and title
39+
self.legend().setVisible(True)
40+
self.legend().setAlignment(Qt.AlignBottom)
41+
self.seriesMem.setName("Memory: %.2f/%.2fGB (%.2f%%)"
42+
%(self.yMem.used / self.GBCONV, self.memoryTotal, self.yMem.percent))
43+
self.seriesSwp.setName("Swap: %.2f/%.2fGB (%.2f%%)"
44+
%(self.ySwp.used / self.GBCONV, self.swapTotal, self.ySwp.percent))
45+
self.axisX.setLabelFormat("%d")
46+
self.axisX.setTitleText("Time(s)")
47+
self.axisYleft.setTitleText("Percent(%)")
48+
self.axisYright.setTitleText("Percent(%)")
49+
50+
# set series properties
51+
colorRed = QPen(Qt.red)
52+
colorBlue = QPen(Qt.blue)
53+
colorRed.setWidth(3)
54+
colorBlue.setWidth(3)
55+
self.seriesMem.setPen(colorRed)
56+
self.seriesSwp.setPen(colorBlue)
57+
self.seriesMem.append(self.x, self.yMem.percent)
58+
self.seriesSwp.append(self.x, self.ySwp.percent)
59+
60+
# set axes properties
61+
self.addAxis(self.axisX, Qt.AlignBottom)
62+
self.addAxis(self.axisYleft, Qt.AlignLeft)
63+
self.addAxis(self.axisYright, Qt.AlignRight)
64+
65+
self.axisX.setTickCount(self.xRange + 1)
66+
self.axisYleft.setTickCount(11)
67+
self.axisYright.setTickCount(11)
68+
69+
self.axisX.setRange(-self.xRange, 0)
70+
self.axisYleft.setRange(0, 100)
71+
self.axisYright.setRange(0, 100)
72+
73+
# attach axes to the graph
74+
self.addSeries(self.seriesMem)
75+
self.addSeries(self.seriesSwp)
76+
self.seriesMem.attachAxis(self.axisX)
77+
self.seriesMem.attachAxis(self.axisYleft)
78+
self.seriesMem.attachAxis(self.axisYright)
79+
self.seriesSwp.attachAxis(self.axisX)
80+
self.seriesSwp.attachAxis(self.axisYleft)
81+
self.seriesSwp.attachAxis(self.axisYright)
82+
83+
# start timing
84+
self.timer.start()
85+
86+
def handleTimeout(self):
87+
# one step forward
88+
self.x += self.step
89+
self.yMem = psutil.virtual_memory()
90+
self.ySwp = psutil.swap_memory()
91+
self.seriesMem.setName("Memory: %.2f/%.2fGB (%.2f%%)"
92+
%(self.yMem.used / self.GBCONV, self.memoryTotal, self.yMem.percent))
93+
self.seriesSwp.setName("Swap: %.2f/%.2fGB (%.2f%%)"
94+
%(self.ySwp.used / self.GBCONV, self.swapTotal, self.ySwp.percent))
95+
self.seriesMem.append(self.x, self.yMem.percent)
96+
self.seriesSwp.append(self.x, self.ySwp.percent)
97+
# move the line to the left for one step
98+
self.scroll(self.plotArea().width() / (self.axisX.tickCount() - 1), 0)

0 commit comments

Comments
 (0)