pmonitor 1.4.6__py3-none-any.whl → 1.4.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- monitor/monitor_mac_vmmap.py +51 -0
- monitor/monitor_pids.py +14 -5
- monitor/run_monitor.py +1 -1
- {pmonitor-1.4.6.dist-info → pmonitor-1.4.8.dist-info}/METADATA +1 -1
- {pmonitor-1.4.6.dist-info → pmonitor-1.4.8.dist-info}/RECORD +8 -8
- monitor/exec/__init__.py +0 -0
- {pmonitor-1.4.6.dist-info → pmonitor-1.4.8.dist-info}/WHEEL +0 -0
- {pmonitor-1.4.6.dist-info → pmonitor-1.4.8.dist-info}/entry_points.txt +0 -0
- {pmonitor-1.4.6.dist-info → pmonitor-1.4.8.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import asyncio
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class VmMapMemory:
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.last_nonzero_memory = None
|
|
8
|
+
|
|
9
|
+
async def get_process_memory(self, pid):
|
|
10
|
+
cmd = ["top", "-l", "1", "-stats", "mem", "-pid", str(pid)]
|
|
11
|
+
try:
|
|
12
|
+
# 创建子进程并设置超时
|
|
13
|
+
proc = await asyncio.create_subprocess_exec(
|
|
14
|
+
*cmd,
|
|
15
|
+
stdout=asyncio.subprocess.PIPE,
|
|
16
|
+
stderr=asyncio.subprocess.DEVNULL
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
stdout, _ = await asyncio.wait_for(proc.communicate(), timeout=1)
|
|
21
|
+
except asyncio.TimeoutError:
|
|
22
|
+
# 超时后确保子进程被终止
|
|
23
|
+
proc.kill()
|
|
24
|
+
await proc.wait()
|
|
25
|
+
raise
|
|
26
|
+
|
|
27
|
+
if proc.returncode == 0:
|
|
28
|
+
# 解析 top 输出中的内存信息
|
|
29
|
+
lines = stdout.decode().strip().split('\n')
|
|
30
|
+
if lines:
|
|
31
|
+
last_line = lines[-1].strip() # 获取最后一行
|
|
32
|
+
match = re.match(r'^([\d.]+)([GMK]?)$', last_line)
|
|
33
|
+
|
|
34
|
+
if match:
|
|
35
|
+
val, unit = float(match.group(1)), match.group(2)
|
|
36
|
+
memory_value = val * {
|
|
37
|
+
'G': 1024, 'M': 1, 'K': 1 / 1024, '': 1 / (1024 * 1024)
|
|
38
|
+
}.get(unit, 1)
|
|
39
|
+
|
|
40
|
+
if memory_value > 0:
|
|
41
|
+
self.last_nonzero_memory = memory_value
|
|
42
|
+
# 返回当前值或上一次非零值
|
|
43
|
+
return memory_value if memory_value != 0 else self.last_nonzero_memory
|
|
44
|
+
except Exception as e:
|
|
45
|
+
print(e)
|
|
46
|
+
finally:
|
|
47
|
+
if proc and proc.returncode is None:
|
|
48
|
+
proc.kill()
|
|
49
|
+
await proc.wait()
|
|
50
|
+
# 如果未能获取到有效值,返回上一次非零值或默认值 0.0
|
|
51
|
+
return self.last_nonzero_memory if self.last_nonzero_memory is not None else 0.0
|
monitor/monitor_pids.py
CHANGED
|
@@ -9,6 +9,8 @@ import ctypes
|
|
|
9
9
|
import os
|
|
10
10
|
from PyLibreHardwareMonitor.computer import Computer
|
|
11
11
|
|
|
12
|
+
from monitor.monitor_mac_vmmap import VmMapMemory
|
|
13
|
+
|
|
12
14
|
if platform.system() != 'Windows':
|
|
13
15
|
from monitor.mac_gpu import get_gpu_memory
|
|
14
16
|
|
|
@@ -20,9 +22,11 @@ class PidsPerf:
|
|
|
20
22
|
self.interval = interval
|
|
21
23
|
self.processUtil = ProcessName()
|
|
22
24
|
|
|
23
|
-
def get_mac_perf(self):
|
|
25
|
+
async def get_mac_perf(self):
|
|
24
26
|
current_pid = self.processUtil.find_main_process_pid(self.process_name)
|
|
27
|
+
vm = VmMapMemory()
|
|
25
28
|
while True:
|
|
29
|
+
start_time = time.time() # 记录循环开始时间
|
|
26
30
|
minor_cpu_sum = 0
|
|
27
31
|
minor_real_mem_sum = 0
|
|
28
32
|
minor_mem_percent_sum = 0
|
|
@@ -39,12 +43,14 @@ class PidsPerf:
|
|
|
39
43
|
mem_available = round(mem_info.available / 1024 / 1024, 2) # 剩余虚拟内存
|
|
40
44
|
|
|
41
45
|
gpu_memory_usage, gpu_memory_total, gpu_memory_free = get_gpu_memory()
|
|
42
|
-
|
|
46
|
+
# 使用 asyncio.gather 并发获取内存数据
|
|
47
|
+
memory_tasks = [vm.get_process_memory(process.pid) for process in pids_process]
|
|
48
|
+
memory_results = await asyncio.gather(*memory_tasks, return_exceptions=True)
|
|
49
|
+
for idx, process in enumerate(pids_process):
|
|
43
50
|
try:
|
|
44
51
|
cpu_percent = process.cpu_percent()
|
|
45
|
-
process_memory = process.memory_info()
|
|
46
52
|
mem_percent = u'%.2f' % (process.memory_percent()) # 内存利用率
|
|
47
|
-
real_mem =
|
|
53
|
+
real_mem = memory_results[idx] # 实际内存
|
|
48
54
|
thread_count = process.num_threads() # 线程总数
|
|
49
55
|
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
50
56
|
cpu_percent = 0
|
|
@@ -74,7 +80,7 @@ class PidsPerf:
|
|
|
74
80
|
# minor_thread_count_sum)
|
|
75
81
|
# 获取磁盘IO
|
|
76
82
|
io_read_bytes_start, io_write_bytes_start = self.get_disk_usage()
|
|
77
|
-
time.sleep(
|
|
83
|
+
time.sleep(0.1) # 采集间隔
|
|
78
84
|
io_read_bytes_end, io_write_bytes_end = self.get_disk_usage() # io read/write
|
|
79
85
|
io_read_bytes = io_read_bytes_end - io_read_bytes_start # io read/byte
|
|
80
86
|
io_write_bytes = io_write_bytes_end - io_write_bytes_start # io write/byte
|
|
@@ -85,6 +91,9 @@ class PidsPerf:
|
|
|
85
91
|
"memory_total": mem_total, "memory_available": mem_available}
|
|
86
92
|
json_data = json.dumps(data)
|
|
87
93
|
print(json_data)
|
|
94
|
+
elapsed = time.time() - start_time
|
|
95
|
+
remaining_time = max(0, self.interval / 1000 - elapsed)
|
|
96
|
+
await asyncio.sleep(remaining_time)
|
|
88
97
|
sys.stdout.flush()
|
|
89
98
|
|
|
90
99
|
def get_disk_usage(self):
|
monitor/run_monitor.py
CHANGED
|
@@ -3,9 +3,10 @@ monitor/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
3
3
|
monitor/get_process_name.py,sha256=HxePo2gdrTo2Ukg4DTnYzUMdczVWywCeLCXQ-RRWyu8,3913
|
|
4
4
|
monitor/mac_gpu.py,sha256=kQDPMW04lIYPBbJw772Jh1OQxj-D4JNmdjJlP6BuR4w,2320
|
|
5
5
|
monitor/monitor_mac.py,sha256=OoptTTXTZQupzBwgGOfKNjyYAKBxDy-wI3l6T7XVE8s,4651
|
|
6
|
-
monitor/
|
|
6
|
+
monitor/monitor_mac_vmmap.py,sha256=Lu1AAw6auuZzuNZtASOaNBpRPOOgyzGr1jYSTErmRmo,1971
|
|
7
|
+
monitor/monitor_pids.py,sha256=1Bmemd-cf1NkrcCbKwOg-56fDgxSGLXam-EGmr_5AWU,16531
|
|
7
8
|
monitor/monitor_win.py,sha256=xs5nzqqEPoDmJTegh3lQhVjjpPcOWnruWKK65ttqnTo,6161
|
|
8
|
-
monitor/run_monitor.py,sha256=
|
|
9
|
+
monitor/run_monitor.py,sha256=HDQXHIx47ZRN4Gp0PTr0yWKVM28n8A7-NaO--_pdgso,1577
|
|
9
10
|
monitor/sys_info.py,sha256=aNultuRoQuRYPkYo397xAXVDXP07Qx5JOHtYzNmEvuc,3208
|
|
10
11
|
monitor/DLL/GpuMonitorLib.dll,sha256=xbH4eyDvnv2g6go1fvUt6qQd7qA9n6piP8My4XubLPQ,66560
|
|
11
12
|
monitor/DLL/KernelBase.dll,sha256=MSJ2WSh-2Lk6yFVLX_a8G2EnNuaOtJGpfg-Vw9HY_go,3860744
|
|
@@ -23,9 +24,8 @@ monitor/DLL/sechost.dll,sha256=1mXCrgNDRBiZ2XZk7eTpDm4oPG-x9BW4uYUVkrfvBkk,69986
|
|
|
23
24
|
monitor/DLL/ucrtbased.dll,sha256=vtmKFPEHyr2OXkrUOu3Qs1dlbKG1dxZ8ItKCkTTU5S4,2238056
|
|
24
25
|
monitor/DLL/vcruntime140.dll,sha256=AsaqDm5iRBGp8ZsDYKeGWrFZCOJgJFEOXDipwINiw1o,119888
|
|
25
26
|
monitor/DLL/vcruntime140_1.dll,sha256=fdmqAuJxxoym1fGNZR0joV1yWXFa9DMmV4993ifzdjc,49640
|
|
26
|
-
|
|
27
|
-
pmonitor-1.4.
|
|
28
|
-
pmonitor-1.4.
|
|
29
|
-
pmonitor-1.4.
|
|
30
|
-
pmonitor-1.4.
|
|
31
|
-
pmonitor-1.4.6.dist-info/RECORD,,
|
|
27
|
+
pmonitor-1.4.8.dist-info/METADATA,sha256=cHkAUgHDu0voEx1JNF_q1qbPo7YGVzlt6_h4J53KZRs,291
|
|
28
|
+
pmonitor-1.4.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
29
|
+
pmonitor-1.4.8.dist-info/entry_points.txt,sha256=vmlLEANgf2fZar9BeXYiKdF6GMJbVXip-SIZx5yPXDo,55
|
|
30
|
+
pmonitor-1.4.8.dist-info/top_level.txt,sha256=tGkQDkVeyKgP5Rr7acpp0df83NBAnI8up0sGwRxuuQ4,8
|
|
31
|
+
pmonitor-1.4.8.dist-info/RECORD,,
|
monitor/exec/__init__.py
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|