Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #ifndef ADUMP_COMMON_ADUMP_PLATFORM_MANAGER_H
11 : #define ADUMP_COMMON_ADUMP_PLATFORM_MANAGER_H
12 :
13 : #include <memory>
14 : #include <mutex>
15 : #include "adump_dsmi.h"
16 : #include "adump_platform_registry.h"
17 : #include "features_support_interface.h"
18 : #include "coredump_interface.h"
19 : #include "exception_dump_interface.h"
20 : #include "data_dump_interface.h"
21 : #include "log/adx_log.h"
22 :
23 : namespace Adx {
24 :
25 : // 各平台接口域共用的懒加载单例管理器基类:加锁缓存成功实例;创建失败不固化,
26 : // 后续 Get() 可重试;Reset() 供 UT 清理缓存以重新 mock 平台类型。
27 : // 每个域用一个空派生类特化出独立的静态存储(见下方 4 个 Manager)。
28 : template<typename Iface, typename Derived>
29 : class AdumpPlatformManagerBase {
30 : public:
31 451 : static Iface* Get()
32 : {
33 451 : std::lock_guard<std::mutex> lock(mtx_);
34 451 : if (instance_ != nullptr) {
35 326 : return instance_.get();
36 : }
37 125 : instance_ = CreateOnce();
38 125 : return instance_.get();
39 451 : }
40 :
41 2528 : static void Reset()
42 : {
43 2528 : std::lock_guard<std::mutex> lock(mtx_);
44 2528 : instance_.reset();
45 2528 : }
46 :
47 : private:
48 125 : static std::shared_ptr<Iface> CreateOnce()
49 : {
50 125 : uint32_t type = 0;
51 125 : if (!AdumpDsmi::DrvGetPlatformType(type)) {
52 7 : IDE_LOGE("[AdumpPlatform] Failed to get platform type.");
53 7 : return nullptr;
54 : }
55 118 : auto plat = PlatformReflection<Iface>::CreatePlatform(static_cast<PlatformType>(type));
56 118 : if (plat == nullptr) {
57 86 : IDE_LOGW("[AdumpPlatform] Platform type %u not registered for this capability.", type);
58 : }
59 118 : return plat;
60 118 : }
61 :
62 : static std::shared_ptr<Iface> instance_;
63 : static std::mutex mtx_;
64 : };
65 :
66 : template<typename Iface, typename Derived>
67 : std::shared_ptr<Iface> AdumpPlatformManagerBase<Iface, Derived>::instance_;
68 : template<typename Iface, typename Derived>
69 : std::mutex AdumpPlatformManagerBase<Iface, Derived>::mtx_;
70 :
71 : class FeaturesSupportManager
72 : : public AdumpPlatformManagerBase<FeaturesSupportInterface, FeaturesSupportManager> {};
73 :
74 : class CoredumpManager
75 : : public AdumpPlatformManagerBase<CoredumpInterface, CoredumpManager> {};
76 :
77 : class ExceptionDumpManager
78 : : public AdumpPlatformManagerBase<ExceptionDumpInterface, ExceptionDumpManager> {};
79 :
80 : class DataDumpManager
81 : : public AdumpPlatformManagerBase<DataDumpInterface, DataDumpManager> {};
82 :
83 : // 聚合重置,供 UT fixture 在 SetUp/TearDown 中一次清空全部域缓存。
84 632 : inline void ResetAllPlatformManagers()
85 : {
86 632 : FeaturesSupportManager::Reset();
87 632 : CoredumpManager::Reset();
88 632 : ExceptionDumpManager::Reset();
89 632 : DataDumpManager::Reset();
90 632 : }
91 :
92 : } // namespace Adx
93 : #endif // ADUMP_COMMON_ADUMP_PLATFORM_MANAGER_H
|