tlkh / asitop

Perf monitoring CLI tool for Apple Silicon
https://tlkh.github.io/asitop/
MIT License
3.36k stars 147 forks source link

Use Python threading to avoid creating temp files #39

Open 6-6-6 opened 2 years ago

6-6-6 commented 2 years ago

This is an effort to avoid creating temp files under /tmp folder.

It uses Python's threading and queueing library to receive and preprocess the output of powermetrics.

The main change is presented in commit 1daf3add70f113a8f1e97f0d410231c7b57f0cec The other 2 commits are for code cleanups.

The whole process is described as follows:

  1. powermetrics is configured to output to stdout
  2. a LIFO queue is created for communicating between threads
  3. the output will be passed to another thread and processed by enqueue_powermetrics(), which will
    • convert the lines of stdout into separate plists (by the magic b'\x00' character)
    • each plist will be added to the LIFO queue
  4. instead of reading the whole temp file, parse_powermetrics() will get the latest plist from the LIFO queue, and parse the plist as usual

This PR will hopefully resolve issues #18, #17 and #35. Also #6 may also be resolved (without restarting powermetrics multiple times).

buliaoyin commented 2 years ago

Seems nice, I'll try to check if the memory issue had been resolved.

buliaoyin commented 2 years ago

Seems nice, I'll try to check if the memory issue had been resolved.

After 6 hours, the python process memory was still only around 30MB, memory issue has gone.

6-6-6 commented 2 years ago

Hi, I have just added two more commits.

https://github.com/tlkh/asitop/pull/39/commits/1acead9e3d0216324f5c62b82a04fb5c3a03a31f is to meet the requirement of "Codacy Static Code Analysis".

https://github.com/tlkh/asitop/pull/39/commits/5b6b58c03f4b02523f8e55fd98e5809a6a59093d is to remove the redundant time.sleep(args.interval) in asitop.asitop.py, and the reason is explained in the commit message.