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 TSD_BASE_PACKAGE_WORKER_H
12 : #define TSD_BASE_PACKAGE_WORKER_H
13 :
14 : #include <chrono>
15 : #include <string>
16 : #include <mutex>
17 : #include <sstream>
18 : #include <map>
19 : #include "tsd/status.h"
20 : #include "tsd_log.h"
21 : #include "tsd_util_func.h"
22 :
23 : namespace tsd {
24 : using TimePoint = std::chrono::high_resolution_clock::time_point;
25 :
26 : struct PackageWorkerParas {
27 : uint32_t deviceId;
28 : uint32_t vfId;
29 :
30 9 : PackageWorkerParas() : deviceId(0U), vfId(0U){};
31 12 : PackageWorkerParas(const uint32_t devId, const uint32_t vfid) : deviceId(devId), vfId(vfid){};
32 :
33 23 : std::string DebugString() const
34 : {
35 23 : std::stringstream oss;
36 : oss << "Package worker parameters, "
37 23 : << "deviceId=" << deviceId << ", "
38 23 : << "vfId=" << vfId;
39 :
40 46 : return oss.str();
41 23 : }
42 : };
43 :
44 : class BasePackageWorker {
45 : public:
46 20 : explicit BasePackageWorker(const PackageWorkerParas paras)
47 20 : : deviceId_(paras.deviceId),
48 20 : vfId_(paras.vfId),
49 40 : uniqueVfId_(CalcUniqueVfId(paras.deviceId, paras.vfId)),
50 20 : originPackagePath_(),
51 20 : decomPackagePath_(),
52 20 : packageMtx_(),
53 20 : isVfMode_(false),
54 20 : isAsanMode_(false),
55 20 : originPackageSize_(0UL),
56 20 : checkCode_(0UL),
57 40 : decompressTime_()
58 : {
59 20 : isVfMode_ = IsCurrentVfMode(deviceId_, vfId_);
60 20 : };
61 20 : virtual ~BasePackageWorker(){};
62 :
63 : virtual TSD_StatusT LoadPackage(const std::string& packagePath, const std::string& packageName) = 0;
64 : virtual TSD_StatusT UnloadPackage() = 0;
65 : virtual uint64_t GetPackageCheckCode();
66 :
67 1 : inline void ClearPackageCheckCode()
68 : {
69 1 : const std::lock_guard<std::mutex> lk(packageMtx_);
70 1 : checkCode_ = 0UL;
71 2 : return;
72 1 : }
73 :
74 2 : inline void SetAsanMode(bool mode)
75 : {
76 2 : const std::lock_guard<std::mutex> lk(packageMtx_);
77 2 : isAsanMode_ = mode;
78 4 : return;
79 2 : }
80 :
81 : protected:
82 : struct PackagePath {
83 : std::string path;
84 : std::string name;
85 : std::string realPath;
86 :
87 280 : PackagePath() : path(""), name(""), realPath(""){};
88 22 : PackagePath(const std::string& packagePath, const std::string& packageName)
89 44 : : path(packagePath), name(packageName), realPath("")
90 : {
91 22 : if (path.back() != '/') {
92 13 : path.append("/");
93 : }
94 22 : realPath = path + name;
95 22 : };
96 :
97 4 : void Clear()
98 : {
99 4 : path.clear();
100 4 : name.clear();
101 4 : realPath.clear();
102 :
103 4 : return;
104 : }
105 : };
106 :
107 : virtual void PreProcessPackage(const std::string& packagePath, const std::string& packageName);
108 : void DefaultPreProcessPackage(const std::string& packagePath, const std::string& packageName);
109 : virtual void SetDecompressPackagePath() = 0;
110 : virtual bool IsNeedLoadPackage();
111 : virtual TSD_StatusT MoveOriginPackageToDecompressDir() const;
112 : virtual std::string GetMovePackageToDecompressDirCmd() const = 0;
113 : virtual TSD_StatusT DecompressPackage() const;
114 : virtual std::string GetDecompressPackageCmd() const = 0;
115 : virtual TSD_StatusT PostProcessPackage();
116 : virtual void Clear();
117 : void DefaultClear();
118 : virtual bool IsNeedUnloadPackage();
119 :
120 17 : inline uint64_t GetCheckCode() const { return checkCode_; }
121 :
122 : inline TimePoint GetDecompressTime() const { return decompressTime_; }
123 :
124 14 : inline void SetCheckCode(const uint64_t checkCode)
125 : {
126 14 : checkCode_ = checkCode;
127 :
128 14 : return;
129 : }
130 :
131 15 : inline void SetOriginPackageSize(const uint64_t packageSize)
132 : {
133 15 : originPackageSize_ = packageSize;
134 :
135 15 : return;
136 : }
137 :
138 14 : inline uint64_t GetOriginPackageSize() const { return originPackageSize_; }
139 :
140 12 : inline void SetOriginPackagePath(const std::string& path, const std::string& fileName)
141 : {
142 12 : originPackagePath_ = PackagePath(path, fileName);
143 :
144 12 : return;
145 : }
146 :
147 7 : inline void SetDecompressTimeToNow()
148 : {
149 7 : decompressTime_ = std::chrono::high_resolution_clock::now();
150 :
151 7 : return;
152 : }
153 :
154 : inline bool IsVfMode() const { return isVfMode_; }
155 :
156 3 : inline bool IsAsanMode() const { return isAsanMode_; }
157 :
158 : const uint32_t deviceId_;
159 : const uint32_t vfId_;
160 : const uint32_t uniqueVfId_;
161 : PackagePath originPackagePath_; // Path where the package is first uploaded to the device, mostly is the hdcd dir.
162 : PackagePath decomPackagePath_; // Path where the package is move to and wait for decompress.
163 : std::mutex packageMtx_;
164 :
165 : private:
166 : bool isVfMode_;
167 : bool isAsanMode_;
168 : uint64_t originPackageSize_;
169 : uint64_t checkCode_;
170 : TimePoint decompressTime_;
171 : };
172 :
173 : } // namespace tsd
174 :
175 : #endif // TSD_BASE_PACKAGE_WORKER_H
|