1 /*
2 * Copyright (c) 2022-2024 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 <cinttypes>
19
20 #include "ffrt.h"
21 #include "ffrt_inner.h"
22
23 #include "bytrace_adapter.h"
24 #include "define_multimodal.h"
25 #include "input_manager_impl.h"
26 #include "multimodal_input_connect_manager.h"
27
28 #undef MMI_LOG_DOMAIN
29 #define MMI_LOG_DOMAIN MMI_LOG_ANRDETECT
30 #undef MMI_LOG_TAG
31 #define MMI_LOG_TAG "ANRHandler"
32
33 namespace OHOS {
34 namespace MMI {
35 namespace {
36 constexpr int64_t MAX_MARK_PROCESS_DELAY_TIME { 3500000 };
37 constexpr int64_t MIN_MARK_PROCESS_DELAY_TIME { 50000 };
38 constexpr int32_t INVALID_OR_PROCESSED_ID { -1 };
39 constexpr int32_t TIME_TRANSITION { 1000 };
40 constexpr int32_t PRINT_INTERVAL_COUNT { 30 };
41 } // namespace
42
ANRHandler()43 ANRHandler::ANRHandler() {}
44
~ANRHandler()45 ANRHandler::~ANRHandler() {}
46
SetLastProcessedEventId(int32_t eventType,int32_t eventId,int64_t actionTime)47 void ANRHandler::SetLastProcessedEventId(int32_t eventType, int32_t eventId, int64_t actionTime)
48 {
49 CALL_DEBUG_ENTER;
50 MMI_HILOGD("Processed event type:%{public}d, id:%{public}d, actionTime:%{public}" PRId64, eventType, eventId,
51 actionTime);
52 processedCount_++;
53 if (processedCount_ == PRINT_INTERVAL_COUNT) {
54 MMI_HILOG_FREEZEI("Last eventId:%{public}d, current eventId:%{public}d", lastEventId_, eventId);
55 processedCount_ = 0;
56 lastEventId_ = eventId;
57 }
58 SendEvent(eventType, eventId);
59 }
60
MarkProcessed(int32_t eventType,int32_t eventId)61 void ANRHandler::MarkProcessed(int32_t eventType, int32_t eventId)
62 {
63 CALL_DEBUG_ENTER;
64 BytraceAdapter::StartMarkedTracker(eventId);
65 MMI_HILOGD("Processed event type:%{public}d, id:%{public}d", eventType, eventId);
66 {
67 std::lock_guard<std::mutex> guard(mutex_);
68 idList_.push_back(eventId);
69 if (idList_.size() >= PRINT_INTERVAL_COUNT) {
70 std::string idList = std::to_string(idList_.front()) + " " + std::to_string(idList_.back());
71 MMI_HILOG_FREEZEI("Ffrt PE: %{public}s", idList.c_str());
72 idList_.clear();
73 }
74 }
75 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->MarkProcessed(eventType, eventId);
76 BytraceAdapter::StopMarkedTracker();
77 if (ret != 0) {
78 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
79 }
80 }
81
SendEvent(int32_t eventType,int32_t eventId)82 void ANRHandler::SendEvent(int32_t eventType, int32_t eventId)
83 {
84 CALL_DEBUG_ENTER;
85 auto task = [this, eventType, eventId] {
86 MarkProcessed(eventType, eventId);
87 };
88 ffrt::submit(task, {}, {}, ffrt::task_attr().qos(ffrt_qos_deadline_request));
89 }
90
ResetAnrArray()91 void ANRHandler::ResetAnrArray()
92 {
93 for (int i = 0; i < ANR_EVENT_TYPE_NUM; i++) {
94 event_[i].sendStatus = false;
95 event_[i].lastEventId = -1;
96 event_[i].lastReportId = -1;
97 }
98 }
99 } // namespace MMI
100 } // namespace OHOS