• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
21 #include <utility>
22 
23 #include "hidl_profiler.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace camera {
28 namespace implementation {
29 namespace hidl_profiler {
30 namespace {
31 
32 struct HidlProfiler {
HidlProfilerandroid::hardware::camera::implementation::hidl_profiler::__anon8912d7fe0111::HidlProfiler33   HidlProfiler() {
34     int32_t mode = property_get_int32("persist.camera.profiler.open_close", 0);
35     if (mode) {
36       // Use stop watch mode to print result.
37       mode |= google::camera_common::Profiler::SetPropFlag::kStopWatch;
38     }
39     profiler = google::camera_common::Profiler::Create(mode);
40     if (profiler != nullptr) {
41       profiler->SetDumpFilePrefix(
42           "/data/vendor/camera/profiler/hidl_open_close_");
43       profiler->Start("Overall", 0);
44     }
45   }
46 
~HidlProfilerandroid::hardware::camera::implementation::hidl_profiler::__anon8912d7fe0111::HidlProfiler47   ~HidlProfiler() {
48     if (profiler != nullptr) {
49       profiler->End("Overall", 0);
50     }
51   }
52 
53   std::shared_ptr<google::camera_common::Profiler> profiler = nullptr;
54   bool has_camera_open = false;
55   uint8_t config_counter = 0;
56   uint8_t flush_counter = 0;
57   uint8_t connector_counter = 0;
58 };
59 
60 std::unique_ptr<HidlProfiler> gHidlProfiler = nullptr;
61 // Mutex to make all API functions mutually exclusive.
62 std::mutex api_mutex;
63 
StartNewConnector()64 void StartNewConnector() {
65   if (gHidlProfiler != nullptr && gHidlProfiler->profiler != nullptr) {
66     gHidlProfiler->profiler->Start("<-- IDLE -->",
67                                    ++gHidlProfiler->connector_counter);
68   }
69 }
70 
EndConnector()71 void EndConnector() {
72   if (gHidlProfiler != nullptr && gHidlProfiler->profiler != nullptr &&
73       gHidlProfiler->connector_counter != 0) {
74     gHidlProfiler->profiler->End("<-- IDLE -->",
75                                  gHidlProfiler->connector_counter);
76   }
77 }
78 
EndProfiler()79 void EndProfiler() {
80   gHidlProfiler = nullptr;
81 }
82 
83 }  // anonymous namespace
84 
OnCameraOpen()85 std::unique_ptr<HidlProfilerItem> OnCameraOpen() {
86   std::lock_guard lock(api_mutex);
87   gHidlProfiler = std::make_unique<HidlProfiler>();
88   if (gHidlProfiler == nullptr || gHidlProfiler->profiler == nullptr) {
89     ALOGE("%s: gHidlProfiler or profiler is nullptr.", __FUNCTION__);
90     return nullptr;
91   }
92 
93   gHidlProfiler->has_camera_open = true;
94   gHidlProfiler->profiler->SetUseCase("Open Camera");
95 
96   return std::make_unique<HidlProfilerItem>(gHidlProfiler->profiler, "Open",
97                                             StartNewConnector);
98 }
99 
OnCameraFlush()100 std::unique_ptr<HidlProfilerItem> OnCameraFlush() {
101   std::lock_guard lock(api_mutex);
102   EndConnector();
103   if (gHidlProfiler == nullptr) {
104     gHidlProfiler = std::make_unique<HidlProfiler>();
105   }
106 
107   if (gHidlProfiler == nullptr || gHidlProfiler->profiler == nullptr) {
108     ALOGE("%s: gHidlProfiler or profiler is nullptr.", __FUNCTION__);
109     return nullptr;
110   }
111   gHidlProfiler->profiler->SetUseCase("Flush Camera");
112   return std::make_unique<HidlProfilerItem>(gHidlProfiler->profiler, "Flush",
113                                             StartNewConnector,
114                                             gHidlProfiler->flush_counter++);
115 }
116 
OnCameraClose()117 std::unique_ptr<HidlProfilerItem> OnCameraClose() {
118   std::lock_guard lock(api_mutex);
119   EndConnector();
120   if (gHidlProfiler == nullptr) {
121     gHidlProfiler = std::make_unique<HidlProfiler>();
122   }
123 
124   if (gHidlProfiler == nullptr || gHidlProfiler->profiler == nullptr) {
125     ALOGE("%s: gHidlProfiler or profiler is nullptr.", __FUNCTION__);
126     return nullptr;
127   }
128 
129   gHidlProfiler->profiler->SetUseCase("Close Camera");
130   return std::make_unique<HidlProfilerItem>(gHidlProfiler->profiler, "Close",
131                                             EndProfiler);
132 }
133 
OnCameraStreamConfigure()134 std::unique_ptr<HidlProfilerItem> OnCameraStreamConfigure() {
135   std::lock_guard lock(api_mutex);
136   EndConnector();
137   if (gHidlProfiler == nullptr) {
138     gHidlProfiler = std::make_unique<HidlProfiler>();
139   }
140 
141   if (gHidlProfiler == nullptr || gHidlProfiler->profiler == nullptr) {
142     ALOGE("%s: gHidlProfiler or profiler is nullptr.", __FUNCTION__);
143     return nullptr;
144   }
145 
146   if (!gHidlProfiler->has_camera_open) {
147     gHidlProfiler->profiler->SetUseCase("Reconfigure Stream");
148   }
149 
150   return std::make_unique<HidlProfilerItem>(
151       gHidlProfiler->profiler, "configureStreams", StartNewConnector,
152       gHidlProfiler->config_counter++);
153 }
154 
OnFirstFrameRequest()155 void OnFirstFrameRequest() {
156   std::lock_guard lock(api_mutex);
157   EndConnector();
158   if (gHidlProfiler != nullptr && gHidlProfiler->profiler != nullptr) {
159     gHidlProfiler->profiler->Start(
160         "First frame", google::camera_common::Profiler::kInvalidRequestId);
161     gHidlProfiler->profiler->Start(
162         "HAL Total", google::camera_common::Profiler::kInvalidRequestId);
163   }
164 }
165 
OnFirstFrameResult()166 void OnFirstFrameResult() {
167   std::lock_guard lock(api_mutex);
168   if (gHidlProfiler != nullptr && gHidlProfiler->profiler != nullptr) {
169     gHidlProfiler->profiler->End(
170         "First frame", google::camera_common::Profiler::kInvalidRequestId);
171     gHidlProfiler->profiler->End(
172         "HAL Total", google::camera_common::Profiler::kInvalidRequestId);
173     EndProfiler();
174   }
175 }
176 
HidlProfilerItem(std::shared_ptr<google::camera_common::Profiler> profiler,const std::string target,std::function<void ()> on_end,int request_id)177 HidlProfilerItem::HidlProfilerItem(
178     std::shared_ptr<google::camera_common::Profiler> profiler,
179     const std::string target, std::function<void()> on_end, int request_id)
180     : profiler_(profiler), target_(std::move(target)), request_id_(request_id) {
181   on_end_ = on_end;
182   profiler_->Start(target_, request_id_);
183   profiler_->Start("HAL Total",
184                    google::camera_common::Profiler::kInvalidRequestId);
185 }
186 
~HidlProfilerItem()187 HidlProfilerItem::~HidlProfilerItem() {
188   profiler_->End("HAL Total",
189                  google::camera_common::Profiler::kInvalidRequestId);
190   profiler_->End(target_, request_id_);
191   if (on_end_) {
192     on_end_();
193   }
194 }
195 
196 }  // namespace hidl_profiler
197 }  // namespace implementation
198 }  // namespace camera
199 }  // namespace hardware
200 }  // namespace android
201