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 :
11 : #ifndef __PLATFORM_INFOS_UTILS_H__
12 : #define __PLATFORM_INFOS_UTILS_H__
13 :
14 : #include <mutex>
15 : #include <dlfcn.h>
16 : #include <climits>
17 : #include <algorithm>
18 : #include "platform/platform_infos_def.h"
19 : #include "platform_log.h"
20 :
21 : namespace fe {
22 : extern std::mutex plt_info_mutex;
23 : extern std::mutex opt_info_mutex;
24 : const std::string PLATFORM_RELATIVE_PATH = "data/platform_config";
25 : const std::string PLATFORM_RELATIVE_PATH_KIRIN = "../data/platform_config";
26 : const std::string PLATFORM_RELATIVE_PATH_KIRIN_EXT = "../../platform/";
27 :
28 : class PlatformInfosUtils {
29 : public:
30 : PlatformInfosUtils(const PlatformInfosUtils&) = delete;
31 : PlatformInfosUtils& operator=(const PlatformInfosUtils&) = delete;
32 :
33 : static PlatformInfosUtils& GetInstance();
34 : void Clone(PlatFormInfos& dest_platform_infos, const PlatFormInfos& platform_infos) const;
35 :
36 : static void Trim(std::string& str);
37 : static void Split(const std::string& str, char pattern, std::vector<std::string>& res_vec);
38 :
39 : private:
40 : PlatformInfosUtils();
41 : ~PlatformInfosUtils();
42 : };
43 :
44 : std::string RealSoFilePath(const std::string& path);
45 :
46 : template <typename ManagerType>
47 158 : std::string GetSoFilePath()
48 : {
49 : Dl_info dl_info;
50 158 : std::string real_file_path = "";
51 158 : ManagerType& (*instance_ptr)() = &ManagerType::Instance;
52 158 : if (dladdr(reinterpret_cast<void*>(instance_ptr), &dl_info) == 0) {
53 0 : PF_LOGE("Failed to read the so file path.");
54 0 : return real_file_path;
55 : } else {
56 158 : std::string so_path = dl_info.dli_fname;
57 158 : if (so_path.empty()) {
58 0 : PF_LOGE("The so file path is empty.");
59 0 : return real_file_path;
60 : }
61 158 : real_file_path = RealSoFilePath(so_path);
62 158 : size_t pos = real_file_path.rfind('/');
63 158 : real_file_path = real_file_path.substr(0, pos + 1);
64 158 : }
65 158 : return real_file_path;
66 0 : }
67 :
68 : template <typename ManagerType>
69 156 : std::string GetConfigFilePath()
70 : {
71 156 : std::string so_file_path = GetSoFilePath<ManagerType>();
72 312 : while (!so_file_path.empty() && so_file_path.back() == '/') {
73 156 : so_file_path.pop_back();
74 : }
75 156 : size_t pos = so_file_path.find_last_of("/\\");
76 156 : if (pos == std::string::npos) {
77 0 : return "";
78 : }
79 156 : so_file_path = so_file_path.substr(0, pos + 1);
80 156 : PF_LOGI("Current so file path is [%s].", so_file_path.c_str());
81 :
82 156 : return RealSoFilePath(so_file_path + PLATFORM_RELATIVE_PATH);
83 156 : }
84 :
85 : template <typename ManagerType>
86 1 : std::string GetConfigFilePath(std::string socVersion)
87 : {
88 1 : std::string so_file_path = GetSoFilePath<ManagerType>();
89 1 : while (!so_file_path.empty() && so_file_path.back() == '/') {
90 0 : so_file_path.pop_back();
91 : }
92 1 : size_t pos = so_file_path.find_last_of("/\\");
93 1 : if (pos == std::string::npos) {
94 0 : return "";
95 : }
96 1 : so_file_path = so_file_path.substr(0, pos + 1);
97 1 : PF_LOGI("Current so file path is [%s].", so_file_path.c_str());
98 :
99 1 : std::string config_file_path = "";
100 1 : config_file_path = RealSoFilePath(so_file_path + PLATFORM_RELATIVE_PATH);
101 1 : if (config_file_path.empty()) {
102 : // kirin
103 1 : config_file_path = RealSoFilePath(so_file_path + PLATFORM_RELATIVE_PATH_KIRIN);
104 1 : if (config_file_path.empty()) {
105 : // kirin ext
106 1 : std::transform(socVersion.begin(), socVersion.end(), socVersion.begin(), ::tolower);
107 1 : config_file_path = RealSoFilePath(so_file_path + PLATFORM_RELATIVE_PATH_KIRIN_EXT + socVersion + "/config");
108 : }
109 : }
110 1 : return config_file_path;
111 1 : }
112 : } // namespace fe
113 : #endif // __PLATFORM_INFOS_UTILS_H__
|