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 "fsm/idle_state.h"
12 : #include "state_manager.h"
13 : #include "entity_manager.h"
14 : #include "common/bqs_log.h"
15 : #include "driver/ascend_hal.h"
16 :
17 : namespace dgw {
18 : namespace {
19 : uint32_t g_dequeueFailtimes = 0U;
20 : constexpr uint32_t FAIL_PRINT_THRESHOLD = 10U;
21 : constexpr uint32_t DYNAMIC_SCHEDULE_THRESHOLD = 100U;
22 : } // namespace
23 :
24 45 : FsmStatus IdleState::ProcessMessage(Entity& entity, const InnerMessage& msg)
25 : {
26 45 : DGW_LOG_INFO(
27 : "[FSM] Entity qid:[%u] type:[%s] state:[%s] desc:[%s].", entity.GetId(), entity.GetTypeDesc().c_str(),
28 : entity.GetStateDesc(FsmState::FSM_IDLE_STATE).c_str(), entity.ToString().c_str());
29 : (void)msg;
30 45 : entity.ResetScheduleCount();
31 45 : if (entity.GetWaitDecisionState()) {
32 1 : DGW_LOG_INFO("Enitity[%s] ProcessWaitingData", entity.ToString().c_str());
33 1 : const auto processRet = ProcessWaitingData(entity);
34 1 : if ((processRet != FsmStatus::FSM_SUCCESS) || (entity.GetSendDataObjs().size() > DYNAMIC_SCHEDULE_THRESHOLD)) {
35 0 : DGW_LOG_WARN(
36 : "processRet is %d, cached data size is %zu", static_cast<int32_t>(processRet),
37 : entity.GetSendDataObjs().size());
38 0 : return processRet;
39 : }
40 : }
41 :
42 45 : int32_t srcStatus = static_cast<int32_t>(QUEUE_NORMAL);
43 45 : const auto ret = halQueueGetStatus(
44 45 : entity.GetDeviceId(), entity.GetQueueId(), QUERY_QUEUE_STATUS, static_cast<uint32_t>(sizeof(uint32_t)),
45 : &srcStatus);
46 45 : if (ret != DRV_ERROR_NONE) {
47 4 : g_dequeueFailtimes++;
48 9 : if ((ret == DRV_ERROR_NOT_EXIST) ||
49 5 : ((entity.GetQueueType() == bqs::CLIENT_Q) && (ret == DRV_ERROR_INNER_ERR))) {
50 3 : return entity.ChangeState(FsmState::FSM_ERROR_STATE);
51 : } else {
52 1 : if (g_dequeueFailtimes < FAIL_PRINT_THRESHOLD) {
53 1 : DGW_LOG_ERROR(
54 : "halQueueGetStatus failed, queueId=[%u], ret=[%d].", entity.GetQueueId(),
55 : static_cast<int32_t>(ret));
56 : }
57 :
58 1 : return FsmStatus::FSM_FAILED;
59 : }
60 : } else {
61 41 : g_dequeueFailtimes = 0U;
62 : }
63 :
64 41 : if (srcStatus != static_cast<int32_t>(QUEUE_EMPTY)) {
65 37 : return PostProcess(entity);
66 : }
67 4 : return FsmStatus::FSM_SUCCESS;
68 : }
69 :
70 42 : FsmStatus IdleState::PostProcess(Entity& entity) { return entity.ChangeState(FsmState::FSM_PEEK_STATE); }
71 :
72 40 : FsmStatus IdleState::PreProcess(Entity& entity)
73 : {
74 : (void)entity;
75 40 : return FsmStatus::FSM_SUCCESS;
76 : }
77 :
78 1 : FsmStatus IdleState::ProcessWaitingData(Entity& entity) const
79 : {
80 1 : auto& sendDataObjs = entity.GetSendDataObjs();
81 1 : auto sendDataCount = sendDataObjs.size();
82 1 : DGW_LOG_INFO("Entity[%s] current send objects is %zu", entity.ToString().c_str(), sendDataCount);
83 : do {
84 2 : sendDataCount = sendDataObjs.size();
85 2 : if (sendDataCount == 0U) {
86 1 : break;
87 : }
88 1 : auto& sendDataObj = sendDataObjs.front();
89 : // process sendDataObj, when data bonding to this obj was sent completely,
90 : // the sendDataObj will be removed from sendDataObjs, then sendDataObjs's size will decrease
91 : // and we should process the sendDataObjs following
92 1 : std::vector<Entity*> processableRecvEntities;
93 2 : for (auto elem : sendDataObj->GetRecvEntities()) {
94 1 : if (elem->GetType() != EntityType::ENTITY_GROUP) {
95 1 : processableRecvEntities.emplace_back(elem);
96 : }
97 : }
98 1 : DGW_LOG_INFO(
99 : "Entity[%s] has %zu recvEntity to process", entity.ToString().c_str(), processableRecvEntities.size());
100 1 : InnerMessage msg;
101 1 : msg.msgType = InnerMsgType::INNER_MSG_PUSH;
102 2 : for (auto recvEntityPtr : processableRecvEntities) {
103 1 : (void)recvEntityPtr->AddDataObjToRecvList(sendDataObj);
104 1 : if (recvEntityPtr->ProcessMessage(msg) == FsmStatus::FSM_ERROR) {
105 0 : return FsmStatus::FSM_ERROR;
106 : }
107 : }
108 2 : } while (sendDataObjs.size() != sendDataCount);
109 1 : DGW_LOG_INFO("Entity[%s] send objects is %zu after process", entity.ToString().c_str(), sendDataObjs.size());
110 :
111 1 : return FsmStatus::FSM_SUCCESS;
112 : }
113 :
114 5 : FsmStatus GroupIdleState::ProcessMessage(Entity& entity, const InnerMessage& msg)
115 : {
116 : (void)msg;
117 5 : entity.ResetScheduleCount();
118 5 : if (entity.GetWaitDecisionState()) {
119 0 : DGW_LOG_INFO("Enitity[%s] ProcessWaitingData", entity.ToString().c_str());
120 0 : const auto processRet = ProcessWaitingData(entity);
121 0 : if ((processRet != FsmStatus::FSM_SUCCESS) || (entity.GetSendDataObjs().size() > DYNAMIC_SCHEDULE_THRESHOLD)) {
122 0 : DGW_LOG_WARN(
123 : "processRet is %d, cached data size is %zu", static_cast<int32_t>(processRet),
124 : entity.GetSendDataObjs().size());
125 0 : return processRet;
126 : }
127 : }
128 5 : return PostProcess(entity);
129 : }
130 :
131 : REGISTER_STATE(FSM_IDLE_STATE, ENTITY_QUEUE, IdleState);
132 : REGISTER_STATE(FSM_IDLE_STATE, ENTITY_TAG, IdleState);
133 : REGISTER_STATE(FSM_IDLE_STATE, ENTITY_GROUP, GroupIdleState);
134 : } // namespace dgw
|