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 : #include "package_worker.h"
12 : #include "tsd_path_mgr.h"
13 :
14 : namespace tsd {
15 : std::map<std::pair<uint32_t, uint32_t>, std::shared_ptr<PackageWorker>> PackageWorker::workerManager_ = {};
16 : std::mutex PackageWorker::workerManagerMutex_;
17 :
18 16 : std::shared_ptr<PackageWorker> PackageWorker::GetInstance(const uint32_t devId, const uint32_t vfId)
19 : {
20 : // All device use unique obj in physical scenarios
21 16 : uint32_t deviceKey = (vfId == 0U) ? 0U : devId;
22 16 : if (IsVfModeCheckedByDeviceId(devId)) {
23 0 : deviceKey = devId;
24 : }
25 16 : std::pair<uint32_t, uint32_t> key = std::make_pair(deviceKey, vfId);
26 :
27 16 : std::shared_ptr<PackageWorker> inst = nullptr;
28 : {
29 16 : const std::lock_guard<std::mutex> lk(PackageWorker::workerManagerMutex_);
30 16 : const auto iter = workerManager_.find(key);
31 16 : if (iter != workerManager_.end()) {
32 14 : inst = iter->second;
33 14 : return inst;
34 : }
35 :
36 2 : inst = std::make_shared<PackageWorker>(deviceKey, vfId);
37 2 : if (inst == nullptr) {
38 0 : TSD_ERROR("Failed to create new package worker manager, deviceId=%u, vfId=%u", devId, vfId);
39 : }
40 2 : (void)workerManager_.insert(std::make_pair(key, inst));
41 16 : }
42 :
43 2 : return inst;
44 0 : }
45 :
46 5 : TSD_StatusT PackageWorker::LoadPackage(
47 : const PackageWorkerType type, const std::string& path, const std::string& fileName)
48 : {
49 5 : const std::shared_ptr<BasePackageWorker> packageWorker = GetPackageWorker(type);
50 5 : if (packageWorker == nullptr) {
51 2 : TSD_ERROR(
52 : "Get package worker inst failed by nullptr, type=%u, path=%s, fileName=%s", static_cast<uint32_t>(type),
53 : path.c_str(), fileName.c_str());
54 2 : return TSD_INSTANCE_NOT_FOUND;
55 : }
56 :
57 3 : const TSD_StatusT ret = packageWorker->LoadPackage(path, fileName);
58 3 : if (ret != TSD_OK) {
59 0 : TSD_ERROR(
60 : "Load package failed, ret=%u, type=%u, path=%s, fileName=%s", ret, static_cast<uint32_t>(type),
61 : path.c_str(), fileName.c_str());
62 : }
63 :
64 3 : return ret;
65 5 : }
66 :
67 2 : TSD_StatusT PackageWorker::UnloadPackage(const PackageWorkerType type)
68 : {
69 2 : const std::shared_ptr<BasePackageWorker> packageWorker = GetPackageWorker(type);
70 2 : if (packageWorker == nullptr) {
71 1 : TSD_ERROR("Get package worker inst failed by nullptr, type=%u", static_cast<uint32_t>(type));
72 1 : return TSD_INSTANCE_NOT_FOUND;
73 : }
74 :
75 1 : const TSD_StatusT ret = packageWorker->UnloadPackage();
76 1 : if (ret != TSD_OK) {
77 0 : TSD_ERROR("Unload package failed, ret=%u, type=%u", ret, static_cast<uint32_t>(type));
78 : }
79 :
80 1 : return ret;
81 2 : }
82 :
83 2 : uint64_t PackageWorker::GetPackageCheckCode(const PackageWorkerType type)
84 : {
85 2 : const std::shared_ptr<BasePackageWorker> packageWorker = GetPackageWorker(type);
86 2 : if (packageWorker == nullptr) {
87 1 : TSD_ERROR("Get package worker inst failed by nullptr, type=%u", static_cast<uint32_t>(type));
88 1 : return 0UL;
89 : }
90 :
91 1 : return packageWorker->GetPackageCheckCode();
92 2 : }
93 :
94 2 : void PackageWorker::ClearPackageCheckCode(const PackageWorkerType type)
95 : {
96 2 : const std::shared_ptr<BasePackageWorker> packageWorker = GetPackageWorker(type);
97 2 : if (packageWorker == nullptr) {
98 1 : TSD_ERROR("Get package worker inst failed by nullptr, type=%u", static_cast<uint32_t>(type));
99 1 : return;
100 : }
101 :
102 1 : packageWorker->ClearPackageCheckCode();
103 2 : }
104 :
105 5 : std::shared_ptr<BasePackageWorker> PackageWorker::GetPackageWorker(const PackageWorkerType type)
106 : {
107 5 : std::lock_guard<std::mutex> lk(workersMutex_);
108 5 : const std::shared_ptr<BasePackageWorker> worker = workers_[static_cast<size_t>(type)];
109 5 : if (worker != nullptr) {
110 2 : return worker;
111 : }
112 :
113 3 : if (isDestroy_) {
114 1 : return nullptr;
115 : }
116 :
117 2 : const PackageWorkerParas paras(deviceId_, vfId_);
118 2 : const auto newWorker = PackageWorkerFactory::GetInstance().CreatePackageWorker(type, paras);
119 2 : if (newWorker == nullptr) {
120 1 : TSD_ERROR("Create worker failed, type=%u", static_cast<uint32_t>(type));
121 1 : return nullptr;
122 : }
123 :
124 1 : workers_[static_cast<size_t>(type)] = newWorker;
125 :
126 1 : TSD_INFO("Get and create package worker success, type=%u", static_cast<uint32_t>(type));
127 1 : return newWorker;
128 5 : }
129 :
130 1 : void PackageWorker::DestroyPackageWorker()
131 : {
132 1 : Stop();
133 1 : ClearWorkerManager();
134 1 : }
135 :
136 4 : void PackageWorker::Stop() noexcept { isDestroy_ = true; }
137 :
138 2 : void PackageWorker::ClearWorkerManager()
139 : {
140 2 : const std::lock_guard<std::mutex> lk(PackageWorker::workerManagerMutex_);
141 2 : const auto iter = PackageWorker::workerManager_.find(std::make_pair(deviceId_, vfId_));
142 2 : if (iter == workerManager_.end()) {
143 1 : TSD_WARN("Delete package worker manager but not exist, deviceId=%u, vfId=%u", deviceId_, vfId_);
144 1 : return;
145 : }
146 :
147 1 : (void)workerManager_.erase(iter);
148 2 : }
149 :
150 2 : void PackageWorker::SetAsanMode(const bool isAsan)
151 : {
152 2 : std::lock_guard<std::mutex> lk(workersMutex_);
153 68 : for (const auto& worker : workers_) {
154 66 : if (worker != nullptr) {
155 2 : worker->SetAsanMode(isAsan);
156 : }
157 : }
158 2 : }
159 : } // namespace tsd
|