-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathactivetasks.py
163 lines (128 loc) · 5.21 KB
/
activetasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from PyQt6.QtGui import QHideEvent
from base_ui import WidgetUI,CommunicationHandler
import PyQt6.QtWidgets
from PyQt6.QtCore import QAbstractTableModel,Qt,QModelIndex,QTimer
import re
class ActiveTaskModel(QAbstractTableModel):
def __init__(self):
super(ActiveTaskModel, self).__init__()
self.items = []
self.header = ["Name","CPU %","State","Prio","Stack","Num"]
def data(self, index, role):
if role == Qt.ItemDataRole.DisplayRole:
idx = index.row()
d = self.items[idx]
if(index.column() == 0):
return d.get("name")
elif(index.column() == 1):
return f"{d['cpu']}%" if "cpu" in d else None
elif(index.column() == 2):
return d.get("state")
elif(index.column() == 3):
return d.get("prio")
elif(index.column() == 4):
return d.get("stack")
elif(index.column() == 5):
return d.get("num")
else:
return None
def headerData(self,section,orientation,role):
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return self.header[section]
def getHeader(self):
return self.header
def rowCount(self, index):
return len(self.items)
def columnCount(self, index):
return len(self.header)
def clearItems(self):
self.beginResetModel()
self.items.clear()
self.endResetModel()
def count(self):
return len(self.items)
def addItem(self,item):
cnt = len(self.items)
self.beginInsertRows(QModelIndex(),cnt,cnt)
self.items.append(item)
self.endInsertRows()
def setItems(self,items):
self.beginResetModel()
self.items = items
self.endResetModel()
class ActiveTaskDialog(PyQt6.QtWidgets.QDialog):
def __init__(self, parent = None):
PyQt6.QtWidgets.QDialog.__init__(self, parent)
self.active_class_ui = ActiveTaskUI(parent)
self.layout = PyQt6.QtWidgets.QVBoxLayout()
self.layout.setContentsMargins(0,0,0,0)
self.layout.addWidget(self.active_class_ui)
self.setLayout(self.layout)
self.setWindowTitle("Active threads")
def set_taskstats_enabled(self,enabled):
self.active_class_ui.taskstats_enabled = enabled
def set_tasklist_enabled(self,enabled):
self.active_class_ui.tasklist_enabled = enabled
class ActiveTaskUI(WidgetUI, CommunicationHandler):
def __init__(self, parent = None):
WidgetUI.__init__(self, parent, 'activelist.ui')
CommunicationHandler.__init__(self)
self.parent = parent
self.pushButton_refresh.clicked.connect(self.read)
self.items = ActiveTaskModel()
self.tableView.setModel(self.items)
header = self.tableView.horizontalHeader()
header.setSectionResizeMode(0,PyQt6.QtWidgets.QHeaderView.ResizeMode.Stretch) # Stretch first section
self.tableView.setSortingEnabled(True)
self.timer = QTimer(self)
self.timer.timeout.connect(self.read)
self.items_list = []
self.items_stats = []
self.taskstats_enabled = False
self.tasklist_enabled = False
def showEvent(self, a0):
self.tableView.resizeColumnsToContents()
self.read()
self.timer.start(1000)
return super().showEvent(a0)
def hideEvent(self, a0: QHideEvent) -> None:
self.timer.stop()
return super().hideEvent(a0)
def read(self):
self.items_stats.clear()
self.items_list.clear()
if self.taskstats_enabled:
self.get_value_async("sys","taskstats",self.updateStatsCb)
if self.tasklist_enabled:
self.get_value_async("sys","tasklist",self.updateListCb)
def updateItems(self):
items = {}
if (not self.taskstats_enabled or self.items_stats) and (not self.tasklist_enabled or self.items_list):
for item in self.items_stats:
items.setdefault(item["name"], {})
items[item["name"]].update(item)
for item in self.items_list:
items.setdefault(item["name"], {})
items[item["name"]].update(item)
self.items.setItems(list(items.values()))
self.parent.show()
def updateStatsCb(self,string):
for line in string.split("\n"):
e = re.split("\t+",line)
if(len(e) < 3):
continue
item = {"name":e[0].strip(),"cpu":int(e[1])}
self.items_stats.append(item)
totalcnt = sum([int(c["cpu"]) for c in self.items_stats])
for item in self.items_stats:
item["cpu"] = round(100*item["cpu"] / totalcnt,2)
self.updateItems()
def updateListCb(self,string):
for line in string.split("\n"):
e = re.split("\t+",line)
if(len(e) < 5):
continue
item = {"name":e[0].strip(),"state":(e[1]),"prio":int(e[2]),"stack":int(e[3]),"num":int(e[4])}
self.items_list.append(item)
self.updateItems()