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 9 : static auto zero = std::chrono::system_clock::now();
76 9 : auto us = std::chrono::duration_cast<std::chrono::nanoseconds>(
77 18 : std::chrono::system_clock::now() - zero).count();
78 9 : return uint64_t(us);
79 : }
80 : void DynamicSchedDurationEnd(uint64_t begin);
81 : void DynamicSchedDurationPrint();
82 : private:
83 : struct CacheRouteKey {
84 : SrcQueueInfo srcQueueInfo;
85 : DstGroupInfo dstGroupInfo;
86 10 : bool operator<(const CacheRouteKey &other) const {
87 10 : if (srcQueueInfo.rootModelId < other.srcQueueInfo.rootModelId) {
88 0 : return true;
89 : }
90 10 : if ((srcQueueInfo.rootModelId == other.srcQueueInfo.rootModelId) &&
91 10 : (srcQueueInfo.queueLogicId < other.srcQueueInfo.queueLogicId)) {
92 0 : return true;
93 : }
94 10 : if ((srcQueueInfo.rootModelId == other.srcQueueInfo.rootModelId) &&
95 10 : (srcQueueInfo.queueLogicId == other.srcQueueInfo.queueLogicId) &&
96 10 : (dstGroupInfo.logicGroupId < other.dstGroupInfo.logicGroupId)) {
97 0 : return true;
98 : }
99 10 : return false;
100 : }
101 : };
102 : struct CacheRouteValue {
103 : GroupResult result;
104 : uint32_t num;
105 : };
106 : void GenerateRequest(const std::vector<RequestInfo> &requests,
107 : const int32_t centerResponseQueIdx,
108 : dynamic::FlowgwRequest &flowgwRequest) const;
109 : void PrintRequestLog(const dynamic::FlowgwRequest &flowgwRequest) const;
110 : void PrintResponseLog(const dynamic::FlowgwResponse &flowgwResponse) const;
111 : void SendRequestToCacheResult(const std::vector<RequestInfo> &requests, std::vector<RequestInfo> &requestsAfterCache);
112 : void UpdateCacheResult(const ResponseInfo &getResponseInfo);
113 : void GetResponseFromCacheResult(std::vector<ResponseInfo> &responses);
114 : FsmStatus EnqueueRequest(const dynamic::FlowgwRequest &flowgwRequest, const uint32_t deviceId, const uint32_t queueId) const;
115 :
116 : int32_t nodeId_;
117 : std::unordered_map<uint32_t, RootModelInfo> rootModelInfos_;
118 : std::map<CacheRouteKey, CacheRouteValue> validCacheInfos_;
119 : std::map<CacheRouteKey, uint32_t> invalidCacheInfos_;
120 : uint32_t requestSentNum_ = 0UL;
121 : uint64_t durationTotal_ = 0ULL;
122 : uint64_t cntTotal_ = 0ULL;
123 : uint64_t durationMax_ = 0ULL;
124 : uint64_t durationSize_ = 0ULL;
125 : uint64_t call_ = 0ULL;
126 : };
127 : }
128 : #endif
|