已合并
refactor: decouple Runtime from ApiImplMbuf #4265
refactor: decouple Runtime from ApiImplMbuf #4265
已合并
zhangpengpeng8创建于 8月15日
10 个文件变更+68-43
@@ -10,15 +10,12 @@
10 10 
11#include "api_impl_creator.hpp"11#include "api_impl_creator.hpp"
12#include "api_impl.hpp"12#include "api_impl.hpp"
13-#include "api_impl_mbuf.hpp"
14#include "api_impl_soma.hpp"13#include "api_impl_soma.hpp"
15#include "api_impl_event.hpp"14#include "api_impl_event.hpp"
16 15 
17namespace cce {16namespace cce {
18namespace runtime {17namespace runtime {
19 18 
20-ApiMbuf* CreateImplMbufAndGet() { return new (std::nothrow) ApiImplMbuf(); }
21- 
22ApiSoma* CreateImplSomaAndGet() { return new (std::nothrow) ApiImplSoma(); }19ApiSoma* CreateImplSomaAndGet() { return new (std::nothrow) ApiImplSoma(); }
23 20 
24ApiEvent* CreateImplEventAndGet() { return new (std::nothrow) ApiImplEvent(); }21ApiEvent* CreateImplEventAndGet() { return new (std::nothrow) ApiImplEvent(); }
@@ -11,14 +11,16 @@
11#define __CCE_RUNTIME_API_IMPL_CREATOR_C_HPP__11#define __CCE_RUNTIME_API_IMPL_CREATOR_C_HPP__
12 12 
13#include "api.hpp"13#include "api.hpp"
14-#include "api_mbuf.hpp"
15 14 
16namespace cce {15namespace cce {
17namespace runtime {16namespace runtime {
17+class ApiMbuf;
18class ApiEvent;18class ApiEvent;
19 19 
20Api* CreateImplAndGet();20Api* CreateImplAndGet();
21+bool IsImplMbufSupported();
21ApiMbuf* CreateImplMbufAndGet();22ApiMbuf* CreateImplMbufAndGet();
23+void DestroyImplMbuf(ApiMbuf*& apiImplMbuf);
22ApiSoma* CreateImplSomaAndGet();24ApiSoma* CreateImplSomaAndGet();
23ApiEvent* CreateImplEventAndGet();25ApiEvent* CreateImplEventAndGet();
24} // namespace runtime26} // 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#include "api_impl_mbuf.hpp"10#include "api_impl_mbuf.hpp"
11+#include "api_impl_creator.hpp"
11#include "npu_driver.hpp"12#include "npu_driver.hpp"
12 13 
13namespace cce {14namespace cce {
14namespace runtime {15namespace 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+ 
16rtError_t ApiImplMbuf::MbufInit(rtMemBuffCfg_t* const cfg)37rtError_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 runtime148} // namespace runtime
128-} // namespace cce149+} // namespace cce
@@ -15,7 +15,6 @@
15#include "mmpa/mmpa_api.h"15#include "mmpa/mmpa_api.h"
16#include "driver/ascend_hal.h"16#include "driver/ascend_hal.h"
17#include "api_impl.hpp"17#include "api_impl.hpp"
18-#include "api_impl_mbuf.hpp"
19#include "api_impl_event.hpp"18#include "api_impl_event.hpp"
20#include "context.hpp"19#include "context.hpp"
21#include "engine_stream_observer.hpp"20#include "engine_stream_observer.hpp"
@@ -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#include <fstream>12#include <fstream>
13#include "driver/ascend_hal.h"13#include "driver/ascend_hal.h"
14#include "api_impl.hpp"14#include "api_impl.hpp"
15-#include "api_impl_mbuf.hpp"
16#include "api_impl_soma.hpp"15#include "api_impl_soma.hpp"
17#include "api_event.hpp"16#include "api_event.hpp"
18#include "context.hpp"17#include "context.hpp"
@@ -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#include "mmpa/mmpa_api.h"13#include "mmpa/mmpa_api.h"
14#include "driver/ascend_hal.h"14#include "driver/ascend_hal.h"
15#include "api_impl.hpp"15#include "api_impl.hpp"
16-#include "api_impl_mbuf.hpp"
17#include "api_impl_soma.hpp"16#include "api_impl_soma.hpp"
18#include "api_event.hpp"17#include "api_event.hpp"
19#include "context.hpp"18#include "context.hpp"
@@ -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_);
Msrc/runtime/driver/npu_driver.cc+0-16文件内容审核中,请稍后刷新重试
@@ -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- 
390TEST_F(CloudV2IpcApiTest, MemRetainAllocationHandle01)381TEST_F(CloudV2IpcApiTest, MemRetainAllocationHandle01)
391{382{
392 size_t size = 1024 * 1024; // 1mb383 size_t size = 1024 * 1024; // 1mb
@@ -17,6 +17,7 @@
17#include "npu_driver.hpp"17#include "npu_driver.hpp"
18#include "api_event.hpp"18#include "api_event.hpp"
19#include "api_impl.hpp"19#include "api_impl.hpp"
20+#include "api_impl_creator.hpp"
20#include "program.hpp"21#include "program.hpp"
21#include "profiler.hpp"22#include "profiler.hpp"
22#include "api_profile_decorator.hpp"23#include "api_profile_decorator.hpp"
@@ -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+ 
154TEST_F(RuntimeTest, BOOT_RUNTIME_TEST_PrepareRuntimeProcessExitDoesNotDeleteRuntime)189TEST_F(RuntimeTest, BOOT_RUNTIME_TEST_PrepareRuntimeProcessExitDoesNotDeleteRuntime)
155{190{
156 Runtime* const oldRuntime = Runtime::runtime_;191 Runtime* const oldRuntime = Runtime::runtime_;