1 /*
2  * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
3  * Not a Contribution.
4  *
5  * Copyright (C) 2012 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef __QCAMERATRACE_H__
21 #define __QCAMERATRACE_H__
22 #include <stdlib.h>
23 #include <utils/Trace.h>
24 #include "camscope_packet_type.h"
25 
26 #ifdef QCAMERA_REDEFINE_LOG
27 #define CAM_MODULE CAM_HAL_MODULE
28 extern "C" {
29 #include "mm_camera_dbg.h"
30 }
31 #endif
32 
33 #undef ATRACE_CALL
34 #undef ATRACE_NAME
35 #undef ATRACE_BEGIN
36 #undef ATRACE_INT
37 #undef ATRACE_END
38 #undef ATRACE_BEGIN_SNPRINTF
39 #undef KPI_ATRACE_BEGIN
40 #undef KPI_ATRACE_END
41 #undef KPI_ATRACE_INT
42 #undef ATRACE_TAG
43 #undef ATRACE_BEGIN_DBG
44 #undef ATRACE_INT_DBG
45 #undef ATRACE_END_DBG
46 
47 #define KPI_ONLY 1
48 #define KPI_DBG 2
49 
50 #define CAMERA_TRACE_BUF 32
51 
52 #define ATRACE_TAG ATRACE_TAG_ALWAYS
53 
54 //to enable only KPI logs
55 #define KPI_ATRACE_BEGIN(name) ({\
56 if (gKpiDebugLevel >= KPI_ONLY) { \
57      atrace_begin(ATRACE_TAG, name); \
58 }\
59 })
60 
61 #define KPI_ATRACE_END() ({\
62 if (gKpiDebugLevel >= KPI_ONLY) { \
63      atrace_end(ATRACE_TAG); \
64 }\
65 })
66 
67 #define KPI_ATRACE_INT(name,val) ({\
68 if (gKpiDebugLevel >= KPI_ONLY) { \
69      atrace_int(ATRACE_TAG, name, val); \
70 }\
71 })
72 
73 
74 #define ATRACE_BEGIN_SNPRINTF(fmt_str, ...) \
75  if (gKpiDebugLevel >= KPI_DBG) { \
76    char trace_tag[CAMERA_TRACE_BUF]; \
77    snprintf(trace_tag, CAMERA_TRACE_BUF, fmt_str, ##__VA_ARGS__); \
78    ATRACE_BEGIN(trace_tag); \
79 }
80 
81 #define ATRACE_BEGIN_DBG(name) ({\
82 if (gKpiDebugLevel >= KPI_DBG) { \
83      atrace_begin(ATRACE_TAG, name); \
84 }\
85 })
86 
87 #define ATRACE_END_DBG() ({\
88 if (gKpiDebugLevel >= KPI_DBG) { \
89      atrace_end(ATRACE_TAG); \
90 }\
91 })
92 
93 #define ATRACE_INT_DBG(name,val) ({\
94 if (gKpiDebugLevel >= KPI_DBG) { \
95      atrace_int(ATRACE_TAG, name, val); \
96 }\
97 })
98 
99 #define ATRACE_BEGIN ATRACE_BEGIN_DBG
100 #define ATRACE_INT ATRACE_INT_DBG
101 #define ATRACE_END ATRACE_END_DBG
102 
103 #define CAMSCOPE_MAX_STRING_LENGTH 64
104 
105 /* Initializes CameraScope tool */
106 void camscope_init(camscope_section_type camscope_section);
107 
108 /* Cleans up CameraScope tool */
109 void camscope_destroy(camscope_section_type camscope_section);
110 
111 /* Reserves a number of bytes on the memstore flushing to the
112  * file system if remaining space is insufficient */
113 uint32_t camscope_reserve(camscope_section_type camscope_section,
114                                  uint32_t num_bytes_to_reserve);
115 
116 /* Store the data to the memstore and calculate remaining space */
117 void camscope_store_data(camscope_section_type camscope_section,
118                        void* data, uint32_t size);
119 
120 /* Lock the camscope mutex lock for the given camscope section */
121 void camscope_mutex_lock(camscope_section_type camscope_section);
122 
123 /* Unlock the camscope mutex lock for the given camscope section */
124 void camscope_mutex_unlock(camscope_section_type camscope_section);
125 
126 #define CAMSCOPE_SYSTRACE_TIME_MARKER() { \
127     if (kpi_camscope_frame_count != 0) { \
128         if (kpi_camscope_flags & CAMSCOPE_ON_FLAG) { \
129             struct timeval t_domain; \
130             char trace_time_conv[CAMSCOPE_MAX_STRING_LENGTH]; \
131             gettimeofday(&t_domain, NULL); \
132             snprintf(trace_time_conv, sizeof(trace_time_conv), \
133                      "_CAMSCOPE_TIME_CONV_:%ld:%ld", t_domain.tv_sec, \
134                      t_domain.tv_usec); \
135             atrace_int(ATRACE_TAG_ALWAYS, trace_time_conv, 0); \
136         } \
137     } \
138 }
139 
140 #define CAMSCOPE_MASK(mask) { \
141     char prop[PROPERTY_VALUE_MAX]; \
142     property_get("persist.camera.kpi.camscope", prop, "0"); \
143     mask = atoi(prop); \
144 }
145 
146 #define CAMSCOPE_FRAME_COUNT_MASK(mask) { \
147     char prop[PROPERTY_VALUE_MAX]; \
148     property_get("persist.camera.kpi.camscope_cnt", prop, "0"); \
149     mask = atoi(prop); \
150 }
151 
152 #define CAMSCOPE_UPDATE_FLAGS(camscope_section, camscope_prop) { \
153     if (kpi_camscope_frame_count != 0) { \
154         static uint32_t camscope_frame_counter = 0; \
155         if (camscope_frame_counter >= kpi_camscope_frame_count) { \
156             uint32_t prev_prop = camscope_prop; \
157             CAMSCOPE_MASK(camscope_prop); \
158             uint32_t is_prev_prop_on = (prev_prop & CAMSCOPE_ON_FLAG) \
159                                         ? 1 : 0; \
160             uint32_t is_prop_on = (camscope_prop & CAMSCOPE_ON_FLAG) \
161                                    ? 1 : 0; \
162             if (is_prev_prop_on ^ is_prop_on) { \
163                 if (is_prop_on) { \
164                     camscope_init(camscope_section); \
165                 } else { \
166                     camscope_destroy(camscope_section); \
167                 } \
168             } \
169             CAMSCOPE_SYSTRACE_TIME_MARKER(); \
170             camscope_frame_counter = 0; \
171         } \
172         else { \
173             ++camscope_frame_counter; \
174         } \
175     } \
176 }
177 
178 #define CAMSCOPE_INIT(camscope_section) { \
179     CAMSCOPE_FRAME_COUNT_MASK(kpi_camscope_frame_count); \
180     if (kpi_camscope_frame_count != 0) { \
181         CAMSCOPE_MASK(kpi_camscope_flags); \
182         if (kpi_camscope_flags & CAMSCOPE_ON_FLAG) { \
183             camscope_init(camscope_section); \
184             CAMSCOPE_SYSTRACE_TIME_MARKER(); \
185         } \
186     } \
187 }
188 
189 #define CAMSCOPE_DESTROY(camscope_section) { \
190     if (kpi_camscope_frame_count != 0) { \
191         if (kpi_camscope_flags & CAMSCOPE_ON_FLAG) { \
192             camscope_destroy(camscope_section); \
193         } \
194     } \
195 }
196 
197 #define KPI_ATRACE_CAMSCOPE_BEGIN(camscope_name) ({\
198 if (camscope_name < CAMSCOPE_EVENT_NAME_SIZE && \
199     camscope_name >= 0) { \
200     KPI_ATRACE_BEGIN(camscope_atrace_names[camscope_name]); \
201 } \
202 camscope_sw_base_log((uint32_t)CAMSCOPE_SECTION_HAL, \
203                      CAMSCOPE_KPI_MASK, \
204                      CAMSCOPE_SYNC_BEGIN, \
205                      camscope_name); \
206 })
207 
208 #define KPI_ATRACE_CAMSCOPE_END(camscope_name) ({\
209 KPI_ATRACE_END(); \
210 camscope_sw_base_log((uint32_t)CAMSCOPE_SECTION_HAL, \
211                      CAMSCOPE_KPI_MASK, \
212                      CAMSCOPE_SYNC_END, \
213                      camscope_name); \
214 })
215 
216 // This macro only works with counter values that act like begin/end
217 #define KPI_ATRACE_CAMSCOPE_INT(name, camscope_name, counter) ({\
218 KPI_ATRACE_INT(name, counter); \
219 camscope_timing_log((uint32_t)CAMSCOPE_SECTION_HAL, \
220                      CAMSCOPE_KPI_MASK, \
221                      counter ? CAMSCOPE_ASYNC_BEGIN : CAMSCOPE_ASYNC_END, \
222                      camscope_name, 0); \
223 })
224 
225 #define ATRACE_CAMSCOPE_BEGIN(camscope_name) ({\
226 if (camscope_name < CAMSCOPE_EVENT_NAME_SIZE && \
227     camscope_name >= 0) { \
228     ATRACE_BEGIN_DBG(camscope_atrace_names[camscope_name]); \
229 } \
230 camscope_sw_base_log((uint32_t)CAMSCOPE_SECTION_HAL, \
231                      CAMSCOPE_KPI_DBG_MASK, \
232                      CAMSCOPE_SYNC_BEGIN, \
233                      camscope_name); \
234 })
235 
236 #define ATRACE_CAMSCOPE_END(camscope_name) ({\
237 ATRACE_END_DBG(); \
238 camscope_sw_base_log(CAMSCOPE_SECTION_HAL, \
239                      CAMSCOPE_KPI_DBG_MASK, \
240                      CAMSCOPE_SYNC_END, \
241                      camscope_name); \
242 })
243 
244 #define KPI_ATRACE_CAMSCOPE_NAME(camscope_name) qcamera::CamscopeTraceKpi ___tracer(camscope_name)
245 #define ATRACE_CAMSCOPE_NAME(camscope_name) qcamera::CamscopeTraceDbg ___tracer(camscope_name)
246 #define KPI_ATRACE_CAMSCOPE_CALL(camscope_name) KPI_ATRACE_CAMSCOPE_NAME(camscope_name)
247 #define ATRACE_CAMSCOPE_CALL(camscope_name) ATRACE_CAMSCOPE_NAME(camscope_name)
248 
249 #define KPI_ATRACE_NAME(name) qcamera::ScopedTraceKpi ___tracer(ATRACE_TAG, name)
250 #define ATRACE_NAME(name) qcamera::ScopedTraceDbg ___tracer(ATRACE_TAG, name)
251 #define KPI_ATRACE_CALL() KPI_ATRACE_NAME(__FUNCTION__)
252 #define ATRACE_CALL() ATRACE_NAME(__FUNCTION__)
253 
254 namespace qcamera {
255 extern volatile uint32_t gKpiDebugLevel;
256 class ScopedTraceKpi {
257 public:
ScopedTraceKpi(uint64_t tag,const char * name)258     inline ScopedTraceKpi(uint64_t tag, const char *name)
259     : mTag(tag) {
260         if (gKpiDebugLevel >= KPI_ONLY) {
261             atrace_begin(mTag,name);
262         }
263     }
264 
~ScopedTraceKpi()265     inline ~ScopedTraceKpi() {
266         if (gKpiDebugLevel >= KPI_ONLY) {
267             atrace_end(mTag);
268         }
269     }
270 
271     private:
272         uint64_t mTag;
273 };
274 
275 class ScopedTraceDbg {
276 public:
ScopedTraceDbg(uint64_t tag,const char * name)277     inline ScopedTraceDbg(uint64_t tag, const char *name)
278     : mTag(tag) {
279         if (gKpiDebugLevel >= KPI_DBG) {
280             atrace_begin(mTag,name);
281         }
282     }
283 
~ScopedTraceDbg()284     inline ~ScopedTraceDbg() {
285         if (gKpiDebugLevel >= KPI_DBG) {
286             atrace_end(mTag);
287         }
288     }
289 
290     private:
291         uint64_t mTag;
292 };
293 
294 class CamscopeTraceKpi {
295 public:
CamscopeTraceKpi(const uint32_t camscope_name)296     inline CamscopeTraceKpi(const uint32_t camscope_name)
297     : mCamscopeName(camscope_name) {
298         KPI_ATRACE_CAMSCOPE_BEGIN(mCamscopeName);
299     }
300 
~CamscopeTraceKpi()301     inline ~CamscopeTraceKpi() {
302         KPI_ATRACE_CAMSCOPE_END(mCamscopeName);
303     }
304 
305     private:
306         const uint32_t mCamscopeName;
307 };
308 
309 class CamscopeTraceDbg {
310 public:
CamscopeTraceDbg(const uint32_t camscope_name)311     inline CamscopeTraceDbg(const uint32_t camscope_name)
312     : mCamscopeName(camscope_name) {
313         ATRACE_CAMSCOPE_BEGIN(mCamscopeName);
314     }
315 
~CamscopeTraceDbg()316     inline ~CamscopeTraceDbg() {
317         ATRACE_CAMSCOPE_END(mCamscopeName);
318     }
319 
320     private:
321         const uint32_t mCamscopeName;
322 };
323 };
324 
325 extern volatile uint32_t gKpiDebugLevel;
326 
327 #endif /* __QCAMREATRACE_H__ */
328