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 "res_sched_event_reporter.h"
17
18 #include "file_ex.h"
19 #include "hisysevent.h"
20 #include "res_sched_log.h"
21 #include "res_common_util.h"
22
23
24 namespace OHOS {
25 namespace ResourceSchedule {
26 namespace {
27 static const int64_t FIRST_REPORT_TIME = 10 * 1000 * 1000;
28 static const char* FILE_SIZE_REPORTER_RECORDER = "/data/service/el1/public/ressched/file_size_report_time";
29 static const int64_t ONE_DAY_MICRO_SECOND = static_cast<int64_t>(24) * 60 * 60 * 1000 * 1000;
30 static const char* DATA_FILE_PATH = "/data";
31 }
32
33 IMPLEMENT_SINGLE_INSTANCE(ResschedEventReporter);
34
ReportFileSizeEvent(const std::vector<std::string> & filePaths)35 void ResschedEventReporter::ReportFileSizeEvent(const std::vector<std::string>& filePaths)
36 {
37 std::lock_guard<ffrt::mutex> autoLock(mutex_);
38 if (!filePaths.empty()) {
39 allFilePaths_.insert(filePaths.begin(), filePaths.end());
40 }
41 if (!isTaskSubmit_) {
42 allFilePaths_.insert(FILE_SIZE_REPORTER_RECORDER);
43 SubmitDelayTask(FIRST_REPORT_TIME);
44 isTaskSubmit_ = true;
45 }
46 }
47
ReportFileSizeEvent(const std::string & filePath)48 void ResschedEventReporter::ReportFileSizeEvent(const std::string& filePath)
49 {
50 std::vector<std::string> tmpVector;
51 tmpVector.emplace_back(filePath);
52 ReportFileSizeEvent(tmpVector);
53 }
54
SubmitDelayTask(int64_t delayTime)55 void ResschedEventReporter::SubmitDelayTask(int64_t delayTime)
56 {
57 ffrt::submit([]() {
58 ResschedEventReporter::GetInstance().ReportFileSizeDaily();
59 }, ffrt::task_attr().delay(delayTime));
60 }
61
ReportFileSizeDaily()62 void ResschedEventReporter::ReportFileSizeDaily()
63 {
64 std::string lastReportTime;
65 LoadStringFromFile(FILE_SIZE_REPORTER_RECORDER, lastReportTime);
66 if (lastReportTime.empty()) {
67 ReportFileSizeInner();
68 } else {
69 int64_t lastReportTimeValue = -1;
70 if (!ResCommonUtil::StrToInt64(lastReportTime, lastReportTimeValue) ||
71 lastReportTimeValue < 0) {
72 ReportFileSizeInner();
73 } else {
74 int64_t nowTime = ResCommonUtil::GetNowMicroTime();
75 if (nowTime - lastReportTimeValue < ONE_DAY_MICRO_SECOND) {
76 int64_t nextReportTime = ONE_DAY_MICRO_SECOND - (nowTime- lastReportTimeValue);
77 SubmitDelayTask(nextReportTime);
78 } else {
79 ReportFileSizeInner();
80 }
81 }
82 }
83 }
84
ReportFileSizeInner()85 void ResschedEventReporter::ReportFileSizeInner()
86 {
87 RESSCHED_LOGI("ReportFileSizeInner call");
88 std::lock_guard<ffrt::mutex> autoLock(mutex_);
89 std::vector<std::string> files;
90 std::vector<uint64_t> filesSize;
91 for (const auto& item : allFilePaths_) {
92 files.emplace_back(item);
93 filesSize.emplace_back(ResCommonUtil::GetFileSize(item));
94 }
95 double remainSize = ResCommonUtil::GetDeviceVailidSize(DATA_FILE_PATH);
96 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE",
97 HiviewDFX::HiSysEvent::EventType::STATISTIC,
98 "COMPONENT_NAME", "resource_schedule_service",
99 "PARTITION_NAME", DATA_FILE_PATH,
100 "REMAIN_PARTITION_SIZE", remainSize,
101 "FILE_OR_FOLDER_PATH", files,
102 "FILE_OR_FOLDER_SIZE", filesSize);
103 int64_t nowMicroTime = ResCommonUtil::GetNowMicroTime();
104 SaveStringToFile(FILE_SIZE_REPORTER_RECORDER, std::to_string(nowMicroTime));
105 SubmitDelayTask(ONE_DAY_MICRO_SECOND);
106 }
107 } // namespace ResourceSchedule
108 } // namespace OHOS