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 458 : static Iface* Get()
32 : {
33 458 : std::lock_guard<std::mutex> lock(mtx_);
34 458 : if (instance_ != nullptr) {
35 327 : return instance_.get();
36 : }
37 131 : instance_ = CreateOnce();
38 131 : return instance_.get();
39 458 : }
40 :
41 2876 : static void Reset()
42 : {
43 2876 : std::lock_guard<std::mutex> lock(mtx_);
44 2876 : instance_.reset();
45 2876 : }
46 :
47 : private:
48 131 : static std::shared_ptr<Iface> CreateOnce()
49 : {
50 131 : uint32_t type = 0;
51 131 : if (!AdumpDsmi::DrvGetPlatformType(type)) {
52 7 : IDE_LOGE("[AdumpPlatform] Failed to get platform type.");
53 7 : return nullptr;
54 : }
55 124 : auto plat = PlatformReflection<Iface>::CreatePlatform(static_cast<PlatformType>(type));
56 124 : if (plat == nullptr) {
57 88 : IDE_LOGW("[AdumpPlatform] Platform type %u not registered for this capability.", type);
58 : }
59 124 : return plat;
60 124 : }
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 : public AdumpPlatformManagerBase<FeaturesSupportInterface, FeaturesSupportManager> {};
72 :
73 : class CoredumpManager : public AdumpPlatformManagerBase<CoredumpInterface, CoredumpManager> {};
74 :
75 : class ExceptionDumpManager : public AdumpPlatformManagerBase<ExceptionDumpInterface, ExceptionDumpManager> {};
76 :
77 : class DataDumpManager : public AdumpPlatformManagerBase<DataDumpInterface, DataDumpManager> {};
78 :
79 : // 聚合重置,供 UT fixture 在 SetUp/TearDown 中一次清空全部域缓存。
80 719 : inline void ResetAllPlatformManagers()
81 : {
82 719 : FeaturesSupportManager::Reset();
83 719 : CoredumpManager::Reset();
84 719 : ExceptionDumpManager::Reset();
85 719 : DataDumpManager::Reset();
86 719 : }
87 :
88 : } // namespace Adx
89 : #endif // ADUMP_COMMON_ADUMP_PLATFORM_MANAGER_H
|