From 662e7443571ffce9b15e7183e1416a4e383ec41f Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Thu, 1 Feb 2024 19:48:44 +0800 Subject: [PATCH 1/2] modify dinput cost too much power Signed-off-by: hwzhangchuang --- common/include/input_hub.cpp | 57 ++++++++++++------- common/include/input_hub.h | 16 ++++-- .../src/distributed_input_handler.cpp | 2 +- .../distributed_input_handler_test.cpp | 2 +- .../src/distributed_input_collector.cpp | 3 +- .../distributed_input_collector_test.cpp | 2 +- .../include/distributed_input_node_manager.h | 1 - .../src/distributed_input_node_manager.cpp | 3 +- utils/src/dinput_utils_tool.cpp | 4 +- 9 files changed, 55 insertions(+), 35 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index ef403e1..e24c3b1 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -44,9 +44,9 @@ const uint32_t SLEEP_TIME_US = 100 * 1000; const std::string MOUSE_NODE_KEY = "mouse"; } -InputHub::InputHub() : epollFd_(0), iNotifyFd_(0), inputWd_(0), needToScanDevices_(true), - mPendingEventItems{}, pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), deviceChanged_(false), - inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false) +InputHub::InputHub(bool isPluginMonitor) : epollFd_(-1), iNotifyFd_(-1), inputWd_(-1), isPluginMonitor_(isPluginMonitor), + needToScanDevices_(true), mPendingEventItems{}, pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), + deviceChanged_(false), inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false) { Initialize(); } @@ -64,21 +64,25 @@ int32_t InputHub::Initialize() return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL; } - iNotifyFd_ = inotify_init(); - inputWd_ = inotify_add_watch(iNotifyFd_, DEVICE_PATH, IN_DELETE | IN_CREATE); - if (inputWd_ < 0) { - DHLOGE( - "Could not register INotify for %s: %s", DEVICE_PATH, ConvertErrNo().c_str()); - return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL; - } + if (isPluginMonitor_) { + DHLOGI("Init InputHub for device plugin monitor"); + iNotifyFd_ = inotify_init(); + inputWd_ = inotify_add_watch(iNotifyFd_, DEVICE_PATH, IN_DELETE | IN_CREATE); + if (inputWd_ < 0) { + DHLOGE("Could not register INotify for %s: %s", DEVICE_PATH, ConvertErrNo().c_str()); + return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL; + } - struct epoll_event eventItem = {}; - eventItem.events = EPOLLIN; - eventItem.data.fd = iNotifyFd_; - int result = epoll_ctl(epollFd_, EPOLL_CTL_ADD, iNotifyFd_, &eventItem); - if (result != 0) { - DHLOGE("Could not add INotify to epoll instance. errno=%d", errno); - return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL; + struct epoll_event eventItem = {}; + eventItem.events = EPOLLIN; + eventItem.data.fd = iNotifyFd_; + int result = epoll_ctl(epollFd_, EPOLL_CTL_ADD, iNotifyFd_, &eventItem); + if (result != 0) { + DHLOGE("Could not add INotify to epoll instance. errno=%d", errno); + return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL; + } + } else { + DHLOGI("Init InputHub for read device events"); } return DH_SUCCESS; @@ -87,11 +91,22 @@ int32_t InputHub::Initialize() int32_t InputHub::Release() { CloseAllDevicesLocked(); + if (epollFd_ != -1) { + ::close(epollFd_); + epollFd_ = -1; + } + + if (iNotifyFd_ != -1) { + ::close(iNotifyFd_); + iNotifyFd_ = -1; + } + + if (isPluginMonitor_) { + StopCollectInputHandler(); + } else { + StopCollectInputEvents(); + } - ::close(epollFd_); - ::close(iNotifyFd_); - StopCollectInputEvents(); - StopCollectInputHandler(); sharedDHIds_.clear(); return DH_SUCCESS; } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index a12e230..f96fa41 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -75,7 +75,7 @@ public: int32_t absYIndex; }; - InputHub(); + InputHub(bool isPluginMonitor); ~InputHub(); size_t StartCollectInputEvents(RawEvent *buffer, size_t bufferSize); size_t StartCollectInputHandler(InputDeviceEvent *buffer, size_t bufferSize); @@ -103,6 +103,11 @@ public: void SavePressedKeyState(const Device *dev, int32_t keyCode); void ClearDeviceStates(); void ClearSkipDevicePaths(); + /* + * Scan the input device node and save info. + */ + void ScanAndRecordInputDevices(); + private: int32_t Initialize(); int32_t Release(); @@ -185,10 +190,6 @@ private: void MatchAndDealEvent(Device *device, const RawEvent &event); void DealTouchPadEvent(const RawEvent &event); void DealNormalKeyEvent(Device *device, const RawEvent &event); - /* - * Scan the input device node and save info. - */ - void ScanAndRecordInputDevices(); /* * Check is this node has been scaned for collecting info. @@ -209,6 +210,11 @@ private: int epollFd_; int iNotifyFd_; int inputWd_; + /* + * true: for just monitor device plugin/unplugin; + * false: for read device events. + */ + bool isPluginMonitor_; std::vector> openingDevices_; std::vector> closingDevices_; diff --git a/inputdevicehandler/src/distributed_input_handler.cpp b/inputdevicehandler/src/distributed_input_handler.cpp index c99ff35..6f2e6c4 100644 --- a/inputdevicehandler/src/distributed_input_handler.cpp +++ b/inputdevicehandler/src/distributed_input_handler.cpp @@ -44,7 +44,7 @@ IMPLEMENT_SINGLE_INSTANCE(DistributedInputHandler); DistributedInputHandler::DistributedInputHandler() : collectThreadID_(-1), isCollectingEvents_(false), isStartCollectEventThread(false) { - inputHub_ = std::make_unique(); + inputHub_ = std::make_unique(true); this->m_listener = nullptr; } diff --git a/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp b/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp index bcf713b..47765dc 100644 --- a/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp +++ b/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp @@ -75,7 +75,7 @@ HWTEST_F(DInputHandlerTest, FindDevicesInfoByType_001, testing::ext::TestSize.Le dInputHandler.NotifyHardWare(2); std::map ret = dInputHandler.QueryExtraInfo(); EXPECT_EQ(0, ret.size()); - dInputHandler.inputHub_ = std::make_unique(); + dInputHandler.inputHub_ = std::make_unique(true); dInputHandler.StartInputMonitorDeviceThread(); } } // namespace DistributedInput diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 27c8093..48e05d9 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -39,7 +39,7 @@ namespace DistributedInput { DistributedInputCollector::DistributedInputCollector() : mEventBuffer{}, collectThreadID_(-1), isCollectingEvents_(false), isStartGetDeviceHandlerThread(false), inputTypes_(0) { - inputHub_ = std::make_unique(); + inputHub_ = std::make_unique(false); } DistributedInputCollector::~DistributedInputCollector() @@ -265,6 +265,7 @@ void DistributedInputCollector::ClearSkipDevicePaths() return; } inputHub_->ClearSkipDevicePaths(); + inputHub_->ScanAndRecordInputDevices(); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp b/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp index 0c2d5ef..30a46bd 100644 --- a/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp +++ b/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp @@ -97,7 +97,7 @@ HWTEST_F(DistributedInputCollectorTest, IsAllDevicesStoped02, testing::ext::Test HWTEST_F(DistributedInputCollectorTest, SetSharingTypes01, testing::ext::TestSize.Level1) { - DistributedInputCollector::GetInstance().inputHub_ = std::make_unique(); + DistributedInputCollector::GetInstance().inputHub_ = std::make_unique(false); bool enabled = true; uint32_t inputType = static_cast(DInputDeviceType::ALL); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index ace7b0e..0dc111b 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -124,7 +124,6 @@ private: std::mutex injectThreadMutex_; std::condition_variable conditionVariable_; std::queue injectQueue_; - std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; std::shared_ptr callBackHandler_; diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 8b3954c..25e589e 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -32,7 +32,7 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), - isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) + isInjectThreadRunning_(false), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { DHLOGI("DistributedInputNodeManager ctor"); std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); @@ -345,7 +345,6 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice const std::string &dhId) { std::unique_lock my_lock(operationMutex_); - std::call_once(callOnceFlag_, [this]() { inputHub_->ScanInputDevices(DEVICE_PATH); }); std::unique_ptr virtualDevice = std::make_unique(inputDevice); virtualDevice->SetNetWorkId(devId); diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index b6eb6e5..a457c4f 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -305,12 +305,12 @@ int OpenInputDeviceFdByPath(const std::string &devicePath) DHLOGI("path: %s is a dir.", devicePath.c_str()); return -1; } - int fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); + int fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC); int32_t count = 0; while ((fd < 0) && (count < MAX_RETRY_COUNT)) { ++count; usleep(SLEEP_TIME_US); - fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK); + fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC); DHLOGE("could not open the path: %s, errno: %s; retry: %d", devicePath.c_str(), ConvertErrNo().c_str(), count); } if (count >= MAX_RETRY_COUNT) { -- Gitee From 43698436e2b53e3a7463a9e4bed11d08c2d62823 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Thu, 1 Feb 2024 20:09:46 +0800 Subject: [PATCH 2/2] add Signed-off-by: hwzhangchuang --- common/include/input_hub.cpp | 7 ++++--- common/include/input_hub.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index e24c3b1..5a75651 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -44,9 +44,10 @@ const uint32_t SLEEP_TIME_US = 100 * 1000; const std::string MOUSE_NODE_KEY = "mouse"; } -InputHub::InputHub(bool isPluginMonitor) : epollFd_(-1), iNotifyFd_(-1), inputWd_(-1), isPluginMonitor_(isPluginMonitor), - needToScanDevices_(true), mPendingEventItems{}, pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), - deviceChanged_(false), inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false) +InputHub::InputHub(bool isPluginMonitor) : epollFd_(-1), iNotifyFd_(-1), inputWd_(-1), + isPluginMonitor_(isPluginMonitor), needToScanDevices_(true), mPendingEventItems{}, + pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), deviceChanged_(false), + inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false) { Initialize(); } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index f96fa41..4d31827 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -75,7 +75,7 @@ public: int32_t absYIndex; }; - InputHub(bool isPluginMonitor); + explicit InputHub(bool isPluginMonitor); ~InputHub(); size_t StartCollectInputEvents(RawEvent *buffer, size_t bufferSize); size_t StartCollectInputHandler(InputDeviceEvent *buffer, size_t bufferSize); -- Gitee