-
Notifications
You must be signed in to change notification settings - Fork 19
/
MonitorController.py
179 lines (140 loc) · 5.53 KB
/
MonitorController.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- encoding:utf-8 -*-
"""
封装process monitor的
ui层,使用traitsui, chaco
"""
from __future__ import print_function
import os
import platform
from collections import deque
import numpy as np
from ProcessMonitor import ProcessMonitorClass
from ProcessMonitor import TaskEnum
from traits.api import HasTraits, Instance, List, Button, Str, Int
from traitsui.api import View, UItem, Item, ListStrEditor, Group
__author__ = 'BBFamily'
K_CPU_TEMP_CNT = 100
"""
是否开启cpu及温度监控
"""
g_enable_cpu_monitor = False
if g_enable_cpu_monitor:
"""
conda的chaco会导致问题如果是conda环境就不要开了
"""
from chaco import Plot, ArrayPlotData
import CpuHelper
class MonitorController(HasTraits):
print_pool = List(Str)
cpu_pool = List(Str)
if g_enable_cpu_monitor:
plot_cpu_temp = Instance(Plot)
start_work = Button()
end_work = Button()
refresh_str = Button()
refresh_call = Button()
clear_pool = Button()
suspend_all = Button()
suspend_all_time = Button()
kill_all = Button()
resume_all = Button()
manual_add = Button()
manual_pid = Int
def __init__(self):
super(MonitorController, self).__init__()
self.title = 'Process Monitor'
run_task = dict()
if g_enable_cpu_monitor:
run_task.update({TaskEnum.E_WEEK_TASK: [self._cpu_change]})
self._cpu_init()
self.pMonitor = ProcessMonitorClass(self._add_print_pool, **run_task)
def default_title(self):
self.title = 'Process Monitor'
def __do_cpu_temp_plot(self, temp):
x = np.arange(0, len(self.deque_cpu))
y = self.deque_cpu
plot_data = ArrayPlotData(x=x, y=y)
plot = Plot(plot_data)
self.renderer = plot.plot(("x", "y"), color="blue")[0]
plot.title = "cpu temperature: " + str(temp)
self.plot_cpu_temp = plot
def __do_cpu_inv(self):
cpu_percent = CpuHelper.cpu_percent()
self.cpu_pool = ['cpu %d: %f' % (cpu_index, percent) for cpu_index, percent in enumerate(cpu_percent)]
self.cpu_pool.insert(0, 'mean cpu: %f' % (np.mean(cpu_percent)))
def _cpu_change(self):
temp, temp_str = CpuHelper.get_cpu_temp_proxy()
self.deque_cpu.append(temp)
self.__do_cpu_temp_plot(temp)
self.__do_cpu_inv()
def _cpu_init(self):
temp, temp_str = CpuHelper.get_cpu_temp_proxy()
self.deque_cpu = deque(maxlen=K_CPU_TEMP_CNT)
self.deque_cpu.extend((np.ones(K_CPU_TEMP_CNT) * temp).tolist())
self.__do_cpu_temp_plot(temp)
self.__do_cpu_inv()
def _start_work_fired(self):
self.pMonitor.start_work()
def _end_work_fired(self):
self.pMonitor.end_work()
def _refresh_str_fired(self):
self.print_pool.append(str(self.pMonitor))
def _refresh_call_fired(self):
self.pMonitor()
def _suspend_all_fired(self):
self.pMonitor.rest_all(rest_time=True)
def _suspend_all_time_fired(self):
self.pMonitor.rest_all()
def _resume_all_fired(self):
self.pMonitor.awake_all()
def _kill_all_fired(self):
self.pMonitor.kill_all()
def _clear_pool_fired(self):
self.print_pool = []
cmd_clear = 'cls' if platform.system().lower().find("windows") >= 0 else 'clear'
os.system(cmd_clear)
def _manual_add_fired(self):
self.pMonitor.manual_add_pid(self.manual_pid)
def _add_print_pool(self, *args):
self.print_pool.extend(args)
view = View(
Group(
UItem('print_pool', editor=ListStrEditor(auto_add=False)),
Item('start_work', label=u'监控开始', show_label=False),
Item('end_work', label=u'监控结束', show_label=False),
# Item('refresh_str', label=u'刷新str', show_label=False),
Item('refresh_call', label=u'刷新call', show_label=False),
Item('suspend_all', label=u'挂起所有', show_label=False),
Item('suspend_all_time', label=u'挂起所有(定时失效)', show_label=False),
Item('resume_all', label=u'恢复所有', show_label=False),
Item('kill_all', label=u'杀死所有', show_label=False),
Item('clear_pool', label=u'屏幕clear', show_label=False),
Group(
Item('manual_add', label=u'手动添加pid', show_label=False),
Item('manual_pid', tooltip=u"输入pid后,按‘手动添加pid’", width=360, show_label=False),
orientation='horizontal',
show_border=True
),
label=u'控制处理',
show_border=True
)
# 如果打开cpu监控g_enable_cpu_monitor且安装了chaco等
# 打开下面代码onda的chaco会导致问题如果是conda环境就不要开了
# ,
# Group(
# Item('temp', label=u'cpu温度监控关闭,g_enable_cpu_monitor控制', show_label=False,
# width=500, height=320),
# label = u'信息显示',
# show_border = True,
# Item('plot_cpu_temp', editor=ComponentEditor() if g_enable_cpu_monitor
# else label=u'cpu温度监控关闭,g_enable_cpu_monitor控制', show_label=False,
# width=500, height=320),
# UItem('cpu_pool', editor=ListStrEditor(auto_add=False)),
# label = u'信息显示',
# show_border = True,
# ),
# resizable = True,
)
if __name__ == "__main__":
MonitorController().configure_traits()
# help(Item)