diff --git a/interfaces/kits/hyperaio/include/hyperaio.h b/interfaces/kits/hyperaio/include/hyperaio.h new file mode 100644 index 0000000000000000000000000000000000000000..49f0c8812efe95cc37748b306a988c89a811168d --- /dev/null +++ b/interfaces/kits/hyperaio/include/hyperaio.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILEMANAGEMENT_FILE_API_INTERFACES_KITS_IOURING_HYPER_AIO_H +#define FILEMANAGEMENT_FILE_API_INTERFACES_KITS_IOURING_HYPER_AIO_H + +#include +#include +#include "liburing.h" + +namespace OHOS { +namespace HyperAio { +#define IOURING_APP_PERMISSION (1U << 0) + +struct ReadInfo { + int32_t fd; + uint32_t len; + uint64_t offset; + void *buf; + uint64_t userData; +}; + +struct ReadReqs { + uint32_t reqNum; + struct ReadInfo *reqs; +}; + +struct OpenInfo { + int32_t dfd; + int32_t flags; + mode_t mode; + void *path; + uint64_t userData; +}; + +struct OpenReqs { + uint32_t reqNum; + struct OpenInfo *reqs; +}; + +struct CancelInfo { + uint64_t userData; + uint64_t targetUserData; +}; + +struct CancelReqs { + uint32_t reqNum; + struct CancelInfo *reqs; +}; + +struct IoResponse { + uint64_t userData; + int32_t res; + uint32_t flags; + IoResponse(uint64_t userData, int32_t res, uint32_t flags) + : userData(userData), + res(res), + flags(flags) { + } +}; + +class HyperAio { +public: + using ProcessIoResultCallBack = std::function)>; + uint32_t SupportIouring(); + int32_t CtxInit(ProcessIoResultCallBack *callBack); + int32_t StartReadReqs(ReadReqs *req); + int32_t StartOpenReqs(OpenReqs *req); + int32_t StartCancelReqs(CancelReqs *req); + int32_t DestroyCtx(); +private: + io_uring uring_; + ProcessIoResultCallBack ioResultCallBack_ = nullptr; + std::thread harvestThread_; + std::atomic stopThread_; + std::atomic initialized_; + void HarvestRes(); + struct io_uring_sqe* GetSqeWithRetry(struct io_uring *ring); +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/kits/hyperaio/include/libhilog.h b/interfaces/kits/hyperaio/include/libhilog.h new file mode 100644 index 0000000000000000000000000000000000000000..9e882f46b4731ff0a5deaf7f7ea74405a50ac14f --- /dev/null +++ b/interfaces/kits/hyperaio/include/libhilog.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILEMGMT_LIBHILOG_H +#define FILEMGMT_LIBHILOG_H + +#include "hilog/log.h" + +#include + +namespace OHOS { +#ifndef LOG_DOMAIN +#define LOG_DOMAIN 0xD001600 +#endif + +#ifndef LOG_TAG +#define LOG_TAG "FileManagement" +#endif + +#if defined __FILE_NAME__ +#define FILEMGMT_FILE_NAME __FILE_NAME__ +#else +#include +#define FILEMGMT_FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +#endif + +#define HILOGF(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_DOMAIN, LOG_TAG, \ + "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__)) +#define HILOGE(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, LOG_TAG, \ + "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__)) +#define HILOGW(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, LOG_TAG, \ + "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__)) +#define HILOGI(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, LOG_TAG, \ + "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__)) +#define HILOGD(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, \ + "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__)) +} // namespace OHOS + +#endif // FILEMGMT_LIBHILOG_H \ No newline at end of file diff --git a/interfaces/kits/hyperaio/src/hyperaio.cpp b/interfaces/kits/hyperaio/src/hyperaio.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70f424b5cba590840bb049e34f53dae4bf949a95 --- /dev/null +++ b/interfaces/kits/hyperaio/src/hyperaio.cpp @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hyperaio.h" + +#include +#include +#include "accesstoken_kit.h" +#include "libhilog.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace HyperAio { +const uint32_t URING_QUEUE_SIZE = 512; +const uint32_t DELAY = 20; +const uint32_t BATCH_SIZE = 128; +const uint32_t RETRIES = 3; + +static bool HasAccessIouringPermission() +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + const std::string permissionName = "ohos.permission.ALLOW_IOURING"; + int32_t res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permissionName); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOGE("have no ALLOW_IOURING permission"); + return false; + } + return true; +} + +uint32_t HyperAio::SupportIouring() +{ + uint32_t flags = 0; + if (HasAccessIouringPermission()) { + flags |= IOURING_APP_PERMISSION; + } + return flags; +} + +int32_t HyperAio::CtxInit(ProcessIoResultCallBack *callBack) +{ + if (callBack == nullptr) { + HILOGE("callBack is null"); + return -EINVAL; + } + int32_t ret = io_uring_queue_init(URING_QUEUE_SIZE, &uring_, 0); + if (ret < 0) { + HILOGE("init io_uring failed, ret = %{public}d", ret); + return ret; + } + ioResultCallBack_ = *callBack; + stopThread_.store(false); + harvestThread_ = std::thread(&HyperAio::HarvestRes, this); + initialized_.store(true); + HILOGI("init hyperaio success"); + return EOK; +} + +struct io_uring_sqe* HyperAio::GetSqeWithRetry(struct io_uring *ring) +{ + struct io_uring_sqe *sqe; + for (uint32_t i = 0; i < RETRIES; i++) { + sqe = io_uring_get_sqe(ring); + if (sqe != nullptr) { + return sqe; + } + io_uring_submit(ring); + std::this_thread::sleep_for(std::chrono::milliseconds(DELAY)); + } + return nullptr; +} + +int32_t HyperAio::StartOpenReqs(OpenReqs *req) +{ + if (req == nullptr || req->reqs == nullptr) { + return -EINVAL; + } + if (!initialized_.load()) { + HILOGE("HyperAio is not initialized"); + return -EPERM; + } + uint32_t totalReqs = req->reqNum; + uint32_t count = 0; + for(uint32_t i = 0; i < totalReqs; i++){ + struct io_uring_sqe *sqe = GetSqeWithRetry(&uring_); + if (sqe == nullptr) { + HILOGE("get sqe failed"); + return -ENOMEM; + } + struct OpenInfo *openInfo = &req->reqs[i]; + io_uring_sqe_set_data(sqe, reinterpret_cast(openInfo->userData)); + io_uring_prep_openat(sqe, openInfo->dfd, static_cast(openInfo->path), + openInfo->flags, openInfo->mode); + count++; + if (count >= BATCH_SIZE) { + count = 0; + int32_t ret = io_uring_submit(&uring_); + if (ret < 0) { + HILOGE("submit open reqs failed, ret = %{public}d", ret); + return ret; + } + } + } + if (count > 0 && count < BATCH_SIZE) { + int32_t ret = io_uring_submit(&uring_); + if (ret < 0) { + HILOGE("submit open reqs failed, ret = %{public}d", ret); + return ret; + } + } + return EOK; +} + +int32_t HyperAio::StartReadReqs(ReadReqs *req) +{ + if (req == nullptr || req->reqs == nullptr) { + return -EINVAL; + } + if (!initialized_.load()) { + HILOGE("HyperAio is not initialized"); + return -EPERM; + } + uint32_t totalReqs = req->reqNum; + uint32_t count = 0; + for(uint32_t i = 0; i < totalReqs; i++) + { + struct io_uring_sqe *sqe = GetSqeWithRetry(&uring_); + if (sqe == nullptr) { + HILOGE("get sqe failed"); + return -ENOMEM; + } + struct ReadInfo *readInfo = &req->reqs[i]; + io_uring_sqe_set_data(sqe, reinterpret_cast(readInfo->userData)); + io_uring_prep_read(sqe, readInfo->fd, readInfo->buf, readInfo->len, readInfo->offset); + count++; + if (count >= BATCH_SIZE) { + count = 0; + int32_t ret = io_uring_submit(&uring_); + if (ret < 0) { + HILOGE("submit read reqs failed, ret = %{public}d", ret); + return ret; + } + } + } + if (count > 0 && count < BATCH_SIZE) { + int32_t ret = io_uring_submit(&uring_); + if (ret < 0) { + HILOGE("submit read reqs failed, ret = %{public}d", ret); + return ret; + } + } + return EOK; +} + +int32_t HyperAio::StartCancelReqs(CancelReqs *req) +{ + if (req == nullptr || req->reqs == nullptr) { + return -EINVAL; + } + if (!initialized_.load()) { + HILOGE("HyperAio is not initialized"); + return -EPERM; + } + uint32_t totalReqs = req->reqNum; + uint32_t count = 0; + for(uint32_t i = 0; i < totalReqs; i++) + { + struct io_uring_sqe *sqe = GetSqeWithRetry(&uring_); + if (sqe == nullptr) { + HILOGE("get sqe failed"); + return -ENOMEM; + } + struct CancelInfo *cancelInfo = &req->reqs[i]; + io_uring_sqe_set_data(sqe, reinterpret_cast(cancelInfo->userData)); + io_uring_prep_cancel(sqe, reinterpret_cast(cancelInfo->targetUserData), 0); + count++; + if (count >= BATCH_SIZE) { + count = 0; + int32_t ret = io_uring_submit(&uring_); + if (ret < 0) { + HILOGE("submit cancel reqs failed, ret = %{public}d", ret); + return ret; + } + } + } + if (count > 0 && count < BATCH_SIZE) { + int32_t ret = io_uring_submit(&uring_); + if (ret < 0) { + HILOGE("submit cancel reqs failed, ret = %{public}d", ret); + return ret; + } + } + return EOK; +} + +void HyperAio::HarvestRes() +{ + while (!stopThread_.load()) { + struct io_uring_cqe *cqe; + int32_t ret = io_uring_wait_cqe(&uring_, &cqe); + if (ret < 0 || cqe == nullptr) { + HILOGI("wait cqe failed, ret = %{public}d", ret); + continue; + } + auto response = std::make_unique(cqe->user_data, cqe->res, cqe->flags); + io_uring_cqe_seen(&uring_, cqe); + if (ioResultCallBack_) { + ioResultCallBack_(std::move(response)); + } + } +} + +int32_t HyperAio::DestroyCtx() +{ + stopThread_.store(true); + if (harvestThread_.joinable()) { + harvestThread_.join(); + } + io_uring_queue_exit(&uring_); + initialized_.store(false); + return EOK; +} +} +} \ No newline at end of file