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