1 /*
2 * Copyright (c) 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 "feature/memory_mgr/form_render_report.h"
17 #include "common/event/form_event_report.h"
18 #include "common/util/form_util.h"
19 #include "data_center/form_data_mgr.h"
20 #include "fms_log_wrapper.h"
21 #include "form_render/form_render_mgr_inner.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
26 namespace {
27 constexpr int8_t FRS_DEAD = 0;
28 constexpr int8_t FRS_START = 1;
29 constexpr int8_t FRS_STATUS_MAX_COUNT = 20;
30 }
31
32 /**
33 * @class FormRenderReady
34 * FormRenderReady is used to call frs report.
35 */
FormRenderReport()36 FormRenderReport::FormRenderReport() {}
37
~FormRenderReport()38 FormRenderReport::~FormRenderReport() {}
39
RecordFRSStart()40 void FormRenderReport::RecordFRSStart()
41 {
42 HILOG_INFO("call");
43 if (isFRSFirstLoaded_.load()) {
44 isFRSFirstLoaded_.store(false);
45 RecordFRSStatus(FRS_START);
46 }
47 }
48
RecordFRSDead()49 void FormRenderReport::RecordFRSDead()
50 {
51 HILOG_INFO("call");
52 if (!isFRSFirstLoaded_.load()) {
53 isFRSFirstLoaded_.store(true);
54 }
55 RecordFRSStatus(FRS_DEAD);
56 }
57
RecordFRSStatus(uint8_t status)58 void FormRenderReport::RecordFRSStatus(uint8_t status)
59 {
60 std::unique_lock<std::mutex> lock(frsStatusRecordsMutex_);
61 if (frsStatusRecords_.size() < FRS_STATUS_MAX_COUNT) {
62 int64_t formCount = GetAllFormsCount();
63 bool isLowMemory = FormDataMgr::GetInstance().IsLowMemory();
64 HILOG_INFO("formCount: %{public}" PRId64 ", isLowMemory: %{public}d", formCount, isLowMemory);
65 FormRenderStatusRecord record = {
66 .isLowMemory = isLowMemory,
67 .status = status,
68 .formCount = formCount,
69 .occurrenceTime = FormUtil::GetNowMillisecond()
70 };
71 frsStatusRecords_.push_back(record);
72 }
73 }
74
ReportFRSStatus()75 void FormRenderReport::ReportFRSStatus()
76 {
77 HILOG_INFO("call");
78 {
79 std::unique_lock<std::mutex> lock(frsStatusRecordsMutex_);
80 if (frsStatusRecords_.empty()) {
81 return;
82 }
83 }
84 std::vector<bool> isLowMemoryList;
85 std::vector<uint8_t> statusList;
86 std::vector<int32_t> formCountList;
87 std::vector<int64_t> occurrenceTimeList;
88 {
89 std::unique_lock<std::mutex> lock(frsStatusRecordsMutex_);
90 for (const auto &iter : frsStatusRecords_) {
91 isLowMemoryList.push_back(iter.isLowMemory);
92 statusList.push_back(iter.status);
93 formCountList.push_back(iter.formCount);
94 occurrenceTimeList.push_back(iter.occurrenceTime);
95 }
96 frsStatusRecords_.clear();
97 }
98 FormEventReport::SendFRSStatusEvent(isLowMemoryList, statusList, formCountList, occurrenceTimeList);
99 }
100
GetAllFormsCount()101 int32_t FormRenderReport::GetAllFormsCount()
102 {
103 int32_t tempFormsCount;
104 FormDataMgr::GetInstance().GetTempFormsCount(tempFormsCount);
105 int32_t castFormsCount;
106 FormDataMgr::GetInstance().GetCastFormsCount(castFormsCount);
107 return tempFormsCount + castFormsCount;
108 }
109 }
110 }
111