Home | History | Annotate | Download | only in xcore
      1 /*
      2  * xcam_obj_debug.h - object profiling and debug
      3  *
      4  *  Copyright (c) 2015 Intel Corporation
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  *
     18  * Author: Wind Yuan <feng.yuan (at) intel.com>
     19  */
     20 
     21 #ifndef XCAM_OBJ_DEBUG_H
     22 #define XCAM_OBJ_DEBUG_H
     23 
     24 #include <stdio.h>
     25 
     26 // default duration of frame numbers
     27 #define XCAM_OBJ_DUR_FRAME_NUM 30
     28 
     29 #define XCAM_STATIC_FPS_CALCULATION(objname, count) \
     30     do{                                             \
     31         static uint32_t num_frame = 0;              \
     32         static struct timeval last_sys_time;        \
     33         static struct timeval first_sys_time;       \
     34         static bool b_last_sys_time_init = false;   \
     35         if (!b_last_sys_time_init) {                \
     36             gettimeofday (&last_sys_time, NULL);    \
     37             gettimeofday (&first_sys_time, NULL);   \
     38             b_last_sys_time_init = true;            \
     39         } else {                                    \
     40             if ((num_frame%count)==0) {             \
     41                 double total, current;              \
     42                 struct timeval cur_sys_time;        \
     43                 gettimeofday (&cur_sys_time, NULL); \
     44                 total = (cur_sys_time.tv_sec - first_sys_time.tv_sec)*1.0f +     \
     45                     (cur_sys_time.tv_usec - first_sys_time.tv_usec)/1000000.0f;  \
     46                 current = (cur_sys_time.tv_sec - last_sys_time.tv_sec)*1.0f +    \
     47                     (cur_sys_time.tv_usec - last_sys_time.tv_usec)/1000000.0f;   \
     48                 printf("%s Current fps: %.2f, Total avg fps: %.2f\n",            \
     49                     #objname, ((float)(count))/current, (float)num_frame/total); \
     50                 last_sys_time = cur_sys_time;       \
     51             }                                       \
     52         }                                           \
     53         ++num_frame;                                \
     54     }while(0)
     55 
     56 #define XCAM_STATIC_PROFILING_START(name)               \
     57     static unsigned int name##_times = 0;               \
     58     static struct timeval name##_start_time;            \
     59     static struct timeval name##_end_time;              \
     60     gettimeofday (& name##_start_time, NULL);           \
     61     ++ name##_times;
     62 
     63 #define XCAM_STATIC_PROFILING_END(name, times_of_print) \
     64     static double name##_sum_time = 0;                  \
     65     gettimeofday (& name##_end_time, NULL);             \
     66     name##_sum_time += (name##_end_time.tv_sec - name##_start_time.tv_sec)*1000.0f +  \
     67                    (name##_end_time.tv_usec - name##_start_time.tv_usec)/1000.0f; \
     68     if (name##_times >= times_of_print) {               \
     69         printf ("profiling %s, fps:%.2f duration:%.2fms\n", #name, \
     70             (name##_times*1000.0f/name##_sum_time), name##_sum_time/name##_times); \
     71         name##_times = 0;                               \
     72         name##_sum_time = 0.0;                          \
     73     }
     74 
     75 #if ENABLE_PROFILING
     76 #define XCAM_OBJ_PROFILING_DEFINES          \
     77     struct timeval   _profiling_start_time; \
     78     uint32_t         _profiling_times;      \
     79     double           _profiling_sum_duration
     80 
     81 #define XCAM_OBJ_PROFILING_INIT             \
     82     xcam_mem_clear (_profiling_start_time); \
     83     _profiling_times = 0;                   \
     84     _profiling_sum_duration = 0.0
     85 
     86 #define XCAM_OBJ_PROFILING_START \
     87     gettimeofday (&_profiling_start_time, NULL)
     88 
     89 #define XCAM_OBJ_PROFILING_END(name, times) \
     90     struct timeval profiling_now;           \
     91     gettimeofday (&profiling_now, NULL);    \
     92     _profiling_sum_duration +=              \
     93         (profiling_now.tv_sec - _profiling_start_time.tv_sec) * 1000.0f +  \
     94         (profiling_now.tv_usec - _profiling_start_time.tv_usec) / 1000.0f; \
     95     ++_profiling_times;                     \
     96     if (_profiling_times >= times) {        \
     97         char buf[1024];                     \
     98         snprintf (buf, 1024, "profiling %s,average duration:%.2fms\n", \
     99         (name), (_profiling_sum_duration/times)); \
    100         printf ("%s", buf);                 \
    101         _profiling_times = 0;               \
    102         _profiling_sum_duration = 0.0;      \
    103     }
    104 #else
    105 #define XCAM_OBJ_PROFILING_DEFINES
    106 #define XCAM_OBJ_PROFILING_INIT
    107 #define XCAM_OBJ_PROFILING_START
    108 #define XCAM_OBJ_PROFILING_END(name, times)
    109 #endif
    110 
    111 #endif //XCAM_OBJ_DEBUG_H
    112