From fa732867c3090b62ddd282ef94e1e9aec9c6e1bd Mon Sep 17 00:00:00 2001
From: Siarhei Siniak <siarheisiniak@gmail.com>
Date: Tue, 1 Apr 2025 12:40:53 +0300
Subject: [PATCH] [+] tune power management for air 2018

---
 Makefile                                      |   7 +-
 deps/com.github.aiortc.aiortc                 |   2 +-
 .../etc/systemd/system/suspend-fix-t2.service |  26 ++++
 .../bin/online-fxreader-pr34-suspend-fix-t2   | 113 ++++++++++++++++++
 .../usr/local/bin/online-fxreader-pr34-udev   |  16 ++-
 python/online/fxreader/pr34/commands.py       |  18 ++-
 python/pyproject.toml                         |   2 +-
 ...ine_fxreader_pr34-0.1.5.4-py3-none-any.whl |   3 +
 ...ine_fxreader_pr34-0.1.5.5-py3-none-any.whl |   3 +
 ...ine_fxreader_pr34-0.1.5.6-py3-none-any.whl |   3 +
 ...ine_fxreader_pr34-0.1.5.7-py3-none-any.whl |   3 +
 ...ine_fxreader_pr34-0.1.5.8-py3-none-any.whl |   3 +
 12 files changed, 184 insertions(+), 15 deletions(-)
 create mode 100644 platform_dotfiles/macbook_air_2018/etc/systemd/system/suspend-fix-t2.service
 create mode 100755 platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-suspend-fix-t2
 create mode 100644 releases/whl/online_fxreader_pr34-0.1.5.4-py3-none-any.whl
 create mode 100644 releases/whl/online_fxreader_pr34-0.1.5.5-py3-none-any.whl
 create mode 100644 releases/whl/online_fxreader_pr34-0.1.5.6-py3-none-any.whl
 create mode 100644 releases/whl/online_fxreader_pr34-0.1.5.7-py3-none-any.whl
 create mode 100644 releases/whl/online_fxreader_pr34-0.1.5.8-py3-none-any.whl

diff --git a/Makefile b/Makefile
index ce2b9ba..71486f7 100644
--- a/Makefile
+++ b/Makefile
@@ -83,11 +83,16 @@ dotfiles_put:
 	#commands install -f -p dotfiles -s dotfiles/ -t ~/.config/
 
 PLATFORM ?= macbook_air_2012
+PLATFORM_TMP ?= tmp/platform_dotfiles/$(PLATFORM)
 
 dotfiles_put_platform:
 	@echo to be installed
 	find platform_dotfiles/$(PLATFORM);
-	sudo cp -rp -T platform_dotfiles/$(PLATFORM)/ /
+	echo remove $(PLATFORM_TMP)'?'; read; sudo rm -fr $(PLATFORM_TMP)
+	sudo mkdir -p $(PLATFORM_TMP)
+	sudo cp -rp -T platform_dotfiles/$(PLATFORM)/ $(PLATFORM_TMP)
+	sudo chown -R root:root $(PLATFORM_TMP)
+	sudo cp -rp -T $(PLATFORM_TMP) /
 	sudo udevadm control --reload
 	sudo systemctl daemon-reload
 
