mirror of
https://github.com/openharmony/distributedhardware_distributed_input.git
synced 2026-07-19 17:43:35 -04:00
Description:fix fd leak and reduce cpu use rate
Match-id-1c39041f702b3b057abc95718dc7077b3b4e1cff
This commit is contained in:
@@ -78,6 +78,8 @@ namespace DistributedInput {
|
||||
|
||||
constexpr int32_t SESSION_WAIT_TIMEOUT_SECOND = 5;
|
||||
|
||||
constexpr int32_t EPOLL_WAITTIME = -1;
|
||||
|
||||
/* The input device is a keyboard or has buttons. */
|
||||
constexpr uint32_t INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001;
|
||||
|
||||
|
||||
@@ -381,7 +381,7 @@ void InputHub::GetDeviceHandler()
|
||||
int32_t InputHub::RefreshEpollItem(bool isSleep)
|
||||
{
|
||||
pendingEventIndex_ = 0;
|
||||
int pollResult = epoll_wait(epollFd_, mPendingEventItems, EPOLL_MAX_EVENTS, 0);
|
||||
int pollResult = epoll_wait(epollFd_, mPendingEventItems, EPOLL_MAX_EVENTS, EPOLL_WAITTIME);
|
||||
if (pollResult == 0) {
|
||||
// Timed out.
|
||||
pendingEventCount_ = 0;
|
||||
@@ -445,15 +445,31 @@ void InputHub::ScanInputDevices(const std::string& dirname)
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
void InputHub::CloseFd(int fd)
|
||||
{
|
||||
if (fd < 0) {
|
||||
DHLOGE("No fd need to be closed.");
|
||||
return;
|
||||
}
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
bool InputHub::IsDeviceRegistered(const std::string& devicePath)
|
||||
{
|
||||
std::unique_lock<std::mutex> deviceLock(devicesMutex_);
|
||||
for (const auto& [deviceId, device] : devices_) {
|
||||
if (device->path == devicePath) {
|
||||
return true; // device was already registered
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath)
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> deviceLock(devicesMutex_);
|
||||
for (const auto& [deviceId, device] : devices_) {
|
||||
if (device->path == devicePath) {
|
||||
return DH_SUCCESS; // device was already registered
|
||||
}
|
||||
}
|
||||
if (IsDeviceRegistered(devicePath)) {
|
||||
return DH_SUCCESS;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> my_lock(operationMutex_);
|
||||
@@ -481,11 +497,13 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath)
|
||||
}
|
||||
if (count >= MAX_RETRY_COUNT) {
|
||||
DHLOGE("could not open %s, %s\n", devicePath.c_str(), ConvertErrNo().c_str());
|
||||
CloseFd(fd);
|
||||
return ERR_DH_INPUT_HUB_OPEN_DEVICEPATH_FAIL;
|
||||
}
|
||||
|
||||
InputDevice identifier;
|
||||
if (QueryInputDeviceInfo(fd, identifier) < 0) {
|
||||
CloseFd(fd);
|
||||
return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL;
|
||||
}
|
||||
GenerateDescriptor(identifier);
|
||||
@@ -496,6 +514,7 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string& devicePath)
|
||||
RecordDeviceLog(deviceId, devicePath, identifier);
|
||||
|
||||
if (MakeDevice(fd, std::move(device)) < 0) {
|
||||
CloseFd(fd);
|
||||
return ERR_DH_INPUT_HUB_MAKE_DEVICE_FAIL;
|
||||
}
|
||||
|
||||
@@ -522,14 +541,12 @@ int32_t InputHub::QueryInputDeviceInfo(int fd, InputDevice& identifier)
|
||||
int driverVersion;
|
||||
if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
|
||||
DHLOGE("could not get driver version for %s\n", ConvertErrNo().c_str());
|
||||
close(fd);
|
||||
return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL;
|
||||
}
|
||||
// Get device identifier.
|
||||
struct input_id inputId;
|
||||
if (ioctl(fd, EVIOCGID, &inputId)) {
|
||||
DHLOGE("could not get device input id for %s\n", ConvertErrNo().c_str());
|
||||
close(fd);
|
||||
return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL;
|
||||
}
|
||||
identifier.bus = inputId.bustype;
|
||||
|
||||
@@ -121,6 +121,8 @@ private:
|
||||
Device* GetDeviceByPathLocked(const std::string& devicePath);
|
||||
Device* GetDeviceByFdLocked(int fd);
|
||||
Device* GetSupportDeviceByFd(int fd);
|
||||
void CloseFd(int fd);
|
||||
bool IsDeviceRegistered(const std::string& devicePath);
|
||||
|
||||
bool ContainsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex);
|
||||
int64_t ProcessEventTimestamp(const input_event& event);
|
||||
|
||||
@@ -550,8 +550,9 @@ void DistributedInputSinkManager::DInputSinkListener::CheckKeyState(const int32_
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (fd > 0) {
|
||||
if (fd >= 0) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -218,7 +218,7 @@ void DistributedInputTransportBase::StopSession(const std::string &remoteDevId)
|
||||
std::unique_lock<std::mutex> sessionLock(operationMutex_);
|
||||
|
||||
if (remoteDevSessionMap_.count(remoteDevId) == 0) {
|
||||
DHLOGI("remoteDevSessionMap_ Not find remoteDevId: %s", GetAnonyString(remoteDevId).c_str());
|
||||
DHLOGE("remoteDevSessionMap not find remoteDevId: %s", GetAnonyString(remoteDevId).c_str());
|
||||
return;
|
||||
}
|
||||
int32_t sessionId = remoteDevSessionMap_[remoteDevId];
|
||||
@@ -288,15 +288,15 @@ int32_t DistributedInputTransportBase::OnSessionOpened(int32_t sessionId, int32_
|
||||
char peerDevId[DEVICE_ID_SIZE_MAX] = {0};
|
||||
int32_t ret = GetMySessionName(sessionId, mySessionName, sizeof(mySessionName));
|
||||
if (ret != DH_SUCCESS) {
|
||||
DHLOGI("get my session name failed, session id is %d", sessionId);
|
||||
DHLOGE("get my session name failed, session id is %d", sessionId);
|
||||
}
|
||||
ret = GetPeerSessionName(sessionId, peerSessionName, sizeof(peerSessionName));
|
||||
if (ret != DH_SUCCESS) {
|
||||
DHLOGI("get peer session name failed, session id is %d", sessionId);
|
||||
DHLOGE("get peer session name failed, session id is %d", sessionId);
|
||||
}
|
||||
ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId));
|
||||
if (ret != DH_SUCCESS) {
|
||||
DHLOGI("get peer device id failed, session id is %d", sessionId);
|
||||
DHLOGE("get peer device id failed, session id is %d", sessionId);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -338,13 +338,13 @@ void DistributedInputTransportBase::OnSessionClosed(int32_t sessionId)
|
||||
channelStatusMap_.erase(deviceId);
|
||||
|
||||
if (sinkCallback_ == nullptr) {
|
||||
DHLOGI("sinkCallback is nullptr.");
|
||||
DHLOGE("sinkCallback is nullptr.");
|
||||
return;
|
||||
}
|
||||
sinkCallback_->NotifySessionClosed(sessionId);
|
||||
|
||||
if (srcCallback_ == nullptr) {
|
||||
DHLOGI("srcCallback is nullptr.");
|
||||
DHLOGE("srcCallback is nullptr.");
|
||||
return;
|
||||
}
|
||||
srcCallback_->NotifySessionClosed();
|
||||
@@ -406,7 +406,7 @@ void DistributedInputTransportBase::HandleSession(int32_t sessionId, const std::
|
||||
DHLOGI("HandleSession cmdType %u.", cmdType);
|
||||
if (cmdType < TRANS_MSG_SRC_SINK_SPLIT) {
|
||||
if (srcCallback_ == nullptr) {
|
||||
DHLOGI("srcCallback is nullptr.");
|
||||
DHLOGE("srcCallback is nullptr.");
|
||||
return;
|
||||
}
|
||||
DHLOGI("HandleSession to source.");
|
||||
@@ -415,7 +415,7 @@ void DistributedInputTransportBase::HandleSession(int32_t sessionId, const std::
|
||||
}
|
||||
if (cmdType > TRANS_MSG_SRC_SINK_SPLIT) {
|
||||
if (sinkCallback_ == nullptr) {
|
||||
DHLOGI("sinkCallback is nullptr.");
|
||||
DHLOGE("sinkCallback is nullptr.");
|
||||
return;
|
||||
}
|
||||
DHLOGI("HandleSession to sink.");
|
||||
|
||||
Reference in New Issue
Block a user