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 "platform/platform_infos_def.h"
18 : #include "platform_log.h"
19 :
20 : namespace fe {
21 : extern std::mutex plt_info_mutex;
22 : extern std::mutex opt_info_mutex;
23 : const std::string PLATFORM_RELATIVE_PATH = "data/platform_config";
24 :
25 : class PlatformInfosUtils {
26 : public:
27 : PlatformInfosUtils(const PlatformInfosUtils&) = delete;
28 : PlatformInfosUtils& operator=(const PlatformInfosUtils&) = delete;
29 :
30 : static PlatformInfosUtils& GetInstance();
31 : void Clone(PlatFormInfos& dest_platform_infos, const PlatFormInfos& platform_infos) const;
32 :
33 : static void Trim(std::string& str);
34 : static void Split(const std::string& str, char pattern, std::vector<std::string>& res_vec);
35 :
36 : private:
37 : PlatformInfosUtils();
38 : ~PlatformInfosUtils();
39 : };
40 :
41 : std::string RealSoFilePath(const std::string& path);
42 :
43 : template <typename ManagerType>
44 150 : std::string GetSoFilePath()
45 : {
46 : Dl_info dl_info;
47 150 : std::string real_file_path = "";
48 150 : ManagerType& (*instance_ptr)() = &ManagerType::Instance;
49 150 : if (dladdr(reinterpret_cast<void*>(instance_ptr), &dl_info) == 0) {
50 0 : PF_LOGE("Failed to read the so file path.");
51 0 : return real_file_path;
52 : } else {
53 150 : std::string so_path = dl_info.dli_fname;
54 150 : if (so_path.empty()) {
55 0 : PF_LOGE("The so file path is empty.");
56 0 : return real_file_path;
57 : }
58 150 : real_file_path = RealSoFilePath(so_path);
59 150 : size_t pos = real_file_path.rfind('/');
60 150 : real_file_path = real_file_path.substr(0, pos + 1);
61 150 : }
62 150 : return real_file_path;
63 0 : }
64 :
65 : template <typename ManagerType>
66 148 : std::string GetConfigFilePath()
67 : {
68 148 : std::string so_file_path = GetSoFilePath<ManagerType>();
69 296 : while (!so_file_path.empty() && so_file_path.back() == '/') {
70 148 : so_file_path.pop_back();
71 : }
72 148 : size_t pos = so_file_path.find_last_of("/\\");
73 148 : if (pos == std::string::npos) {
74 0 : return "";
75 : }
76 148 : so_file_path = so_file_path.substr(0, pos + 1);
77 148 : PF_LOGI("Current so file path is [%s].", so_file_path.c_str());
78 :
79 148 : return RealSoFilePath(so_file_path + PLATFORM_RELATIVE_PATH);
80 148 : }
81 : } // namespace fe
82 :
83 : #endif // __PLATFORM_INFOS_UTILS_H__
|