diff --git a/interfaces/kits/hyperaio/src/hyperaio.cpp b/interfaces/kits/hyperaio/src/hyperaio.cpp index bd4d38d2ada6c84c206e2219fffa284832a0f504..c4f16cc4639e6eae61b8523e21ecf514d08a6c38 100644 --- a/interfaces/kits/hyperaio/src/hyperaio.cpp +++ b/interfaces/kits/hyperaio/src/hyperaio.cpp @@ -53,6 +53,11 @@ static bool HasAccessIouringPermission() return true; } +static bool ValidateReqNum(uint32_t reqNum) +{ + return reqNum > 0 && reqNum <= URING_QUEUE_SIZE; +} + uint32_t HyperAio::SupportIouring() { HyperaioTrace trace("SupportIouring"); @@ -121,6 +126,10 @@ int32_t HyperAio::StartOpenReqs(OpenReqs *req) HILOGE("HyperAio is not initialized"); return -EPERM; } + if (!ValidateReqNum(req->reqNum)) { + HILOGE("reqNum is out of range: %{public}u", req->reqNum); + return -EINVAL; + } HyperaioTrace trace("StartOpenReqs" + std::to_string(req->reqNum)); uint32_t totalReqs = req->reqNum; uint32_t count = 0; @@ -139,7 +148,7 @@ int32_t HyperAio::StartOpenReqs(OpenReqs *req) HyperaioTrace trace("open flags:" + std::to_string(openInfo->flags) + "mode:" + std::to_string(openInfo->mode) + "userData:" + std::to_string(openInfo->userData)); count++; - if (count >= BATCH_SIZE) { + if (count >= BATCH_SIZE || i == totalReqs - 1) { int32_t ret = io_uring_submit(&pImpl_->uring_); if (ret < 0) { HILOGE("submit open reqs failed, ret = %{public}d", ret); @@ -149,14 +158,6 @@ int32_t HyperAio::StartOpenReqs(OpenReqs *req) count = 0; } } - if (count > 0 && count < BATCH_SIZE) { - int32_t ret = io_uring_submit(&pImpl_->uring_); - if (ret < 0) { - HILOGE("submit open reqs failed, ret = %{public}d", ret); - return ret; - } - openReqCount_ += count; - } return EOK; } @@ -172,6 +173,10 @@ int32_t HyperAio::StartReadReqs(ReadReqs *req) HILOGE("HyperAio is not initialized"); return -EPERM; } + if (!ValidateReqNum(req->reqNum)) { + HILOGE("reqNum is out of range: %{public}u", req->reqNum); + return -EINVAL; + } HyperaioTrace trace("StartReadReqs" + std::to_string(req->reqNum)); uint32_t totalReqs = req->reqNum; uint32_t count = 0; @@ -189,7 +194,7 @@ int32_t HyperAio::StartReadReqs(ReadReqs *req) HyperaioTrace trace("read len:" + std::to_string(readInfo->len) + "offset:" + std::to_string(readInfo->offset) + "userData:" + std::to_string(readInfo->userData)); count++; - if (count >= BATCH_SIZE) { + if (count >= BATCH_SIZE || i == totalReqs - 1) { int32_t ret = io_uring_submit(&pImpl_->uring_); if (ret < 0) { HILOGE("submit read reqs failed, ret = %{public}d", ret); @@ -199,15 +204,6 @@ int32_t HyperAio::StartReadReqs(ReadReqs *req) count = 0; } } - if (count > 0 && count < BATCH_SIZE) { - int32_t ret = io_uring_submit(&pImpl_->uring_); - if (ret < 0) { - HILOGE("submit read reqs failed, ret = %{public}d", ret); - return ret; - } - readReqCount_ += count; - } - return EOK; } @@ -223,6 +219,10 @@ int32_t HyperAio::StartCancelReqs(CancelReqs *req) HILOGE("HyperAio is not initialized"); return -EPERM; } + if (!ValidateReqNum(req->reqNum)) { + HILOGE("reqNum is out of range: %{public}u", req->reqNum); + return -EINVAL; + } HyperaioTrace trace("StartCancelReqs" + std::to_string(req->reqNum)); uint32_t totalReqs = req->reqNum; uint32_t count = 0; @@ -240,7 +240,7 @@ int32_t HyperAio::StartCancelReqs(CancelReqs *req) HyperaioTrace trace("cancel userData:" + std::to_string(cancelInfo->userData) + "targetUserData:" + std::to_string(cancelInfo->targetUserData)); count++; - if (count >= BATCH_SIZE) { + if (count >= BATCH_SIZE || i == totalReqs - 1) { int32_t ret = io_uring_submit(&pImpl_->uring_); if (ret < 0) { HILOGE("submit cancel reqs failed, ret = %{public}d", ret); @@ -250,14 +250,6 @@ int32_t HyperAio::StartCancelReqs(CancelReqs *req) count = 0; } } - if (count > 0 && count < BATCH_SIZE) { - int32_t ret = io_uring_submit(&pImpl_->uring_); - if (ret < 0) { - HILOGE("submit cancel reqs failed, ret = %{public}d", ret); - return ret; - } - cancelReqCount_ += count; - } return EOK; } @@ -277,8 +269,6 @@ void HyperAio::HarvestRes() } cqeCount_++; auto response = std::make_unique(cqe->user_data, cqe->res, cqe->flags); - HILOGI("get cqe, user_data = %{public}lld, res = %{public}d, flags = %{public}u", - cqe->user_data, cqe->res, cqe->flags); HyperaioTrace trace("harvest: userdata " + std::to_string(cqe->user_data) + " res " + std::to_string(cqe->res) + "flags " + std::to_string(cqe->flags)); io_uring_cqe_seen(&pImpl_->uring_, cqe); @@ -302,6 +292,7 @@ int32_t HyperAio::DestroyCtx() if (harvestThread_.joinable()) { HILOGI("start harvest thread join"); harvestThread_.join(); + // No print this log means join failed HILOGI("join success"); } diff --git a/interfaces/kits/hyperaio/src/hyperaio_trace.cpp b/interfaces/kits/hyperaio/src/hyperaio_trace.cpp index b51c24b3404de8448460271adc7ad748ea05e9f3..4dfce0b0cd23b5d37f21033dc7264f845c88abb4 100644 --- a/interfaces/kits/hyperaio/src/hyperaio_trace.cpp +++ b/interfaces/kits/hyperaio/src/hyperaio_trace.cpp @@ -23,13 +23,13 @@ HyperaioTrace::HyperaioTrace(const std::string& value, bool isShowLog) : value_( if (isShowLog) { HILOGI("%{public}s", value_.c_str()); } - StartTrace(HITRACE_TAG_OHOS, "[HyperAio]" + value); + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "[HyperAio]" + value); } void HyperaioTrace::End() { if (!isFinished_) { - FinishTrace(HITRACE_TAG_OHOS); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); isFinished_ = true; } } diff --git a/interfaces/test/unittest/hyperaio/hyperaio_test.cpp b/interfaces/test/unittest/hyperaio/hyperaio_test.cpp index 2d43b7d86e9b778fd8bfe03832eb262f85b2d0e6..f966a16a236c5daf2e5506b5d849fb4b81de8f49 100644 --- a/interfaces/test/unittest/hyperaio/hyperaio_test.cpp +++ b/interfaces/test/unittest/hyperaio/hyperaio_test.cpp @@ -35,6 +35,7 @@ namespace OHOS::HyperAio { const uint64_t userData = 12345; const uint32_t len = 1024; const uint32_t batchSize = 300; + const uint32_t Threshold = 600; HyperAio::ProcessIoResultCallBack callBack = [](std::unique_ptr response) { GTEST_LOG_(INFO) << "HyperAioTest callBack"; }; @@ -227,6 +228,36 @@ namespace OHOS::HyperAio { GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartOpenReqs_0003"; } + /** + * @tc.name: HyperAio_StartOpenReqs_0004 + * @tc.desc: Test function of StartOpenReqs() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(HyperAioTest, HyperAio_StartOpenReqs_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "HyperAioTest-begin HyperAio_StartOpenReqs_0004"; + std::unique_ptr hyperAio_ = std::make_unique(); + int32_t result = hyperAio_->CtxInit(&callBack); + EXPECT_EQ(result, 0); + auto openInfos = std::make_unique(Threshold); + for (int i = 0; i < Threshold; ++i) { + openInfos[i].dfd = 0; + openInfos[i].flags = O_RDWR; + openInfos[i].mode = 0; + openInfos[i].path = nullptr; + openInfos[i].userData = userData + i; + } + OpenReqs openReqs = {Threshold, openInfos.get()}; + result = hyperAio_->StartOpenReqs(&openReqs); + EXPECT_EQ(result, -EINVAL); + result = hyperAio_->DestroyCtx(); + EXPECT_EQ(result, 0); + GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartOpenReqs_0004"; + } + /** * @tc.name: HyperAio_StartReadReqs_0000 * @tc.desc: Test function of StartReadReqs() interface for SUCCESS. @@ -293,6 +324,65 @@ namespace OHOS::HyperAio { GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartReadReqs_0002"; } + /** + * @tc.name: HyperAio_StartReadReqs_0003 + * @tc.desc: Test function of StartReadReqs() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(HyperAioTest, HyperAio_StartReadReqs_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "HyperAioTest-begin HyperAio_StartReadReqs_0003"; + std::unique_ptr hyperAio_ = std::make_unique(); + int32_t result = hyperAio_->CtxInit(&callBack); + EXPECT_EQ(result, 0); + auto readInfos = std::make_unique(batchSize); + for (int i = 0; i < batchSize; ++i) { + readInfos[i].fd = 0; + readInfos[i].len = len; + readInfos[i].offset = 0; + readInfos[i].buf = nullptr; + readInfos[i].userData = userData + i; + } + ReadReqs readReqs = {batchSize, readInfos.get()}; + result = hyperAio_->StartReadReqs(&readReqs); + EXPECT_EQ(result, 0); + result = hyperAio_->DestroyCtx(); + EXPECT_EQ(result, 0); + GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartReadReqs_0003"; + } + + /** + * @tc.name: HyperAio_StartReadReqs_0004 + * @tc.desc: Test function of StartReadReqs() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(HyperAioTest, HyperAio_StartReadReqs_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "HyperAioTest-begin HyperAio_StartReadReqs_0004"; + std::unique_ptr hyperAio_ = std::make_unique(); + int32_t result = hyperAio_->CtxInit(&callBack); + EXPECT_EQ(result, 0); + auto readInfos = std::make_unique(Threshold); + for (int i = 0; i < Threshold; ++i) { + readInfos[i].fd = 0; + readInfos[i].len = len; + readInfos[i].offset = 0; + readInfos[i].buf = nullptr; + readInfos[i].userData = userData + i; + } + ReadReqs readReqs = {Threshold, readInfos.get()}; + result = hyperAio_->StartReadReqs(&readReqs); + EXPECT_EQ(result, -EINVAL); + result = hyperAio_->DestroyCtx(); + EXPECT_EQ(result, 0); + GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartReadReqs_0004"; + } /** * @tc.name: HyperAio_StartCancelReqs_0000 * @tc.desc: Test function of StartCancelReqs() interface for SUCCESS. @@ -357,5 +447,76 @@ namespace OHOS::HyperAio { EXPECT_EQ(result, 0); GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartCancelReqs_0002"; } + + /** + * @tc.name: HyperAio_StartCancelReqs_0003 + * @tc.desc: Test function of StartCancelReqs() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(HyperAioTest, HyperAio_StartCancelReqs_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "HyperAioTest-begin HyperAio_StartCancelReqs_0003"; + std::unique_ptr hyperAio_ = std::make_unique(); + int32_t result = hyperAio_->CtxInit(&callBack); + EXPECT_EQ(result, 0); + auto cancelInfos = std::make_unique(batchSize); + for (int i = 0; i < batchSize; ++i) { + cancelInfos[i].userData = userData + i; + cancelInfos[i].targetUserData = userData + i; + } + CancelReqs cancelReqs = {batchSize, cancelInfos.get()}; + result = hyperAio_->StartCancelReqs(&cancelReqs); + EXPECT_EQ(result, 0); + result = hyperAio_->DestroyCtx(); + EXPECT_EQ(result, 0); + GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartCancelReqs_0003"; + } + + /** + * @tc.name: HyperAio_StartCancelReqs_0004 + * @tc.desc: Test function of StartCancelReqs() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(HyperAioTest, HyperAio_StartCancelReqs_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "HyperAioTest-begin HyperAio_StartCancelReqs_0004"; + std::unique_ptr hyperAio_ = std::make_unique(); + int32_t result = hyperAio_->CtxInit(&callBack); + EXPECT_EQ(result, 0); + auto cancelInfos = std::make_unique(Threshold); + for (int i = 0; i < Threshold; ++i) { + cancelInfos[i].userData = userData + i; + cancelInfos[i].targetUserData = userData + i; + } + CancelReqs cancelReqs = {Threshold, cancelInfos.get()}; + result = hyperAio_->StartCancelReqs(&cancelReqs); + EXPECT_EQ(result, -EINVAL); + result = hyperAio_->DestroyCtx(); + EXPECT_EQ(result, 0); + GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_StartCancelReqs_0004"; + } + + /** + * @tc.name: HyperAio_DestoryCtx_0000 + * @tc.desc: Test function of StartCancelReqs() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(HyperAioTest, HyperAio_DestoryCtx_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "HyperAioTest-begin HyperAio_DestoryCtx_0000"; + std::unique_ptr hyperAio_ = std::make_unique(); + int32_t result = hyperAio_->DestroyCtx(); + EXPECT_EQ(result, 0); + GTEST_LOG_(INFO) << "HyperAioTest-end HyperAio_DestoryCtx_0000"; + } #endif }