1 /*
2  * Copyright (C) 2023 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 #define MLOG_TAG "MultiStagesCaptureDfxTriggerRatio"
17 
18 #include "multistages_capture_dfx_trigger_ratio.h"
19 
20 #include <cstdlib>
21 
22 #include "media_file_utils.h"
23 #include "media_log.h"
24 #include "post_event_utils.h"
25 
26 namespace OHOS {
27 namespace Media {
28 const int64_t REPORT_TIME_INTERVAL = 24 * 60 * 60 * 1000L; // 24 hours
29 
MultiStagesCaptureDfxTriggerRatio()30 MultiStagesCaptureDfxTriggerRatio::MultiStagesCaptureDfxTriggerRatio()
31     : thirdPartCount_(0), autoCount_(0), lastReportTime_(0), isReporting_(false) {}
32 
~MultiStagesCaptureDfxTriggerRatio()33 MultiStagesCaptureDfxTriggerRatio::~MultiStagesCaptureDfxTriggerRatio() {}
34 
GetInstance()35 MultiStagesCaptureDfxTriggerRatio& MultiStagesCaptureDfxTriggerRatio::GetInstance()
36 {
37     static MultiStagesCaptureDfxTriggerRatio instance;
38     return instance;
39 }
40 
SetTrigger(const MultiStagesCaptureTriggerType & type)41 void MultiStagesCaptureDfxTriggerRatio::SetTrigger(const MultiStagesCaptureTriggerType &type)
42 {
43     if (type == MultiStagesCaptureTriggerType::AUTO) {
44         autoCount_++;
45     } else {
46         thirdPartCount_++;
47     }
48 
49     if (ShouldReport()) {
50         Report();
51     }
52 }
53 
ShouldReport()54 bool MultiStagesCaptureDfxTriggerRatio::ShouldReport()
55 {
56     std::lock_guard<std::mutex> lock(shouldReportMutex_);
57     if (isReporting_) {
58         return false;
59     }
60 
61     int64_t currentTime = MediaFileUtils::UTCTimeMilliSeconds();
62     if ((currentTime - lastReportTime_) < REPORT_TIME_INTERVAL) {
63         return false;
64     }
65 
66     isReporting_ = true;
67     return true;
68 }
69 
Report()70 void MultiStagesCaptureDfxTriggerRatio::Report()
71 {
72     MEDIA_INFO_LOG("Report thirdPartCount_ = %{public}d, autoCount_ = %{public}d", thirdPartCount_, autoCount_);
73     if (thirdPartCount_ == 0 && autoCount_ == 0) {
74         return;
75     }
76 
77     VariantMap map = {{KEY_THIRD_PART_COUNT, thirdPartCount_}, {KEY_AUTO_COUNT, autoCount_}};
78     PostEventUtils::GetInstance().PostStatProcess(StatType::MSC_TRIGGER_RATIO_STAT, map);
79 
80     thirdPartCount_ = 0;
81     autoCount_ = 0;
82     lastReportTime_ = MediaFileUtils::UTCTimeMilliSeconds();
83     {
84         std::lock_guard<std::mutex> lock(shouldReportMutex_);
85         isReporting_ = false;
86     }
87 }
88 
89 } // namespace Media
90 } // namespace OHOS