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()45 void CameraReportDfxUtils::SetFirstBufferStartInfo()
46 {
47 MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetFirstBufferStartInfo");
48 unique_lock<mutex> lock(mutex_);
49 {
50 setFirstBufferStartTime_ = DeferredProcessing::SteadyClock::GetTimestampMilli();
51 isBufferSetting_ = true;
52 }
53 }
54
SetFirstBufferEndInfo()55 void CameraReportDfxUtils::SetFirstBufferEndInfo()
56 {
57 MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetFirstBufferEndInfo");
58 unique_lock<mutex> lock(mutex_);
59 {
60 CHECK_ERROR_RETURN_LOG(!isBufferSetting_, "CameraReportDfxUtils::SetFirstBufferEndInfo cancel report");
61 setFirstBufferEndTime_ = DeferredProcessing::SteadyClock::GetTimestampMilli();
62 isBufferSetting_ = false;
63 }
64 }
65
SetPrepareProxyStartInfo()66 void CameraReportDfxUtils::SetPrepareProxyStartInfo()
67 {
68 MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetPrepareProxyStartInfo");
69 unique_lock<mutex> lock(mutex_);
70 {
71 setPrepareProxyStartTime_ = DeferredProcessing::SteadyClock::GetTimestampMilli();
72 isPrepareProxySetting_ = true;
73 }
74 }
75
SetPrepareProxyEndInfo()76 void CameraReportDfxUtils::SetPrepareProxyEndInfo()
77 {
78 MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetPrepareProxyEndInfo");
79 unique_lock<mutex> lock(mutex_);
80 {
81 CHECK_ERROR_RETURN_LOG(!isPrepareProxySetting_, "CameraReportDfxUtils::SetPrepareProxyEndInfo cancel report");
82 setPrepareProxyEndTime_ = DeferredProcessing::SteadyClock::GetTimestampMilli();
83 isPrepareProxySetting_ = false;
84 }
85 }
86
SetAddProxyStartInfo()87 void CameraReportDfxUtils::SetAddProxyStartInfo()
88 {
89 MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetAddProxyStartInfo");
90 unique_lock<mutex> lock(mutex_);
91 {
92 setAddProxyStartTime_ = DeferredProcessing::SteadyClock::GetTimestampMilli();
93 isAddProxySetting_ = true;
94 }
95 }
96
SetAddProxyEndInfo()97 void CameraReportDfxUtils::SetAddProxyEndInfo()
98 {
99 MEDIA_DEBUG_LOG("CameraReportDfxUtils::SetAddProxyEndInfo");
100 unique_lock<mutex> lock(mutex_);
101 {
102 CHECK_ERROR_RETURN_LOG(!isAddProxySetting_, "CameraReportDfxUtils::SetAddProxyEndInfo cancel report");
103 setAddProxyEndTime_ = DeferredProcessing::SteadyClock::GetTimestampMilli();
104 isAddProxySetting_ = false;
105 ReportPerformanceDeferredPhoto();
106 }
107 }
108
ReportPerformanceDeferredPhoto()109 void CameraReportDfxUtils::ReportPerformanceDeferredPhoto()
110 {
111 MEDIA_DEBUG_LOG("CameraReportDfxUtils::ReportPerformanceDeferredPhoto");
112 stringstream ss;
113 uint64_t firstBufferCostTime = setFirstBufferEndTime_ - setFirstBufferStartTime_;
114 uint64_t prepareProxyCostTime = setPrepareProxyEndTime_ - setPrepareProxyStartTime_;
115 uint64_t addProxyCostTime = setAddProxyEndTime_ - setAddProxyStartTime_;
116 ss << "firstBufferCostTime:" << firstBufferCostTime
117 << ",prepareProxyCostTime:" << prepareProxyCostTime
118 << ",addProxyCostTime:" << addProxyCostTime;
119
120 HiSysEventWrite(
121 HiviewDFX::HiSysEvent::Domain::CAMERA,
122 "PERFORMANCE_DEFERRED_PHOTO",
123 HiviewDFX::HiSysEvent::EventType::STATISTIC,
124 "MSG", ss.str());
125 }
126 } // namespace CameraStandard
127 } // namespace OHOS