From a26ad205cdfbb37b0145b71e0af68fa293d5d90b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?YuSaki=E4=B8=B6Kanade?= Date: Wed, 11 Sep 2024 23:14:35 +0800 Subject: [PATCH] Fixed abnormal cpu usage --- .../cirno/services/BinderService.java | 95 ++++++++++--------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/nep/timeline/cirno/services/BinderService.java b/app/src/main/java/nep/timeline/cirno/services/BinderService.java index aa80dc5..c1c5325 100644 --- a/app/src/main/java/nep/timeline/cirno/services/BinderService.java +++ b/app/src/main/java/nep/timeline/cirno/services/BinderService.java @@ -9,6 +9,8 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import nep.timeline.cirno.GlobalVars; import nep.timeline.cirno.entity.AppRecord; @@ -19,6 +21,7 @@ import nep.timeline.cirno.utils.StringUtils; public class BinderService { + private final static ExecutorService executorService = Executors.newSingleThreadExecutor(); public static boolean received = false; private static boolean isRunning = false; private static final int NETLINK_UNIT_DEFAULT = 22; @@ -28,37 +31,37 @@ public static void start(ClassLoader classLoader) { if (isRunning) return; - try { - int netlinkUnit; - int configNetlinkUnit = GlobalVars.globalSettings.netlinkUnit; - if (configNetlinkUnit >= NETLINK_UNIT_DEFAULT && configNetlinkUnit <= NETLINK_UNIT_MAX) { - netlinkUnit = configNetlinkUnit; - } else { - File dir = new File("/proc/rekernel"); - if (dir.exists()) { - File[] files = dir.listFiles(); - if (files == null) { - Log.e("找不到ReKernel单元"); + executorService.execute(() -> { + try { + int netlinkUnit; + int configNetlinkUnit = GlobalVars.globalSettings.netlinkUnit; + if (configNetlinkUnit >= NETLINK_UNIT_DEFAULT && configNetlinkUnit <= NETLINK_UNIT_MAX) { + netlinkUnit = configNetlinkUnit; + } else { + File dir = new File("/proc/rekernel"); + if (dir.exists()) { + File[] files = dir.listFiles(); + if (files == null) { + Log.e("找不到ReKernel单元"); + return; + } + File unitFile = files[0]; + netlinkUnit = StringUtils.StringToInteger(unitFile.getName()); + } else netlinkUnit = NETLINK_UNIT_DEFAULT; + } + + try (NetlinkClient netlinkClient = new NetlinkClient(classLoader, netlinkUnit)) { + if (!netlinkClient.getMDescriptor().valid()) { + Log.e("无法连接至ReKernel服务器"); return; } - File unitFile = files[0]; - netlinkUnit = StringUtils.StringToInteger(unitFile.getName()); - } else netlinkUnit = NETLINK_UNIT_DEFAULT; - } - try (NetlinkClient netlinkClient = new NetlinkClient(classLoader, netlinkUnit)) { - if (!netlinkClient.getMDescriptor().valid()) { - Log.e("无法连接至ReKernel服务器"); - return; - } - - netlinkClient.bind((SocketAddress) new NetlinkSocketAddress(100).toInstance(classLoader)); + netlinkClient.bind((SocketAddress) new NetlinkSocketAddress(100).toInstance(classLoader)); - isRunning = true; + isRunning = true; - Log.i("已连接至ReKernel, " + netlinkUnit + "#100"); + Log.i("已连接至ReKernel, " + netlinkUnit + "#100"); - Handlers.rekernel.post(() -> { while (true) { try { ByteBuffer byteBuffer = netlinkClient.recvMessage(); @@ -68,24 +71,26 @@ public static void start(ClassLoader classLoader) { Log.i("成功接收到来自ReKernel的消息"); received = true; } - String type = StringUtils.getSubString(data, "type=", ",").trim(); - if (type.equals("Binder")) { - String bindertype = StringUtils.getSubString(data, "bindertype=", ",").trim(); - int oneway = StringUtils.StringToInteger(StringUtils.getSubString(data, "oneway=", ",")); - int targetUid = StringUtils.StringToInteger(StringUtils.getSubString(data, "target=", ";")); - if (oneway == 1 && !bindertype.equals("free_buffer_full")) - return; + Handlers.rekernel.post(() -> { + String type = StringUtils.getSubString(data, "type=", ",").trim(); + if (type.equals("Binder")) { + String bindertype = StringUtils.getSubString(data, "bindertype=", ",").trim(); + int oneway = StringUtils.StringToInteger(StringUtils.getSubString(data, "oneway=", ",")); + int targetUid = StringUtils.StringToInteger(StringUtils.getSubString(data, "target=", ";")); + if (oneway == 1 && !bindertype.equals("free_buffer_full")) + return; - List appRecords = AppService.getByUid(targetUid); - if (appRecords == null || appRecords.isEmpty()) - return; - for (AppRecord appRecord : appRecords) { - if (appRecord == null) - continue; + List appRecords = AppService.getByUid(targetUid); + if (appRecords == null || appRecords.isEmpty()) + return; + for (AppRecord appRecord : appRecords) { + if (appRecord == null) + continue; - FreezerService.temporaryUnfreezeIfNeed(appRecord, "内核Binder(" + (oneway == 1 ? "ASYNC" : "SYNC") + "), 类型: " + bindertype, 3000); + FreezerService.temporaryUnfreezeIfNeed(appRecord, "内核Binder(" + (oneway == 1 ? "ASYNC" : "SYNC") + "), 类型: " + bindertype, 3000); + } } - } + }); } } catch (ErrnoException | InterruptedIOException | NumberFormatException ignored) { @@ -94,10 +99,12 @@ public static void start(ClassLoader classLoader) { Log.e("ReKernel", e); } } - }); + } + } catch (ErrnoException | IOException e) { + Log.e("无法连接至ReKernel服务器"); + } catch (Throwable throwable) { + Log.e("ReKernel", throwable); } - } catch (ErrnoException | IOException e) { - Log.e("无法连接至ReKernel服务器"); - } + }); } }