diff --git a/services/include/sensor_data_processer.h b/services/include/sensor_data_processer.h old mode 100644 new mode 100755 index 802cf040b7bca9b11a5d769dca478239fb305b35..c0794915ae60917aee6c49704bd0416cc5d8461a --- a/services/include/sensor_data_processer.h +++ b/services/include/sensor_data_processer.h @@ -54,7 +54,7 @@ private: uint64_t fifoCount); void SendRawData(std::unordered_map &cacheBuf, sptr channel, std::vector events); - void EventFilter(CircularEventBuf &eventsBuf); + void EventFilter(SensorData *event); 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 old mode 100644 new mode 100755 index 55449e6b0ed1e9b47e0e212042dc80e96ec27672..6574c2359d81dd456f6eed684abfdf7b895427b0 --- a/services/src/sensor_data_processer.cpp +++ b/services/src/sensor_data_processer.cpp @@ -263,11 +263,15 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorData &data, sptrsensorTypeId; if (sensorId == SENSOR_TYPE_ID_HALL_EXT) { - PrintSensorData::GetInstance().PrintSensorDataLog("EventFilter", eventsBuf.circularBuf[eventsBuf.readPos]); + PrintSensorData::GetInstance().PrintSensorDataLog("EventFilter", *event); } std::vector> channelList = clientInfo_.GetSensorChannel(sensorId); for (auto &channel : channelList) { @@ -275,7 +279,7 @@ void SensorDataProcesser::EventFilter(CircularEventBuf &eventsBuf) SEN_HILOGW("Sensor status is not active"); continue; } - SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); + SendEvents(channel, *event); } } @@ -285,19 +289,14 @@ 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); - auto &eventsBuf = dataCallback->GetEventData(); - if (eventsBuf.eventNum <= 0) { + std::vector events; + dataCallback->GetEventData(events); + if (events.empty()) { SEN_HILOGE("Data cannot be empty"); return NO_EVENT; } - 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--; + for (size_t i = 0; i < events.size(); i++) { + EventFilter(events[i]); } return SUCCESS; } diff --git a/test/unittest/coverage/report_data_callback_test.cpp b/test/unittest/coverage/report_data_callback_test.cpp old mode 100644 new mode 100755 index e9320acbad5274f8b38eee32ab952ddc25d3c365..6112c729aa4cb2032592ee236580a62ccf9b7ae0 --- a/test/unittest/coverage/report_data_callback_test.cpp +++ b/test/unittest/coverage/report_data_callback_test.cpp @@ -23,9 +23,9 @@ namespace OHOS { namespace Sensors { using namespace testing::ext; - namespace { - SensorData* g_sensorData = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; + constexpr uint8_t BLOCK_EVENT_BUF_LEN = 16; + SensorData* g_sensorData = new (std::nothrow) SensorData[BLOCK_EVENT_BUF_LEN]; } // namespace class SensorBasicDataChannelTest : public testing::Test { @@ -60,11 +60,10 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_001, TestSize.Level1 ASSERT_EQ(ret, ERROR); sptr callback = new (std::nothrow) ReportDataCallback(); - if (callback->eventsBuf_.circularBuf != nullptr) { - delete[] callback->eventsBuf_.circularBuf; - callback->eventsBuf_.circularBuf = nullptr; + if (!callback->eventsBuf_.blockList.empty()) { + callback->eventsBuf_.blockList.clear(); } - callback->eventsBuf_.circularBuf = nullptr; + callback->eventsBuf_.blockList.clear(); ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERROR); } @@ -75,15 +74,10 @@ 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); ASSERT_EQ(ret, ERROR); - - callback->eventsBuf_.eventNum = 1; - callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN + 1; - ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); - ASSERT_EQ(ret, ERROR); } HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_003, TestSize.Level1) @@ -91,8 +85,7 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_003, TestSize.Level1 SEN_HILOGI("ReportDataCallbackTest_003 in"); ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); - callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; - callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writeFullBlockNum = BLOCK_EVENT_BUF_LEN; int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERR_OK); } @@ -102,8 +95,7 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_004, TestSize.Level1 SEN_HILOGI("ReportDataCallbackTest_004 in"); ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); - callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; - callback->eventsBuf_.writePosition = 1; + callback->eventsBuf_.writeFullBlockNum = BLOCK_EVENT_BUF_LEN + 1; int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); ASSERT_EQ(ret, ERR_OK); } @@ -113,8 +105,7 @@ HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_005, TestSize.Level1 SEN_HILOGI("ReportDataCallbackTest_005 in"); ReportDataCallback reportDataCallback = ReportDataCallback(); sptr callback = new (std::nothrow) ReportDataCallback(); - callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; - callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN - 1; + callback->eventsBuf_.writeFullBlockNum = BLOCK_EVENT_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 old mode 100644 new mode 100755 index 961f6147a57227e30eb459ab9f86fc50890fb830..e937c0748237f29103adee3057f1995a4020724d --- a/utils/common/include/report_data_callback.h +++ b/utils/common/include/report_data_callback.h @@ -16,19 +16,22 @@ #ifndef REPORT_DATA_CALLBACK_H #define REPORT_DATA_CALLBACK_H +#include + #include "refbase.h" #include "sensor_data_event.h" namespace OHOS { namespace Sensors { -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; + +struct SensorDataBlock { + struct SensorData *dataBuf = nullptr; + int32_t eventNum = 0; +}; + +struct SaveEventBuf { + std::vector blockList; + int32_t writeFullBlockNum; }; class ReportDataCallback : public RefBase { @@ -36,8 +39,13 @@ public: ReportDataCallback(); ~ReportDataCallback(); int32_t ReportEventCallback(SensorData *sensorData, sptr cb); - CircularEventBuf &GetEventData(); - CircularEventBuf eventsBuf_; + void GetEventData(std::vector &events); + SaveEventBuf eventsBuf_; +private: + void FreeRedundantEventBuffer(); +private: + std::vector recentWriteBlockNums_; + int32_t blockNumsUpdateIndex_ = 0; }; 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 old mode 100644 new mode 100755 index cd4e0d049ef55d3adab657ae35f882036133baec..519346b5584473867db41b1b8e88ca3a168e1d2b --- a/utils/common/src/report_data_callback.cpp +++ b/utils/common/src/report_data_callback.cpp @@ -22,64 +22,108 @@ 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_.circularBuf = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; - CHKPL(eventsBuf_.circularBuf); - eventsBuf_.readPos = 0; - eventsBuf_.writePosition = 0; - eventsBuf_.eventNum = 0; + eventsBuf_.blockList.resize(EVENT_BLOCK_NUM); + eventsBuf_.writeFullBlockNum = 0; + recentWriteBlockNums_.resize(RECENT_WRITE_BLOCK_NUM_SIZE); } ReportDataCallback::~ReportDataCallback() { - if (eventsBuf_.circularBuf != nullptr) { - delete[] eventsBuf_.circularBuf; - eventsBuf_.circularBuf = nullptr; + for (auto& block : eventsBuf_.blockList) { + if (block.dataBuf != nullptr) { + delete[] block.dataBuf; + block.dataBuf = nullptr; + } + block.eventNum = 0; } - eventsBuf_.circularBuf = nullptr; - eventsBuf_.readPos = 0; - eventsBuf_.writePosition = 0; - eventsBuf_.eventNum = 0; + eventsBuf_.writeFullBlockNum = 0; } int32_t ReportDataCallback::ReportEventCallback(SensorData *sensorData, sptr cb) { CHKPR(sensorData, ERROR); - if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { - SEN_HILOGE("Callback or circularBuf or event cannot be null"); + if (cb == nullptr) { + SEN_HILOGE("Callback is null"); return ERROR; } - 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"); + if (cb->eventsBuf_.writeFullBlockNum >= cb->eventsBuf_.blockList.size()) { + SEN_HILOGE("Event buffer more than the blockList size"); return ERROR; } - if (toEndLen == 0) { - cb->eventsBuf_.circularBuf[0] = *sensorData; - cb->eventsBuf_.writePosition = 1; - } else { - cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *sensorData; - cb->eventsBuf_.writePosition += 1; + 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; } - cb->eventsBuf_.eventNum += 1; - if (cb->eventsBuf_.eventNum >= CIRCULAR_BUF_LEN) { - cb->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + if (block.eventNum < BLOCK_EVENT_BUF_LEN) { + block.dataBuf[block.eventNum] = *sensorData; + block.eventNum++; } - if (cb->eventsBuf_.writePosition >= CIRCULAR_BUF_LEN) { - cb->eventsBuf_.writePosition = 0; - } - if (leftSize < 1) { - cb->eventsBuf_.readPos = cb->eventsBuf_.writePosition; + if (block.eventNum >= BLOCK_EVENT_BUF_LEN) { + cb->eventsBuf_.writeFullBlockNum++; } return ERR_OK; } -CircularEventBuf &ReportDataCallback::GetEventData() +void ReportDataCallback::GetEventData(std::vector &events) +{ + int32_t writeBlockNum = 0; + for (auto& block : eventsBuf_.blockList) { + if (block.dataBuf == nullptr) { + SEN_HILOGE("Data buf is null"); + 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; + } + eventsBuf_.writeFullBlockNum = 0; + recentWriteBlockNums_[blockNumsUpdateIndex_] = writeBlockNum; + blockNumsUpdateIndex_++; + if (blockNumsUpdateIndex_ >= recentWriteBlockNums_.size()) { + blockNumsUpdateIndex_ = 0; + } + if (!events.empty()) { + FreeRedundantEventBuffer(); + } +} + +void ReportDataCallback::FreeRedundantEventBuffer() { - return eventsBuf_; + 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 < eventsBuf_.blockList.size(); ++index) { + if (eventsBuf_.blockList[index].dataBuf == nullptr) { + break; + } + delete[] eventsBuf_.blockList[index].dataBuf; + eventsBuf_.blockList[index].dataBuf = nullptr; + } } } // namespace Sensors } // namespace OHOS