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("[FSM] Entity qid:[%u] type:[%s] state:[%s] desc:[%s].",
27 : 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("processRet is %d, cached data size is %zu",
36 : static_cast<int32_t>(processRet), entity.GetSendDataObjs().size());
37 0 : return processRet;
38 : }
39 : }
40 :
41 45 : int32_t srcStatus = static_cast<int32_t>(QUEUE_NORMAL);
42 45 : const auto ret = halQueueGetStatus(entity.GetDeviceId(), entity.GetQueueId(),
43 : QUERY_QUEUE_STATUS, static_cast<uint32_t>(sizeof(uint32_t)), &srcStatus);
44 45 : if (ret != DRV_ERROR_NONE) {
45 4 : g_dequeueFailtimes++;
46 9 : if ((ret == DRV_ERROR_NOT_EXIST) ||
47 5 : ((entity.GetQueueType() == bqs::CLIENT_Q) && (ret == DRV_ERROR_INNER_ERR))) {
48 3 : return entity.ChangeState(FsmState::FSM_ERROR_STATE);
49 : } else {
50 1 : if (g_dequeueFailtimes < FAIL_PRINT_THRESHOLD) {
51 1 : DGW_LOG_ERROR("halQueueGetStatus failed, queueId=[%u], ret=[%d].", entity.GetQueueId(),
52 : static_cast<int32_t>(ret));
53 : }
54 :
55 1 : return FsmStatus::FSM_FAILED;
56 : }
57 : } else {
58 41 : g_dequeueFailtimes = 0U;
59 : }
60 :
61 41 : if (srcStatus != static_cast<int32_t>(QUEUE_EMPTY)) {
62 37 : return PostProcess(entity);
63 : }
64 4 : return FsmStatus::FSM_SUCCESS;
65 : }
66 :
67 42 : FsmStatus IdleState::PostProcess(Entity &entity)
68 : {
69 42 : return entity.ChangeState(FsmState::FSM_PEEK_STATE);
70 : }
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("Entity[%s] has %zu recvEntity to process", entity.ToString().c_str(),
99 : 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("processRet is %d, cached data size is %zu",
123 : static_cast<int32_t>(processRet), entity.GetSendDataObjs().size());
124 0 : return processRet;
125 : }
126 : }
127 5 : return PostProcess(entity);
128 : }
129 :
130 : REGISTER_STATE(FSM_IDLE_STATE, ENTITY_QUEUE, IdleState);
131 : REGISTER_STATE(FSM_IDLE_STATE, ENTITY_TAG, IdleState);
132 : REGISTER_STATE(FSM_IDLE_STATE, ENTITY_GROUP, GroupIdleState);
133 : } // namespace dgw
|