1 /*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "anr_handler.h"
17
18 #include "ffrt_inner.h"
19
20 #include "bytrace_adapter.h"
21 #include "multimodal_input_connect_manager.h"
22 #include "mmi_log.h"
23
24 #undef MMI_LOG_DOMAIN
25 #define MMI_LOG_DOMAIN MMI_LOG_ANRDETECT
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "ANRHandler"
28
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 [[ maybe_unused ]] constexpr int64_t MAX_MARK_PROCESS_DELAY_TIME { 3500000 };
33 [[ maybe_unused ]] constexpr int64_t MIN_MARK_PROCESS_DELAY_TIME { 50000 };
34 [[ maybe_unused ]] constexpr int32_t INVALID_OR_PROCESSED_ID { -1 };
35 [[ maybe_unused ]] constexpr int32_t TIME_TRANSITION { 1000 };
36 constexpr int32_t PRINT_INTERVAL_COUNT { 100 };
37 } // namespace
38
ANRHandler()39 ANRHandler::ANRHandler() {}
40
~ANRHandler()41 ANRHandler::~ANRHandler() {}
42
SetLastProcessedEventId(int32_t eventType,int32_t eventId,int64_t actionTime)43 void ANRHandler::SetLastProcessedEventId(int32_t eventType, int32_t eventId, int64_t actionTime)
44 {
45 CALL_DEBUG_ENTER;
46 MMI_HILOGD("Processed event type:%{public}d, id:%{public}d, actionTime:%{public}" PRId64, eventType, eventId,
47 actionTime);
48 processedCount_++;
49 if (processedCount_ == PRINT_INTERVAL_COUNT) {
50 MMI_HILOGD("Last eventId:%{public}d, current eventId:%{public}d", lastEventId_, eventId);
51 processedCount_ = 0;
52 lastEventId_ = eventId;
53 }
54 SendEvent(eventType, eventId);
55 }
56
MarkProcessed(int32_t eventType,int32_t eventId)57 void ANRHandler::MarkProcessed(int32_t eventType, int32_t eventId)
58 {
59 CALL_DEBUG_ENTER;
60 BytraceAdapter::StartMarkedTracker(eventId);
61 MMI_HILOGD("Processed event type:%{public}d, id:%{public}d", eventType, eventId);
62 {
63 std::lock_guard<std::mutex> guard(mutex_);
64 idList_.push_back(eventId);
65 if (idList_.size() >= PRINT_INTERVAL_COUNT) {
66 std::string idList = std::to_string(idList_.front()) + " " + std::to_string(idList_.back());
67 MMI_HILOG_FREEZEI("Ffrt PE:%{public}s", idList.c_str());
68 idList_.clear();
69 }
70 }
71 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->MarkProcessed(eventType, eventId);
72 BytraceAdapter::StopMarkedTracker();
73 if (ret != 0) {
74 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
75 }
76 }
77
SendEvent(int32_t eventType,int32_t eventId)78 void ANRHandler::SendEvent(int32_t eventType, int32_t eventId)
79 {
80 CALL_DEBUG_ENTER;
81 auto task = [this, eventType, eventId] {
82 MarkProcessed(eventType, eventId);
83 };
84 ffrt::submit(task, {}, {}, ffrt::task_attr().qos(ffrt_qos_deadline_request));
85 }
86
ResetAnrArray()87 void ANRHandler::ResetAnrArray()
88 {
89 for (int i = 0; i < ANR_EVENT_TYPE_NUM; i++) {
90 event_[i].sendStatus = false;
91 event_[i].lastEventId = -1;
92 event_[i].lastReportId = -1;
93 }
94 }
95 } // namespace MMI
96 } // namespace OHOS