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