1 /* 2 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. 3 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 13 * * Neither the name of The Linux Foundation nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #define LOG_NDDEBUG 0 31 #include "profiler.h" 32 33 #ifdef DEBUG_CALC_FPS 34 35 namespace android { 36 ANDROID_SINGLETON_STATIC_INSTANCE(qdutils::CalcFps) ; 37 } 38 39 namespace qdutils { 40 41 CalcFps::CalcFps() { 42 debug_fps_level = 0; 43 Init(); 44 } 45 46 CalcFps::~CalcFps() { 47 } 48 49 void CalcFps::Init() { 50 char prop[PROPERTY_VALUE_MAX]; 51 property_get("debug.gr.calcfps", prop, "0"); 52 debug_fps_level = atoi(prop); 53 if (debug_fps_level > MAX_DEBUG_FPS_LEVEL) { 54 ALOGW("out of range value for debug.gr.calcfps, using 0"); 55 debug_fps_level = 0; 56 } 57 58 ALOGD("DEBUG_CALC_FPS: %d", debug_fps_level); 59 populate_debug_fps_metadata(); 60 } 61 62 void CalcFps::Fps() { 63 if (debug_fps_level > 0) 64 calc_fps(ns2us(systemTime())); 65 } 66 67 void CalcFps::populate_debug_fps_metadata(void) 68 { 69 char prop[PROPERTY_VALUE_MAX]; 70 71 /*defaults calculation of fps to based on number of frames*/ 72 property_get("debug.gr.calcfps.type", prop, "0"); 73 debug_fps_metadata.type = (debug_fps_metadata_t::DfmType) atoi(prop); 74 75 /*defaults to 1000ms*/ 76 property_get("debug.gr.calcfps.timeperiod", prop, "1000"); 77 debug_fps_metadata.time_period = atoi(prop); 78 79 property_get("debug.gr.calcfps.period", prop, "10"); 80 debug_fps_metadata.period = atoi(prop); 81 82 if (debug_fps_metadata.period > MAX_FPS_CALC_PERIOD_IN_FRAMES) { 83 debug_fps_metadata.period = MAX_FPS_CALC_PERIOD_IN_FRAMES; 84 } 85 86 /* default ignorethresh_us: 500 milli seconds */ 87 property_get("debug.gr.calcfps.ignorethresh_us", prop, "500000"); 88 debug_fps_metadata.ignorethresh_us = atoi(prop); 89 90 debug_fps_metadata.framearrival_steps = 91 (debug_fps_metadata.ignorethresh_us / 16666); 92 93 if (debug_fps_metadata.framearrival_steps > MAX_FRAMEARRIVAL_STEPS) { 94 debug_fps_metadata.framearrival_steps = MAX_FRAMEARRIVAL_STEPS; 95 debug_fps_metadata.ignorethresh_us = 96 debug_fps_metadata.framearrival_steps * 16666; 97 } 98 99 /* 2ms margin of error for the gettimeofday */ 100 debug_fps_metadata.margin_us = 2000; 101 102 for (unsigned int i = 0; i < MAX_FRAMEARRIVAL_STEPS; i++) 103 debug_fps_metadata.accum_framearrivals[i] = 0; 104 105 ALOGD("period: %d", debug_fps_metadata.period); 106 ALOGD("ignorethresh_us: %lld", debug_fps_metadata.ignorethresh_us); 107 } 108 109 void CalcFps::print_fps(float fps) 110 { 111 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type) 112 ALOGD("FPS for last %d frames: %3.2f", debug_fps_metadata.period, fps); 113 else 114 ALOGD("FPS for last (%f ms, %d frames): %3.2f", 115 debug_fps_metadata.time_elapsed, 116 debug_fps_metadata.curr_frame, fps); 117 118 debug_fps_metadata.curr_frame = 0; 119 debug_fps_metadata.time_elapsed = 0.0; 120 121 if (debug_fps_level > 1) { 122 ALOGD("Frame Arrival Distribution:"); 123 for (unsigned int i = 0; 124 i < ((debug_fps_metadata.framearrival_steps / 6) + 1); 125 i++) { 126 ALOGD("%lld %lld %lld %lld %lld %lld", 127 debug_fps_metadata.accum_framearrivals[i*6], 128 debug_fps_metadata.accum_framearrivals[i*6+1], 129 debug_fps_metadata.accum_framearrivals[i*6+2], 130 debug_fps_metadata.accum_framearrivals[i*6+3], 131 debug_fps_metadata.accum_framearrivals[i*6+4], 132 debug_fps_metadata.accum_framearrivals[i*6+5]); 133 } 134 135 /* We are done with displaying, now clear the stats */ 136 for (unsigned int i = 0; 137 i < debug_fps_metadata.framearrival_steps; 138 i++) 139 debug_fps_metadata.accum_framearrivals[i] = 0; 140 } 141 return; 142 } 143 144 void CalcFps::calc_fps(nsecs_t currtime_us) 145 { 146 static nsecs_t oldtime_us = 0; 147 148 nsecs_t diff = currtime_us - oldtime_us; 149 150 oldtime_us = currtime_us; 151 152 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type && 153 diff > debug_fps_metadata.ignorethresh_us) { 154 return; 155 } 156 157 if (debug_fps_metadata.curr_frame < MAX_FPS_CALC_PERIOD_IN_FRAMES) { 158 debug_fps_metadata.framearrivals[debug_fps_metadata.curr_frame] = diff; 159 } 160 161 debug_fps_metadata.curr_frame++; 162 163 if (debug_fps_level > 1) { 164 unsigned int currstep = (diff + debug_fps_metadata.margin_us) / 16666; 165 166 if (currstep < debug_fps_metadata.framearrival_steps) { 167 debug_fps_metadata.accum_framearrivals[currstep-1]++; 168 } 169 } 170 171 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type) { 172 if (debug_fps_metadata.curr_frame == debug_fps_metadata.period) { 173 /* time to calculate and display FPS */ 174 nsecs_t sum = 0; 175 for (unsigned int i = 0; i < debug_fps_metadata.period; i++) 176 sum += debug_fps_metadata.framearrivals[i]; 177 print_fps((debug_fps_metadata.period * float(1000000))/float(sum)); 178 } 179 } 180 else if (debug_fps_metadata_t::DFM_TIME == debug_fps_metadata.type) { 181 debug_fps_metadata.time_elapsed += ((float)diff/1000.0); 182 if (debug_fps_metadata.time_elapsed >= debug_fps_metadata.time_period) { 183 float fps = (1000.0 * debug_fps_metadata.curr_frame)/ 184 (float)debug_fps_metadata.time_elapsed; 185 print_fps(fps); 186 } 187 } 188 return; 189 } 190 };//namespace qomutils 191 #endif 192