1/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2*
3* Redistribution and use in source and binary forms, with or without
4* modification, are permitted provided that the following conditions are
5* met:
6*     * Redistributions of source code must retain the above copyright
7*       notice, this list of conditions and the following disclaimer.
8*     * Redistributions in binary form must reproduce the above
9*       copyright notice, this list of conditions and the following
10*       disclaimer in the documentation and/or other materials provided
11*       with the distribution.
12*     * Neither the name of The Linux Foundation nor the names of its
13*       contributors may be used to endorse or promote products derived
14*       from this software without specific prior written permission.
15*
16* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*
28*/
29
30#include "camscope_packet_type.h"
31#include "QCameraTrace.h"
32
33#if defined(__linux__) && !defined(__ANDROID__)
34#include <unistd.h>
35#include <sys/syscall.h>
36#endif
37
38const char * camscope_atrace_names[CAMSCOPE_EVENT_NAME_SIZE] = {
39    "Mct_Sof",
40    "Mct_super_params",
41    "Mct_special_event",
42    "Mct_process_bus_msg",
43    "Camera:AFD",
44    "Camera:ASD",
45    "Camera:AEC",
46    "Camera:AWB",
47    "Camera:AF",
48    "CPP",
49    "CPP_Capture",
50    "CPP_clock_request",
51    "CPP_Holding_Time",
52    "CPP_Hardware_On",
53    "Snapshot",
54    "ISP_Hardware_Update",
55    "JPEG",
56    "FaceProc",
57    "Sensor_process_event",
58    "FD_num_faces_detected",
59    "Camera:alloc",
60    "iface:streamon_fwd",
61    "iface:streamon_to_thread",
62    "iface:streamoff_fwd",
63    "iface:streamoff_to_thread",
64    "iface:config_ISP",
65    "iface:hw_config",
66    "iface:create_axi_hw",
67    "iface:config_axi_hw",
68    "iface:streamon",
69    "iface:streamoff",
70    "AF_START",
71    "AF_SET",
72    "Camera:IS",
73    "ISP:streamon",
74    "ISP:streamoff",
75    "ISP:set_Strm_config",
76    "VFE_HW_UPDATE",
77    "ISP:streamon_fwd",
78    "SENSOR_SD_OPEN",
79    "SENSOR_START_SESSION",
80    "SENSOR_SET_RESOLUTION",
81    "SENSOR_SET_STREAM_CONFIG",
82    "SENSOR_CONFIG_PDAF",
83    "SENSOR_LOAD_CHROMATIX",
84    "SENSOR_START_STREAM",
85    "SENSOR_SET_FPS",
86    "SENSOR_STREAMOFF",
87    "Camera:WNR",
88    "Camera:WNR:memcpy",
89    "PPROC_streamoff",
90    "CPP:Streamon",
91    "Camera:CAC",
92    "CPP_create_hw_frame",
93    "CPP_set_Strm_config",
94    "Mct_start_session",
95    "Mct_stop_session",
96    "IMG:streamon",
97    "MCT:create_buf",
98    "start_preview",
99    "stop_preview",
100    "take_picture",
101    "close_camera_device",
102    "openCamera",
103    "startPreview",
104    "stopPreview",
105    "capture_channel_cb_routine",
106    "preview_stream_cb_routine",
107    "SNAPSHOT",
108    "getStreamBufs",
109    "openCamera",
110    "closeCamera",
111    "flush",
112    "zsl_channel_cb",
113    "postproc_channel_cb_routine",
114    "synchronous_stream_cb_routine",
115    "nodisplay_preview_stream_cb_routine",
116    "rdi_mode_stream_cb_routine",
117    "postview_stream_cb_routine",
118    "video_stream_cb_routine",
119    "snapshot_channel_cb_routine",
120    "raw_stream_cb_routine",
121    "raw_channel_cb_routine",
122    "preview_raw_stream_cb_routine",
123    "snapshot_raw_stream_cb_routine",
124    "metadata_stream_cb_routine",
125    "reprocess_stream_cb_routine",
126    "callback_stream_cb_routine",
127    "set_preview_window",
128    "set_CallBacks",
129    "enable_msg_type",
130    "disable_msg_type",
131    "msg_type_enabled",
132    "prepare_preview",
133    "preview_enabled",
134    "restart_start_preview",
135    "restart_stop_preview",
136    "pre_start_recording",
137    "start_recording",
138    "stop_recording",
139    "recording_enabled",
140    "release_recording_frame",
141    "cancel_auto_focus",
142    "pre_take_picture",
143    "cancel_picture",
144    "set_parameters",
145    "stop_after_set_params",
146    "commit_params",
147    "restart_after_set_params",
148    "get_parameters",
149    "put_parameters",
150    "send_command",
151    "send_command_restart",
152    "release",
153    "register_face_image",
154    "prepare_snapshot",
155    "QCamera2HardwareInterface",
156    "initCapabilities",
157    "getCapabilities",
158    "preparePreview",
159    "prepareHardwareForSnapshot",
160    "initialize",
161    "configureStreams",
162    "configureStreamsPerfLocked",
163    "handleBatchMetadata",
164    "handleMetadataWithLock",
165    "handleInputBufferWithLock",
166    "handleBufferWithLock",
167    "processCaptureRequest",
168    "flushPerf",
169    "getCamInfo",
170    "dynamicUpdateMetaStreamInfo",
171    "start",
172    "stop",
173    "flush",
174    "streamCbRoutine",
175    "registerBuffer",
176    "reprocessCbRoutine",
177    "initialize",
178    "request",
179    "initialize",
180    "streamCbRoutine",
181    "initialize",
182    "streamCbRoutine",
183    "jpegEvtHandle",
184    "request",
185    "dataNotifyCB",
186    "streamCbRoutine",
187    "registerBuffer",
188    "start",
189    "stop",
190    "init",
191    "initJpeg",
192    "releaseJpegJobData",
193    "releasePPJobData",
194    "encodeData",
195    "preview_stream_cb_routine",
196    "stop_preview",
197    "capture_channel_cb_routine"
198};
199
200/*===========================================================================
201 * FUNCTION       : get_thread_id
202 *
203 * DESCRIPTION    : helper function to get the current thread ID
204 *
205 * PARAMETERS     : N/A
206 *
207 * RETURN         : the thread ID
208 *==========================================================================*/
209 pid_t get_thread_id() {
210#if defined(__linux__) && !defined(__ANDROID__)
211    return syscall(__NR_gettid);
212#else
213    return gettid();
214#endif
215}
216
217/*===========================================================================
218 * FUNCTION       : fill_camscope_base
219 *
220 * DESCRIPTION    : helper function to set the struct's data with the given
221 *                  parameters
222 *
223 * PARAMETERS     :
224 *   @scope_struct: struct to fill out
225 *   @packet_type : packet_type data value to set
226 *   @size        : size data value to set
227 *
228 * RETURN         : void
229 *==========================================================================*/
230void fill_camscope_base(camscope_base *scope_struct, uint32_t packet_type,
231                        uint32_t size) {
232    scope_struct->packet_type = packet_type;
233    scope_struct->size = size;
234}
235
236/*===========================================================================
237 * FUNCTION       : fill_camscope_sw_base
238 *
239 * DESCRIPTION    : helper function to set the struct's data with the given
240 *                  parameters
241 *
242 * PARAMETERS     :
243 *   @scope_struct: struct to fill out
244 *   @packet_type : packet_type data value to set
245 *   @size        : size data value to set
246 *   @timestamp   : timestamp value to store
247 *   @thread_id   : identifier of where the packet originates from
248 *   @event_name  : name of the event to store
249 *
250 * RETURN         : void
251 *==========================================================================*/
252void fill_camscope_sw_base(camscope_sw_base *scope_struct,
253                           uint32_t packet_type, uint32_t size,
254                           struct timeval timestamp,
255                           int32_t thread_id, uint32_t event_name) {
256    fill_camscope_base(&(scope_struct->base), packet_type, size);
257    scope_struct->timestamp = timestamp;
258    scope_struct->thread_id = thread_id;
259    scope_struct->event_name = event_name;
260}
261
262/*===========================================================================
263 * FUNCTION       : fill_camscope_timing
264 *
265 * DESCRIPTION    : helper function to set the struct's data with the given
266 *                  parameters
267 *
268 * PARAMETERS     :
269 *   @scope_struct: struct to fill out
270 *   @packet_type : packet_type data value to set
271 *   @size        : size data value to set
272 *   @timestamp   : timestamp value to store
273 *   @thread_id   : identifier of where the packet originates from
274 *   @event_name  : name of the event to store
275 *   @frame_id    : frame identifier of which frame the packet originates from
276 *
277 * RETURN         : void
278 *==========================================================================*/
279void fill_camscope_timing(camscope_timing *scope_struct, uint32_t packet_type,
280                          uint32_t size, struct timeval timestamp,
281                          int32_t thread_id, uint32_t event_name,
282                          uint32_t frame_id) {
283    fill_camscope_sw_base(&(scope_struct->sw_base), packet_type, size,
284                          timestamp, thread_id, event_name);
285    scope_struct->frame_id = frame_id;
286}
287
288/*===========================================================================
289 * FUNCTION        : fill_camscope_in_out_timing
290 *
291 * DESCRIPTION     : helper function to set the struct's data with the given
292 *                   parameters
293 *
294 * PARAMETERS      :
295 *   @scope_struct : struct to fill out
296 *   @packet_type  : packet_type data value to set
297 *   @size         : size data value to set
298 *   @timestamp    : timestamp value to store
299 *   @thread_id    : identifier of where the packet originates from
300 *   @event_name   : name of the event to store
301 *   @in_timestamp : timestamp of when start of event occurred
302 *   @out_timestamp: timestamp of when end of event occurred
303 *   @frame_id     : frame identifier of which frame the packet
304 *                   originates from
305 *
306 * RETURN          : void
307 *==========================================================================*/
308void fill_camscope_in_out_timing(camscope_in_out_timing *scope_struct,
309                                 uint32_t packet_type, uint32_t size,
310                                 struct timeval timestamp,
311                                 int32_t thread_id, uint32_t event_name,
312                                 struct timeval in_timestamp,
313                                 struct timeval out_timestamp,
314                                 uint32_t frame_id) {
315    fill_camscope_sw_base(&(scope_struct->sw_base), packet_type, size,
316                          timestamp, thread_id, event_name);
317    scope_struct->in_timestamp = in_timestamp;
318    scope_struct->out_timestamp = out_timestamp;
319    scope_struct->frame_id = frame_id;
320}
321
322/*===========================================================================
323 * FUNCTION               : camscope_base_log
324 *
325 * DESCRIPTION            : CameraScope Base logging function that stores
326 *                          the base amount of data for a camscope packet
327 *
328 * PARAMETERS             :
329 *   @camscope_section    : section of code where this log is being called
330 *   @camscope_enable_mask: Enable/Disable mask
331 *   @packet_type         : camscope packet_type
332 *
333 * RETURN                 : void
334 *==========================================================================*/
335void camscope_base_log(uint32_t camscope_section,
336                       uint32_t camscope_enable_mask, uint32_t packet_type) {
337    if (kpi_camscope_frame_count != 0) {
338        if (kpi_camscope_flags & camscope_enable_mask) {
339            struct timeval timestamp;
340            gettimeofday(&timestamp, NULL);
341            camscope_mutex_lock((camscope_section_type)camscope_section);
342            camscope_base scope_struct;
343            uint32_t size = sizeof(scope_struct);
344            uint32_t total_size =
345              camscope_reserve((camscope_section_type)camscope_section, size);
346            if (size == total_size) {
347                fill_camscope_base(&scope_struct, packet_type, size);
348                camscope_store_data((camscope_section_type)camscope_section,
349                                    &scope_struct, size);
350            }
351            camscope_mutex_unlock((camscope_section_type)camscope_section);
352        }
353    }
354}
355
356/*===========================================================================
357 * FUNCTION               : camscope_sw_base_log
358 *
359 * DESCRIPTION            : CameraScope Software Base logging function that
360 *                          stores the minimum amount of data for tracing
361 *
362 * PARAMETERS             :
363 *   @camscope_section    : section of code where this log is being called
364 *   @camscope_enable_mask: enable/disable mask
365 *   @packet_type         : camscope packet_type
366 *   @event_name          : name of the event that the packet is storing
367 *
368 * RETURN                 : void
369 *==========================================================================*/
370void camscope_sw_base_log(uint32_t camscope_section,
371                          uint32_t camscope_enable_mask,
372                          uint32_t packet_type, uint32_t event_name) {
373    if (kpi_camscope_frame_count != 0) {
374        if (kpi_camscope_flags & camscope_enable_mask) {
375            struct timeval timestamp;
376            gettimeofday(&timestamp, NULL);
377            camscope_mutex_lock((camscope_section_type)camscope_section);
378            camscope_sw_base scope_struct;
379            uint32_t size = sizeof(scope_struct);
380            int32_t thread_id = (int32_t)get_thread_id();
381            uint32_t total_size =
382              camscope_reserve((camscope_section_type)camscope_section, size);
383            if (size == total_size) {
384                fill_camscope_sw_base(&scope_struct, packet_type, size,
385                                      timestamp, thread_id, event_name);
386                camscope_store_data((camscope_section_type)camscope_section,
387                                    &scope_struct, size);
388            }
389            camscope_mutex_unlock((camscope_section_type)camscope_section);
390        }
391    }
392}
393
394/*===========================================================================
395 * FUNCTION               : camscope_timing_log
396 *
397 * DESCRIPTION            : CameraScope Timing logging function that
398 *                          stores data used for the timing of events
399 *                          with respect to their frame id
400 *
401 * PARAMETERS             :
402 *   @camscope_section    : section of code where this log is being called
403 *   @camscope_enable_mask: enable/Disable mask
404 *   @packet_type         : camscope packet_type
405 *   @event_name          : name of the event that the packet is storing
406 *   @frame_id            : frame id that the packet is logging
407 *
408 * RETURN                 : void
409 *==========================================================================*/
410void camscope_timing_log(uint32_t camscope_section,
411                         uint32_t camscope_enable_mask, uint32_t packet_type,
412                         uint32_t event_name, uint32_t frame_id) {
413    if (kpi_camscope_frame_count != 0) {
414        if (kpi_camscope_flags & camscope_enable_mask) {
415            struct timeval timestamp;
416            gettimeofday(&timestamp, NULL);
417            camscope_mutex_lock((camscope_section_type)camscope_section);
418            camscope_timing scope_struct;
419            uint32_t size = sizeof(scope_struct);
420            int32_t thread_id = (int32_t)get_thread_id();
421            uint32_t total_size =
422              camscope_reserve((camscope_section_type)camscope_section, size);
423            if (size == total_size) {
424                fill_camscope_timing(&scope_struct, packet_type, size,
425                                     timestamp, thread_id, event_name,
426                                     frame_id);
427                camscope_store_data((camscope_section_type)camscope_section,
428                                    &scope_struct, size);
429            }
430            camscope_mutex_unlock((camscope_section_type)camscope_section);
431        }
432    }
433}
434
435/*===========================================================================
436 * FUNCTION               : camscope_in_out_timing_log
437 *
438 * DESCRIPTION            : CameraScope In-Out Timing logging function that
439 *                          stores given timestamps with the packet data
440 *
441 * PARAMETERS             :
442 *   @camscope_section    : section of code where this log is being called
443 *   @camscope_enable_mask: enable/Disable mask
444 *   @packet_type         : camscope packet_type
445 *   @event_name          : name of the event that the packet is storing
446 *   @frame_id            : frame id that the packet is logging
447 *
448 * RETURN                 : void
449 *==========================================================================*/
450void camscope_in_out_timing_log(uint32_t camscope_section,
451                                uint32_t camscope_enable_mask,
452                                uint32_t packet_type, uint32_t event_name,
453                                struct timeval in_timestamp,
454                                struct timeval out_timestamp,
455                                uint32_t frame_id) {
456    if (kpi_camscope_frame_count != 0) {
457        if (kpi_camscope_flags & camscope_enable_mask) {
458            struct timeval timestamp;
459            gettimeofday(&timestamp, NULL);
460            camscope_mutex_lock((camscope_section_type)camscope_section);
461            camscope_in_out_timing scope_struct;
462            uint32_t size = sizeof(scope_struct);
463            int32_t thread_id = (int32_t)get_thread_id();
464            uint32_t total_size =
465              camscope_reserve((camscope_section_type)camscope_section, size);
466            if (size == total_size) {
467                fill_camscope_in_out_timing(&scope_struct, packet_type, size,
468                                            timestamp, thread_id, event_name,
469                                            in_timestamp, out_timestamp,
470                                            frame_id);
471                camscope_store_data((camscope_section_type)camscope_section,
472                                    &scope_struct, size);
473            }
474            camscope_mutex_unlock((camscope_section_type)camscope_section);
475        }
476    }
477}
478