diff --git a/deps/com.github.aiortc.aiortc b/deps/com.github.aiortc.aiortc
index 3e334c5..56af6d1 160000
--- a/deps/com.github.aiortc.aiortc
+++ b/deps/com.github.aiortc.aiortc
@@ -1 +1 @@
-Subproject commit 3e334c5a51c949c79425c14ed3947d5934ea0617
+Subproject commit 56af6d17019cef5bd52bd47a64697de7245b7a2d
diff --git a/platform_dotfiles/macbook_air_2018/etc/systemd/system/suspend-fix-t2.service b/platform_dotfiles/macbook_air_2018/etc/systemd/system/suspend-fix-t2.service
new file mode 100644
index 0000000..b063de4
--- /dev/null
+++ b/platform_dotfiles/macbook_air_2018/etc/systemd/system/suspend-fix-t2.service
@@ -0,0 +1,26 @@
+[Unit]
+Description=Disable and Re-Enable Apple BCE Module (and Wi-Fi)
+Before=sleep.target
+Before=hibernate.target
+StopWhenUnneeded=yes
+
+[Service]
+User=root
+Type=oneshot
+RemainAfterExit=yes
+
+ExecStart=/usr/local/bin/online-fxreader-pr34-suspend-fix-t2 disable_apple_bce
+#ExecStart=/usr/bin/modprobe -r apple_bce
+#ExecStart=/usr/bin/modprobe -r brcmfmac_wcc
+#ExecStart=/usr/bin/modprobe -r brcmfmac
+#ExecStart=/usr/bin/rmmod -f apple-bce
+
+ExecStop=/usr/local/bin/online-fxreader-pr34-suspend-fix-t2 enable_apple_bce
+#ExecStop=/usr/bin/modprobe -r apple_bce
+#ExecStop=/usr/bin/modprobe apple-bce
+#ExecStop=/usr/bin/modprobe brcmfmac
+#ExecStop=/usr/bin/modprobe brcmfmac_wcc
+
+[Install]
+WantedBy=sleep.target
+WantedBy=hibernate.target
diff --git a/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-suspend-fix-t2 b/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-suspend-fix-t2
new file mode 100755
index 0000000..71e6315
--- /dev/null
+++ b/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-suspend-fix-t2
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+
+# vi: set filetype=python
+
+import sys
+import time
+import argparse
+import subprocess
+
+parser = argparse.ArgumentParser()
+parser.add_argument('mode', choices=[
+    'disable_apple_bce',
+    'enable_apple_bce',
+])
+
+options = parser.parse_args()
+
+if options.mode == 'disable_apple_bce':
+    while True:
+        ret = subprocess.call([
+            'systemctl', 'stop', 'iwd',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'modprobe', '-r', 'brcmfmac_wcc',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'modprobe', '-r', 'brcmfmac',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'modprobe', '-r', 'applesmc',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'rmmod', '-f', 'apple-bce',
+        ])
+        #if ret != 0:
+        #    time.sleep(1)
+        #else:
+        #    break
+        break
+
+elif options.mode == 'enable_apple_bce':
+    while True:
+        ret = subprocess.call([
+            'modprobe', 'applesmc',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'modprobe', 'apple-bce',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'modprobe', 'brcmfmac',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+    while True:
+        ret = subprocess.call([
+            'modprobe', 'brcmfmac_wcc',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+
+
+
+    while True:
+        ret = subprocess.call([
+            'systemctl', 'start', 'iwd',
+        ])
+        if ret != 0:
+            time.sleep(1)
+        else:
+            break
+else:
+    raise NotImplementedError
diff --git a/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-udev b/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-udev
index 1ab0101..40d15ec 100755
--- a/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-udev
+++ b/platform_dotfiles/macbook_air_2018/usr/local/bin/online-fxreader-pr34-udev
@@ -34,17 +34,20 @@ def run() -> None:
                 ''',
             ),
             intel_pstate=dict(
-                devpath='sys/devices/system/cpu/cpu0',
+                devpath=r'/?sys/devices/system/cpu/cpu0',
                 node='/sys/devices/system/cpu/intel_pstate/no_turbo',
                 cmd=r'''
                     chown root:fan /sys/devices/system/cpu/intel_pstate/no_turbo
-                    chown root:fan /sys/devices/system/cpu/intel_pstate/status
+                    chown root:fan /sys/devices/system/cpu/intel_pstate/max_perf_pct
+                    #chown root:fan /sys/devices/system/cpu/intel_pstate/status
                     chmod g+w /sys/devices/system/cpu/intel_pstate/no_turbo
-                    chmod g+w /sys/devices/system/cpu/intel_pstate/status
+                    chmod g+w /sys/devices/system/cpu/intel_pstate/max_perf_pct
+                    #chmod g+w /sys/devices/system/cpu/intel_pstate/status
+                    echo passive > /sys/devices/system/cpu/intel_pstate/status
                 ''',
             ),
             governor=dict(
-                devpath=r'sys/devices/system/cpu/cpu(\d+)',
+                devpath=r'/?sys/devices/system/cpu/cpu(\d+)',
                 node=r'/sys/devices/system/cpu/cpu{0}/cpufreq/scaling_governor',
                 cmd=r'''
                     chown root:fan /sys/devices/system/cpu/cpu{0}/cpufreq/scaling_governor
@@ -71,8 +74,9 @@ def run() -> None:
 
         # logger.info(dict(devpath_m=devpath_m, node=node_2))
 
-        if not os.path.exists(node_2):
-            continue
+        while not os.path.exists(node_2):
+            #continue
+            time.sleep(1)
 
         cmd_2 = v['cmd'].format(*devpath_m.groups())
 
diff --git a/python/online/fxreader/pr34/commands.py b/python/online/fxreader/pr34/commands.py
index 1bca4ca..5100c83 100644
--- a/python/online/fxreader/pr34/commands.py
+++ b/python/online/fxreader/pr34/commands.py
@@ -2622,7 +2622,9 @@ echo 2000 > /sys/bus/platform/devices/applesmc.768/fan1_output;
             elif cls.profile() == 'intel_pstate':
                 subprocess.check_call(r'''
 echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
-echo powersave | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
+echo 40 > /sys/devices/system/cpu/intel_pstate/max_perf_pct;
+echo 900000 | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq;
+echo schedutil | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
                 ''', shell=True)
             else:
                 raise NotImplementedError
@@ -2639,7 +2641,9 @@ echo 6500 > /sys/bus/platform/devices/applesmc.768/fan1_output;
             elif cls.profile() == 'intel_pstate':
                 subprocess.check_call(r'''
 echo powersave | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
-echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
+echo 60 > /sys/devices/system/cpu/intel_pstate/max_perf_pct;
+echo 1200000 | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq;
+echo schedutil | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
                 ''', shell=True)
             else:
                 raise NotImplementedError
@@ -2721,12 +2725,14 @@ echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor;
                     r'''
                         exec sh -c 'echo cpufreq, user; whoami;
                         while [[ -a /proc/{pid} ]]; do
-                            echo passive > /sys/devices/system/cpu/intel_pstate/status;
+                            #echo passive > /sys/devices/system/cpu/intel_pstate/status;
                             echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo;
-                            echo 40 > /sys/devices/system/cpu/intel_pstate/max_perf_pct;
+                            #echo 40 > /sys/devices/system/cpu/intel_pstate/max_perf_pct;
                             for cpu_path in /sys/devices/system/cpu/cpu?; do
-                                echo 900000 > $cpu_path/cpufreq/scaling_max_freq;
-                                echo schedutil > $cpu_path/cpufreq/scaling_governor;
+                                #online-fxreader-pr34-udev --device $cpu_path;
+                                #echo 900000 > $cpu_path/cpufreq/scaling_max_freq;
+                                #echo schedutil > $cpu_path/cpufreq/scaling_governor;
+                                true;
                             done;
                             sleep 10;
                         done;'
diff --git a/python/pyproject.toml b/python/pyproject.toml
index 7c9cfae..731073b 100644
--- a/python/pyproject.toml
+++ b/python/pyproject.toml
@@ -1,6 +1,6 @@
 [project]
 name = 'online.fxreader.pr34'
-version = '0.1.5.3'
+version = '0.1.5.8'
 
 dependencies = [
     #"-r requirements.txt",
diff --git a/releases/whl/online_fxreader_pr34-0.1.5.4-py3-none-any.whl b/releases/whl/online_fxreader_pr34-0.1.5.4-py3-none-any.whl
new file mode 100644
index 0000000..2be2de3
--- /dev/null
+++ b/releases/whl/online_fxreader_pr34-0.1.5.4-py3-none-any.whl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:cb036a334f814d571262104ec3d5691a03b69c0b2d235df3ded59cb7dcfd4c66
+size 69278
diff --git a/releases/whl/online_fxreader_pr34-0.1.5.5-py3-none-any.whl b/releases/whl/online_fxreader_pr34-0.1.5.5-py3-none-any.whl
new file mode 100644
index 0000000..d3200b1
--- /dev/null
+++ b/releases/whl/online_fxreader_pr34-0.1.5.5-py3-none-any.whl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:466c5a30f2030ffa1aa89571bae65efb9eaed463f78e8fe6bb2554c3c4309068
+size 69277
diff --git a/releases/whl/online_fxreader_pr34-0.1.5.6-py3-none-any.whl b/releases/whl/online_fxreader_pr34-0.1.5.6-py3-none-any.whl
new file mode 100644
index 0000000..0b06bcd
--- /dev/null
+++ b/releases/whl/online_fxreader_pr34-0.1.5.6-py3-none-any.whl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:b4c6ff98228d758359b787a2eeb3afc2ff5d5002badcf4bfe625a4bf80f26d91
+size 69310
diff --git a/releases/whl/online_fxreader_pr34-0.1.5.7-py3-none-any.whl b/releases/whl/online_fxreader_pr34-0.1.5.7-py3-none-any.whl
new file mode 100644
index 0000000..9638d45
--- /dev/null
+++ b/releases/whl/online_fxreader_pr34-0.1.5.7-py3-none-any.whl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:cf8a6ad7c66dc5002c6a841b37d014c1b2d94fc29a48a980c072645f920179f3
+size 69318
diff --git a/releases/whl/online_fxreader_pr34-0.1.5.8-py3-none-any.whl b/releases/whl/online_fxreader_pr34-0.1.5.8-py3-none-any.whl
new file mode 100644
index 0000000..1640afe
--- /dev/null
+++ b/releases/whl/online_fxreader_pr34-0.1.5.8-py3-none-any.whl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:e7b7b718e80a0bfd7d64edc793fb43c9a5d62ae629a2da18f926ebbfb0c9d98f
+size 69317