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 <inttypes.h>
32
33 #include "profiler.h"
34
35 #ifdef DEBUG_CALC_FPS
36
37
38 ANDROID_SINGLETON_STATIC_INSTANCE(qdutils::CalcFps) ;
39
40 namespace qdutils {
41
CalcFps()42 CalcFps::CalcFps() {
43 debug_fps_level = 0;
44 Init();
45 }
46
~CalcFps()47 CalcFps::~CalcFps() {
48 }
49
Init()50 void CalcFps::Init() {
51 char prop[PROPERTY_VALUE_MAX];
52 property_get("debug.gr.calcfps", prop, "0");
53 debug_fps_level = atoi(prop);
54 if (debug_fps_level > MAX_DEBUG_FPS_LEVEL) {
55 ALOGW("out of range value for debug.gr.calcfps, using 0");
56 debug_fps_level = 0;
57 }
58
59 ALOGD("DEBUG_CALC_FPS: %d", debug_fps_level);
60 populate_debug_fps_metadata();
61 }
62
Fps()63 void CalcFps::Fps() {
64 if (debug_fps_level > 0)
65 calc_fps(ns2us(systemTime()));
66 }
67
populate_debug_fps_metadata(void)68 void CalcFps::populate_debug_fps_metadata(void)
69 {
70 char prop[PROPERTY_VALUE_MAX];
71
72 /*defaults calculation of fps to based on number of frames*/
73 property_get("debug.gr.calcfps.type", prop, "0");
74 debug_fps_metadata.type = (debug_fps_metadata_t::DfmType) atoi(prop);
75
76 /*defaults to 1000ms*/
77 property_get("debug.gr.calcfps.timeperiod", prop, "1000");
78 debug_fps_metadata.time_period = atoi(prop);
79
80 property_get("debug.gr.calcfps.period", prop, "10");
81 debug_fps_metadata.period = atoi(prop);
82
83 if (debug_fps_metadata.period > MAX_FPS_CALC_PERIOD_IN_FRAMES) {
84 debug_fps_metadata.period = MAX_FPS_CALC_PERIOD_IN_FRAMES;
85 }
86
87 /* default ignorethresh_us: 500 milli seconds */
88 property_get("debug.gr.calcfps.ignorethresh_us", prop, "500000");
89 debug_fps_metadata.ignorethresh_us = atoi(prop);
90
91 debug_fps_metadata.framearrival_steps =
92 (unsigned int)(debug_fps_metadata.ignorethresh_us / 16666);
93
94 if (debug_fps_metadata.framearrival_steps > MAX_FRAMEARRIVAL_STEPS) {
95 debug_fps_metadata.framearrival_steps = MAX_FRAMEARRIVAL_STEPS;
96 debug_fps_metadata.ignorethresh_us =
97 debug_fps_metadata.framearrival_steps * 16666;
98 }
99
100 /* 2ms margin of error for the gettimeofday */
101 debug_fps_metadata.margin_us = 2000;
102
103 for (unsigned int i = 0; i < MAX_FRAMEARRIVAL_STEPS; i++)
104 debug_fps_metadata.accum_framearrivals[i] = 0;
105
106 debug_fps_metadata.curr_frame = 0;
107
108 ALOGD("period: %u", debug_fps_metadata.period);
109 ALOGD("ignorethresh_us: %" PRId64, debug_fps_metadata.ignorethresh_us);
110 }
111
print_fps(float fps)112 void CalcFps::print_fps(float fps)
113 {
114 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type)
115 ALOGD("FPS for last %d frames: %3.2f", debug_fps_metadata.period, fps);
116 else
117 ALOGD("FPS for last (%f ms, %d frames): %3.2f",
118 debug_fps_metadata.time_elapsed,
119 debug_fps_metadata.curr_frame, fps);
120
121 debug_fps_metadata.curr_frame = 0;
122 debug_fps_metadata.time_elapsed = 0.0;
123
124 if (debug_fps_level > 1) {
125 ALOGD("Frame Arrival Distribution:");
126 for (unsigned int i = 0;
127 i < ((debug_fps_metadata.framearrival_steps / 6) + 1);
128 i++) {
129 ALOGD("%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
130 debug_fps_metadata.accum_framearrivals[i*6],
131 debug_fps_metadata.accum_framearrivals[i*6+1],
132 debug_fps_metadata.accum_framearrivals[i*6+2],
133 debug_fps_metadata.accum_framearrivals[i*6+3],
134 debug_fps_metadata.accum_framearrivals[i*6+4],
135 debug_fps_metadata.accum_framearrivals[i*6+5]);
136 }
137
138 /* We are done with displaying, now clear the stats */
139 for (unsigned int i = 0;
140 i < debug_fps_metadata.framearrival_steps;
141 i++)
142 debug_fps_metadata.accum_framearrivals[i] = 0;
143 }
144 return;
145 }
146
calc_fps(nsecs_t currtime_us)147 void CalcFps::calc_fps(nsecs_t currtime_us)
148 {
149 static nsecs_t oldtime_us = 0;
150
151 nsecs_t diff = currtime_us - oldtime_us;
152
153 oldtime_us = currtime_us;
154
155 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type &&
156 diff > debug_fps_metadata.ignorethresh_us) {
157 return;
158 }
159
160 if (debug_fps_metadata.curr_frame < MAX_FPS_CALC_PERIOD_IN_FRAMES) {
161 debug_fps_metadata.framearrivals[debug_fps_metadata.curr_frame] = diff;
162 }
163
164 debug_fps_metadata.curr_frame++;
165
166 if (debug_fps_level > 1) {
167 unsigned int currstep =
168 (unsigned int)(diff + debug_fps_metadata.margin_us) / 16666;
169
170 if (currstep < debug_fps_metadata.framearrival_steps) {
171 debug_fps_metadata.accum_framearrivals[currstep-1]++;
172 }
173 }
174
175 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type) {
176 if (debug_fps_metadata.curr_frame == debug_fps_metadata.period) {
177 /* time to calculate and display FPS */
178 nsecs_t sum = 0;
179 for (unsigned int i = 0; i < debug_fps_metadata.period; i++)
180 sum += debug_fps_metadata.framearrivals[i];
181 print_fps(float(float(debug_fps_metadata.period * 1000000) /
182 (float)sum));
183 }
184 }
185 else if (debug_fps_metadata_t::DFM_TIME == debug_fps_metadata.type) {
186 debug_fps_metadata.time_elapsed += (float)((float)diff/1000.0);
187 if (debug_fps_metadata.time_elapsed >= debug_fps_metadata.time_period) {
188 float fps = float(1000.0 * debug_fps_metadata.curr_frame/
189 debug_fps_metadata.time_elapsed);
190 print_fps(fps);
191 }
192 }
193 return;
194 }
195 };//namespace qomutils
196 #endif
197