已合并
refactor: decouple Runtime from ApiImplMbuf #4265
zhangpengpeng8创建于 8月15日
refactor: decouple Runtime from ApiImplMbuf #4265
已合并
共 10 个文件变更+68-43
| @@ -10,15 +10,12 @@ | |||
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | - | ||
| 14 | 13 | ||
| 15 | 14 | ||
| 16 | 15 | ||
| 17 | namespace cce { | 16 | namespace cce { |
| 18 | namespace runtime { | 17 | namespace runtime { |
| 19 | 18 | ||
| 20 | -ApiMbuf* CreateImplMbufAndGet() { return new (std::nothrow) ApiImplMbuf(); } | ||
| 21 | - | ||
| 22 | ApiSoma* CreateImplSomaAndGet() { return new (std::nothrow) ApiImplSoma(); } | 19 | ApiSoma* CreateImplSomaAndGet() { return new (std::nothrow) ApiImplSoma(); } |
| 23 | 20 | ||
| 24 | ApiEvent* CreateImplEventAndGet() { return new (std::nothrow) ApiImplEvent(); } | 21 | ApiEvent* CreateImplEventAndGet() { return new (std::nothrow) ApiImplEvent(); } |
| @@ -11,14 +11,16 @@ | |||
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | - | ||
| 15 | 14 | ||
| 16 | namespace cce { | 15 | namespace cce { |
| 17 | namespace runtime { | 16 | namespace runtime { |
| 17 | +class ApiMbuf; | ||
| 18 | class ApiEvent; | 18 | class ApiEvent; |
| 19 | 19 | ||
| 20 | Api* CreateImplAndGet(); | 20 | Api* CreateImplAndGet(); |
| 21 | +bool IsImplMbufSupported(); | ||
| 21 | ApiMbuf* CreateImplMbufAndGet(); | 22 | ApiMbuf* CreateImplMbufAndGet(); |
| 23 | +void DestroyImplMbuf(ApiMbuf*& apiImplMbuf); | ||
| 22 | ApiSoma* CreateImplSomaAndGet(); | 24 | ApiSoma* CreateImplSomaAndGet(); |
| 23 | ApiEvent* CreateImplEventAndGet(); | 25 | ApiEvent* CreateImplEventAndGet(); |
| 24 | } // namespace runtime | 26 | } // namespace runtime |
| @@ -8,11 +8,32 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | + | ||
| 11 | 12 | ||
| 12 | 13 | ||
| 13 | namespace cce { | 14 | namespace cce { |
| 14 | namespace runtime { | 15 | namespace runtime { |
| 15 | 16 | ||
| 17 | +bool IsImplMbufSupported() { return true; } | ||
| 18 | + | ||
| 19 | +ApiMbuf* CreateImplMbufAndGet() | ||
| 20 | +{ | ||
| 21 | + ApiMbuf* const apiImplMbuf = new (std::nothrow) ApiImplMbuf(); | ||
| 22 | + if (apiImplMbuf == nullptr) { | ||
| 23 | + RT_LOG_OUTER_MSG_IMPL(ErrorCode::EE1013, sizeof(ApiImplMbuf), "new"); | ||
| 24 | + RT_LOG(RT_LOG_ERROR, "create ApiImplMbuf failed."); | ||
| 25 | + return nullptr; | ||
| 26 | + } | ||
| 27 | + RT_LOG(RT_LOG_INFO, "ApiImplMbuf:Runtime_alloc_size %zu", sizeof(ApiImplMbuf)); | ||
| 28 | + return apiImplMbuf; | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +void DestroyImplMbuf(ApiMbuf*& apiImplMbuf) | ||
| 32 | +{ | ||
| 33 | + delete apiImplMbuf; | ||
| 34 | + apiImplMbuf = nullptr; | ||
| 35 | +} | ||
| 36 | + | ||
| 16 | rtError_t ApiImplMbuf::MbufInit(rtMemBuffCfg_t* const cfg) | 37 | rtError_t ApiImplMbuf::MbufInit(rtMemBuffCfg_t* const cfg) |
| 17 | { | 38 | { |
| 18 | RT_LOG(RT_LOG_INFO, "Start to init mbuf."); | 39 | RT_LOG(RT_LOG_INFO, "Start to init mbuf."); |
| @@ -125,4 +146,4 @@ rtError_t ApiImplMbuf::MbufChainGetMbuf( | |||
| 125 | } | 146 | } |
| 126 | 147 | ||
| 127 | } // namespace runtime | 148 | } // namespace runtime |
| 128 | -} // namespace cce | 149 | +} // namespace cce |
| @@ -15,7 +15,6 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | - | ||
| 19 | 18 | ||
| 20 | 19 | ||
| 21 | 20 | ||
| @@ -1070,13 +1069,12 @@ rtError_t Runtime::InitApiImplies() | |||
| 1070 | } | 1069 | } |
| 1071 | RT_LOG(RT_LOG_INFO, "ApiImpl:Runtime_alloc_size %zu", sizeof(ApiImpl)); | 1070 | RT_LOG(RT_LOG_INFO, "ApiImpl:Runtime_alloc_size %zu", sizeof(ApiImpl)); |
| 1072 | 1071 | ||
| 1073 | - apiImplMbuf_ = CreateImplMbufAndGet(); | 1072 | + if (IsImplMbufSupported()) { |
| 1074 | - if (apiImplMbuf_ == nullptr) { | 1073 | + apiImplMbuf_ = CreateImplMbufAndGet(); |
| 1075 | - RT_LOG_OUTER_MSG_IMPL(ErrorCode::EE1013, sizeof(ApiImplMbuf), "new"); | 1074 | + if (apiImplMbuf_ == nullptr) { |
| 1076 | - RT_LOG(RT_LOG_ERROR, "create ApiImplMbuf failed."); | 1075 | + return RT_ERROR_API_NEW; |
| 1077 | - return RT_ERROR_API_NEW; | 1076 | + } |
| 1078 | } | 1077 | } |
| 1079 | - RT_LOG(RT_LOG_INFO, "ApiImplMbuf:Runtime_alloc_size %zu", sizeof(ApiImplMbuf)); | ||
| 1080 | 1078 | ||
| 1081 | apiImplSoma_ = CreateImplSomaAndGet(); | 1079 | apiImplSoma_ = CreateImplSomaAndGet(); |
| 1082 | if (apiImplSoma_ == nullptr) { | 1080 | if (apiImplSoma_ == nullptr) { |
| @@ -1585,7 +1583,7 @@ INIT_FAIL: | |||
| 1585 | DELETE_O(profiler_); | 1583 | DELETE_O(profiler_); |
| 1586 | DELETE_O(logger_); | 1584 | DELETE_O(logger_); |
| 1587 | DELETE_O(apiImpl_); | 1585 | DELETE_O(apiImpl_); |
| 1588 | - DELETE_O(apiImplMbuf_); | 1586 | + DestroyImplMbuf(apiImplMbuf_); |
| 1589 | DELETE_O(apiImplSoma_); | 1587 | DELETE_O(apiImplSoma_); |
| 1590 | DELETE_O(apiImplEvent_); | 1588 | DELETE_O(apiImplEvent_); |
| 1591 | return error; | 1589 | return error; |
| @@ -12,7 +12,6 @@ | |||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | - | ||
| 16 | 15 | ||
| 17 | 16 | ||
| 18 | 17 | ||
| @@ -81,7 +80,7 @@ Runtime::~Runtime() | |||
| 81 | apiEvent_ = nullptr; | 80 | apiEvent_ = nullptr; |
| 82 | 81 | ||
| 83 | DELETE_O(apiImpl_); | 82 | DELETE_O(apiImpl_); |
| 84 | - DELETE_O(apiImplMbuf_); | 83 | + DestroyImplMbuf(apiImplMbuf_); |
| 85 | DELETE_O(apiImplSoma_); | 84 | DELETE_O(apiImplSoma_); |
| 86 | DELETE_O(apiImplEvent_); | 85 | DELETE_O(apiImplEvent_); |
| 87 | DELETE_O(apiError_); | 86 | DELETE_O(apiError_); |
| @@ -13,7 +13,6 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | - | ||
| 17 | 16 | ||
| 18 | 17 | ||
| 19 | 18 | ||
| @@ -82,7 +81,7 @@ Runtime::~Runtime() | |||
| 82 | apiEvent_ = nullptr; | 81 | apiEvent_ = nullptr; |
| 83 | 82 | ||
| 84 | DELETE_O(apiImpl_); | 83 | DELETE_O(apiImpl_); |
| 85 | - DELETE_O(apiImplMbuf_); | 84 | + DestroyImplMbuf(apiImplMbuf_); |
| 86 | DELETE_O(apiImplSoma_); | 85 | DELETE_O(apiImplSoma_); |
| 87 | DELETE_O(apiImplEvent_); | 86 | DELETE_O(apiImplEvent_); |
| 88 | DELETE_O(apiError_); | 87 | DELETE_O(apiError_); |
| @@ -410,7 +410,6 @@ public: | |||
| 410 | static rtError_t DeviceCanAccessPeer(int32_t* const canAccessPeer, const uint32_t dev, const uint32_t peerDevice); | 410 | static rtError_t DeviceCanAccessPeer(int32_t* const canAccessPeer, const uint32_t dev, const uint32_t peerDevice); |
| 411 | static rtError_t GetP2PStatus(const uint32_t devIdDes, const uint32_t phyIdSrc, uint32_t* const status); | 411 | static rtError_t GetP2PStatus(const uint32_t devIdDes, const uint32_t phyIdSrc, uint32_t* const status); |
| 412 | static rtError_t SetIpcNotifyDisablePidVerify(const char* const name); | 412 | static rtError_t SetIpcNotifyDisablePidVerify(const char* const name); |
| 413 | - static rtError_t GetIpcNotifyPeerPhyDevId(const char* const name, uint32_t* const peerPhyDevId); | ||
| 414 | static rtError_t GetPhyDevIdByIpcMemName(const char* name, uint32_t* const phyDevId); | 413 | static rtError_t GetPhyDevIdByIpcMemName(const char* name, uint32_t* const phyDevId); |
| 415 | static rtError_t SetMemShareHandleDisablePidVerify(uint64_t shareableHandle); | 414 | static rtError_t SetMemShareHandleDisablePidVerify(uint64_t shareableHandle); |
| 416 | static rtError_t GetPhyDevIdByMemShareHandle(uint64_t shareableHandle, uint32_t* const peerPhyDevId); | 415 | static rtError_t GetPhyDevIdByMemShareHandle(uint64_t shareableHandle, uint32_t* const peerPhyDevId); |
| @@ -378,15 +378,6 @@ TEST_F(CloudV2IpcApiTest, failedSetMemShareHandle) | |||
| 378 | EXPECT_EQ(error, RT_ERROR_DRV_INPUT); | 378 | EXPECT_EQ(error, RT_ERROR_DRV_INPUT); |
| 379 | } | 379 | } |
| 380 | 380 | ||
| 381 | -TEST_F(CloudV2IpcApiTest, GetIpcNotifyPeerPhyDevIdFailed) | ||
| 382 | -{ | ||
| 383 | - MOCKER(halShrIdInfoGet).stubs().will(returnValue(DRV_ERROR_INVALID_VALUE)); | ||
| 384 | - char name[65] = {0}; | ||
| 385 | - uint32_t peerPhyDevId = 0U; | ||
| 386 | - auto error = NpuDriver::GetIpcNotifyPeerPhyDevId(name, &peerPhyDevId); | ||
| 387 | - EXPECT_EQ(error, RT_ERROR_DRV_INPUT); | ||
| 388 | -} | ||
| 389 | - | ||
| 390 | TEST_F(CloudV2IpcApiTest, MemRetainAllocationHandle01) | 381 | TEST_F(CloudV2IpcApiTest, MemRetainAllocationHandle01) |
| 391 | { | 382 | { |
| 392 | size_t size = 1024 * 1024; // 1mb | 383 | size_t size = 1024 * 1024; // 1mb |
| @@ -17,6 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | + | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | 23 | ||
| @@ -151,6 +152,40 @@ TEST_F(RuntimeTest, ApiEventInstanceInitialized) | |||
| 151 | EXPECT_EQ(ApiEvent::Instance(), runtime->ApiEvent_()); | 152 | EXPECT_EQ(ApiEvent::Instance(), runtime->ApiEvent_()); |
| 152 | } | 153 | } |
| 153 | 154 | ||
| 155 | +TEST_F(RuntimeTest, CreateImplMbufAndGetFailed) | ||
| 156 | +{ | ||
| 157 | + MOCKER(static_cast<NothrowNewFunc>(&operator new)).expects(once()).will(invoke(NothrowNewFailStub)); | ||
| 158 | + | ||
| 159 | + EXPECT_EQ(CreateImplMbufAndGet(), nullptr); | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +TEST_F(RuntimeTest, DestroyImplMbufSuccess) | ||
| 163 | +{ | ||
| 164 | + ApiMbuf* apiImplMbuf = CreateImplMbufAndGet(); | ||
| 165 | + ASSERT_NE(apiImplMbuf, nullptr); | ||
| 166 | + | ||
| 167 | + DestroyImplMbuf(apiImplMbuf); | ||
| 168 | + | ||
| 169 | + EXPECT_EQ(apiImplMbuf, nullptr); | ||
| 170 | +} | ||
| 171 | + | ||
| 172 | +TEST_F(RuntimeTest, InitApiImpliesCreateMbufFailed) | ||
| 173 | +{ | ||
| 174 | + Runtime* const rt = static_cast<Runtime*>(Runtime::Instance()); | ||
| 175 | + ASSERT_NE(rt, nullptr); | ||
| 176 | + Api* const oldApiImpl = rt->apiImpl_; | ||
| 177 | + ApiMbuf* const oldApiImplMbuf = rt->apiImplMbuf_; | ||
| 178 | + MOCKER(CreateImplMbufAndGet).expects(once()).will(returnValue(static_cast<ApiMbuf*>(nullptr))); | ||
| 179 | + | ||
| 180 | + const rtError_t error = rt->InitApiImplies(); | ||
| 181 | + Api* const newApiImpl = rt->apiImpl_; | ||
| 182 | + rt->apiImpl_ = oldApiImpl; | ||
| 183 | + rt->apiImplMbuf_ = oldApiImplMbuf; | ||
| 184 | + delete newApiImpl; | ||
| 185 | + | ||
| 186 | + EXPECT_EQ(error, RT_ERROR_API_NEW); | ||
| 187 | +} | ||
| 188 | + | ||
| 154 | TEST_F(RuntimeTest, BOOT_RUNTIME_TEST_PrepareRuntimeProcessExitDoesNotDeleteRuntime) | 189 | TEST_F(RuntimeTest, BOOT_RUNTIME_TEST_PrepareRuntimeProcessExitDoesNotDeleteRuntime) |
| 155 | { | 190 | { |
| 156 | Runtime* const oldRuntime = Runtime::runtime_; | 191 | Runtime* const oldRuntime = Runtime::runtime_; |