1 /*
2  * Copyright (c) 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 <mutex>
17 #include <cinttypes>
18 #include <unordered_map>
19 #include "camera_report_dfx_uitls.h"
20 
21 #include "camera_util.h"
22 #include "camera_log.h"
23 #include "hisysevent.h"
24 #include "ipc_skeleton.h"
25 #include "steady_clock.h"
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 using namespace std;
30 sptr<CameraReportDfxUtils> CameraReportDfxUtils::cameraReportDfx_;
31 std::mutex CameraReportDfxUtils::instanceMutex_;
32 
GetInstance()33 sptr<CameraReportDfxUtils> &CameraReportDfxUtils::GetInstance()
34 {
35     if (CameraReportDfxUtils::cameraReportDfx_ == nullptr) {
36         std::unique_lock<std::mutex> lock(instanceMutex_);
37         if (CameraReportDfxUtils::cameraReportDfx_ == nullptr) {
38             MEDIA_INFO_LOG("Initializing camera report dfx instance");
39             CameraReportDfxUtils::cameraReportDfx_ = new CameraReportDfxUtils();
40         }
41     }
42     return CameraReportDfxUtils::cameraReportDfx_;
43 }
44 
SetFirstBufferStartInfo(CaptureDfxInfo captureInfo)45 void CameraReportDfxUtils::SetFirstBufferStartInfo(CaptureDfxInfo captureInfo)
46 {
47     MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetFirstBufferStartInfo captureID: %{public}d", captureInfo.captureId);
48     captureInfo.firstBufferStartTime = DeferredProcessing::SteadyClock::GetTimestampMilli();
49     unique_lock<mutex> lock(mutex_);
50     captureList_.insert(pair<int32_t, CaptureDfxInfo>(captureInfo.captureId, captureInfo));
51 }
52 
SetFirstBufferEndInfo(int32_t captureId)53 void CameraReportDfxUtils::SetFirstBufferEndInfo(int32_t captureId)
54 {
55     MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetFirstBufferEndInfo captureID: %{public}d", captureId);
56     unique_lock<mutex> lock(mutex_);
57     {
58         map<int32_t, CaptureDfxInfo>::iterator iter = captureList_.find(captureId);
59         if (iter != captureList_.end()) {
60             MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetFirstBufferEndInfo");
61             auto& captureInfo = iter->second;
62             captureInfo.firstBufferEndTime = DeferredProcessing::SteadyClock::GetTimestampMilli();
63         }
64     }
65 }
66 
SetPrepareProxyStartInfo(int32_t captureId)67 void CameraReportDfxUtils::SetPrepareProxyStartInfo(int32_t captureId)
68 {
69     MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetPrepareProxyStartInfo captureID: %{public}d", captureId);
70     unique_lock<mutex> lock(mutex_);
71     {
72         map<int32_t, CaptureDfxInfo>::iterator iter = captureList_.find(captureId);
73         if (iter != captureList_.end()) {
74             MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetPrepareProxyStartInfo");
75             auto& captureInfo = iter->second;
76             captureInfo.prepareProxyStartTime = DeferredProcessing::SteadyClock::GetTimestampMilli();
77         }
78     }
79 }
80 
SetPrepareProxyEndInfo(int32_t captureId)81 void CameraReportDfxUtils::SetPrepareProxyEndInfo(int32_t captureId)
82 {
83     MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetPrepareProxyEndInfo captureID: %{public}d", captureId);
84     unique_lock<mutex> lock(mutex_);
85     {
86         map<int32_t, CaptureDfxInfo>::iterator iter = captureList_.find(captureId);
87         if (iter != captureList_.end()) {
88             MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetPrepareProxyEndInfo");
89             auto& captureInfo = iter->second;
90             captureInfo.prepareProxyEndTime = DeferredProcessing::SteadyClock::GetTimestampMilli();
91         }
92     }
93 }
94 
SetAddProxyStartInfo(int32_t captureId)95 void CameraReportDfxUtils::SetAddProxyStartInfo(int32_t captureId)
96 {
97     MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetAddProxyStartInfo captureID: %{public}d", captureId);
98     unique_lock<mutex> lock(mutex_);
99     {
100         map<int32_t, CaptureDfxInfo>::iterator iter = captureList_.find(captureId);
101         if (iter != captureList_.end()) {
102             MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetAddProxyStartInfo");
103             auto& captureInfo = iter->second;
104             captureInfo.addProxyStartTime = DeferredProcessing::SteadyClock::GetTimestampMilli();
105         }
106     }
107 }
108 
SetAddProxyEndInfo(int32_t captureId)109 void CameraReportDfxUtils::SetAddProxyEndInfo(int32_t captureId)
110 {
111     MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetAddProxyEndInfo captureID: %{public}d", captureId);
112     unique_lock<mutex> lock(mutex_);
113     {
114         map<int32_t, CaptureDfxInfo>::iterator iter = captureList_.find(captureId);
115         if (iter != captureList_.end()) {
116             MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetAddProxyEndInfo");
117             auto& captureInfo = iter->second;
118             captureInfo.addProxyEndTime = DeferredProcessing::SteadyClock::GetTimestampMilli();
119             ReportPerformanceDeferredPhoto(captureInfo);
120             captureList_.erase(captureId);
121         }
122     }
123 }
124 
ReportPerformanceDeferredPhoto(CaptureDfxInfo captureInfo)125 void CameraReportDfxUtils::ReportPerformanceDeferredPhoto(CaptureDfxInfo captureInfo)
126 {
127     MEDIA_DEBUG_LOG("CameraReportDfxUtils::ReportPerformanceDeferredPhoto start");
128     uint64_t firstBufferCostTime = captureInfo.firstBufferEndTime - captureInfo.firstBufferStartTime;
129     uint64_t prepareProxyCostTime = captureInfo.prepareProxyEndTime - captureInfo.prepareProxyStartTime;
130     uint64_t addProxyCostTime = captureInfo.addProxyEndTime - captureInfo.addProxyStartTime;
131 
132     HiSysEventWrite(
133         HiviewDFX::HiSysEvent::Domain::CAMERA,
134         "PERFORMANCE_DEFERRED_PHOTO",
135         HiviewDFX::HiSysEvent::EventType::STATISTIC,
136         "MSG", captureInfo.isSystemApp ? "System Camera" : "Non-system camera",
137         "CAPTURE_ID", captureInfo.captureId,
138         "FIRST_BUFFER_COST_TIME", firstBufferCostTime,
139         "PREPARE_PROXY_COST_TIME", prepareProxyCostTime,
140         "ADD_PROXY_COSTTIME", addProxyCostTime);
141 }
142 } // namespace CameraStandard
143 } // namespace OHOS