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 "cann_error_reporter.h"
12 : #include <cstdio>
13 : #include "log.h"
14 :
15 : namespace dfx {
16 : constexpr uint64_t DEFAULT_SENSOR_HANDLE = 0xFFFFFFFF;
17 :
18 6 : CannErrorReporter::CannErrorReporter() : sensorHandle_(DEFAULT_SENSOR_HANDLE), devId_(0) {}
19 :
20 2 : CannErrorReporter::~CannErrorReporter()
21 : {
22 2 : status2Event_.clear();
23 : void Clear();
24 2 : }
25 :
26 140 : CannErrorReporter& CannErrorReporter::GetInstance()
27 : {
28 140 : static CannErrorReporter instance;
29 140 : return instance;
30 : }
31 :
32 0 : HcclResult CannErrorReporter::Init(uint32_t deviceId)
33 : {
34 0 : if (sensorHandle_ != DEFAULT_SENSOR_HANDLE && devId_ == deviceId) {
35 0 : return HCCL_SUCCESS;
36 : }
37 :
38 0 : if (sensorHandle_ != DEFAULT_SENSOR_HANDLE) {
39 : void Clear();
40 : }
41 :
42 0 : CHK_PRT_RET(
43 : RegisterSensorNode(deviceId, &sensorHandle_) != HCCL_SUCCESS,
44 : HCCL_RUN_WARNING("[CannErrorReporter][Init] init sensor node fail, deviceId[%u].", deviceId), HCCL_E_INTERNAL);
45 0 : devId_ = deviceId;
46 0 : return HCCL_SUCCESS;
47 : }
48 :
49 140 : HcclResult CannErrorReporter::Clear()
50 : {
51 140 : if (sensorHandle_ != DEFAULT_SENSOR_HANDLE) {
52 0 : HcclResult ret = UnRegisterSensorNode(devId_, sensorHandle_);
53 0 : if (ret != HCCL_SUCCESS) {
54 0 : HCCL_RUN_WARNING(
55 : "[CannErrorReporter][Clear] unregister sensor node fail, handle[%llu], deviceId[%u].", sensorHandle_,
56 : devId_);
57 : } else {
58 0 : HCCL_INFO(
59 : "[CannErrorReporter][Clear] unregister sensor node success, handle[%llu], devId[%u].", sensorHandle_,
60 : devId_);
61 : }
62 : }
63 140 : sensorHandle_ = DEFAULT_SENSOR_HANDLE;
64 140 : devId_ = 0;
65 140 : return HCCL_SUCCESS;
66 : }
67 :
68 0 : HcclResult CannErrorReporter::UpdateSensorNode(uint32_t deviceId, ReportStatus reportStatus)
69 : {
70 0 : if (sensorHandle_ == DEFAULT_SENSOR_HANDLE || devId_ != deviceId) {
71 0 : CHK_PRT_RET(
72 : Init(deviceId) != HCCL_SUCCESS,
73 : HCCL_RUN_WARNING("[CannErrorReporter][UpdateSensorNode] fail to get a valid node sensor."),
74 : HCCL_E_INTERNAL);
75 : }
76 0 : uint64_t handle = sensorHandle_;
77 :
78 0 : auto iter = status2Event_.find(reportStatus);
79 0 : if (iter == status2Event_.end()) {
80 0 : HCCL_RUN_WARNING(
81 : "[CannErrorReporter][UpdateSensorNode] "
82 : "unknown reportStatus[%d], fail to find corresponding event type, update node sensor fail.",
83 : static_cast<int>(reportStatus));
84 0 : return HCCL_E_NOT_SUPPORT;
85 : }
86 0 : int32_t eventType = static_cast<int32_t>(iter->second.first);
87 0 : HcclGeneralEventType eventAssert = iter->second.second;
88 :
89 0 : HCCL_INFO(
90 : "[CannErrorReporter][UpdateSensorNode]updating sensor, "
91 : "deviceId[%u], reportStatus[%d], event val[%d], event assert[%d].",
92 : deviceId, static_cast<int>(reportStatus), eventType, static_cast<int>(eventAssert));
93 :
94 0 : CHK_PRT_RET(
95 : hrtHalSensorNodeUpdateState(deviceId, handle, eventType, eventAssert) != HCCL_SUCCESS,
96 : HCCL_RUN_WARNING(
97 : "[CannErrorReporter][UpdateSensorNode] "
98 : "update sensor node fail, deviceId[%u], reportStatus[%d], val[%d], handle[%llu].",
99 : deviceId, static_cast<int>(reportStatus), eventType, handle),
100 : HCCL_E_INTERNAL);
101 :
102 0 : HCCL_RUN_INFO(
103 : "[CannErrorReporter][UpdateSensorNode] send report success, "
104 : "deviceId[%u], reportStatus[%d], val[%d], handle[%llu].",
105 : deviceId, static_cast<int>(reportStatus), eventType, handle);
106 0 : return HCCL_SUCCESS;
107 : }
108 :
109 0 : HcclResult CannErrorReporter::RegisterSensorNode(uint32_t deviceId, uint64_t* handle)
110 : {
111 0 : HcclResult ret = hrtHalSensorNodeRegister(deviceId, handle);
112 0 : CHK_PRT_RET(
113 : ret != HCCL_SUCCESS,
114 : HCCL_RUN_WARNING("[CannErrorReporter][RegisterSensorNode] register sensor node fail, device id[%u].", deviceId),
115 : HCCL_E_INTERNAL);
116 :
117 0 : HCCL_INFO(
118 : "[CannErrorReporter][RegisterSensorNode] register node sensor success, deviceId[%u], handle[%llu].", deviceId,
119 : *handle);
120 0 : return HCCL_SUCCESS;
121 : }
122 :
123 0 : HcclResult CannErrorReporter::UnRegisterSensorNode(uint32_t deviceId, uint64_t handle)
124 : {
125 0 : HcclResult ret = hrtHalSensorNodeUnregister(deviceId, handle);
126 0 : CHK_PRT_RET(
127 : ret != HCCL_SUCCESS,
128 : HCCL_RUN_WARNING(
129 : "[CannErrorReporter][UnRegisterSensorNode] "
130 : "unregister sensor node fail, device id[%u], handle[%llu].",
131 : deviceId, handle),
132 : HCCL_E_INTERNAL);
133 0 : HCCL_INFO(
134 : "[CannErrorReporter][UnRegisterSensorNode] unregister sensor success, device id[%u], handle[%llu].", deviceId,
135 : handle);
136 :
137 0 : return HCCL_SUCCESS;
138 : }
139 :
140 : } // namespace dfx
|