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 DYNAMIC_SCHED_MGR_H
12 : #define DYNAMIC_SCHED_MGR_H
13 :
14 : #include <vector>
15 : #include <queue>
16 : #include <unordered_map>
17 : #include <map>
18 : #include "queue_schedule/dgw_client.h"
19 : #include "fsm/state_define.h"
20 : #include "proto/dynamic_sched_message.pb.h"
21 :
22 : namespace dgw {
23 : class DynamicSchedMgr {
24 : public:
25 : struct SrcQueueInfo {
26 : uint32_t queueId;
27 : int32_t deviceId;
28 : bool isProxy;
29 : uint32_t queueLogicId;
30 : uint32_t modelUuid;
31 : uint32_t rootModelId;
32 : };
33 :
34 : struct DstGroupInfo {
35 : uint32_t logicGroupId;
36 : };
37 :
38 : struct DecisionInfo {
39 : uint64_t transId;
40 : uint32_t routeLabel;
41 : };
42 :
43 : struct RequestInfo {
44 : SrcQueueInfo src;
45 : std::vector<DstGroupInfo> dsts;
46 : std::vector<DecisionInfo> decisions;
47 : };
48 :
49 : struct GroupResult {
50 : uint32_t logicGroupId;
51 : uint32_t index;
52 : };
53 :
54 : struct ResponseInfo {
55 : SrcQueueInfo src;
56 : std::vector<GroupResult> groupResults;
57 : };
58 :
59 : struct RootModelInfo {
60 : uint32_t rootModelId;
61 : bqs::DynamicSchedQueueAttr requestQue;
62 : bqs::DynamicSchedQueueAttr responseQue;
63 : std::vector<RequestInfo> requestCache;
64 : };
65 :
66 : public:
67 : static DynamicSchedMgr& GetInstance(uint32_t deviceId = 0U);
68 : FsmStatus AddRootModelInfo(const RootModelInfo& rootModelInfo);
69 : void DeleteQueue(const uint32_t globalLogicId, const uint32_t rootModelId);
70 : void UpdateNodeId(const int32_t nodeId);
71 : FsmStatus SendRequest(const uint32_t rootModelId, const std::vector<RequestInfo>& requests);
72 : FsmStatus GetResponse(const uint32_t rootModelId, std::vector<ResponseInfo>& responses);
73 : FsmStatus ClearCacheRouteResult();
74 9 : static inline uint64_t DynamicSchedNow()
75 : {
76 9 : static auto zero = std::chrono::system_clock::now();
77 9 : auto us = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now() - zero).count();
78 9 : return uint64_t(us);
79 : }
80 : void DynamicSchedDurationEnd(uint64_t begin);
81 : void DynamicSchedDurationPrint();
82 :
83 : private:
84 : struct CacheRouteKey {
85 : SrcQueueInfo srcQueueInfo;
86 : DstGroupInfo dstGroupInfo;
87 10 : bool operator<(const CacheRouteKey& other) const
88 : {
89 10 : if (srcQueueInfo.rootModelId < other.srcQueueInfo.rootModelId) {
90 0 : return true;
91 : }
92 10 : if ((srcQueueInfo.rootModelId == other.srcQueueInfo.rootModelId) &&
93 10 : (srcQueueInfo.queueLogicId < other.srcQueueInfo.queueLogicId)) {
94 0 : return true;
95 : }
96 10 : if ((srcQueueInfo.rootModelId == other.srcQueueInfo.rootModelId) &&
97 10 : (srcQueueInfo.queueLogicId == other.srcQueueInfo.queueLogicId) &&
98 10 : (dstGroupInfo.logicGroupId < other.dstGroupInfo.logicGroupId)) {
99 0 : return true;
100 : }
101 10 : return false;
102 : }
103 : };
104 : struct CacheRouteValue {
105 : GroupResult result;
106 : uint32_t num;
107 : };
108 : void GenerateRequest(
109 : const std::vector<RequestInfo>& requests, const int32_t centerResponseQueIdx,
110 : dynamic::FlowgwRequest& flowgwRequest) const;
111 : void PrintRequestLog(const dynamic::FlowgwRequest& flowgwRequest) const;
112 : void PrintResponseLog(const dynamic::FlowgwResponse& flowgwResponse) const;
113 : void SendRequestToCacheResult(
114 : const std::vector<RequestInfo>& requests, std::vector<RequestInfo>& requestsAfterCache);
115 : void UpdateCacheResult(const ResponseInfo& getResponseInfo);
116 : void GetResponseFromCacheResult(std::vector<ResponseInfo>& responses);
117 : FsmStatus EnqueueRequest(
118 : const dynamic::FlowgwRequest& flowgwRequest, const uint32_t deviceId, const uint32_t queueId) const;
119 :
120 : int32_t nodeId_;
121 : std::unordered_map<uint32_t, RootModelInfo> rootModelInfos_;
122 : std::map<CacheRouteKey, CacheRouteValue> validCacheInfos_;
123 : std::map<CacheRouteKey, uint32_t> invalidCacheInfos_;
124 : uint32_t requestSentNum_ = 0UL;
125 : uint64_t durationTotal_ = 0ULL;
126 : uint64_t cntTotal_ = 0ULL;
127 : uint64_t durationMax_ = 0ULL;
128 : uint64_t durationSize_ = 0ULL;
129 : uint64_t call_ = 0ULL;
130 : };
131 : } // namespace dgw
132 : #endif
|