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