From 3f80e18d9fc986ea9228be1e7b531df213862877 Mon Sep 17 00:00:00 2001 From: meng-xinhai Date: Mon, 10 Mar 2025 17:44:16 +0800 Subject: [PATCH 1/3] Fallback code Signed-off-by: meng-xinhai Change-Id: I80c9a0bac7be10fd6bcc8a4b69d0314bffd37f5a --- services/include/sensor_data_processer.h | 2 +- services/src/sensor_data_processer.cpp | 25 ++-- .../coverage/report_data_callback_test.cpp | 27 +++-- utils/common/include/report_data_callback.h | 25 ++-- utils/common/src/report_data_callback.cpp | 112 ++++++------------ 5 files changed, 74 insertions(+), 117 deletions(-) diff --git a/services/include/sensor_data_processer.h b/services/include/sensor_data_processer.h index c344aee3..a4428b5a 100755 --- a/services/include/sensor_data_processer.h +++ b/services/include/sensor_data_processer.h @@ -46,7 +46,7 @@ private: uint64_t fifoCount); void SendRawData(std::unordered_map &cacheBuf, sptr channel, std::vector events); - void EventFilter(SensorData *event); + void EventFilter(CircularEventBuf &eventsBuf); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); std::mutex dataCountMutex_; diff --git a/services/src/sensor_data_processer.cpp b/services/src/sensor_data_processer.cpp index c008752d..2ea5890e 100755 --- a/services/src/sensor_data_processer.cpp +++ b/services/src/sensor_data_processer.cpp @@ -259,13 +259,9 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorData &data, sptrsensorTypeId; + int32_t sensorId = eventsBuf.circularBuf[eventsBuf.readPos].sensorTypeId; if (sensorId == SENSOR_TYPE_ID_HALL_EXT) { - PrintSensorData::GetInstance().PrintSensorDataLog("EventFilter", *event); + PrintSensorData::GetInstance().PrintSensorDataLog("EventFilter", eventsBuf.circularBuf[eventsBuf.readPos]); } std::vector> channelList = clientInfo_.GetSensorChannel(sensorId); for (auto &channel : channelList) { @@ -273,7 +269,7 @@ void SensorDataProcesser::EventFilter(SensorData *event) SEN_HILOGW("Sensor status is not active"); continue; } - SendEvents(channel, *event); + SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); } } @@ -283,14 +279,19 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback std::unique_lock lk(ISensorHdiConnection::dataMutex_); ISensorHdiConnection::dataCondition_.wait(lk, [this] { return ISensorHdiConnection::dataReady_.load(); }); ISensorHdiConnection::dataReady_.store(false); - std::vector events; - dataCallback->GetEventData(events); - if (events.empty()) { + auto &eventsBuf = dataCallback->GetEventData(); + if (eventsBuf.eventNum <= 0) { SEN_HILOGE("Data cannot be empty"); return NO_EVENT; } - for (size_t i = 0; i < events.size(); i++) { - EventFilter(events[i]); + int32_t eventNum = eventsBuf.eventNum; + for (int32_t i = 0; i < eventNum; i++) { + EventFilter(eventsBuf); + eventsBuf.readPos++; + if (eventsBuf.readPos >= CIRCULAR_BUF_LEN) { + eventsBuf.readPos = 0; + } + eventsBuf.eventNum--; } return SUCCESS; } diff --git a/test/unittest/coverage/report_data_callback_test.cpp b/test/unittest/coverage/report_data_callback_test.cpp index 6112c729..706d9ec4 100755 --- a/test/unittest/coverage/report_data_callback_test.cpp +++ b/test/unittest/coverage/report_data_callback_test.cpp @@ -24,8 +24,7 @@ namespace OHOS { namespace Sensors { using namespace testing::ext; namespace { - constexpr uint8_t BLOCK_EVENT_BUF_LEN = 16; - SensorData* g_sensorData = new (std::nothrow) SensorData[BLOCK_EVENT_BUF_LEN]; + SensorData* g_sensorData = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; } // namespace class SensorBasicDataChannelTest : public testing::Test { @@ -59,11 +58,11 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_001, TestSize.Level1 int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, cb); ASSERT_EQ(ret, ERROR); - sptr callback = new (std::nothrow) ReportDataCallback(); - if (!callback->eventsBuf_.blockList.empty()) { - callback->eventsBuf_.blockList.clear(); + if (callback->eventsBuf_.circularBuf != nullptr) { + delete[] callback->eventsBuf_.circularBuf; + callback->eventsBuf_.circularBuf = nullptr; } - callback->eventsBuf_.blockList.clear(); + callback->eventsBuf_.circularBuf = nullptr; ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERROR); } @@ -74,9 +73,12 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_002, TestSize.Level1 ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN + 1; + callback->eventsBuf_.writePosition = 1; - callback->eventsBuf_.writeFullBlockNum = -1; - int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + callback->eventsBuf_.eventNum = 1; + callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN + 1; + ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERROR); } @@ -85,7 +87,8 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_003, TestSize.Level1 SEN_HILOGI("ReportDataCallbackTest_003 in"); ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); - callback->eventsBuf_.writeFullBlockNum = BLOCK_EVENT_BUF_LEN; + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN; int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERR_OK); } @@ -95,7 +98,8 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_004, TestSize.Level1 SEN_HILOGI("ReportDataCallbackTest_004 in"); ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); - callback->eventsBuf_.writeFullBlockNum = BLOCK_EVENT_BUF_LEN + 1; + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writePosition = 1; int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERR_OK); } @@ -105,7 +109,8 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_005, TestSize.Level1 SEN_HILOGI("ReportDataCallbackTest_005 in"); ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); - callback->eventsBuf_.writeFullBlockNum = BLOCK_EVENT_BUF_LEN - 1; + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN - 1; int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERR_OK); } diff --git a/utils/common/include/report_data_callback.h b/utils/common/include/report_data_callback.h index 4e15e6c4..d02fd6c7 100755 --- a/utils/common/include/report_data_callback.h +++ b/utils/common/include/report_data_callback.h @@ -24,14 +24,14 @@ namespace OHOS { namespace Sensors { -struct SensorDataBlock { - struct SensorData *dataBuf = nullptr; - int32_t eventNum = 0; -}; - -struct SaveEventBuf { - std::vector blockList; - int32_t writeFullBlockNum; +constexpr int32_t CIRCULAR_BUF_LEN = 1024; +constexpr int32_t SENSOR_DATA_LENGTH = 64; + +struct CircularEventBuf { + struct SensorData *circularBuf; + int32_t readPos; + int32_t writePosition; + int32_t eventNum; }; class ReportDataCallback : public RefBase { @@ -39,13 +39,8 @@ public: ReportDataCallback(); ~ReportDataCallback(); int32_t ReportEventCallback(SensorData *sensorData, sptr cb); - void GetEventData(std::vector &events); - SaveEventBuf eventsBuf_; -private: - void FreeRedundantEventBuffer(); -private: - std::vector recentWriteBlockNums_; - uint32_t blockNumsUpdateIndex_ = 0; + CircularEventBuf &GetEventData(); + CircularEventBuf eventsBuf_; }; using ReportDataCb = int32_t (ReportDataCallback::*)(SensorData *sensorData, sptr cb); diff --git a/utils/common/src/report_data_callback.cpp b/utils/common/src/report_data_callback.cpp index 6577415a..6a0b6f76 100755 --- a/utils/common/src/report_data_callback.cpp +++ b/utils/common/src/report_data_callback.cpp @@ -22,107 +22,63 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; -namespace { -constexpr uint8_t EVENT_BLOCK_NUM = 64; -constexpr uint8_t BLOCK_EVENT_BUF_LEN = 16; -constexpr uint8_t RECENT_WRITE_BLOCK_NUM_SIZE = 5; -} ReportDataCallback::ReportDataCallback() { - eventsBuf_.blockList.resize(EVENT_BLOCK_NUM); - eventsBuf_.writeFullBlockNum = 0; - recentWriteBlockNums_.resize(RECENT_WRITE_BLOCK_NUM_SIZE); + eventsBuf_.circularBuf = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; + CHKPL(eventsBuf_.circularBuf); + eventsBuf_.readPos = 0; + eventsBuf_.writePosition = 0; + eventsBuf_.eventNum = 0; } ReportDataCallback::~ReportDataCallback() { - for (auto& block : eventsBuf_.blockList) { - if (block.dataBuf != nullptr) { - delete[] block.dataBuf; - block.dataBuf = nullptr; - } - block.eventNum = 0; + if (eventsBuf_.circularBuf != nullptr) { + delete[] eventsBuf_.circularBuf; + eventsBuf_.circularBuf = nullptr; } - eventsBuf_.writeFullBlockNum = 0; + eventsBuf_.circularBuf = nullptr; + eventsBuf_.readPos = 0; + eventsBuf_.writePosition = 0; + eventsBuf_.eventNum = 0; } int32_t ReportDataCallback::ReportEventCallback(SensorData *sensorData, sptr cb) { CHKPR(sensorData, ERROR); - if (cb == nullptr) { - SEN_HILOGE("Callback is null"); + if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { + SEN_HILOGE("Callback or circularBuf or event cannot be null"); return ERROR; } - if (cb->eventsBuf_.writeFullBlockNum >= static_cast(cb->eventsBuf_.blockList.size())) { - SEN_HILOGE("Event buffer more than the blockList size"); + int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum; + int32_t toEndLen = CIRCULAR_BUF_LEN - cb->eventsBuf_.writePosition; + if (leftSize < 0 || toEndLen < 0) { + SEN_HILOGE("Leftsize and toendlen cannot be less than zero"); return ERROR; } - auto& block = cb->eventsBuf_.blockList[cb->eventsBuf_.writeFullBlockNum]; - if (block.dataBuf == nullptr) { - block.dataBuf = new(std::nothrow) SensorData[BLOCK_EVENT_BUF_LEN]; - if (block.dataBuf == nullptr) { - SEN_HILOGE("New block buffer fail"); - return ERROR; - } - block.eventNum = 0; - } - if (block.eventNum < BLOCK_EVENT_BUF_LEN) { - block.dataBuf[block.eventNum] = *sensorData; - block.eventNum++; + if (toEndLen == 0) { + cb->eventsBuf_.circularBuf[0] = *sensorData; + cb->eventsBuf_.writePosition = 1; + } else { + cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *sensorData; + cb->eventsBuf_.writePosition += 1; } - if (block.eventNum >= BLOCK_EVENT_BUF_LEN) { - cb->eventsBuf_.writeFullBlockNum++; - } - return ERR_OK; -} - -void ReportDataCallback::GetEventData(std::vector &events) -{ - int32_t writeBlockNum = 0; - for (auto& block : eventsBuf_.blockList) { - if (block.dataBuf == nullptr) { - break; - } - if (block.eventNum <= 0) { - SEN_HILOGE("Get eventNum fail"); - break; - } - writeBlockNum++; - for (int32_t i = 0; i < block.eventNum; ++i) { - events.push_back(&block.dataBuf[i]); - } - block.eventNum = 0; + cb->eventsBuf_.eventNum += 1; + if (cb->eventsBuf_.eventNum >= CIRCULAR_BUF_LEN) { + cb->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; } - eventsBuf_.writeFullBlockNum = 0; - recentWriteBlockNums_[blockNumsUpdateIndex_] = writeBlockNum; - blockNumsUpdateIndex_++; - if (blockNumsUpdateIndex_ >= recentWriteBlockNums_.size()) { - blockNumsUpdateIndex_ = 0; + if (cb->eventsBuf_.writePosition >= CIRCULAR_BUF_LEN) { + cb->eventsBuf_.writePosition = 0; } - if (!events.empty()) { - FreeRedundantEventBuffer(); + if (leftSize < 1) { + cb->eventsBuf_.readPos = cb->eventsBuf_.writePosition; } + return ERR_OK; } -void ReportDataCallback::FreeRedundantEventBuffer() +CircularEventBuf &ReportDataCallback::GetEventData() { - int32_t maxWriteBlockNum = 0; - for (auto num : recentWriteBlockNums_) { - maxWriteBlockNum = std::max(maxWriteBlockNum, num); - } - - // keep at least 1 block - if (maxWriteBlockNum <= 0) { - return; - } - - for (int32_t index = maxWriteBlockNum; index < static_cast(eventsBuf_.blockList.size()); ++index) { - if (eventsBuf_.blockList[index].dataBuf == nullptr) { - break; - } - delete[] eventsBuf_.blockList[index].dataBuf; - eventsBuf_.blockList[index].dataBuf = nullptr; - } + return eventsBuf_; } } // namespace Sensors } // namespace OHOS -- Gitee From 60086f4d00c8869bda4fd22316d9d119a80bc9ea Mon Sep 17 00:00:00 2001 From: meng-xinhai Date: Mon, 10 Mar 2025 17:53:45 +0800 Subject: [PATCH 2/3] Fallback code Signed-off-by: meng-xinhai Change-Id: I3d0cddfa4a8468a9a116f0e0f4802973dc7fb6c5 --- services/src/sensor_data_processer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/src/sensor_data_processer.cpp b/services/src/sensor_data_processer.cpp index 2ea5890e..11d81157 100755 --- a/services/src/sensor_data_processer.cpp +++ b/services/src/sensor_data_processer.cpp @@ -257,7 +257,7 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorData &data, sptr Date: Mon, 10 Mar 2025 18:10:35 +0800 Subject: [PATCH 3/3] Fallback code Signed-off-by: meng-xinhai Change-Id: Id6c7244f2d881c3052bbdea6996839d526a455e0 --- test/unittest/coverage/report_data_callback_test.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unittest/coverage/report_data_callback_test.cpp b/test/unittest/coverage/report_data_callback_test.cpp index 706d9ec4..01a5c937 100755 --- a/test/unittest/coverage/report_data_callback_test.cpp +++ b/test/unittest/coverage/report_data_callback_test.cpp @@ -58,6 +58,7 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_001, TestSize.Level1 int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, cb); ASSERT_EQ(ret, ERROR); + sptr callback = new (std::nothrow) ReportDataCallback(); if (callback->eventsBuf_.circularBuf != nullptr) { delete[] callback->eventsBuf_.circularBuf; callback->eventsBuf_.circularBuf = nullptr; @@ -75,7 +76,9 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_002, TestSize.Level1 sptr callback = new (std::nothrow) ReportDataCallback(); callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN + 1; callback->eventsBuf_.writePosition = 1; - + int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERROR); + callback->eventsBuf_.eventNum = 1; callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN + 1; ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); -- Gitee