1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "GCH_HidlProfiler"
19 #include <log/log.h>
20 #include <utility>
21 
22 #include "hidl_profiler.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace camera {
27 namespace implementation {
28 namespace hidl_profiler {
29 namespace {
30 
31 struct HidlProfiler {
HidlProfilerandroid::hardware::camera::implementation::hidl_profiler::__anon187dbcd30111::HidlProfiler32   HidlProfiler() {
33     int32_t mode = property_get_int32("persist.camera.profiler.open_close", 0);
34     if (mode) {
35       // Use stop watch mode to print result.
36       mode |= google::camera_common::Profiler::SetPropFlag::kStopWatch;
37     }
38     profiler = google::camera_common::Profiler::Create(mode);
39     profiler->SetDumpFilePrefix(
40         "/data/vendor/camera/profiler/hidl_open_close_");
41     profiler->Start("Overall", 0);
42   }
43 
~HidlProfilerandroid::hardware::camera::implementation::hidl_profiler::__anon187dbcd30111::HidlProfiler44   ~HidlProfiler() {
45     profiler->End("Overall", 0);
46   }
47 
48   std::shared_ptr<google::camera_common::Profiler> profiler = nullptr;
49   bool has_camera_open = false;
50   uint8_t config_counter = 0;
51   uint8_t flush_counter = 0;
52   uint8_t connector_counter = 0;
53 };
54 
55 std::unique_ptr<HidlProfiler> gHidlProfiler = nullptr;
56 
StartNewConnector()57 void StartNewConnector() {
58   if (gHidlProfiler != nullptr) {
59     gHidlProfiler->profiler->Start("<-- IDLE -->",
60                                    ++gHidlProfiler->connector_counter);
61   }
62 }
63 
EndConnector()64 void EndConnector() {
65   if (gHidlProfiler != nullptr && gHidlProfiler->connector_counter != 0) {
66     gHidlProfiler->profiler->End("<-- IDLE -->",
67                                  gHidlProfiler->connector_counter);
68   }
69 }
70 
EndProfiler()71 void EndProfiler() {
72   gHidlProfiler = nullptr;
73 }
74 
75 }  // anonymous namespace
76 
OnCameraOpen()77 std::unique_ptr<HidlProfilerItem> OnCameraOpen() {
78   gHidlProfiler = std::make_unique<HidlProfiler>();
79   gHidlProfiler->has_camera_open = true;
80   gHidlProfiler->profiler->SetUseCase("Open Camera");
81 
82   return std::make_unique<HidlProfilerItem>(gHidlProfiler->profiler, "Open",
83                                             StartNewConnector);
84 }
85 
OnCameraFlush()86 std::unique_ptr<HidlProfilerItem> OnCameraFlush() {
87   EndConnector();
88   if (gHidlProfiler == nullptr) {
89     gHidlProfiler = std::make_unique<HidlProfiler>();
90   }
91   gHidlProfiler->profiler->SetUseCase("Flush Camera");
92   return std::make_unique<HidlProfilerItem>(gHidlProfiler->profiler, "Flush",
93                                             StartNewConnector,
94                                             gHidlProfiler->flush_counter++);
95 }
96 
OnCameraClose()97 std::unique_ptr<HidlProfilerItem> OnCameraClose() {
98   EndConnector();
99   if (gHidlProfiler == nullptr) {
100     gHidlProfiler = std::make_unique<HidlProfiler>();
101   }
102   gHidlProfiler->profiler->SetUseCase("Close Camera");
103   return std::make_unique<HidlProfilerItem>(gHidlProfiler->profiler, "Close",
104                                             EndProfiler);
105 }
106 
OnCameraStreamConfigure()107 std::unique_ptr<HidlProfilerItem> OnCameraStreamConfigure() {
108   EndConnector();
109   if (gHidlProfiler == nullptr) {
110     gHidlProfiler = std::make_unique<HidlProfiler>();
111   }
112   if (!gHidlProfiler->has_camera_open) {
113     gHidlProfiler->profiler->SetUseCase("Reconfigure Stream");
114   }
115 
116   return std::make_unique<HidlProfilerItem>(
117       gHidlProfiler->profiler, "configureStreams", StartNewConnector,
118       gHidlProfiler->config_counter++);
119 }
120 
OnFirstFrameRequest()121 void OnFirstFrameRequest() {
122   EndConnector();
123   if (gHidlProfiler != nullptr) {
124     gHidlProfiler->profiler->Start(
125         "First frame", google::camera_common::Profiler::kInvalidRequestId);
126     gHidlProfiler->profiler->Start(
127         "HAL Total", google::camera_common::Profiler::kInvalidRequestId);
128   }
129 }
130 
OnFirstFrameResult()131 void OnFirstFrameResult() {
132   if (gHidlProfiler != nullptr) {
133     gHidlProfiler->profiler->End(
134         "First frame", google::camera_common::Profiler::kInvalidRequestId);
135     gHidlProfiler->profiler->End(
136         "HAL Total", google::camera_common::Profiler::kInvalidRequestId);
137     EndProfiler();
138   }
139 }
140 
HidlProfilerItem(std::shared_ptr<google::camera_common::Profiler> profiler,const std::string target,std::function<void ()> on_end,int request_id)141 HidlProfilerItem::HidlProfilerItem(
142     std::shared_ptr<google::camera_common::Profiler> profiler,
143     const std::string target, std::function<void()> on_end, int request_id)
144     : profiler_(profiler), target_(std::move(target)), request_id_(request_id) {
145   on_end_ = on_end;
146   profiler_->Start(target_, request_id_);
147   profiler_->Start("HAL Total",
148                    google::camera_common::Profiler::kInvalidRequestId);
149 }
150 
~HidlProfilerItem()151 HidlProfilerItem::~HidlProfilerItem() {
152   profiler_->End("HAL Total",
153                  google::camera_common::Profiler::kInvalidRequestId);
154   profiler_->End(target_, request_id_);
155   if (on_end_) {
156     on_end_();
157   }
158 }
159 
160 }  // namespace hidl_profiler
161 }  // namespace implementation
162 }  // namespace camera
163 }  // namespace hardware
164 }  // namespace android
165